示例#1
0
 public CommandState GetCommandState(InsertAllMatchingCaretsCommandArgs args)
 {
     return(CommandState.Available);
 }
示例#2
0
        public bool ExecuteCommand(InsertAllMatchingCaretsCommandArgs args, CommandExecutionContext executionContext)
        {
            var broker = args.TextView.GetMultiSelectionBroker();

            if (broker.PrimarySelection.IsEmpty)
            {
                broker.TryPerformActionOnSelection(broker.PrimarySelection, PredefinedSelectionTransformations.SelectCurrentWord, out _);
            }

            var navigator = TextSearchNavigatorFactoryService.CreateSearchNavigator(args.TextView.TextViewModel.EditBuffer);

            var    primaryRegion = broker.PrimarySelection;
            string searchString  = primaryRegion.Extent.GetText();

            // Intentionally look at all whitespace here
            if (!string.IsNullOrEmpty(searchString))
            {
                var snapshot     = args.TextView.TextViewModel.EditBuffer.CurrentSnapshot;
                var documentSpan = snapshot.CreateTrackingSpan(0, snapshot.Length, SpanTrackingMode.EdgeInclusive);

                navigator.SearchSpan    = documentSpan;
                navigator.SearchTerm    = searchString;
                navigator.StartPoint    = broker.PrimarySelection.Start.Position;
                navigator.SearchOptions = FindOptions.MatchCase | FindOptions.Multiline | FindOptions.Wrap;

                navigator.Find(); // Get and ignore the primary region

                var newlySelectedSpans = new List <SnapshotSpan>();
                var oldSelectedSpans   = broker.SelectedSpans;

                while (navigator.Find())
                {
                    var found = navigator.CurrentResult.Value;

                    // If we have found the primary region again, we've gone the whole way around the document.
                    if (found.OverlapsWith(primaryRegion.Extent.SnapshotSpan))
                    {
                        break;
                    }

                    if (!oldSelectedSpans.OverlapsWith(found))
                    {
                        newlySelectedSpans.Add(found);
                    }
                }

                // Make sure that none of the newly selected spans overlap
                for (int i = 0; i < (newlySelectedSpans.Count - 1); i++)
                {
                    if (newlySelectedSpans[i].OverlapsWith(newlySelectedSpans[i + 1]))
                    {
                        newlySelectedSpans.RemoveAt(i + 1);

                        // decrement 1 so we can compare i and what used to be i+2 next time
                        i--;
                    }
                }

                var newlySelectedSpanCollection = new NormalizedSnapshotSpanCollection(newlySelectedSpans);

                // Ok, we've figured out what selections we want to add. Now we need to expand any outlining regions before finally adding the selections
                var outliningManager = OutliningManagerService.GetOutliningManager(args.TextView);
                if (outliningManager != null)
                {
                    var extent = new SnapshotSpan(
                        newlySelectedSpanCollection[0].Start,
                        newlySelectedSpanCollection[newlySelectedSpanCollection.Count - 1].End);
                    outliningManager.ExpandAll(extent, collapsible =>
                    {
                        return(newlySelectedSpanCollection.IntersectsWith(collapsible.Extent.GetSpan(broker.CurrentSnapshot)));
                    });
                }

                // Yay, we can finally actually add the selections
                for (int i = 0; i < newlySelectedSpans.Count; i++)
                {
                    broker.AddSelectionRange(newlySelectedSpans.Select(span => new Selection(span, broker.PrimarySelection.IsReversed)));
                }

                return(true);
            }

            return(false);
        }