Пример #1
0
        public static Task <bool> WaitForLightBulbSessionAsync(
            ILightBulbBroker broker,
            IWpfTextView view
            )
        {
            var startTime = DateTimeOffset.Now;

            return(Helper.RetryAsync(
                       async() =>
            {
                if (broker.IsLightBulbSessionActive(view))
                {
                    return true;
                }

                if (DateTimeOffset.Now > startTime + Helper.HangMitigatingTimeout)
                {
                    throw new InvalidOperationException(
                        "Expected a light bulb session to appear."
                        );
                }

                // checking whether there is any suggested action is async up to editor layer and our waiter doesn't track up to that point.
                // so here, we have no other way than sleep (with timeout) to see LB is available.
                await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(true);

                return broker.IsLightBulbSessionActive(view);
            },
                       TimeSpan.Zero
                       ));
        }
Пример #2
0
        public static async Task <bool> WaitForLightBulbSessionAsync(ILightBulbBroker broker, IWpfTextView view, CancellationToken cancellationToken)
        {
            var startTime = DateTimeOffset.Now;

            var active = await Helper.RetryAsync(async cancellationToken =>
            {
                if (broker.IsLightBulbSessionActive(view))
                {
                    return(true);
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    throw new InvalidOperationException("Expected a light bulb session to appear.");
                }

                // checking whether there is any suggested action is async up to editor layer and our waiter doesn't track up to that point.
                // so here, we have no other way than sleep (with timeout) to see LB is available.
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);

                return(broker.IsLightBulbSessionActive(view));
            }, TimeSpan.FromMilliseconds(1), cancellationToken);

            if (!active)
            {
                return(false);
            }

            await WaitForItemsAsync(broker, view, cancellationToken);

            return(true);
        }
Пример #3
0
        public static bool WaitForLightBulbSession(ILightBulbBroker broker, Microsoft.VisualStudio.Text.Editor.IWpfTextView view)
        => Helper.Retry(() => {
            if (broker.IsLightBulbSessionActive(view))
            {
                return(true);
            }

            // checking whether there is any suggested action is async up to editor layer and our waiter doesnt track up to that point.
            // so here, we have no other way than sleep (with timeout) to see LB is available.
            HostWaitHelper.PumpingWait(Task.Delay(TimeSpan.FromSeconds(1)));

            return(broker.IsLightBulbSessionActive(view));
        }, TimeSpan.FromSeconds(0));
Пример #4
0
        public static bool WaitForLightBulbSession(ILightBulbBroker broker, Microsoft.VisualStudio.Text.Editor.IWpfTextView view)
        {
            return Helper.Retry<bool>(() =>
            {
                if (broker.IsLightBulbSessionActive(view))
                {
                    return true;
                }

                // checking whether there is any suggested action is async up to editor layer and our waiter doesnt track up to that point.
                // so here, we have no other way than sleep (with timeout) to see LB is available.
                HostWaitHelper.PumpingWait(Task.Delay(TimeSpan.FromSeconds(1)));

                return broker.IsLightBulbSessionActive(view);
            }, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(20));
        }
Пример #5
0
        private IEnumerable <ISuggestedAction> GetLightBulbActions(ILightBulbBroker broker, IWpfTextView view)
        {
            if (!broker.IsLightBulbSessionActive(view))
            {
                var bufferType = view.TextBuffer.ContentType.DisplayName;
                throw new Exception(string.Format("No light bulb session in View!  Buffer content type={0}", bufferType));
            }

            var activeSession = broker.GetSession(view);

            if (activeSession == null || !activeSession.IsExpanded)
            {
                var bufferType = view.TextBuffer.ContentType.DisplayName;
                throw new InvalidOperationException(string.Format("No expanded light bulb session found after View.ShowSmartTag.  Buffer content type={0}", bufferType));
            }

            IEnumerable <SuggestedActionSet> actionSets;

            if (activeSession.TryGetSuggestedActionSets(out actionSets) != QuerySuggestedActionCompletionStatus.Completed)
            {
                actionSets = Array.Empty <SuggestedActionSet>();
            }

            return(SelectActions(actionSets));
        }
Пример #6
0
        public static async Task <bool> WaitForLightBulbSessionAsync(TestServices testServices, ILightBulbBroker broker, IWpfTextView view, CancellationToken cancellationToken)
        {
            await testServices.Editor.WaitForEditorOperationsAsync(cancellationToken);

            var active = broker.IsLightBulbSessionActive(view);

            if (!active)
            {
                return(false);
            }

            await WaitForItemsAsync(testServices, broker, view, cancellationToken);

            return(true);
        }
Пример #7
0
        public static async Task <bool> WaitForLightBulbSessionAsync(ILightBulbBroker broker, IWpfTextView view, CancellationToken cancellationToken)
        {
            var startTime = DateTimeOffset.Now;

            var active = await Helper.RetryAsync(async cancellationToken =>
            {
                if (broker.IsLightBulbSessionActive(view))
                {
                    return(true);
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    throw new InvalidOperationException("Expected a light bulb session to appear.");
                }

                if (broker.IsLightBulbSessionActive(view))
                {
                    var session             = broker.GetSession(view);
                    var hasSuggestedActions = await broker.HasSuggestedActionsAsync(session.ActionCategories, view, cancellationToken);

                    return(hasSuggestedActions);
                }

                return(false);
            }, TimeSpan.FromMilliseconds(1), cancellationToken);

            if (!active)
            {
                return(false);
            }

            await WaitForItemsAsync(broker, view, cancellationToken);

            return(true);
        }
Пример #8
0
        private ClassifiedToken[] GetLightbulbPreviewClassifications(
            string menuText,
            ILightBulbBroker broker,
            IWpfTextView view,
            IViewClassifierAggregatorService viewClassifierAggregator,
            IEditorPrimitivesFactoryService editorPrimitives)
        {
            LightBulbHelper.WaitForLightBulbSession(broker, view);

            var bufferType = view.TextBuffer.ContentType.DisplayName;

            if (!broker.IsLightBulbSessionActive(view))
            {
                throw new Exception(string.Format("No Active Smart Tags in View!  Buffer content type={0}", bufferType));
            }

            var activeSession = broker.GetSession(view);

            if (activeSession == null || !activeSession.IsExpanded)
            {
                throw new InvalidOperationException(string.Format("No expanded light bulb session found after View.ShowSmartTag.  Buffer content type={0}", bufferType));
            }

            if (!string.IsNullOrEmpty(menuText))
            {
                IEnumerable <SuggestedActionSet> actionSets;
                if (activeSession.TryGetSuggestedActionSets(out actionSets) != QuerySuggestedActionCompletionStatus.Completed)
                {
                    actionSets = Array.Empty <SuggestedActionSet>();
                }

                var set = actionSets.SelectMany(s => s.Actions).FirstOrDefault(a => a.DisplayText == menuText);
                if (set == null)
                {
                    throw new InvalidOperationException(
                              string.Format("ISuggestionAction {0} not found.  Buffer content type={1}", menuText, bufferType));
                }

                IWpfTextView preview = null;
                object       pane    = HostWaitHelper.PumpingWaitResult(set.GetPreviewAsync(CancellationToken.None));
                if (pane is System.Windows.Controls.UserControl)
                {
                    var container = ((System.Windows.Controls.UserControl)pane).FindName("PreviewDockPanel") as DockPanel;
                    var host      = FindDescendants <UIElement>(container).OfType <IWpfTextViewHost>().LastOrDefault();
                    preview = (host == null) ? null : host.TextView;
                }

                if (preview == null)
                {
                    throw new InvalidOperationException(string.Format("Could not find light bulb preview.  Buffer content type={0}", bufferType));
                }

                activeSession.Collapse();
                var classifier      = viewClassifierAggregator.GetClassifier(preview);
                var classifiedSpans = classifier.GetClassificationSpans(new SnapshotSpan(preview.TextBuffer.CurrentSnapshot, 0, preview.TextBuffer.CurrentSnapshot.Length));
                return(classifiedSpans.Select(x => new ClassifiedToken(x.Span.GetText().ToString(), x.ClassificationType.Classification)).ToArray());
            }

            activeSession.Collapse();
            return(Array.Empty <ClassifiedToken>());
        }