Skip to content

Commit

Permalink
Remove C#s using snippet from completion (#11272)
Browse files Browse the repository at this point in the history
Fixes #11189

Previously we would change "using" to "using statement" in completion.
Completing this item would insert "using statement" into the document,
which not valid Razor or C#. Inline completion also wouldn't work
because "statement" is not a valid C# snippet. Removing it means we
leave our using keyword and using directive snippet completion items in
place, and our using keyword works fine with inline completion, because
completion item type doesn't matter at that point.
  • Loading branch information
davidwengier authored Dec 4, 2024
2 parents 6651d16 + d1082c0 commit e228df4
Show file tree
Hide file tree
Showing 16 changed files with 15 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Frozen;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand All @@ -14,20 +13,10 @@ namespace Microsoft.CodeAnalysis.Razor.Completion.Delegation;
/// Modifies delegated snippet completion items
/// </summary>
/// <remarks>
/// At the moment primarily used to modify C# "using" snippet to "using statement" snippet
/// in order to disambiguate it from Razor "using directive" snippet
/// At the moment primarily used to remove the C# "using" snippet because we have our own
/// </remarks>
internal class SnippetResponseRewriter : IDelegatedCSharpCompletionResponseRewriter
{
private static readonly FrozenDictionary<string, (string Label, string SortText)> s_snippetToCompletionData = new Dictionary<string, (string Label, string SortText)>()
{
// Modifying label of the C# using snippet to "using statement" to disambiguate from
// Razor @using directive, and also appending a space to sort text to make sure it's sorted
// after Razor "using" keyword and "using directive ..." entries (which use "using" as sort text)
["using"] = (Label: $"using {SR.Statement}", SortText: "using ")
}
.ToFrozenDictionary();

public Task<VSInternalCompletionList> RewriteAsync(
VSInternalCompletionList completionList,
int hostDocumentIndex,
Expand All @@ -36,21 +25,22 @@ public Task<VSInternalCompletionList> RewriteAsync(
RazorCompletionOptions completionOptions,
CancellationToken cancellationToken)
{
using var items = new PooledArrayBuilder<CompletionItem>(completionList.Items.Length);

foreach (var item in completionList.Items)
{
if (item.Kind == CompletionItemKind.Snippet)
if (item is { Kind: CompletionItemKind.Snippet, Label: "using" })
{
if (item.Label is null)
{
continue;
}

if (s_snippetToCompletionData.TryGetValue(item.Label, out var completionData))
{
item.Label = completionData.Label;
item.SortText = completionData.SortText;
}
continue;
}

items.Add(item);
}

// If we didn't remove anything, then don't bother materializing the array
if (completionList.Items.Length != items.Count)
{
completionList.Items = items.ToArray();
}

return Task.FromResult(completionList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@
<data name="Blazor_directive_attributes" xml:space="preserve">
<value>Blazor directive attributes</value>
</data>
<data name="Statement" xml:space="preserve">
<value>statement</value>
</data>
<data name="Promote_using_directive_to" xml:space="preserve">
<value>Promote using directive to {0}</value>
</data>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SnippetResponseRewriterTest(ITestOutputHelper testOutput)
: ResponseRewriterTestBase(testOutput)
{
[Fact]
public async Task RewriteAsync_ChangesUsingSnippetLabel()
public async Task RewriteAsync_RemovesUsingSnippetLabel()
{
// Arrange
var documentContent = "@$$";
Expand All @@ -37,11 +37,6 @@ public async Task RewriteAsync_ChangesUsingSnippetLabel()
Assert.Collection(
rewrittenCompletionList.Items,
completion =>
{
Assert.Equal("using statement", completion.Label);
Assert.Equal("using ", completion.SortText);
},
completion =>
{
Assert.Equal("if", completion.Label);
Assert.Equal("if", completion.SortText);
Expand Down

0 comments on commit e228df4

Please sign in to comment.