static void OnCut(object target, ExecutedRoutedEventArgs args) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty && textArea.Options.CutCopyWholeLine) { DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); if (CopyWholeLine(textArea, currentLine)) { ISegment[] segmentsToDelete = textArea.GetDeletableSegments(new SimpleSegment(currentLine.Offset, currentLine.TotalLength)); for (int i = segmentsToDelete.Length - 1; i >= 0; i--) { textArea.Document.Remove(segmentsToDelete[i]); } } } else { if (CopySelectedText(textArea)) { textArea.RemoveSelectedText(); } } textArea.Caret.BringCaretToView(); args.Handled = true; } }
static void OnDeleteLine(object target, ExecutedRoutedEventArgs args) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { int firstLineIndex, lastLineIndex; if (textArea.Selection.Length == 0) { // There is no selection, simply delete current line firstLineIndex = lastLineIndex = textArea.Caret.Line; } else { // There is a selection, remove all lines affected by it (use Min/Max to be independent from selection direction) firstLineIndex = Math.Min(textArea.Selection.StartPosition.Line, textArea.Selection.EndPosition.Line); lastLineIndex = Math.Max(textArea.Selection.StartPosition.Line, textArea.Selection.EndPosition.Line); } DocumentLine startLine = textArea.Document.GetLineByNumber(firstLineIndex); DocumentLine endLine = textArea.Document.GetLineByNumber(lastLineIndex); textArea.Selection = Selection.Create(textArea, startLine.Offset, endLine.Offset + endLine.TotalLength); textArea.RemoveSelectedText(); args.Handled = true; } }
static void OnDeleteLine(object target, ExecutedRoutedEventArgs args) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); textArea.Selection = Selection.Create(textArea, currentLine.Offset, currentLine.Offset + currentLine.TotalLength); textArea.RemoveSelectedText(); args.Handled = true; } }
static ExecutedRoutedEventHandler OnDelete(RoutedUICommand selectingCommand) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { // call BeginUpdate before running the 'selectingCommand' // so that undoing the delete does not select the deleted character using (textArea.Document.RunUpdate()) { if (textArea.Selection.IsEmpty) { TextViewPosition oldCaretPosition = textArea.Caret.Position; if (textArea.Caret.IsInVirtualSpace && selectingCommand == EditingCommands.SelectRightByCharacter) { EditingCommands.SelectRightByWord.Execute(args.Parameter, textArea); } else { selectingCommand.Execute(args.Parameter, textArea); } bool hasSomethingDeletable = false; foreach (ISegment s in textArea.Selection.Segments) { if (textArea.GetDeletableSegments(s).Length > 0) { hasSomethingDeletable = true; break; } } if (!hasSomethingDeletable) { // If nothing in the selection is deletable; then reset caret+selection // to the previous value. This prevents the caret from moving through read-only sections. textArea.Caret.Position = oldCaretPosition; textArea.ClearSelection(); } } textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't do anything if the number of lines of a rectangular selection would be changed by the deletion. if (textArea.Selection is RectangleSelection && startPos.Line != endPos.Line) { return; } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic // Reuse the existing selection, so that we continue using the same logic textArea.Selection.StartSelectionOrSetEndpoint(startPos, endPos) .ReplaceSelectionWithText(string.Empty); } else { textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
static ExecutedRoutedEventHandler OnDelete(RoutedUICommand selectingCommand) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { // call BeginUpdate before running the 'selectingCommand' // so that undoing the delete does not select the deleted character using (textArea.Document.RunUpdate()) { if (textArea.Selection.IsEmpty) { selectingCommand.Execute(args.Parameter, textArea); } textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
static void OnCut(object target, ExecutedRoutedEventArgs args) { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty && textArea.Options.CutCopyWholeLine) { DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line); CopyWholeLine(textArea, currentLine); textArea.Document.Remove(currentLine.Offset, currentLine.TotalLength); } else { CopySelectedText(textArea); textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }
static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } else { textArea.RemoveSelectedText(); } textArea.Caret.BringCaretToView(); args.Handled = true; } }); }
private static ExecutedRoutedEventHandler OnDelete(CaretMovementType caretMovement) { return((target, args) => { TextArea textArea = GetTextArea(target); if (textArea != null && textArea.Document != null) { if (textArea.Selection.IsEmpty) { TextViewPosition startPos = textArea.Caret.Position; bool enableVirtualSpace = textArea.Options.EnableVirtualSpace; // When pressing delete; don't move the caret further into virtual space - instead delete the newline if (caretMovement == CaretMovementType.CharRight) { enableVirtualSpace = false; } double desiredXPos = textArea.Caret.DesiredXPos; TextViewPosition endPos = CaretNavigationCommandHandler.GetNewCaretPosition( textArea.TextView, startPos, caretMovement, enableVirtualSpace, ref desiredXPos); // GetNewCaretPosition may return (0,0) as new position, // thus we need to validate endPos before using it in the selection. if (endPos.Line < 1 || endPos.Column < 1) { endPos = new TextViewPosition(Math.Max(endPos.Line, 1), Math.Max(endPos.Column, 1)); } // Don't select the text to be deleted; just reuse the ReplaceSelectionWithText logic if (endPos.Column == 1) { var a = textArea.Document.GetLineByNumber(textArea.Caret.Line); var b = a.PreviousLine; if (b != null) { var c = b.PreviousLine; if (b.obs != null) { var obs = c.obs; var sa = textArea.Document.Text.Substring(a.Offset, a.Length + a.DelimiterLength); var bs = textArea.Document.Text.Substring(b.Offset, b.Length + b.DelimiterLength); var cs = textArea.Document.Text.Substring(c.Offset, c.Length + c.DelimiterLength); textArea.Document.Remove(c.Offset + c.Length, bs.Length + sa.Length /* + c.DelimiterLength*//* + a.DelimiterLength + b.DelimiterLength*/); textArea.Document.Insert(c.Offset + c.Length, sa); } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { var sel = new SimpleSelection(textArea, startPos, endPos); sel.ReplaceSelectionWithText(string.Empty); } } else { textArea.RemoveSelectedText(); } } textArea.Caret.BringCaretToView(); args.Handled = true; }); }