Ejemplo n.º 1
0
 public void SendDelete()
 {
     EditorOperations.Delete();
 }
 public virtual void SendDelete()
 => EditorOperations.Delete();
Ejemplo n.º 3
0
        // Helped by source of Microsoft.VisualStudio.Text.Editor.DragDrop.DropHandlerBase.cs in assembly Microsoft.VisualStudio.Text.UI.Wpf, Version=14.0.0.0
        public static int HandleCommand(IWpfTextView textView, IClassifier classifier, IOleCommandTarget commandTarget, IEditorOperations editorOperations, bool shiftPressed = false)
        {
            //Guid cmdGroup = VSConstants.VSStd2K;
            var            selectedText  = editorOperations.SelectedText;
            ITrackingPoint trackingPoint = null;

            if (selectedText.Length == 0)
            {
                // if nothing is selected, we can consider the current line as a selection
                var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                trackingPoint = textView.TextSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);

                // Select all the text on the current line. Leaves caret at the start of the next line or end of line if last line.
                editorOperations.SelectLine(textView.Caret.ContainingTextViewLine, false);
                var text = editorOperations.SelectedText;
                // Clear the selection so new inserts will not overwrite the selected line. Caret stays at start of next line.
                editorOperations.ResetSelection();

                // Hack for Last Line: If last line of file, introduce a new line character then delete it after duplicating the line.
                var endOfFile = !EndsWithNewLine(text);
                if (endOfFile)
                {
                    // We are on the last line. Will need to insert a new line. Will be removed later.
                    editorOperations.InsertNewLine();
                }

                // Now we are at the beginning of the line we can insert the duplicate text.
                editorOperations.InsertText(text);

                // Clean up any newline character introduced by earlier hack
                if (endOfFile)
                {
                    editorOperations.Delete();
                }

                // Return the cursor to its original position, then move it down one line (unless doing reverse)
                textView.Caret.MoveTo(new VirtualSnapshotPoint(trackingPoint.GetPoint(textView.TextSnapshot)).TranslateTo(textView.TextSnapshot));
                if (!shiftPressed)
                {
                    editorOperations.MoveLineDown(false);
                }
            }
            else
            {
                var selection    = textView.Selection;
                var isReversed   = selection.IsReversed;
                var text         = selectedText;
                var textSnapshot = textView.TextSnapshot;
                var list         = new List <ITrackingSpan>();
                //var shiftKeyPressed=textVie
                foreach (SnapshotSpan snapshotSpan in selection.SelectedSpans)
                {
                    list.Add(textSnapshot.CreateTrackingSpan(snapshotSpan, SpanTrackingMode.EdgeExclusive));
                }
                if (!selection.IsEmpty)
                {
                    selection.Clear();
                }


                if (list.Count < 2)
                {
                    var offset = 0;
                    var virtualBufferPosition = editorOperations.TextView.Caret.Position.VirtualBufferPosition;
                    var point = editorOperations.TextView.Caret.Position.BufferPosition;
                    virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(point.Add(text.Length))
                       : !isReversed && shiftPressed ? new VirtualSnapshotPoint(point.Add(-text.Length)) : virtualBufferPosition;

                    trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                    if (virtualBufferPosition.IsInVirtualSpace)
                    {
                        offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                    }
                    textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                    editorOperations.InsertText(text);
                    var insertionPoint = trackingPoint.GetPoint(textView.TextSnapshot);
                    if (offset != 0)
                    {
                        insertionPoint = insertionPoint.Add(offset);
                    }

                    var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint);
                    var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(text.Length));
                    if (isReversed)
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint2, virtualSnapshotPoint1, TextSelectionMode.Stream);
                    }
                    else
                    {
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Stream);
                    }
                }
                else
                {
                    var trackingPointOffsetList = new List <Tuple <ITrackingPoint, int, int> >();
                    //Insert Text!
                    if (isReversed)
                    {
                        list.Reverse();
                    }
                    foreach (var trackingSpan in list)
                    {
                        var span = trackingSpan.GetSpan(textSnapshot);
                        text = trackingSpan.GetText(textSnapshot);
                        var offset         = 0;
                        var insertionPoint = !isReversed?trackingSpan.GetEndPoint(span.Snapshot) : trackingSpan.GetStartPoint(span.Snapshot);

                        var virtualBufferPosition = new VirtualSnapshotPoint(insertionPoint);
                        virtualBufferPosition = isReversed && !shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(text.Length))
                           : !isReversed && shiftPressed ? new VirtualSnapshotPoint(insertionPoint.Add(-text.Length)) : virtualBufferPosition;


                        trackingPoint = textSnapshot.CreateTrackingPoint(virtualBufferPosition.Position, PointTrackingMode.Negative);
                        if (virtualBufferPosition.IsInVirtualSpace)
                        {
                            offset = editorOperations.GetWhitespaceForVirtualSpace(virtualBufferPosition).Length;
                        }
                        trackingPointOffsetList.Add(new Tuple <ITrackingPoint, int, int>(trackingPoint, offset, text.Length));
                        textView.Caret.MoveTo(virtualBufferPosition.TranslateTo(textView.TextSnapshot));
                        editorOperations.InsertText(text);
                    }
                    //Make Selections
                    {
                        var trackingPointOffset = trackingPointOffsetList.First();
                        var insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint1 = new VirtualSnapshotPoint(insertionPoint.Add(!isReversed ? 0 : trackingPointOffset.Item3));

                        trackingPointOffset = trackingPointOffsetList.Last();
                        insertionPoint      = trackingPointOffset.Item1.GetPoint(textView.TextSnapshot);
                        if (trackingPointOffset.Item2 != 0)
                        {
                            insertionPoint = insertionPoint.Add(trackingPointOffset.Item2);
                        }
                        var virtualSnapshotPoint2 = new VirtualSnapshotPoint(insertionPoint.Add(isReversed ? 0 : trackingPointOffset.Item3));
                        editorOperations.SelectAndMoveCaret(virtualSnapshotPoint1, virtualSnapshotPoint2, TextSelectionMode.Box);
                    }
                }
            }

            return(VSConstants.S_OK);
        }
Ejemplo n.º 4
0
        public CommandResult Invoke(Guid group, int id, object args, ref object outargs)
        {
            CommandResult result = _braceCompletionTarget.Invoke(group, id, args, ref outargs);

            if (result.WasExecuted)
            {
                return(result);
            }

            if (group == VSConstants.VSStd2K)
            {
                switch (id)
                {
                case (int)VSConstants.VSStd2KCmdID.TYPECHAR:
                    string text;
                    if (args is char)
                    {
                        text = args.ToString();
                    }
                    else
                    {
                        text = Char.ConvertFromUtf32((System.UInt16)args);
                    }

                    result = this.PerformEditAction(() => _editorOperations.InsertText(text));
                    break;

                case (int)VSConstants.VSStd2KCmdID.BACKSPACE:
                    result = this.PerformEditAction(() => _editorOperations.Backspace());
                    break;

                case (int)VSConstants.VSStd2KCmdID.DELETE:
                    result = this.PerformEditAction(() => _editorOperations.Delete());
                    break;

                case (int)VSConstants.VSStd2KCmdID.CANCEL:
                    _editorOperations.ResetSelection();
                    break;

                case (int)VSConstants.VSStd2KCmdID.DOWN_EXT:
                    _editorOperations.MoveLineDown(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.DOWN:
                    _editorOperations.MoveLineDown(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.RIGHT_EXT:
                    _editorOperations.MoveToNextCharacter(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.RIGHT:
                    _editorOperations.MoveToNextCharacter(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.LEFT_EXT:
                    _editorOperations.MoveToPreviousCharacter(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.LEFT:
                    _editorOperations.MoveToPreviousCharacter(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.UP_EXT:
                    _editorOperations.MoveLineUp(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.UP:
                    _editorOperations.MoveLineUp(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.HOME_EXT:
                    _editorOperations.MoveToHome(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.HOME:
                    _editorOperations.MoveToHome(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.PAGEUP_EXT:
                    _editorOperations.PageUp(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.PAGEUP:
                    _editorOperations.PageUp(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.PAGEDN_EXT:
                    _editorOperations.PageDown(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.PAGEDN:
                    _editorOperations.PageDown(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.END_EXT:
                    _editorOperations.MoveToEndOfLine(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.END:
                    _editorOperations.MoveToEndOfLine(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.BACKTAB:
                    result = this.PerformEditAction(() => _editorOperations.Unindent());
                    break;

                case (int)VSConstants.VSStd2KCmdID.TAB:
                    result = this.PerformEditAction(() => _editorOperations.Indent());
                    break;

                case (int)VSConstants.VSStd2KCmdID.RETURN:
                    result = this.PerformEditAction(() => _editorOperations.InsertNewLine());
                    break;

                case (int)VSConstants.VSStd2KCmdID.CTLMOVERIGHT:
                    _editorOperations.MoveToNextWord(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.CTLMOVELEFT:
                    _editorOperations.MoveToPreviousWord(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.TOPLINE_EXT:
                    _editorOperations.MoveToStartOfDocument(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.BOTTOMLINE_EXT:
                    _editorOperations.MoveToEndOfDocument(true);
                    break;

                case (int)VSConstants.VSStd2KCmdID.INSERT:
                    bool isEnabled = _editorOperations.Options.IsOverwriteModeEnabled();
                    _editorOperations.Options.SetOptionValue(DefaultTextViewOptions.OverwriteModeId, !isEnabled);
                    break;

                case (int)VSConstants.VSStd2KCmdID.DELETEWORDLEFT:
                    result = this.PerformEditAction(() => _editorOperations.DeleteWordToLeft());
                    break;

                case (int)VSConstants.VSStd2KCmdID.DELETEWORDRIGHT:
                    result = this.PerformEditAction(() => _editorOperations.DeleteWordToRight());
                    break;

                case (int)VSConstants.VSStd2KCmdID.SELECTALL:
                    _editorOperations.SelectAll();
                    break;

                case (int)VSConstants.VSStd2KCmdID.SELECTCURRENTWORD:
                    _editorOperations.SelectCurrentWord();
                    break;

                case (int)VSConstants.VSStd2KCmdID.WORDNEXT:
                    _editorOperations.MoveToNextWord(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.WORDPREV:
                    _editorOperations.MoveToPreviousWord(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.TOPLINE:
                    _editorOperations.MoveToStartOfDocument(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.BOTTOMLINE:
                    _editorOperations.MoveToEndOfDocument(false);
                    break;

                case (int)VSConstants.VSStd2KCmdID.SCROLLUP:
                    _editorOperations.ScrollUpAndMoveCaretIfNecessary();
                    break;

                case (int)VSConstants.VSStd2KCmdID.SCROLLDN:
                    _editorOperations.ScrollDownAndMoveCaretIfNecessary();
                    break;

                case (int)VSConstants.VSStd2KCmdID.COPY:
                    _editorOperations.CopySelection();
                    break;

                case (int)VSConstants.VSStd2KCmdID.CUT:
                    return(this.PerformEditAction(() => _editorOperations.CutSelection()));

                case (int)VSConstants.VSStd2KCmdID.PASTE:
                    string pastedText = args as string;

                    if (pastedText != null)
                    {
                        return(this.PerformEditAction(() => _editorOperations.InsertText(pastedText)));
                    }
                    else
                    {
                        return(this.PerformEditAction(() => _editorOperations.Paste()));
                    }

                case (int)VSConstants.VSStd2KCmdID.UNDO:

                    if (UndoManager != null &&
                        UndoManager.TextBufferUndoHistory.CanUndo)
                    {
                        UndoManager.TextBufferUndoHistory.Undo(1);
                        break;
                    }

                    return(CommandResult.Disabled);

                case (int)VSConstants.VSStd2KCmdID.REDO:

                    if (UndoManager != null &&
                        UndoManager.TextBufferUndoHistory.CanRedo)
                    {
                        UndoManager.TextBufferUndoHistory.Redo(1);
                        break;
                    }

                    return(CommandResult.Disabled);

                default:
                    return(CommandResult.NotSupported);
                }

                _braceCompletionTarget.PostProcessInvoke(CommandResult.Executed, group, id, args, ref outargs);
                return(result);
            }

            return(CommandResult.NotSupported);
        }
Ejemplo n.º 5
0
        internal static void DeleteToEndOfPhysicalLine(this IEditorOperations editorOperations)
        {
            int position = editorOperations.TextView.GetCaretPosition().Position;

            editorOperations.Delete(position, editorOperations.GetCaretPhysicalLine().End - position);
        }
Ejemplo n.º 6
0
 internal static void Delete(this IEditorOperations editorOperations, int start, int length)
 {
     editorOperations.Delete(new Span(start, length));
 }