Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions ICSharpCode.AvalonEdit/Editing/CaretNavigationCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static CaretNavigationCommandHandler()
AddBinding(EditingCommands.SelectToDocumentEnd, Ctrl | Shift, Key.End, OnMoveCaretExtendSelection(CaretMovementType.DocumentEnd));

CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, OnSelectAll));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Stop, OnClearSelection));

TextAreaDefaultInputHandler.WorkaroundWPFMemoryLeak(InputBindings);
}
Expand All @@ -130,6 +131,15 @@ static void OnSelectAll(object target, ExecutedRoutedEventArgs args)
}
}

private static void OnClearSelection(object target, ExecutedRoutedEventArgs args)
{
TextArea textArea = GetTextArea(target);
if (textArea != null && textArea.Document != null) {
args.Handled = true;
textArea.ClearSelection();
}
}

static TextArea GetTextArea(object target)
{
return target as TextArea;
Expand All @@ -141,8 +151,21 @@ static ExecutedRoutedEventHandler OnMoveCaret(CaretMovementType direction)
TextArea textArea = GetTextArea(target);
if (textArea != null && textArea.Document != null) {
args.Handled = true;
textArea.ClearSelection();
MoveCaret(textArea, direction);

if (!TryMoveCaretToSelectionBoundary(textArea, direction))
MoveCaret(textArea, direction);

if (textArea.Selection is RectangleSelection rectangleSelection) {
var startPosition = rectangleSelection.StartPosition;
var endPosition = rectangleSelection.EndPosition;
startPosition.Column = endPosition.Column = textArea.Caret.Column;
startPosition.VisualColumn = endPosition.VisualColumn = textArea.Caret.VisualColumn;

textArea.Selection = new RectangleSelection(textArea, startPosition, endPosition);
} else {
textArea.ClearSelection();
}

textArea.Caret.BringCaretToView();
}
};
Expand Down Expand Up @@ -188,6 +211,39 @@ static ExecutedRoutedEventHandler OnMoveCaretBoxSelection(CaretMovementType dire
}

#region Caret movement

private static bool TryMoveCaretToSelectionBoundary(TextArea textArea, CaretMovementType direction)
{
if (textArea.Selection.IsEmpty)
return false;

int requiredCompareResult;
if (direction == CaretMovementType.CharLeft)
requiredCompareResult = -1;
else if (direction == CaretMovementType.CharRight)
requiredCompareResult = 1;
else
return false;

TextViewPosition newCaretPosition;
if (textArea.Selection is RectangleSelection) {
if (textArea.Selection.StartPosition.VisualColumn.CompareTo(textArea.Selection.EndPosition.VisualColumn) == requiredCompareResult)
newCaretPosition = textArea.Selection.StartPosition;
else
newCaretPosition = textArea.Selection.EndPosition;

newCaretPosition.Line = textArea.Caret.Line;
} else {
if (textArea.Selection.StartPosition.CompareTo(textArea.Selection.EndPosition) == requiredCompareResult)
newCaretPosition = textArea.Selection.StartPosition;
else
newCaretPosition = textArea.Selection.EndPosition;
}

textArea.Caret.Position = newCaretPosition;
return true;
}

internal static void MoveCaret(TextArea textArea, CaretMovementType direction)
{
double desiredXPos = textArea.Caret.DesiredXPos;
Expand Down