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

FindNext() private method

private FindNext ( string findText ) : int
findText string
return int
        private int FindNext(string findText)
        {
            if (Settings.SearchIn == SearchInOptions.SelectedText &&
                (!_searchScope.HasValue || _searchScope.Value.cpMin == _searchScope.Value.cpMax))
                throw new InvalidOperationException("Search scope has not been defined.");

            if (Settings.SearchIn == SearchInOptions.OpenDocuments && Settings.SearchFromBegining)
            {
                // Assumes that both views ALWAYS have one document open (index 0). Notepad++ always shows a 'New 1' document if everthing else is closed.
                if (!Settings.SearchBackwards)
                {
                    Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_ACTIVATEDOC, IsViewVisible((int)NppMsg.MAIN_VIEW) ? (int)NppMsg.MAIN_VIEW : (int)NppMsg.SUB_VIEW, 0);
                    _editor = Editor.GetActive();
                }
                else
                {
                    if (IsViewVisible((int)NppMsg.SUB_VIEW))
                    {
                        var secondViewFileCount = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETNBOPENFILES, 0, (int)NppMsg.SECOND_VIEW);
                        Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_ACTIVATEDOC, (int)NppMsg.SUB_VIEW, secondViewFileCount > 0 ? secondViewFileCount - 1 : 0);
                    }
                    else
                    {
                        var primaryViewFileCount = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETNBOPENFILES, 0, (int)NppMsg.PRIMARY_VIEW);
                        Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_ACTIVATEDOC, (int)NppMsg.MAIN_VIEW, primaryViewFileCount > 0 ? primaryViewFileCount - 1 : 0);
                    }
                    _editor = Editor.GetActive();
                }
            }

            var selection = _editor.GetSelectionRange();
            var documentLength = _editor.GetDocumentLength();

            var startPosition = Settings.SearchBackwards
                ? Settings.SearchFromBegining
                    ? Settings.SearchIn == SearchInOptions.SelectedText
                        ? _searchScope.Value.cpMax
                        : documentLength
                    : selection.cpMin
                : Settings.SearchFromBegining
                    ? Settings.SearchIn == SearchInOptions.SelectedText
                        ? _searchScope.Value.cpMin
                        : 0
                    : selection.cpMax;
            var endPosition = Settings.SearchBackwards
                ? Settings.SearchIn == SearchInOptions.SelectedText
                    ? _searchScope.Value.cpMin
                    : 0
                : Settings.SearchIn == SearchInOptions.SelectedText
                    ? _searchScope.Value.cpMax
                    : documentLength;

            SetSearchFlags();
            var posFind = _editor.FindInTarget(findText, startPosition, endPosition);
            if (posFind != -1)
            {
                // Highlight if found
                _lastMatch = _editor.GetTargetRange();
                _editor.EnsureRangeVisible(_lastMatch.Value.cpMin, _lastMatch.Value.cpMax);
                _editor.SetSelection(_lastMatch.Value.cpMin, _lastMatch.Value.cpMax);
            }
            else if (Settings.SearchIn == SearchInOptions.OpenDocuments)
            {
                // Check next document
                int currentView;
                Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTSCINTILLA, 0, out currentView);
                var currentFileIndex = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTDOCINDEX, 0, currentView);
                if (!Settings.SearchBackwards)
                {
                    // Searching forwards
                    var fileCount = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETNBOPENFILES, 0, currentView + 1);
                    // If there's another document in the view, switch to it
                    if (currentFileIndex < fileCount - 1)
                        posFind = FindInOtherDocument(findText, currentView, currentFileIndex + 1);
                    else if (currentView == (int)NppMsg.MAIN_VIEW && IsViewVisible((int)NppMsg.SUB_VIEW))
                        posFind = FindInOtherDocument(findText, (int)NppMsg.SUB_VIEW, 0);
                }
                else
                {
                    // Searching backwards
                    if (currentFileIndex > 0)
                        posFind = FindInOtherDocument(findText, currentView, currentFileIndex - 1);
                    else if (currentView == (int)NppMsg.SUB_VIEW && IsViewVisible((int)NppMsg.MAIN_VIEW))
                    {
                        var primaryViewFileCount = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETNBOPENFILES, 0, (int)NppMsg.PRIMARY_VIEW);
                        posFind = FindInOtherDocument(findText, (int)NppMsg.MAIN_VIEW, primaryViewFileCount > 0 ? primaryViewFileCount - 1 : 0);
                    }
                }
            }

            return posFind;
        }