public override void MoveToEndOfViewLine(bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;
            SnapshotPoint        selectionEnd  = this.TextView.Selection.AdvancedSelection.End.Position;
            bool selectionIsEmpty = TextView.Selection.IsEmpty;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            if (!selectionIsEmpty && !extendSelection)
            {
                var selectionEndLine = this.TextView.AdvancedTextView.GetTextViewLineContainingBufferPosition(selectionEnd);
                AdvancedCaret.MoveTo(selectionEndLine.End, PositionAffinity.Predecessor);
            }
            else
            {
                ITextViewLine line = this.AdvancedCaret.ContainingTextViewLine;
                AdvancedCaret.MoveTo(line.End, line.IsLastTextViewLineForSnapshotLine ? PositionAffinity.Successor : PositionAffinity.Predecessor);
            }

            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override void MoveToNextCharacter(bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;
            TextPoint            endPoint      = TextView.Selection.GetEndPoint();
            bool selectionIsEmpty = TextView.Selection.IsEmpty;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            // If the selection is not empty, VS clears the selection and sets
            // the caret at the end of the selection (regardless of it were reversed or not)
            if (!extendSelection && !selectionIsEmpty)
            {
                MoveTo(endPoint.CurrentPosition);
            }
            else
            {
                AdvancedCaret.MoveToNextCaretPosition();
            }

            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override bool DeletePrevious()
        {
            if (TextView.Selection.IsEmpty)
            {
                if (AdvancedCaret.InVirtualSpace)
                {
                    AdvancedCaret.MoveToPreviousCaretPosition();
                }
                else
                {
                    if (!CaretPoint.DeletePrevious())
                    {
                        return(false);
                    }
                    if (AdvancedCaret.InVirtualSpace)
                    {
                        AdvancedCaret.MoveTo(AdvancedCaret.Position.BufferPosition);
                    }
                }
            }
            else
            {
                if (!TextView.Selection.Delete())
                {
                    return(false);
                }
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override void MoveToLine(int lineNumber)
        {
            DisplayTextPoint caretLocation = CaretPoint;

            TextView.Selection.Clear();
            caretLocation.MoveToLine(lineNumber);
            AdvancedCaret.MoveTo(caretLocation.AdvancedTextPoint);
            AdvancedCaret.EnsureVisible();
        }
        public override bool RemovePreviousIndent()
        {
            if (!CaretPoint.RemovePreviousIndent())
            {
                return(false);
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override bool InsertIndent()
        {
            if (!CaretPoint.InsertIndent())
            {
                return(false);
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override bool TransposeCharacter()
        {
            if (!CaretPoint.TransposeCharacter())
            {
                return(false);
            }

            TextView.Selection.Clear();
            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override void MoveToStartOfDocument(bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            AdvancedCaret.MoveTo(new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, 0));
            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override void MoveTo(int position, bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint  = this.AdvancedCaret.Position.VirtualBufferPosition;
            SnapshotPoint        bufferPosition = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, position);

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            AdvancedCaret.MoveTo(bufferPosition);
            AdvancedCaret.EnsureVisible();
            this.UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override void MoveToPreviousWord(bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;
            DisplayTextPoint     caretLocation = CaretPoint;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            caretLocation.MoveToPreviousWord();
            AdvancedCaret.MoveTo(caretLocation.AdvancedTextPoint);
            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override void MoveToLine(int lineNumber, int offset, bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            ITextSnapshotLine line = TextView.AdvancedTextView.TextSnapshot.GetLineFromLineNumber(lineNumber);

            AdvancedCaret.MoveTo(new VirtualSnapshotPoint(line, offset));

            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        public override bool TransposeLine(int lineNumber)
        {
            if (!TextView.Selection.IsEmpty)
            {
                return(true);
            }

            if (!CaretPoint.TransposeLine(lineNumber))
            {
                return(false);
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override bool InsertText(string text)
        {
            if (!TextView.Selection.IsEmpty)
            {
                if (!TextView.Selection.Delete())
                {
                    return(false);
                }
            }

            if (!CaretPoint.InsertText(text))
            {
                return(false);
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override bool TransposeLine()
        {
            if (TextView.Selection.IsEmpty)
            {
                TextPoint caretPoint = CaretPoint;
                double    oldLeft    = AdvancedCaret.Left;

                if (!caretPoint.TransposeLine())
                {
                    return(false);
                }

                AdvancedCaret.MoveTo(caretPoint.AdvancedTextPoint, PositionAffinity.Successor, false);
                AdvancedCaret.EnsureVisible();

                ITextViewLine newLine = AdvancedTextViewLine;
                AdvancedCaret.MoveTo(newLine, oldLeft);
                AdvancedCaret.EnsureVisible();
            }
            return(true);
        }
        public override bool DeleteNext()
        {
            if (TextView.Selection.IsEmpty)
            {
                if (!CaretPoint.DeleteNext())
                {
                    return(false);
                }
            }
            else
            {
                if (!TextView.Selection.Delete())
                {
                    return(false);
                }
            }

            AdvancedCaret.EnsureVisible();

            return(true);
        }
        public override void MoveToPreviousCharacter(bool extendSelection)
        {
            VirtualSnapshotPoint oldCaretPoint = this.AdvancedCaret.Position.VirtualBufferPosition;
            TextPoint            startPoint    = TextView.Selection.GetStartPoint();
            bool selectionIsEmpty = TextView.Selection.IsEmpty;

            if (!extendSelection)
            {
                TextView.AdvancedTextView.Selection.Clear();
            }

            if (!extendSelection && !selectionIsEmpty)
            {
                MoveTo(startPoint.CurrentPosition);
            }
            else
            {
                AdvancedCaret.MoveToPreviousCaretPosition();
            }

            AdvancedCaret.EnsureVisible();
            UpdateSelection(extendSelection, oldCaretPoint);
        }
        private void PageUpDown(ScrollDirection direction)
        {
            if (direction == ScrollDirection.Up)
            {
                //If we scrolled a full page, then the bottom of the first fully visible line will be below
                //the bottom of the view.
                ITextViewLine firstVisibleLine     = TextView.AdvancedTextView.TextViewLines.FirstVisibleLine;
                SnapshotPoint oldFullyVisibleStart = (firstVisibleLine.VisibilityState == VisibilityState.FullyVisible)
                                           ? firstVisibleLine.Start
                                           : firstVisibleLine.EndIncludingLineBreak; //Start of next line.

                if (TextView.AdvancedTextView.ViewScroller.ScrollViewportVerticallyByPage(direction))
                {
                    ITextViewLine newFirstLine = TextView.AdvancedTextView.TextViewLines.GetTextViewLineContainingBufferPosition(oldFullyVisibleStart);

                    //The old fully visible line should -- if we scrolled as much as we could -- be partially
                    //obscured. The shortfall between a full page and what we actually scrolled is the distance
                    //between the bottom of that line and the bottom of the screen.
                    if (TextView.AdvancedTextView.ViewportBottom > newFirstLine.Bottom)
                    {
                        AdvancedCaret.MoveTo(TextView.AdvancedTextView.TextViewLines.FirstVisibleLine);
                    }
                    else
                    {
                        AdvancedCaret.MoveToPreferredCoordinates();
                    }
                }
            }
            else
            {
                //If we scroll a full page, the bottom of the last fully visible line will be
                //positioned at 0.0.
                ITextViewLine lastVisibleLine = TextView.AdvancedTextView.TextViewLines.LastVisibleLine;

                // If the last line in the buffer is fully visible , then just move the caret to
                // the last visible line
                if ((lastVisibleLine.VisibilityState == VisibilityState.FullyVisible) &&
                    (lastVisibleLine.End == lastVisibleLine.Snapshot.Length))
                {
                    AdvancedCaret.MoveTo(lastVisibleLine);

                    // No need to ensure the caret is visible since the line it just moved to is fully visible.
                    return;
                }

                SnapshotPoint oldFullyVisibleStart = ((lastVisibleLine.VisibilityState == VisibilityState.FullyVisible) || (lastVisibleLine.Start == 0))
                                           ? lastVisibleLine.Start
                                           : (lastVisibleLine.Start - 1); //Actually just a point on the previous line.

                if (TextView.AdvancedTextView.ViewScroller.ScrollViewportVerticallyByPage(direction))
                {
                    ITextViewLine newLastLine = TextView.AdvancedTextView.TextViewLines.GetTextViewLineContainingBufferPosition(oldFullyVisibleStart);
                    if (newLastLine.Bottom > TextView.AdvancedTextView.ViewportTop)
                    {
                        AdvancedCaret.MoveTo(TextView.AdvancedTextView.TextViewLines.LastVisibleLine);
                    }
                    else
                    {
                        AdvancedCaret.MoveToPreferredCoordinates();
                    }
                }
            }

            EnsureVisible();
        }
 public override void EnsureVisible()
 {
     AdvancedCaret.EnsureVisible();
 }