Beispiel #1
0
        /// <summary>
        ///     Looks for the next match depending on the settings in the <paramref name = "request" />.
        /// </summary>
        /// <param name = "request">The text find request.</param>
        /// <returns>An updated request with the relevent values adjusted (namely position).</returns>
        public FindTextRequest FindNext(FindTextRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            /*
             * if (request.SearchUp)
             * {
             *  // todo - I think its the TextProvider's job...?
             * }
             * else // search down.
             * {
             * int pos = request.TextProvider.FindString(request.SearchValue, request.Position, request.StringComparison);
             *  //pos = request.TextProvider.FindString(request);
             * }
             */
            int pos = request.TextProvider.FindString(request.SearchValue, request.Position, request.StringComparison);

            if (pos > -1)
            {
                // the editor will highlight the find
                request.Position = pos + request.SearchValue.Length;
            }
            else
            {
                // todo - notify, beep etc

                // reset to start of buffer.
                request.Position = 0;
            }

            return(request);
        }
        /// <summary>
        /// 	Looks for the next match depending on the settings in the <paramref name = "request" />.
        /// </summary>
        /// <param name = "request">The text find request.</param>
        /// <returns>An updated request with the relevent values adjusted (namely position).</returns>
        public FindTextRequest FindNext(FindTextRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            /*
            if (request.SearchUp)
            {
                // todo - I think its the TextProvider's job...?
            }
            else // search down.
            {
            int pos = request.TextProvider.FindString(request.SearchValue, request.Position, request.StringComparison);
                //pos = request.TextProvider.FindString(request);
            }
            */
            int pos = request.TextProvider.FindString(request.SearchValue, request.Position, request.StringComparison);

            if (pos > -1)
            {
                // the editor will highlight the find
                request.Position = pos + request.SearchValue.Length;
            }
            else
            {
                // todo - notify, beep etc

                // reset to start of buffer.
                request.Position = 0;
            }

            return request;
        }
        /// <summary>Two execution methods - through the "Find Text" window or by hitting F3.
        /// If simply hitting F3, we are executing the current search from current cursor position for this window.
        /// Different windows can also have different search text.</summary>
        public override void Execute()
        {
            IFindReplaceProvider editorFindProvider = HostWindow.ActiveChildForm as IFindReplaceProvider;

            if (editorFindProvider != null)
            {
                FindTextRequest findTextRequest = null;
                int key = editorFindProvider.GetHashCode();

                // is there a request in the table for this window?
                if (SearchToolsCommon.FindReplaceTextRequests.ContainsKey(key))
                {
                    findTextRequest = SearchToolsCommon.FindReplaceTextRequests[key];
                }
                else
                {
                    if (SearchToolsCommon.FindReplaceTextRequests.Count > 0)
                    {
                        // if there is an entry in the list of searches create an instance
                        findTextRequest = new FindTextRequest(editorFindProvider);
                        findTextRequest.Position = editorFindProvider.CursorOffset;
                    }
                    else
                    {
                        // none in table, default to curently selected text if its the editor
                        IEditor editor = ActiveFormAsEditor;
                        if (editor != null && editor.SelectedText.Length > 0)
                        {
                            findTextRequest = new FindTextRequest(editorFindProvider, editor.SelectedText);
                            findTextRequest.Position = editorFindProvider.CursorOffset;
                        }
                    }
                }

                if (findTextRequest != null)
                {
                    // wrap around to start if at last pos
                    if (findTextRequest.Position != 0)
                    {
                        findTextRequest.Position = editorFindProvider.CursorOffset;
                    }

                    findTextRequest = editorFindProvider.TextFindService.FindNext(findTextRequest);
                    SearchToolsCommon.FindReplaceTextRequests[key] = findTextRequest;
                }
            }
        }
        /// <summary>The create find request.</summary>
        /// <param name="provider">The provider.</param>
        /// <param name="findString">The find string.</param>
        /// <param name="replaceValue">The replace value.</param>
        private void CreateFindRequest(IFindReplaceProvider provider, string findString, string replaceValue)
        {
            int key = provider.GetHashCode();
            FindTextRequest request;

            if (SearchToolsCommon.FindReplaceTextRequests.ContainsKey(key))
            {
                request = SearchToolsCommon.FindReplaceTextRequests[key];
                if (request.SearchValue != findString)
                {
                    // reset find text and set the starting position to the current cursor location
                    request.SearchValue = findString;
                    request.ReplaceValue = replaceValue;
                    request.Position = provider.CursorOffset;
                }
            }
            else
            {
                request = new FindTextRequest(provider, findString);
                request.ReplaceValue = replaceValue;
            }

            SearchToolsCommon.FindReplaceTextRequests[key] = request;
        }