diff --git a/src/Extension/AssistantCompletion/ShortcutCompletionCommitManager.cs b/src/Extension/AssistantCompletion/ShortcutCompletionCommitManager.cs
index 83b1e8e..1ffbe21 100644
--- a/src/Extension/AssistantCompletion/ShortcutCompletionCommitManager.cs
+++ b/src/Extension/AssistantCompletion/ShortcutCompletionCommitManager.cs
@@ -97,16 +97,7 @@ private static void HandleSnippetNotFoundException(IWpfTextView? wpfTextView, Co
var fileName = ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
- DocumentView? doc;
- try
- {
- doc = wpfTextView.ToDocumentView();
- }
- catch
- {
- doc = ThreadHelper.JoinableTaskFactory.Run(async () => await VS.Documents.GetActiveDocumentViewAsync());
- }
-
+ var doc = EditorUtils.ToDocumentView(wpfTextView);
return DocumentHelper.GetFileName(doc, wpfTextView);
});
diff --git a/src/Extension/Caching/TextViewCreationListener.cs b/src/Extension/Caching/TextViewCreationListener.cs
index 96e17e2..7813176 100644
--- a/src/Extension/Caching/TextViewCreationListener.cs
+++ b/src/Extension/Caching/TextViewCreationListener.cs
@@ -34,21 +34,7 @@ internal sealed class TextViewCreationListener : IWpfTextViewCreationListener
/// The upon which the adornment should be placed
public void TextViewCreated(IWpfTextView textView)
{
- if (textView == null)
- return;
-
- DocumentView doc;
- try
- {
- doc = textView.ToDocumentView();
- }
- catch
- {
- doc = ThreadHelper.JoinableTaskFactory.Run(async () =>
- {
- return await VS.Documents.GetActiveDocumentViewAsync();
- });
- }
+ var doc = EditorUtils.ToDocumentView(textView);
if (doc == null)
return;
diff --git a/src/Extension/InlineCompletion/InlineCompletionClient.cs b/src/Extension/InlineCompletion/InlineCompletionClient.cs
index c276a6e..466dbf1 100644
--- a/src/Extension/InlineCompletion/InlineCompletionClient.cs
+++ b/src/Extension/InlineCompletion/InlineCompletionClient.cs
@@ -110,7 +110,12 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv
var triggeringLine = _wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromPosition(caretPos);
var triggeringLineText = triggeringLine.GetText();
var lineTrackingSpan = _wpfTextView.TextBuffer.CurrentSnapshot.CreateTrackingSpan(triggeringLine.Extent.Span, SpanTrackingMode.EdgePositive);
- var language = LanguageUtils.ParseFromFileName(DocumentHelper.GetFileName(_wpfTextView.ToDocumentView(), _wpfTextView));
+
+ var doc = EditorUtils.ToDocumentView(_wpfTextView);
+ if (doc == null)
+ return _nextCommandHandler.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
+
+ var language = LanguageUtils.ParseFromFileName(DocumentHelper.GetFileName(doc, _wpfTextView));
var shouldTriggerCompletion = char.IsWhiteSpace(typedChar)
&& EditorUtils.IsSemanticSearchComment(triggeringLineText, language)
diff --git a/src/Extension/InlineCompletion/TextViewCreationListener.cs b/src/Extension/InlineCompletion/TextViewCreationListener.cs
index 75a0897..73627c9 100644
--- a/src/Extension/InlineCompletion/TextViewCreationListener.cs
+++ b/src/Extension/InlineCompletion/TextViewCreationListener.cs
@@ -48,18 +48,7 @@ public TextViewCreationListener()
/// The upon which the adornment should be placed
public void TextViewCreated(IWpfTextView textView)
{
- if (textView == null)
- return;
-
- DocumentView? doc;
- try
- {
- doc = textView.ToDocumentView();
- }
- catch
- {
- doc = ThreadHelper.JoinableTaskFactory.Run(async () => await VS.Documents.GetActiveDocumentViewAsync());
- }
+ var doc = EditorUtils.ToDocumentView(textView);
if (doc == null)
return;
diff --git a/src/Extension/SnippetFormats/EditorUtils.cs b/src/Extension/SnippetFormats/EditorUtils.cs
index d094469..44333e4 100644
--- a/src/Extension/SnippetFormats/EditorUtils.cs
+++ b/src/Extension/SnippetFormats/EditorUtils.cs
@@ -2,6 +2,8 @@
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using System.Linq;
+using Community.VisualStudio.Toolkit;
+using Microsoft.VisualStudio.Shell;
using static Extension.SnippetFormats.LanguageUtils;
namespace Extension.SnippetFormats
@@ -254,5 +256,26 @@ public static TextSpan GetLegacyCaretPosition(this CaretPosition caretPosition)
return textSpan;
}
+
+ ///
+ /// Converts the argument to a ,
+ /// either directly, or by retrieving the currently active text and document views.
+ ///
+ /// the text view to convert
+ /// The DocumentView, or null if the argument IWpfTextView is null, or the DocumentView itself is null
+ public static DocumentView? ToDocumentView(IWpfTextView? textView)
+ {
+ if (textView == null)
+ return null;
+
+ try
+ {
+ return textView.ToDocumentView();
+ }
+ catch
+ {
+ return ThreadHelper.JoinableTaskFactory.Run(async () => await VS.Documents.GetActiveDocumentViewAsync());
+ }
+ }
}
}