Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Add extra way to get the document view in inline completion #30

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
16 changes: 1 addition & 15 deletions src/Extension/Caching/TextViewCreationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,7 @@ internal sealed class TextViewCreationListener : IWpfTextViewCreationListener
/// <param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param>
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;
Expand Down
7 changes: 6 additions & 1 deletion src/Extension/InlineCompletion/InlineCompletionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 1 addition & 12 deletions src/Extension/InlineCompletion/TextViewCreationListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,7 @@ public TextViewCreationListener()
/// <param name="textView">The <see cref="IWpfTextView"/> upon which the adornment should be placed</param>
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;
Expand Down
23 changes: 23 additions & 0 deletions src/Extension/SnippetFormats/EditorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -254,5 +256,26 @@ public static TextSpan GetLegacyCaretPosition(this CaretPosition caretPosition)

return textSpan;
}

/// <summary>
/// Converts the argument <see cref="IWpfTextView"/> to a <see cref="DocumentView"/>,
/// either directly, or by retrieving the currently active text and document views.
/// </summary>
/// <param name="textView">the text view to convert</param>
/// <returns>The DocumentView, or null if the argument IWpfTextView is null, or the DocumentView itself is null</returns>
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());
}
}
}
}