private void ReplaceOperation(FindAndReplacePanel.FindReplaceOptions Options, int Position)
        {
            if (Position < 0 ||
                (Position + Options.ReplaceText.Length) >=
                (Options.OnlyInSelection ? this.TextEditor.SelectionLength : this.TextEditor.Text.Length))
            {
                return;
            }

            var AbsolutePosition = ((Options.OnlyInSelection ? this.TextEditor.SelectionStart : 0) + Position);

            this.TextEditor.Document.Replace(AbsolutePosition, Options.FindText.Length, Options.ReplaceText);
        }
        private int FindOperation(FindAndReplacePanel.FindReplaceOptions Options, int Position)
        {
            if (Position < 0 || Position >=
                (Options.OnlyInSelection ? this.TextEditor.SelectionLength : this.TextEditor.Text.Length))
            {
                return(-1);
            }

            var CaseComparer = (Options.IsCaseSensitive
                                ? StringComparison.Ordinal  // Must use 'ordinal' to avoid considering hyphens as part of words (culture dependant).
                                : StringComparison.OrdinalIgnoreCase);

            var SourceText = (Options.OnlyInSelection
                             ? this.TextEditor.SelectedText
                             : this.TextEditor.Text);

            Position = SourceText.IndexOf(Options.FindText, Position, CaseComparer);

            // This loop is just to skip the non-whole-words cases
            while (Position >= 0)
            {
                if (!(Options.ConsiderWholeWord &&
                      ((Position > 0 && (Char.IsLetterOrDigit(SourceText[Position - 1]) || SourceText[Position - 1] == '_')) ||
                       ((Position + Options.FindText.Length) < SourceText.Length &&
                        (Char.IsLetterOrDigit(SourceText[Position + Options.FindText.Length]) || SourceText[Position + Options.FindText.Length] == '_')))))
                {
                    // Do not select! to not affect in-selection find/replace.  //- this.TextEditor.Select(StartPosition, Options.FindText.Length);
                    var AbsolutePosition = ((Options.OnlyInSelection ? this.TextEditor.SelectionStart : 0) + Position);
                    this.TextEditor.PostCall(
                        ted =>
                    {
                        ted.CaretOffset = AbsolutePosition;
                        var Location    = ted.Document.GetLocation(AbsolutePosition);
                        ted.ScrollTo(Location.Line, Location.Column);
                        ted.Focus();
                    });

                    return(Position);
                }

                Position = Position + Options.FindText.Length;
                if (Position >= SourceText.Length)
                {
                    return(-1);
                }

                Position = SourceText.IndexOf(Options.FindText, Position, CaseComparer);
            }

            return(-1);
        }
        private int FindOperation(FindAndReplacePanel.FindReplaceOptions Options, int Position)
        {
            // IMPORTANT: Position here represents the WPF so called 'symbols' (Rich-Text/XAML codes), not chars.
            var SourceRange = (Options.OnlyInSelection
                               ? this.TextEditor.Selection
                               : new TextRange(this.TextEditor.Document.ContentStart, this.TextEditor.Document.ContentEnd));

            if (SourceRange == null)
            {
                return(-1);
            }

            var CaseComparer = (Options.IsCaseSensitive
                                ? StringComparison.Ordinal  // Must use 'ordinal' to avoid considering hyphens as part of words (culture dependant).
                                : StringComparison.OrdinalIgnoreCase);

            var FoundRange = SourceRange.Start.GetPositionAtOffset(Position).FindText(Options.FindText, CaseComparer);

            // This loop is just to skip the non-whole-words cases
            while (FoundRange != null)
            {
                Position = SourceRange.Start.GetOffsetToPosition(FoundRange.Start);

                if (!(Options.ConsiderWholeWord &&
                      ((Char.IsLetterOrDigit(FoundRange.PreviousChar()) || FoundRange.PreviousChar() == '_') ||
                       (Char.IsLetterOrDigit(FoundRange.NextChar()) || FoundRange.NextChar() == '_'))))
                {
                    // Do not select! to not affect in-selection find/replace.
                    var AbsolutePosition = FoundRange.Start;
                    this.TextEditor.PostCall(
                        ted =>
                    {
                        ted.CaretPosition = AbsolutePosition;
                        var Container     = AbsolutePosition.Parent as FrameworkContentElement;
                        if (Container != null)
                        {
                            Container.BringIntoView();
                            ted.Focus();
                        }
                    });

                    return(Position);
                }

                FoundRange = FoundRange.End.GetNextContextPosition(LogicalDirection.Forward).FindText(Options.FindText);
            }

            return(-1);
        }
        private void ReplaceOperation(FindAndReplacePanel.FindReplaceOptions Options, int Position)
        {
            var SourceRange = (Options.OnlyInSelection
                         ? this.TextEditor.Selection
                         : (new TextRange(this.TextEditor.Document.ContentStart, this.TextEditor.Document.ContentEnd)));

            var SourceText = SourceRange.Text;

            if (SourceRange == null || Position < 0 || SourceRange.Start.GetPositionAtOffset(Position) == null)
            {
                return;
            }

            var FoundRange = new TextRange(SourceRange.Start.GetPositionAtOffset(Position),
                                           SourceRange.Start.GetPositionAtOffset(Position + Options.FindText.Length - 1));

            FoundRange.Text = Options.ReplaceText;
        }