/// <summary>
        /// Gets the caret column position when moving caret vertically.
        /// If the Caret is already on first or last line the caret is set
        /// to the start of file or to the end of the file, respectively.
        /// If the Caret is positioned left off the stored column position
        /// the caret is set to the stored column position or the end of line.
        /// </summary>
        /// <param name="caretPosition"></param>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        internal int GetCaretColumnPosition(int caretPosition, ITextSnapshot snapshot)
        {
            var previousLineNumber = snapshot.GetLineNumberFromPosition(Caret.GetPosition(snapshot));
            var caretLine          = snapshot.GetLineFromPosition(caretPosition);

            if (caretLine.LineNumber == previousLineNumber && caretLine.LineNumber == 0)
            {
                return(0);
            }
            else if (caretLine.LineNumber == previousLineNumber &&
                     caretLine.LineNumber == snapshot.LineCount - 1)
            {
                return(snapshot.Length);
            }
            else if (ColumnPosition > (caretPosition - caretLine.Start.Position))
            {
                var correctColumnPosition = (ColumnPosition > caretLine.Length) ?
                                            caretLine.Length
                    : ColumnPosition;
                return(caretLine.Start.Position + correctColumnPosition);
            }
            else
            {
                return(caretPosition);
            }
        }
        internal void UpdateSelection(int previousCaretPosition, ITextSnapshot snapshot)
        {
            var caretPosition = Caret.GetPosition(snapshot);

            if (IsSelection())
            {
                var startPosition = Start.GetPosition(snapshot);
                var endPosition   = End.GetPosition(snapshot);
                if (caretPosition < startPosition && startPosition < previousCaretPosition)
                {
                    End   = Start;
                    Start = Caret;
                }
                else if (previousCaretPosition < endPosition && endPosition < caretPosition)
                {
                    Start = End;
                    End   = Caret;
                }
                else if (caretPosition >= startPosition && startPosition != previousCaretPosition)
                {
                    End = Caret;
                }
                else
                {
                    Start = Caret;
                }
            }
            else
            {
                Start = caretPosition > previousCaretPosition?
                        snapshot.CreateTrackingPoint(previousCaretPosition)
                            : Caret;

                End = caretPosition > previousCaretPosition ?
                      Caret
                    : snapshot.CreateTrackingPoint(previousCaretPosition);
            }
            if (Start.GetPosition(snapshot) == End.GetPosition(snapshot))
            {
                Start = null;
                End   = null;
            }
        }
 internal bool IsReversed(ITextSnapshot snapshot)
 {
     return(Caret.GetPosition(snapshot) == Start?.GetPosition(snapshot));
 }
예제 #4
0
 internal bool Reversing(ITextSnapshot snapshot)
 {
     return(Caret.GetPosition(snapshot) < End?.GetPosition(snapshot));
 }