phdesign.NppToolBucket.FindAndReplace.OnDoAction C# (CSharp) Method

OnDoAction() private method

private OnDoAction ( object sender, DoActionEventArgs doActionEventArgs ) : void
sender object
doActionEventArgs phdesign.NppToolBucket.Forms.DoActionEventArgs
return void
        private void OnDoAction(object sender, DoActionEventArgs doActionEventArgs)
        {
            var window = sender as FindAndReplaceForm;
            if (window == null) return;

            // Set options
            Settings.MatchCase = window.MatchCase;
            Settings.MatchWholeWord = window.MatchWholeWord;
            Settings.UseRegularExpression = window.UseRegularExpression;
            Settings.SearchBackwards = window.SearchBackwards;
            Settings.SearchFromBegining = window.SearchFromBegining;
            Settings.SearchIn = window.SearchIn;
            Settings.WindowSize = window.ClientSize;
            Settings.WindowLocation = window.Location;

            var findText = window.FindText;
            var replaceText = window.ReplaceText;
            // Check find text is not null or empty. Replace text can be.
            if (string.IsNullOrEmpty(findText)) return;
            // Update history
            if (SaveFindHistory(findText))
                window.FindHistory = Settings.FindHistory.ToArray();
            if (Settings.SearchIn == SearchInOptions.SelectedText)
            {
                var sel = _editor.GetSelectionRange();
                // Check if user has changed selection - if so reset search scope. Selection should either be initial selection or last found match.
                if (_searchScope.HasValue &&
                    !sel.Equals(_searchScope.Value) &&
                    _lastMatch.HasValue &&
                    !sel.Equals(_lastMatch.Value) &&
                    sel.cpMin != sel.cpMax)
                    _searchScope = null;

                if (!_searchScope.HasValue)
                {
                    _searchScope = sel;
                    // Check we have a selection
                    if (_searchScope.Value.cpMin == _searchScope.Value.cpMax)
                    {
                        MessageBox.Show(_owner, "No text selected", window.Text);
                        return;
                    }
                    Settings.SearchFromBegining = true;
                }
            }

            switch (doActionEventArgs.Action)
            {
                case Action.FindNext:
                    var posFound = FindNext(findText);
                    if (posFound == -1)
                    {
                        MessageBox.Show(
                            _owner,
                            string.Format(
                                "{0} of {1} reached. No match found",
                                Settings.SearchBackwards ? "Start" : "End",
                                Settings.SearchIn == SearchInOptions.SelectedText ? "scope" : "document(s)"),
                            window.Text);
                        // If we haven't started from beginning of document, turn that option on.
                        if (!Settings.SearchFromBegining)
                            window.SearchFromBegining = true;
                        // Reset search in selection
                        if (Settings.SearchIn == SearchInOptions.SelectedText)
                            _editor.SetSelection(_searchScope.Value.cpMin, _searchScope.Value.cpMax);
                        // Clear search scope
                        _searchScope = null;
                    }
                    else
                    {
                        // Turn off search from beining otherwise we'll only ever find the first match.
                        window.SearchFromBegining = false;
                    }
                    break;
                case Action.FindAll:
                    MessageBox.Show(
                        _owner,
                        string.Format("{0} matches found", MarkAll(findText, false)),
                        window.Text);
                    _searchScope = null;
                    break;
                case Action.Count:
                    MessageBox.Show(
                        _owner,
                        string.Format("{0} matches found", MarkAll(findText, true)),
                        window.Text);
                    _searchScope = null;
                    break;
                case Action.Replace:
                    // Todo: Check if readonly
                    // if ((*_ppEditView)->getCurrentBuffer()->isReadOnly()) return false;
                    if (SaveReplaceHistory(replaceText))
                        window.ReplaceHistory = Settings.ReplaceHistory.ToArray();
                    var posFoundReplace = Replace(findText, replaceText);
                    if (posFoundReplace == -1)
                    {
                        MessageBox.Show(
                            _owner,
                            string.Format(
                                "{0} of {1} reached. No match found",
                                Settings.SearchBackwards ? "Start" : "End",
                                Settings.SearchIn == SearchInOptions.SelectedText ? "scope" : "document"),
                            window.Text);
                        // If we haven't started from begining of document, turn that option on.
                        if (!Settings.SearchFromBegining)
                            window.SearchFromBegining = true;
                        // Reset search in selection
                        if (Settings.SearchIn == SearchInOptions.SelectedText)
                            _editor.SetSelection(_searchScope.Value.cpMin, _searchScope.Value.cpMax);
                        // Clear search scope
                        _searchScope = null;
                    }
                    else
                    {
                        // Turn off search from beining otherwise we'll only ever find the first match.
                        window.SearchFromBegining = false;
                    }
                    break;
                case Action.ReplaceAll:
                    // Todo: Check if readonly
                    // if ((*_ppEditView)->getCurrentBuffer()->isReadOnly()) return false;
                    if (SaveReplaceHistory(replaceText))
                        window.ReplaceHistory = Settings.ReplaceHistory.ToArray();
                    MessageBox.Show(
                        _owner,
                        string.Format("{0} matches replaced", ReplaceAll(findText, replaceText)),
                        window.Text);
                    _searchScope = null;
                    break;
            }
        }