Пример #1
0
        void PerformActionInUndo(Func <bool> action)
        {
            ITrackingPoint    anchor = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.AnchorPoint.Position, PointTrackingMode.Positive);
            ITrackingPoint    active = _textView.TextSnapshot.CreateTrackingPoint(_textView.Selection.ActivePoint.Position, PointTrackingMode.Positive);
            bool              empty  = _textView.Selection.IsEmpty;
            TextSelectionMode mode   = _textView.Selection.Mode;

            using (var undo = _undoHistory.CreateTransaction("Untabify"))
            {
                _operations.AddBeforeTextBufferChangePrimitive();

                if (!action())
                {
                    undo.Cancel();
                    return;
                }

                ITextSnapshot after = _textView.TextSnapshot;

                _operations.SelectAndMoveCaret(new VirtualSnapshotPoint(anchor.GetPoint(after)),
                                               new VirtualSnapshotPoint(active.GetPoint(after)),
                                               mode,
                                               EnsureSpanVisibleOptions.ShowStart);

                _operations.AddAfterTextBufferChangePrimitive();

                undo.Complete();
            }
        }
        public void SelectAndMoveCaret(int offset)
        {
            var currentCaret = GetCaretPoint();

            EditorOperations.SelectAndMoveCaret(
                new VirtualSnapshotPoint(SubjectBuffer.CurrentSnapshot, currentCaret.BufferPosition.Position),
                new VirtualSnapshotPoint(SubjectBuffer.CurrentSnapshot, currentCaret.BufferPosition.Position + offset));
        }
Пример #3
0
        /// <summary>
        /// This method selects the text at the end of the drop operation.
        /// </summary>
        /// <remarks>
        /// This method will only be called if the drop of data resulted in an <see cref="DragDropEffects"/> other than DragDropEffects.None.
        /// </remarks>
        /// <param name="insertionPoint">The position at which data was inserted.</param>
        /// <param name="dataLength">The length of the data inserted in the buffer.</param>
        /// <param name="virtualSpaceLength">The length of whitespace inserted in the buffer to fill the gap between the closest buffer position
        ///  and the position at which data was dropped. This value will be non-zero only if data was dropped into virtual space.</param>
        /// <param name="dragDropInfo">The <see cref="DragDropInfo"/> class containing information about the drop.</param>
        /// <param name="reverse">True if the existing selection prior to the drop was reversed.</param>
        protected virtual void SelectText(SnapshotPoint insertionPoint, int dataLength, DragDropInfo dragDropInfo, bool reverse)
        {
            if (insertionPoint == null)
            {
                throw new ArgumentNullException(nameof(insertionPoint));
            }
            if (dragDropInfo == null)
            {
                throw new ArgumentNullException(nameof(dragDropInfo));
            }

            VirtualSnapshotPoint anchorPoint = new VirtualSnapshotPoint(insertionPoint);
            VirtualSnapshotPoint activePoint = new VirtualSnapshotPoint(insertionPoint.Add(dataLength));

            if (dragDropInfo.IsInternal && reverse)
            {
                _editorOperations.SelectAndMoveCaret(activePoint, anchorPoint, TextSelectionMode.Stream);
            }
            else
            {
                _editorOperations.SelectAndMoveCaret(anchorPoint, activePoint, TextSelectionMode.Stream);
            }
        }
Пример #4
0
        public IncrementalSearchResult SelectNextResult()
        {
            if (!IsActive)
            {
                throw new InvalidOperationException();
            }
            if (SearchString.Length == 0)
            {
                return(searchFailedResult);
            }
            TextView.Selection.Clear();
            var res = textSearchService.FindNext(CaretStartPosition.Position, true, new FindData(SearchString, CaretStartPosition.Snapshot, FindOptions, null));

            if (res == null)
            {
                return(searchFailedResult);
            }
            editorOperations.SelectAndMoveCaret(new VirtualSnapshotPoint(res.Value.Start), new VirtualSnapshotPoint(res.Value.End));
            if (SearchDirection == IncrementalSearchDirection.Forward)
            {
                return(new IncrementalSearchResult(passedEndOfBuffer: res.Value.Start < CaretStartPosition, passedStartOfBuffer: false, passedStartOfSearch: res.Value.Start != CaretStartPosition, resultFound: true));
            }
            return(new IncrementalSearchResult(passedEndOfBuffer: false, passedStartOfBuffer: res.Value.End > CaretStartPosition, passedStartOfSearch: res.Value.End != CaretStartPosition, resultFound: true));
        }
Пример #5
0
        private static void Navigate(IEditorOperations editorOperations, VirtualSnapshotSpan virtualSnapshotSpan, bool selectSpan)
        {
            VirtualSnapshotPoint selectionEnd = selectSpan ? virtualSnapshotSpan.End : virtualSnapshotSpan.Start;

            editorOperations.SelectAndMoveCaret(virtualSnapshotSpan.Start, selectionEnd, TextSelectionMode.Stream, EnsureSpanVisibleOptions.AlwaysCenter);
        }
        private static void Navigate(IEditorOperations editorOperations, VirtualSnapshotSpan virtualSnapshotSpan, bool selectSpan)
        {
            VirtualSnapshotPoint selectionEnd = selectSpan ? virtualSnapshotSpan.End : virtualSnapshotSpan.Start;

            editorOperations.SelectAndMoveCaret(virtualSnapshotSpan.Start, selectionEnd, TextSelectionMode.Stream, EnsureSpanVisibleOptions.AlwaysCenter);
        }
Пример #7
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            isSingleLine  = false;
            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);
                editorOperations.SelectLine(textView.Caret.ContainingTextViewLine, false);
                isSingleLine = true;
            }

            if (isSingleLine)
            {
                editorOperations.CopySelection();
                editorOperations.MoveToNextCharacter(false);
                editorOperations.Paste();
                editorOperations.MoveToPreviousCharacter(false);

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