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;
         }
     });
 }
Пример #2
0
 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;
         }
     });
 }
Пример #3
0
        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;
            });
        }