From 13a68a829557562b8e29cd0a298e8442bb71c8ff Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Fri, 7 Jun 2019 16:33:15 -0500 Subject: [PATCH] IsCompletionActive returns false when no text view is active --- .../TestUtilities/InProcess/Editor_InProc.cs | 34 ++++++++++++++----- .../InProcess/InteractiveWindow_InProc.cs | 3 ++ .../InProcess/TextViewWindow_InProc.cs | 11 +++++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/Editor_InProc.cs b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/Editor_InProc.cs index a41880eee8a..68d32b28b97 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/Editor_InProc.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/Editor_InProc.cs @@ -52,31 +52,47 @@ private Editor_InProc() public static Editor_InProc Create() => new Editor_InProc(); + protected override bool HasActiveTextView() + => ErrorHandler.Succeeded(TryGetActiveTextViewHost().hr); + protected override IWpfTextView GetActiveTextView() => GetActiveTextViewHost().TextView; private static IVsTextView GetActiveVsTextView() { - var vsTextManager = GetGlobalService(); + var (textView, hr) = TryGetActiveVsTextView(); + Marshal.ThrowExceptionForHR(hr); + return textView; + } + private static (IVsTextView textView, int hr) TryGetActiveVsTextView() + { + var vsTextManager = GetGlobalService(); var hresult = vsTextManager.GetActiveView(fMustHaveFocus: 1, pBuffer: null, ppView: out var vsTextView); - Marshal.ThrowExceptionForHR(hresult); - - return vsTextView; + return (vsTextView, hresult); } private static IWpfTextViewHost GetActiveTextViewHost() + { + var (textViewHost, hr) = TryGetActiveTextViewHost(); + Marshal.ThrowExceptionForHR(hr); + return textViewHost; + } + + private static (IWpfTextViewHost textViewHost, int hr) TryGetActiveTextViewHost() { // The active text view might not have finished composing yet, waiting for the application to 'idle' // means that it is done pumping messages (including WM_PAINT) and the window should return the correct text view WaitForApplicationIdle(Helper.HangMitigatingTimeout); - var activeVsTextView = (IVsUserData)GetActiveVsTextView(); - - var hresult = activeVsTextView.GetData(IWpfTextViewId, out var wpfTextViewHost); - Marshal.ThrowExceptionForHR(hresult); + var (activeVsTextView, hr) = TryGetActiveVsTextView(); + if (!ErrorHandler.Succeeded(hr)) + { + return (null, hr); + } - return (IWpfTextViewHost)wpfTextViewHost; + var hresult = ((IVsUserData)activeVsTextView).GetData(IWpfTextViewId, out var wpfTextViewHost); + return ((IWpfTextViewHost)wpfTextViewHost, hresult); } public bool IsUseSuggestionModeOn() diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/InteractiveWindow_InProc.cs b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/InteractiveWindow_InProc.cs index bb741475555..0382f5db674 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/InteractiveWindow_InProc.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/InteractiveWindow_InProc.cs @@ -61,6 +61,9 @@ public bool IsInitializing public string GetReplText() => _interactiveWindow.TextView.TextBuffer.CurrentSnapshot.GetText(); + protected override bool HasActiveTextView() + => _interactiveWindow.TextView is object; + protected override IWpfTextView GetActiveTextView() => _interactiveWindow.TextView; diff --git a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/TextViewWindow_InProc.cs b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/TextViewWindow_InProc.cs index a1c7f51cc0d..9cf87695ec4 100644 --- a/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/TextViewWindow_InProc.cs +++ b/src/VisualStudio/IntegrationTest/TestUtilities/InProcess/TextViewWindow_InProc.cs @@ -94,11 +94,18 @@ public void WaitForLightBulbSession() /// querying the editor /// public bool IsCompletionActive() - => ExecuteOnActiveView(view => + { + if (!HasActiveTextView()) + { + return false; + } + + return ExecuteOnActiveView(view => { var broker = GetComponentModelService(); return broker.IsCompletionActive(view); }); + } protected abstract ITextBuffer GetBufferContainingCaret(IWpfTextView view); @@ -478,6 +485,8 @@ public void DismissLightBulbSession() broker.DismissSession(view); }); + protected abstract bool HasActiveTextView(); + protected abstract IWpfTextView GetActiveTextView(); } } -- GitLab