Skip to content

Commit

Permalink
Add snippet completion item re-writer so we can change C# "using" sni…
Browse files Browse the repository at this point in the history
…ppet to say "using statement"
  • Loading branch information
alexgav committed Feb 24, 2024
1 parent b5dbe49 commit 128b068
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Protocol;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion.Delegation;

/// <summary>
/// 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
/// </remarks>
internal class SnippetResponseRewriter : DelegatedCompletionResponseRewriter
{
private static readonly IReadOnlyDictionary<string, string> s_snippetToLabel = new Dictionary<string, string>()
{
["using"] = $"using {SR.Statement}"
};

public override int Order => ExecutionBehaviorOrder.ChangesCompletionItems;

public override Task<VSInternalCompletionList> RewriteAsync(VSInternalCompletionList completionList, int hostDocumentIndex, DocumentContext hostDocumentContext, DelegatedCompletionParams delegatedParameters, CancellationToken cancellationToken)
{
foreach (var item in completionList.Items)
{
if (item.Kind == CompletionItemKind.Snippet)
{
if (item.Label is null)
{
continue;
}

if (s_snippetToLabel.TryGetValue(item.Label, out var newLabel))
{
item.Label = newLabel;
}
}
}

return Task.FromResult(completionList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static void AddCompletionServices(this IServiceCollection services, Langu
services.AddSingleton<DelegatedCompletionResponseRewriter, TextEditResponseRewriter>();
services.AddSingleton<DelegatedCompletionResponseRewriter, DesignTimeHelperResponseRewriter>();
services.AddSingleton<DelegatedCompletionResponseRewriter, HtmlCommitCharacterResponseRewriter>();
services.AddSingleton<DelegatedCompletionResponseRewriter, SnippetResponseRewriter>();

services.AddSingleton<AggregateCompletionItemResolver>();
services.AddSingleton<CompletionItemResolver, RazorCompletionItemResolver>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,7 @@
<data name="Version_Should_Not_Be_Null" xml:space="preserve">
<value>Provided version should not be null.</value>
</data>
<data name="Statement" xml:space="preserve">
<value>statement</value>
</data>
</root>

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
@@ -0,0 +1,149 @@
// 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.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Completion.Delegation;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Test.Completion.Delegation;
public class SnippetResponseRewriterTest(ITestOutputHelper testOutput)
: ResponseRewriterTestBase(new SnippetResponseRewriter(), testOutput)
{
[Fact]
public async Task RewriteAsync_ChangesUsingSnippetLabel()
{
// Arrange
var documentContent = "@$$";
TestFileMarkupParser.GetPosition(documentContent, out documentContent, out var cursorPosition);
var delegatedCompletionList = GenerateCompletionList(
("using", CompletionItemKind.Snippet),
("if", CompletionItemKind.Keyword)
);
var rewriter = new SnippetResponseRewriter();

// Act
var rewrittenCompletionList = await GetRewrittenCompletionListAsync(
cursorPosition, documentContent, delegatedCompletionList, rewriter);

// Assert
Assert.Null(rewrittenCompletionList.CommitCharacters);
Assert.Collection(
rewrittenCompletionList.Items,
completion =>
{
Assert.Equal("using statement", completion.Label);
},
completion =>
{
Assert.Equal("if", completion.Label);
}
);
}

[Fact]
public async Task RewriteAsync_DoesNotChangeUsingKeywordLabel()
{
// Arrange
var documentContent = "@$$";
TestFileMarkupParser.GetPosition(documentContent, out documentContent, out var cursorPosition);
var delegatedCompletionList = GenerateCompletionList(
("using", CompletionItemKind.Keyword),
("if", CompletionItemKind.Keyword)
);
var rewriter = new SnippetResponseRewriter();

// Act
var rewrittenCompletionList = await GetRewrittenCompletionListAsync(
cursorPosition, documentContent, delegatedCompletionList, rewriter);

// Assert
Assert.Null(rewrittenCompletionList.CommitCharacters);
Assert.Collection(
rewrittenCompletionList.Items,
completion =>
{
Assert.Equal("using", completion.Label);
},
completion =>
{
Assert.Equal("if", completion.Label);
}
);
}

[Fact]
public async Task RewriteAsync_DoesNotChangeIfSnippetLabel()
{
// Arrange
var documentContent = "@$$";
TestFileMarkupParser.GetPosition(documentContent, out documentContent, out var cursorPosition);
var delegatedCompletionList = GenerateCompletionList(
("using", CompletionItemKind.Keyword),
("if", CompletionItemKind.Snippet)
);
var rewriter = new SnippetResponseRewriter();

// Act
var rewrittenCompletionList = await GetRewrittenCompletionListAsync(
cursorPosition, documentContent, delegatedCompletionList, rewriter);

// Assert
Assert.Null(rewrittenCompletionList.CommitCharacters);
Assert.Collection(
rewrittenCompletionList.Items,
completion =>
{
Assert.Equal("using", completion.Label);
},
completion =>
{
Assert.Equal("if", completion.Label);
}
);
}

[Fact]
public async Task RewriteAsync_HandlesNullLabels()
{
// Arrange
var documentContent = "@$$";
TestFileMarkupParser.GetPosition(documentContent, out documentContent, out var cursorPosition);
var delegatedCompletionList = GenerateCompletionList(
(null, CompletionItemKind.Keyword),
("using", CompletionItemKind.Snippet)
);
var rewriter = new SnippetResponseRewriter();

// Act
var rewrittenCompletionList = await GetRewrittenCompletionListAsync(
cursorPosition, documentContent, delegatedCompletionList, rewriter);

// Assert
Assert.Null(rewrittenCompletionList.CommitCharacters);
Assert.Collection(
rewrittenCompletionList.Items,
completion =>
{
Assert.Null(completion.Label);
},
completion =>
{
Assert.Equal("using statement", completion.Label);
}
);
}

private static VSInternalCompletionList GenerateCompletionList(params (string? Label, CompletionItemKind Kind)[] itemsData)
{
var items = itemsData.Select(itemData => new VSInternalCompletionItem() { Label = itemData.Label!, Kind = itemData.Kind}).ToArray();
return new VSInternalCompletionList()
{
Items = items
};
}
}

0 comments on commit 128b068

Please sign in to comment.