Fixed snippet behaviour in the middle of an attribute

This commit is contained in:
DoctorKrolic 2022-09-06 23:40:10 +03:00
Родитель 655e204a5e
Коммит 23ebcfe68d
2 изменённых файлов: 38 добавлений и 13 удалений

Просмотреть файл

@ -180,7 +180,10 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion
var attributeCommitCharacters = ResolveAttributeCommitCharacters(attributeContext);
var isSnippet = false;
var insertText = filterText;
if (TryResolveInsertText(insertText, attributeContext, out var snippetText))
// Do not turn attributes into snippets if we are in an already written full attribute (https://github.com/dotnet/razor-tooling/issues/6724)
if (containingAttribute is not (MarkupTagHelperAttributeSyntax or MarkupAttributeBlockSyntax) &&
TryResolveInsertText(insertText, attributeContext, out var snippetText))
{
isSnippet = true;
insertText = snippetText;
@ -293,19 +296,14 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion
private static IReadOnlyList<RazorCommitCharacter> ResolveAttributeCommitCharacters(AttributeContext attributeContext)
{
switch (attributeContext)
return attributeContext switch
{
case AttributeContext.Indexer:
return s_noCommitCharacters;
case AttributeContext.Minimized:
return MinimizedAttributeCommitCharacters;
case AttributeContext.Full:
return AttributeCommitCharacters;
case AttributeContext.FullSnippet:
return AttributeSnippetCommitCharacters;
default:
throw new InvalidOperationException("Unexpected context");
}
AttributeContext.Indexer => s_noCommitCharacters,
AttributeContext.Minimized => MinimizedAttributeCommitCharacters,
AttributeContext.Full => AttributeCommitCharacters,
AttributeContext.FullSnippet => AttributeSnippetCommitCharacters,
_ => throw new InvalidOperationException("Unexpected context"),
};
}
private enum AttributeContext

Просмотреть файл

@ -545,6 +545,33 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Completion
AssertBoolIntCompletions(completions);
}
[Fact]
[WorkItem("https://github.com/dotnet/razor-tooling/issues/6724")]
public void GetCompletionsAt_MiddleOfFullAttribute_ReturnsCompletions_NoSnippetBehaviour()
{
// Arrange
var service = new TagHelperCompletionProvider(RazorTagHelperCompletionService, HtmlFactsService, TagHelperFactsService);
var txt = $"@addTagHelper *, TestAssembly{Environment.NewLine}<test2 int-val=''>";
var context = CreateRazorCompletionContext(absoluteIndex: 40 + Environment.NewLine.Length, txt, options: new(SnippetsSupported: true), isRazorFile: false, tagHelpers: DefaultTagHelpers);
// Act
var completions = service.GetCompletionItems(context);
// Assert
Assert.Collection(completions,
completion =>
{
Assert.Equal("bool-val", completion.InsertText); // bool-val will be filtered on IDE side anyway, so just check that it exists and then don't care about it properties
},
completion =>
{
Assert.Equal("int-val", completion.InsertText);
Assert.False(completion.IsSnippet);
Assert.Equal(TagHelperCompletionProvider.AttributeSnippetCommitCharacters, completion.CommitCharacters); // we still want `=` to be a commit character, but we don't want it to be inserted
}
);
}
private static void AssertBoolIntCompletions(IReadOnlyList<RazorCompletionItem> completions)
{
Assert.Collection(completions,