Esempio n. 1
0
        public SelectionState(ITextView view)
        {
            var selectionBroker = view.GetMultiSelectionBroker();
            var map             = SelectionState.EditToDataMap(view);

            if (selectionBroker.IsBoxSelection)
            {
                _primary = new SingleSelection(map, selectionBroker.BoxSelection);
                _isBox   = true;
            }
            else
            {
                if (selectionBroker.HasMultipleSelections)
                {
                    var selections = selectionBroker.AllSelections;
                    _selections = new SingleSelection[selections.Count];
                    for (int i = 0; (i < _selections.Length); ++i)
                    {
                        _selections[i] = new SingleSelection(map, selections[i]);
                    }
                }

                _primary = new SingleSelection(map, selectionBroker.PrimarySelection);
            }
        }
        public static void SetBoxSelection(this ITextView textView, IEnumerable <SnapshotSpan> spans, bool isReversed)
        {
            var first = textView.GetSpanInView(spans.First()).Single();
            var last  = textView.GetSpanInView(spans.Last()).Single();

            var length = last.End - first.Start;

            textView.GetMultiSelectionBroker().SetBoxSelection(new Selection(new SnapshotSpan(textView.TextSnapshot, first.Start, length), isReversed));
        }
Esempio n. 3
0
            IEnumerable <SelectionSpan> ISelectionUtil.GetSelectedSpans()
            {
                if (_textView.Selection.Mode == TextSelectionMode.Box)
                {
                    var caretPoint  = _textView.Caret.Position.VirtualBufferPosition;
                    var anchorPoint = _textView.Selection.AnchorPoint;
                    var activePoint = _textView.Selection.ActivePoint;
                    return(new[] { new SelectionSpan(caretPoint, anchorPoint, activePoint) });
                }

                var broker              = _textView.GetMultiSelectionBroker();
                var primarySelection    = broker.PrimarySelection;
                var secondarySelections = broker.AllSelections
                                          .Where(span => span != primarySelection)
                                          .Select(selection => GetSelectedSpan(selection));

                return(new[] { GetSelectedSpan(primarySelection) }.Concat(secondarySelections));
            }
        bool ExecuteCommandCore(EditorCommandArgs args, CommandExecutionContext context, Operation operation)
        {
            ITextView   textView   = args.TextView;
            ITextBuffer textBuffer = args.SubjectBuffer;

            if (!XmlBackgroundParser.TryGetParser(textBuffer, out var parser))
            {
                return(false);
            }

            var xmlParseResult    = parser.GetOrProcessAsync(textBuffer.CurrentSnapshot, default).Result;
            var xmlDocumentSyntax = xmlParseResult.XDocument;

            if (xmlDocumentSyntax == null)
            {
                return(false);
            }

            string description = operation.ToString();

            var editorOperations     = editorOperationsFactoryService.GetEditorOperations(textView);
            var multiSelectionBroker = textView.GetMultiSelectionBroker();
            var selectedSpans        = multiSelectionBroker.AllSelections.Select(selection => selection.Extent);

            using (context.OperationContext.AddScope(allowCancellation: false, description: description)) {
                ITextUndoHistory undoHistory = undoHistoryRegistry.RegisterHistory(textBuffer);

                using (ITextUndoTransaction undoTransaction = undoHistory.CreateTransaction(description)) {
                    switch (operation)
                    {
                    case Operation.Comment:
                        CommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax, editorOperations, multiSelectionBroker);
                        break;

                    case Operation.Uncomment:
                        UncommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax);
                        break;

                    case Operation.Toggle:
                        ToggleCommentSelection(textBuffer, selectedSpans, xmlDocumentSyntax, editorOperations, multiSelectionBroker);
                        break;
                    }

                    undoTransaction.Complete();
                }
            }

            return(true);
        }
Esempio n. 5
0
        public void Restore(ITextView view)
        {
            var selectionBroker = view.GetMultiSelectionBroker();
            var map             = SelectionState.EditToDataMap(view);

            if (_isBox)
            {
                selectionBroker.SetBoxSelection(_primary.Rehydrate(map, selectionBroker.CurrentSnapshot));
            }
            else
            {
                Selection[] rehydradedSelections = null;
                if (_selections != null)
                {
                    rehydradedSelections = new Selection[_selections.Length];
                    for (int i = 0; (i < _selections.Length); ++i)
                    {
                        rehydradedSelections[i] = _selections[i].Rehydrate(map, selectionBroker.CurrentSnapshot);
                    }
                }

                selectionBroker.SetSelectionRange(rehydradedSelections, _primary.Rehydrate(map, selectionBroker.CurrentSnapshot));
            }
        }
Esempio n. 6
0
 /// <summary>
 /// If you are looking at this, you're likely maintaining selection code, and should be aware that
 /// virtual whitespace allowances are not simply checking a flag.
 ///
 /// When dealing with virtual whitespace we have 3 major considerations:
 /// 1) Is the editor option enabled that allows arbitrary virtual whitespace navigation?
 /// 2) Is the current selection a box selection?
 /// 3) Are we at the beginning of a line that is impacted by Auto-Indent.
 ///
 /// This method ignores the 3rd element, since the virtual whitespace added there is not usually based
 /// on the previous whitespace, but on the auto-indent. This method is a convienence method that will return
 /// whether either of the first two conditions apply, and should be used anywhere arbitrary virtual whitespace
 /// is an option.
 /// </summary>
 public static bool IsVirtualSpaceOrBoxSelectionEnabled(this ITextView textView)
 {
     return(textView.Options.IsVirtualSpaceEnabled() || textView.GetMultiSelectionBroker().IsBoxSelection);
 }
        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));
        }