-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
…ppet to say "using statement"
- Loading branch information
There are no files selected for viewing
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); | ||
} | ||
} |
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 | ||
}; | ||
} | ||
} |