Beispiel #1
0
 public void WaitForLightBulbSession()
 {
     ExecuteOnActiveView(view =>
     {
         var broker = GetComponentModel().GetService <ILightBulbBroker>();
         LightBulbHelper.WaitForLightBulbSession(broker, view);
     });
 }
Beispiel #2
0
        public void WaitForLightBulbSession()
        {
            ThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var view   = GetActiveTextView();
                var broker = GetComponentModel().GetService <ILightBulbBroker>();
                await LightBulbHelper.WaitForLightBulbSessionAsync(broker, view);
            });
        }
        private async Task <IEnumerable <SuggestedActionSet> > ShowLightBulbAsync(CancellationToken cancellationToken)
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            var shell = await GetRequiredGlobalServiceAsync <SVsUIShell, IVsUIShell>(cancellationToken);

            var cmdGroup   = typeof(VSConstants.VSStd14CmdID).GUID;
            var cmdExecOpt = OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER;

            var    cmdID = VSConstants.VSStd14CmdID.ShowQuickFixes;
            object?obj   = null;

            shell.PostExecCommand(cmdGroup, (uint)cmdID, (uint)cmdExecOpt, ref obj);

            var view = await GetActiveTextViewAsync(cancellationToken);

            var broker = await GetComponentModelServiceAsync <ILightBulbBroker>(cancellationToken);

            await LightBulbHelper.WaitForLightBulbSessionAsync(broker, view, cancellationToken);

            return(await LightBulbHelper.WaitForItemsAsync(broker, view, cancellationToken));
        }
Beispiel #4
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>());
        }