-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semantic Snippets - Remove reflection and call the LanguageServerSnippetExpander directly #61491
Changes from 4 commits
59c65d6
b81a439
7cad46e
63e7097
8b62f34
b61c0dd
40b519e
9335809
7fdc86f
b2d0561
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.CodeAnalysis.Text.Shared.Extensions; | ||
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion; | ||
using Microsoft.VisualStudio.LanguageServer.Client.Snippets; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Microsoft.VisualStudio.Threading; | ||
|
@@ -39,7 +40,7 @@ internal sealed class CommitManager : IAsyncCompletionCommitManager | |
private readonly ITextView _textView; | ||
private readonly IGlobalOptionService _globalOptions; | ||
private readonly IThreadingContext _threadingContext; | ||
private readonly RoslynLSPSnippetExpander _roslynLSPSnippetExpander; | ||
private readonly LanguageServerSnippetExpander _languageServerSnippetExpander; | ||
|
||
public IEnumerable<char> PotentialCommitCharacters | ||
{ | ||
|
@@ -62,13 +63,13 @@ internal CommitManager( | |
RecentItemsManager recentItemsManager, | ||
IGlobalOptionService globalOptions, | ||
IThreadingContext threadingContext, | ||
RoslynLSPSnippetExpander roslynLSPSnippetExpander) | ||
LanguageServerSnippetExpander languageServerSnippetExpander) | ||
{ | ||
_globalOptions = globalOptions; | ||
_threadingContext = threadingContext; | ||
_recentItemsManager = recentItemsManager; | ||
_textView = textView; | ||
_roslynLSPSnippetExpander = roslynLSPSnippetExpander; | ||
_languageServerSnippetExpander = languageServerSnippetExpander; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -241,20 +242,24 @@ private AsyncCompletionData.CommitResult Commit( | |
return new AsyncCompletionData.CommitResult(isHandled: true, AsyncCompletionData.CommitBehavior.None); | ||
} | ||
|
||
// Specifically for snippets, we use reflection to try and invoke the LanguageServerSnippetExpander's | ||
// TryExpand method and determine if it succeeded or not. | ||
var textChange = change.TextChange; | ||
var triggerSnapshotSpan = new SnapshotSpan(triggerSnapshot, textChange.Span.ToSpan()); | ||
var mappedSpan = triggerSnapshotSpan.TranslateTo(subjectBuffer.CurrentSnapshot, SpanTrackingMode.EdgeInclusive); | ||
|
||
// Specifically for snippets, we check to see if the associated completion item is a snippet, | ||
// and if so, we call upon the LanguageServerSnippetExpander's TryExpand to insert the snippet. | ||
if (SnippetCompletionItem.IsSnippet(roslynItem)) | ||
{ | ||
var lspSnippetText = change.Properties[SnippetCompletionItem.LSPSnippetKey]; | ||
|
||
_roslynLSPSnippetExpander.Expand(change.TextChange.Span, lspSnippetText, _textView, triggerSnapshot); | ||
if (!_languageServerSnippetExpander.TryExpand(lspSnippetText!, triggerSnapshotSpan, _textView)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 I think I'd rather have |
||
{ | ||
FatalError.ReportAndCatch(new InvalidOperationException("The invoked LSP snippet expander came back as false."), ErrorSeverity.Critical); | ||
} | ||
|
||
return new AsyncCompletionData.CommitResult(isHandled: true, AsyncCompletionData.CommitBehavior.None); | ||
} | ||
|
||
var textChange = change.TextChange; | ||
var triggerSnapshotSpan = new SnapshotSpan(triggerSnapshot, textChange.Span.ToSpan()); | ||
var mappedSpan = triggerSnapshotSpan.TranslateTo(subjectBuffer.CurrentSnapshot, SpanTrackingMode.EdgeInclusive); | ||
|
||
using (var edit = subjectBuffer.CreateEdit(EditOptions.DefaultMinimalChange, reiteratedVersionNumber: null, editTag: null)) | ||
{ | ||
edit.Replace(mappedSpan.Span, change.TextChange.NewText); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
<DefineConstants>$(DefineConstants);EDITOR_FEATURES</DefineConstants> | ||
<ApplyNgenOptimization Condition="'$(TargetFramework)' == 'netstandard2.0'">full</ApplyNgenOptimization> | ||
|
||
<!-- CA1416 can go away when we target net6.0-macos --> | ||
<NoWarn>$(NoWarn);NU1701;</NoWarn> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❗ This is covered by suppressions on the specific There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a NoWarn on here: I was still getting issues because the packages seemingly had implicit dependencies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to deal with similar in #61234 and can help update this. |
||
|
||
<!-- NuGet --> | ||
<PackageId>Microsoft.CodeAnalysis.EditorFeatures.Common</PackageId> | ||
<IsPackable>true</IsPackable> | ||
|
@@ -39,6 +42,7 @@ | |
<PackageReference Include="Microsoft.VisualStudio.Language" Version="$(MicrosoftVisualStudioLanguageVersion)" /> | ||
<!-- No warn on NU1701 until the LSP client targets netstandard2.0 https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1369985/ --> | ||
<PackageReference Include="Microsoft.VisualStudio.LanguageServer.Client" Version="$(MicrosoftVisualStudioLanguageServerClientVersion)" PrivateAssets="all" NoWarn="NU1701" /> | ||
<PackageReference Include="Microsoft.VisualStudio.LanguageServer.Client.Implementation" Version="$(MicrosoftVisualStudioLanguageServerClientImplementationVersion)" PrivateAssets="all" NoWarn="NU1701" /> | ||
davidwengier marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ What API are we using from this, and why can't it be relocated to the WPF assembly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the LanguageServerSnippetExpander, we need to use the TryExpand method. We are specifically calling it in the CommitManager, so not sure if it can be relocated to the WPF assembly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this code will probably also eventually be needed in VS Mac. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think moving to EditorFeatures.WPF makes sense, even if we have to mirror the change in EditorFeatures.Cocoa (and ideally that would be done at the same time), as it prevents someone else from later adding some new usage of a Windows-only aspect of the implementation assembly without realizing it. |
||
<!-- Explicit reference to Shell.15.0 et. al. with NU1701 only until the LSP client targets netstandard2.0 https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1369985/ --> | ||
<PackageReference Include="Microsoft.VisualStudio.Shell.15.0" Version="$(MicrosoftVisualStudioShell150Version)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. u love to see it! |
||
<PackageReference Include="Microsoft.VisualStudio.Shell.Framework" Version="$(MicrosoftVisualStudioShellFrameworkVersion)" PrivateAssets="all" ExcludeAssets="all" NoWarn="NU1701" /> | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,19 @@ Namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.UnitTests | |
Public NotInheritable Class FoldingRangeTests | ||
Private Const TestProjectAssemblyName As String = "TestProject" | ||
|
||
' Expected 'FoldingRangeKind' argument would likely change for some of the tests when | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😕 Is this intended to be part of this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was discussed here, it's already been changed in a PR David made. I'm waiting for the above PR to go in prior to fixing this one up. |
||
' https://github.com/dotnet/roslyn/projects/45#card-20049168 is implemented. | ||
<Theory> | ||
<InlineData(" | ||
using {|foldingRange:System; | ||
using System.Linq;|}")> | ||
<InlineData(" | ||
using {|foldingRange:S = System.String; | ||
using System.Linq;|}")> | ||
Public Async Function TestFoldingRangesImports(code As String) As Task | ||
Await TestFoldingRanges(code, rangeKind:=FoldingRangeKind.Imports) | ||
End Function | ||
|
||
' Expected 'FoldingRangeKind' argument would likely change for some of the tests when | ||
' https://github.com/dotnet/roslyn/projects/45#card-20049168 is implemented. | ||
<Theory> | ||
|
@@ -30,19 +43,17 @@ class C{|foldingRange: | |
M(); | ||
}|} | ||
}|} | ||
}|}", Nothing)> | ||
}|}")> | ||
<InlineData(" | ||
{|foldingRange:#region | ||
#endregion|}", Nothing)> | ||
<InlineData(" | ||
using {|foldingRange:System; | ||
using System.Linq;|}", FoldingRangeKind.Imports)> | ||
<InlineData(" | ||
using {|foldingRange:S = System.String; | ||
using System.Linq;|}", FoldingRangeKind.Imports)> | ||
#endregion|}")> | ||
<InlineData(" | ||
{|foldingRange:// Comment Line 1 | ||
// Comment Line 2|}", Nothing)> | ||
// Comment Line 2|}")> | ||
Public Async Function TestFoldingRangesStandard(code As String) As Task | ||
Await TestFoldingRanges(code, rangeKind:=Nothing) | ||
End Function | ||
|
||
Public Async Function TestFoldingRanges(code As String, rangeKind As FoldingRangeKind?) As Task | ||
Using workspace = TestWorkspace.CreateWorkspace( | ||
<Workspace> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Can we define a new variable
MicrosoftVisualStudioLanguageServerClientPackagesVersion
to cover both of these?