Esempio n. 1
0
        private static Selection InsertDiscoveredMatchRegion(IMultiSelectionBroker broker, Selection primaryRegion, SnapshotSpan found)
        {
            var newSpan      = new VirtualSnapshotSpan(found);
            var newSelection = new Selection(newSpan, primaryRegion.IsReversed);

            broker.AddSelection(newSelection);
            return(newSelection);
        }
Esempio n. 2
0
        public TestTextCaret(ITextView textView, ISmartIndentationService indentService, IMultiSelectionBroker multiSelectionBroker)
        {
            this.textView             = textView;
            this.indentService        = indentService;
            this.multiSelectionBroker = multiSelectionBroker;
            multiSelectionBroker.MultiSelectionSessionChanged += MultiSelectionSessionChanged;

            var point = new SnapshotPoint(textView.TextBuffer.CurrentSnapshot, 0);

            Position = new CaretPosition(
                new VirtualSnapshotPoint(point),
                textView.BufferGraph.CreateMappingPoint(point, PointTrackingMode.Positive),
                PositionAffinity.Successor
                );
        }
        private async Task ExecuteAsync()
        {
            IWpfTextView textView = await ServiceProvider.GetWpfTextViewAsync();

            if (textView == null)
            {
                return;
            }

            IMultiSelectionBroker multiSelection = textView.GetMultiSelectionBroker();

            if (multiSelection.HasMultipleSelections && multiSelection.AllSelections.Count > 1)
            {
                multiSelection.TryRemoveSelection(multiSelection.AllSelections[multiSelection.AllSelections.Count - 1]);
            }
        }
        private async Task ExecuteAsync()
        {
            IWpfTextView textView = await ServiceProvider.GetWpfTextViewAsync();

            if (textView == null)
            {
                return;
            }

            IMultiSelectionBroker multiSelectioBroker = textView.GetMultiSelectionBroker();

            if (multiSelectioBroker.HasMultipleSelections)
            {
                string firstSelectionValue = multiSelectioBroker.AllSelections[0].Extent.GetText();

                if (int.TryParse(firstSelectionValue, out int currentValue))
                {
                    using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                    {
                        for (int i = 0; i < multiSelectioBroker.AllSelections.Count; i++)
                        {
                            multiSelectioBroker.AllSelections[i].ReplaceWith(edit, currentValue.ToString());
                            currentValue++;
                        }

                        edit.Apply();
                    }
                }
                else if (firstSelectionValue.Length == 1)
                {
                    char currentChar = firstSelectionValue[0];

                    using (ITextEdit edit = textView.TextBuffer.CreateEdit())
                    {
                        for (int i = 0; i < multiSelectioBroker.AllSelections.Count; i++)
                        {
                            multiSelectioBroker.AllSelections[i].ReplaceWith(edit, currentChar.ToString());
                            currentChar++;
                        }

                        edit.Apply();
                    }
                }
            }
        }
Esempio n. 5
0
        private static Selection FindLastSelection(IMultiSelectionBroker broker)
        {
            int primaryIndex = 0;
            var selections   = broker.AllSelections;

            for (; selections[primaryIndex] != broker.PrimarySelection; primaryIndex++)
            {
                ;
            }
            // Intentional empty loop.

            if (primaryIndex != 0)
            {
                return(selections[primaryIndex - 1]);
            }
            else
            {
                return(selections[selections.Count - 1]);
            }
        }
        private async Task ExecuteAsync()
        {
            IWpfTextView textView = await ServiceProvider.GetWpfTextViewAsync();

            if (textView == null)
            {
                return;
            }

            IMultiSelectionBroker multiSelection = textView.GetMultiSelectionBroker();

            if (!multiSelection.HasMultipleSelections)
            {
                var firstSelection = new Selection(textView.Selection.SelectedSpans[0]);
                multiSelection.SetSelection(firstSelection);
            }

            var newSelection = multiSelection.TransformSelection(multiSelection.AllSelections[multiSelection.AllSelections.Count - 1], PredefinedSelectionTransformations.MoveToNextLine);

            multiSelection.AddSelection(newSelection);
        }
        public static void ToggleCommentSelection(
            ITextBuffer textBuffer,
            IEnumerable <VirtualSnapshotSpan> selectedSpans,
            XDocument xmlDocumentSyntax,
            IEditorOperations editorOperations         = null,
            IMultiSelectionBroker multiSelectionBroker = null)
        {
            var commentedSpans = GetCommentedSpansInSelection(xmlDocumentSyntax, selectedSpans);

            if (!commentedSpans.Any())
            {
                CommentSelection(
                    textBuffer,
                    selectedSpans,
                    xmlDocumentSyntax,
                    editorOperations,
                    multiSelectionBroker);
                return;
            }

            UncommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax);
        }
        public static void CommentSelection(
            ITextBuffer textBuffer,
            IEnumerable <VirtualSnapshotSpan> selectedSpans,
            XDocument xmlDocumentSyntax,
            IEditorOperations editorOperations         = null,
            IMultiSelectionBroker multiSelectionBroker = null)
        {
            var snapshot = textBuffer.CurrentSnapshot;

            var spansToExpandIntoComments = new List <SnapshotSpan> ();
            var newCommentInsertionPoints = new List <VirtualSnapshotPoint> ();

            foreach (var selectedSpan in selectedSpans)
            {
                // empty selection on an empty line results in inserting a new comment
                if (selectedSpan.IsEmpty && string.IsNullOrWhiteSpace(snapshot.GetLineFromPosition(selectedSpan.Start.Position).GetText()))
                {
                    newCommentInsertionPoints.Add(selectedSpan.Start);
                }
                else
                {
                    spansToExpandIntoComments.Add(selectedSpan.SnapshotSpan);
                }
            }

            NormalizedSnapshotSpanCollection commentSpans = NormalizedSnapshotSpanCollection.Empty;

            if (spansToExpandIntoComments.Any())
            {
                commentSpans = GetCommentableSpansInSelection(xmlDocumentSyntax, spansToExpandIntoComments);
            }

            using (var edit = textBuffer.CreateEdit()) {
                if (commentSpans.Any())
                {
                    CommentSpans(edit, commentSpans);
                }

                if (newCommentInsertionPoints.Any())
                {
                    CommentEmptySpans(edit, newCommentInsertionPoints, editorOperations);
                }

                edit.Apply();
            }

            var newSnapshot = textBuffer.CurrentSnapshot;

            // Now fix up the selections after the edit.
            var translatedInsertionPoints = newCommentInsertionPoints.Select(p => p.TranslateTo(newSnapshot)).ToHashSet();
            var fixupSelectionStarts      = selectedSpans.Where(s => !s.IsEmpty).ToDictionary(
                c => c.Start.TranslateTo(newSnapshot, PointTrackingMode.Positive),
                c => c.Start.TranslateTo(newSnapshot, PointTrackingMode.Negative));

            if (multiSelectionBroker != null)
            {
                multiSelectionBroker.PerformActionOnAllSelections(transformer => {
                    // for newly inserted comments position the caret inside the comment
                    if (translatedInsertionPoints.Contains(transformer.Selection.ActivePoint))
                    {
                        transformer.MoveTo(
                            new VirtualSnapshotPoint(transformer.Selection.End.Position - CloseComment.Length),
                            select: false,
                            insertionPointAffinity: PositionAffinity.Successor);
                        // for commented code make sure the new selection includes the opening <!--
                    }
                    else if (fixupSelectionStarts.TryGetValue(transformer.Selection.Start, out var newStart))
                    {
                        var end = transformer.Selection.End;
                        transformer.MoveTo(newStart, select: false, PositionAffinity.Successor);
                        transformer.MoveTo(end, select: true, PositionAffinity.Successor);
                    }
                });
            }
        }
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == _guid && nCmdID == _commandId && Clipboard.ContainsText())
            {
                IMultiSelectionBroker selection = _textView.GetMultiSelectionBroker();
                if (selection.HasMultipleSelections)
                {
                    string clipText = Clipboard.GetText(TextDataFormat.Text);

                    if (!string.IsNullOrEmpty(clipText))
                    {
                        int clipTextLinesCount = 0;
                        for (int i = 0; i < clipText.Length; i++)
                        {
                            if (clipText[i] == '\n')
                            {
                                clipTextLinesCount++;
                            }
                        }
                        if (clipText[clipText.Length - 1] != '\n')
                        {
                            clipTextLinesCount++;
                        }

                        if (clipTextLinesCount == selection.AllSelections.Count)
                        {
                            using (ITextEdit edit = _textView.TextBuffer.CreateEdit())
                            {
                                int currentClipTextPosition = 0;

                                for (int i = 0; i < selection.AllSelections.Count; i++)
                                {
                                    int clipNextTextPosition = clipText.IndexOf('\n', currentClipTextPosition);
                                    if (clipNextTextPosition < 0)
                                    {
                                        clipNextTextPosition = clipText.Length;
                                    }

                                    int clipLength = clipNextTextPosition - currentClipTextPosition - 1;
                                    if (clipLength > 0 && clipText[clipNextTextPosition - 2] == '\r')
                                    {
                                        clipLength--;
                                    }

                                    string newText = clipText.Substring(currentClipTextPosition, clipLength);

                                    selection.AllSelections[i].ReplaceWith(edit, newText);

                                    currentClipTextPosition = clipNextTextPosition + 1;
                                }
                                edit.Apply();
                            }

                            return(VSConstants.S_OK);
                        }
                    }
                }
            }

            return(_nextCommandTarget.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut));
        }
Esempio n. 10
0
 public TestTextSelection(ITextView textView, IMultiSelectionBroker multiSelectionBroker)
 {
     TextView = textView;
     this.multiSelectionBroker = multiSelectionBroker;
     multiSelectionBroker.MultiSelectionSessionChanged += MultiSelectionSessionChanged;
 }