-
Notifications
You must be signed in to change notification settings - Fork 212
Use snippet InsertText in directive attributes to insert equals and quotes #12010
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
Merged
alexgav
merged 6 commits into
main
from
dev/alexgav/UseSnippetInsertTextInDirectiveAttributes
Jul 10, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6853aec
Use sinppet InsertText in directive attributes
alexgav ef6335c
CR feedback
alexgav f638e97
More CR suggestions
alexgav e567f00
CR feeback - don't create another string unnecessarily
alexgav 958729a
User range operator instead of Slice per analyzer suggestion
alexgav 7dff5f9
CR feedback - make local function static
alexgav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,19 @@ | |
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
using Microsoft.AspNetCore.Razor.PooledObjects; | ||
using Microsoft.CodeAnalysis.Razor.Tooltip; | ||
using Microsoft.VisualStudio.Editor.Razor; | ||
using RazorSyntaxNode = Microsoft.AspNetCore.Razor.Language.Syntax.SyntaxNode; | ||
|
||
namespace Microsoft.CodeAnalysis.Razor.Completion; | ||
|
||
internal class DirectiveAttributeCompletionItemProvider : DirectiveAttributeCompletionItemProviderBase | ||
{ | ||
private static ReadOnlyMemory<char> QuotedAttributeValueSnippet => "=\"$0\"".AsMemory(); | ||
private static ReadOnlyMemory<char> UnquotedAttributeValueSnippet => "=$0".AsMemory(); | ||
|
||
public override ImmutableArray<RazorCompletionItem> GetCompletionItems(RazorCompletionContext context) | ||
{ | ||
if (!context.SyntaxTree.Options.FileKind.IsComponent()) | ||
|
@@ -48,7 +53,7 @@ public override ImmutableArray<RazorCompletionItem> GetCompletionItems(RazorComp | |
|
||
// At this point we've determined that completions have been requested for the name portion of the selected attribute. | ||
|
||
var completionItems = GetAttributeCompletions(attributeName, containingTagName, attributes, context.TagHelperDocumentContext); | ||
var completionItems = GetAttributeCompletions(owner, attributeName, containingTagName, attributes, context.TagHelperDocumentContext, context.Options); | ||
|
||
// We don't provide Directive Attribute completions when we're in the middle of | ||
// another unrelated (doesn't start with @) partially completed attribute. | ||
|
@@ -63,10 +68,12 @@ public override ImmutableArray<RazorCompletionItem> GetCompletionItems(RazorComp | |
|
||
// Internal for testing | ||
internal static ImmutableArray<RazorCompletionItem> GetAttributeCompletions( | ||
RazorSyntaxNode containingAttribute, | ||
string selectedAttributeName, | ||
string containingTagName, | ||
ImmutableArray<string> attributes, | ||
TagHelperDocumentContext tagHelperDocumentContext) | ||
TagHelperDocumentContext tagHelperDocumentContext, | ||
RazorCompletionOptions razorCompletionOptions) | ||
{ | ||
var descriptorsForTag = TagHelperFacts.GetTagHelpersGivenTag(tagHelperDocumentContext, containingTagName, parentTag: null); | ||
if (descriptorsForTag.Length == 0) | ||
|
@@ -115,21 +122,35 @@ internal static ImmutableArray<RazorCompletionItem> GetAttributeCompletions( | |
|
||
foreach (var (displayText, (attributeDescriptions, commitCharacters)) in attributeCompletions) | ||
{ | ||
var insertText = displayText; | ||
var insertTextSpan = displayText.AsSpan(); | ||
var originalInsertTextSpan = insertTextSpan; | ||
|
||
// Strip off the @ from the insertion text. This change is here to align the insertion text with the | ||
// completion hooks into VS and VSCode. Basically, completion triggers when `@` is typed so we don't | ||
// want to insert `@bind` because `@` already exists. | ||
var startIndex = insertText.StartsWith('@') ? 1 : 0; | ||
if (SpanExtensions.StartsWith(insertTextSpan, '@')) | ||
{ | ||
insertTextSpan = insertTextSpan[1..]; | ||
} | ||
|
||
var isSnippet = false; | ||
// Indexer attribute, we don't want to insert with the triple dot. | ||
var endIndex = insertText.EndsWith("...", StringComparison.Ordinal) ? ^3 : ^0; | ||
|
||
// Don't allocate a new string unless we need to make a change. | ||
if (startIndex > 0 || endIndex.Value > 0) | ||
if (MemoryExtensions.EndsWith(insertTextSpan, "...".AsSpan())) | ||
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. As above 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. As above :) |
||
{ | ||
insertText = insertText[startIndex..endIndex]; | ||
insertTextSpan = insertTextSpan[..^3]; | ||
} | ||
else | ||
{ | ||
// We are trying for snippet text only for non-indexer attributes, e.g. *not* something like "@bind-..." | ||
if (TryGetSnippetText(containingAttribute, insertTextSpan, razorCompletionOptions, out var snippetTextSpan)) | ||
{ | ||
insertTextSpan = snippetTextSpan; | ||
isSnippet = true; | ||
} | ||
} | ||
|
||
// Don't create another string annecessarily, even thouth ReadOnlySpan.ToString() special-cases the string to avoid allocation | ||
var insertText = insertTextSpan == originalInsertTextSpan ? displayText : insertTextSpan.ToString(); | ||
|
||
using var razorCommitCharacters = new PooledArrayBuilder<RazorCommitCharacter>(capacity: commitCharacters.Count); | ||
|
||
|
@@ -142,13 +163,40 @@ internal static ImmutableArray<RazorCompletionItem> GetAttributeCompletions( | |
displayText, | ||
insertText, | ||
descriptionInfo: new([.. attributeDescriptions]), | ||
commitCharacters: razorCommitCharacters.ToImmutableAndClear()); | ||
commitCharacters: razorCommitCharacters.ToImmutableAndClear(), | ||
isSnippet); | ||
|
||
completionItems.Add(razorCompletionItem); | ||
} | ||
|
||
return completionItems.ToImmutableAndClear(); | ||
|
||
static bool TryGetSnippetText( | ||
RazorSyntaxNode owner, | ||
ReadOnlySpan<char> baseTextSpan, | ||
RazorCompletionOptions razorCompletionOptions, | ||
out ReadOnlySpan<char> snippetTextSpan) | ||
{ | ||
if (razorCompletionOptions.SnippetsSupported | ||
// Don't create snippet text when attribute is already in the tag and we are trying to replace it | ||
// Otherwise you could have something like @onabort=""="" | ||
&& owner is not (MarkupTagHelperDirectiveAttributeSyntax or MarkupAttributeBlockSyntax) | ||
&& owner.Parent is not (MarkupTagHelperDirectiveAttributeSyntax or MarkupAttributeBlockSyntax)) | ||
{ | ||
var suffixTextSpan = razorCompletionOptions.AutoInsertAttributeQuotes ? QuotedAttributeValueSnippet : UnquotedAttributeValueSnippet; | ||
|
||
var buffer = new char[baseTextSpan.Length + suffixTextSpan.Length]; | ||
baseTextSpan.CopyTo(buffer); | ||
suffixTextSpan.CopyTo(buffer.AsMemory()[baseTextSpan.Length..]); | ||
|
||
snippetTextSpan = buffer.AsSpan(); | ||
return true; | ||
} | ||
|
||
snippetTextSpan = []; | ||
return false; | ||
} | ||
|
||
bool TryAddCompletion(string attributeName, BoundAttributeDescriptor boundAttributeDescriptor, TagHelperDescriptor tagHelperDescriptor) | ||
{ | ||
if (selectedAttributeName != attributeName && | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why can't this just be used as an extension method?
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.
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.
both of them are in System - not sure if there is a better way to address this, but this is super-weird.