예제 #1
0
        /// <summary>
        /// Replace all occurrences of the currently configured search in the given text editor
        /// </summary>
        /// <param name="editor">Text Editor</param>
        public void ReplaceAll(ITextEditorAdaptor <T> editor)
        {
            if (String.IsNullOrEmpty(this.ReplaceText))
            {
                this.ShowMessage("No Replace Text specified");
            }

            int origPos = editor.CaretOffset;

            if (this.Scope != FindAndReplaceScope.Selection)
            {
                // Always clear any existing selection if replacing over the entire document
                editor.SelectionStart  = 0;
                editor.SelectionLength = 0;
            }

            //Replace All works over the entire document unless there was already a selection present
            int  minPos, maxPos;
            bool restoreSelection = false;

            if (editor.SelectionLength > 0 && this.Scope == FindAndReplaceScope.Selection)
            {
                restoreSelection   = true;
                minPos             = editor.SelectionStart - 1;
                maxPos             = editor.SelectionStart + editor.SelectionLength;
                editor.CaretOffset = Math.Max(minPos, 0);
            }
            else
            {
                minPos             = -1;
                maxPos             = editor.Text.Length;
                editor.CaretOffset = 0;
            }
            editor.SelectionStart  = 0;
            editor.SelectionLength = 0;

            try
            {
                editor.BeginUpdate();
                String replace = this.ReplaceText;
                while (this.FindNext(editor, minPos, maxPos))
                {
                    int diff;

                    // Do replacement
                    if (this.UseRegex && this.HasCaptureGroups)
                    {
                        // Using capture groups
                        StringBuilder builder = new StringBuilder();
                        int           start   = 0;
                        foreach (Match m in this._hasCaptureGroups.Matches(replace))
                        {
                            // Append preceding verbatim text
                            // +1 is necessary because
                            builder.Append(replace.Substring(start, m.Index + 1));
                            start = m.Index + m.Length;

                            int captureGrop = Int32.Parse(m.Groups[1].Value);
                            builder.Append(this._currentMatch.Groups[captureGrop].Value);
                        }
                        // Append remaining verbatim text
                        builder.Append(replace.Substring(start));

                        diff = builder.Length - editor.SelectionLength;
                        editor.Replace(editor.SelectionStart, editor.SelectionLength, builder.ToString());
                    }
                    else
                    {
                        // Simple text replacement
                        diff = replace.Length - editor.SelectionLength;
                        editor.Replace(editor.SelectionStart, editor.SelectionLength, replace);
                    }
                    editor.SelectionLength = replace.Length;
                    editor.CaretOffset     = editor.SelectionStart;

                    minPos  = editor.SelectionStart + editor.SelectionLength + 1;
                    maxPos += diff;
                }
            }
            finally
            {
                editor.EndUpdate();
            }

            editor.CaretOffset = origPos;
            editor.ScrollToLine(editor.GetLineByOffset(origPos));

            if (restoreSelection)
            {
                editor.Select(minPos + 1, maxPos - minPos - 1);
            }
        }
예제 #2
0
        /// <summary>
        /// Replace all occurrences of the currently configured search in the given text editor
        /// </summary>
        /// <param name="editor">Text Editor</param>
        public void ReplaceAll(ITextEditorAdaptor <T> editor)
        {
            if (String.IsNullOrEmpty(this.ReplaceText))
            {
                this.ShowMessage("No Replace Text specified");
            }

            int origPos = editor.CaretOffset;

            if (this.Scope != FindAndReplaceScope.Selection)
            {
                //Check whether the relevant Text is already selected
                if (!String.IsNullOrEmpty(this.FindText))
                {
                    if (this.FindText.Equals(editor.GetText(editor.SelectionStart, editor.SelectionLength)))
                    {
                        //If it is remove the selection so the FindNext() call will simply find the currently highlighted text
                        editor.SelectionStart  = 0;
                        editor.SelectionLength = 0;
                    }
                }
            }

            //Replace All works over the entire document unless there was already a selection present
            int  minPos, maxPos;
            bool restoreSelection = false;

            if (editor.SelectionLength > 0 && this.Scope == FindAndReplaceScope.Selection)
            {
                restoreSelection   = true;
                minPos             = editor.SelectionStart - 1;
                maxPos             = editor.SelectionStart + editor.SelectionLength;
                editor.CaretOffset = Math.Max(minPos, 0);
            }
            else
            {
                minPos             = -1;
                maxPos             = editor.Text.Length;
                editor.CaretOffset = 0;
            }
            editor.SelectionStart  = 0;
            editor.SelectionLength = 0;

            try
            {
                editor.BeginUpdate();
                String replace = this.ReplaceText;
                while (this.FindNext(editor, minPos, maxPos))
                {
                    int diff = replace.Length - editor.SelectionLength;

                    editor.Replace(editor.SelectionStart, editor.SelectionLength, replace);
                    editor.SelectionLength = replace.Length;
                    editor.CaretOffset     = editor.SelectionStart;

                    maxPos += diff;
                }
            }
            finally
            {
                editor.EndUpdate();
            }

            editor.CaretOffset = origPos;
            editor.ScrollToLine(editor.GetLineByOffset(origPos));

            if (restoreSelection)
            {
                editor.Select(minPos + 1, maxPos - minPos - 1);
            }
        }