-
Notifications
You must be signed in to change notification settings - Fork 217
Add UnboundDirectiveAttributeAddUsingCodeActionProvider for directive attributes #12404
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
Merged
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8404c74
Initial plan
Copilot a7d3b05
Add AddUsingsCodeActionProvider to offer @using directives for fully …
Copilot f793980
Refactor AddUsingsCodeActionProvider to avoid duplicate namespace ext…
Copilot 4f69d7f
Implement UnboundDirectiveAttributeAddUsingCodeActionProvider for dir…
Copilot 3b52a62
Remove incorrect AddUsingsCodeActionProvider and add UnboundDirective…
Copilot 98fbf03
Remove redundant namespace check and clarify .Component suffix usage
Copilot 328584c
Allow opting out of default imports in tests
davidwengier 0a5c30b
Fix provider to look for MarkupAttributeBlockSyntax instead of Markup…
Copilot 345c10d
Fix attribute name matching - use Name property directly and strip '@…
Copilot 38d9cb0
Address code review feedback - extract method, use GetTagHelpers(), k…
Copilot c9a3e72
Fix namespace extraction to handle generic types and improve heuristics
Copilot d3bf9ec
Fix test expectations for @bind tests
Copilot e057c87
Fix implementation, unskip tests, and remove failing test
davidwengier 47767f6
Ensure action is only offered on the name portion of the attribute
davidwengier e4bb3a3
Address code review feedback: fix cursor position check, add WorkItem…
Copilot e302b42
Remove incorrectly re-added Skip attribute from AddUsing_BindWithPara…
Copilot 03b62c1
Use TryGetTagHelpers instead of GetTagHelpers with null check
Copilot 97011f7
Use ReadOnlySpan<char> to avoid string allocations in attribute name …
Copilot 6d3dffa
Use extension method syntax for SequenceEqual instead of static metho…
Copilot 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
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
138 changes: 138 additions & 0 deletions
138
...Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Razor.Language; | ||
| using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
| using Microsoft.AspNetCore.Razor.Threading; | ||
| using Microsoft.CodeAnalysis.Razor.CodeActions.Models; | ||
| using Microsoft.CodeAnalysis.Razor.CodeActions.Razor; | ||
| using Microsoft.CodeAnalysis.Razor.Workspaces; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Razor.CodeActions; | ||
|
|
||
| internal class UnboundDirectiveAttributeAddUsingCodeActionProvider : IRazorCodeActionProvider | ||
| { | ||
| public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeActionContext context, CancellationToken cancellationToken) | ||
| { | ||
| if (context.HasSelection) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Only work in component files | ||
| if (!FileKinds.IsComponent(context.CodeDocument.FileKind)) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| if (!context.CodeDocument.TryGetSyntaxRoot(out var syntaxRoot)) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Find the node at the cursor position | ||
| var owner = syntaxRoot.FindInnermostNode(context.StartAbsoluteIndex, includeWhitespace: false); | ||
| if (owner is null) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Find a regular markup attribute (not a tag helper attribute) that starts with '@' | ||
| // Unbound directive attributes are just regular attributes that happen to start with '@' | ||
| var attributeBlock = owner.FirstAncestorOrSelf<MarkupAttributeBlockSyntax>(); | ||
| if (attributeBlock is null) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Make sure the cursor is actually on the name part, since the attribute block is the whole attribute, including | ||
| // value and even some whitespace | ||
| if (!attributeBlock.Name.Span.Contains(context.StartAbsoluteIndex)) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Try to find the missing namespace for this directive attribute | ||
| if (!TryGetMissingDirectiveAttributeNamespace(context.CodeDocument, attributeBlock, out var missingNamespace)) | ||
| { | ||
| return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>(); | ||
| } | ||
|
|
||
| // Create the code action | ||
| var resolutionParams = AddUsingsCodeActionResolver.CreateAddUsingResolutionParams( | ||
| missingNamespace, | ||
| context.Request.TextDocument, | ||
| additionalEdit: null, | ||
| context.DelegatedDocumentUri); | ||
|
|
||
| var addUsingCodeAction = RazorCodeActionFactory.CreateAddComponentUsing( | ||
| missingNamespace, | ||
| newTagName: null, | ||
| resolutionParams); | ||
|
|
||
| return Task.FromResult<ImmutableArray<RazorVSInternalCodeAction>>([addUsingCodeAction]); | ||
| } | ||
|
|
||
| private static bool TryGetMissingDirectiveAttributeNamespace( | ||
| RazorCodeDocument codeDocument, | ||
| MarkupAttributeBlockSyntax attributeBlock, | ||
| [NotNullWhen(true)] out string? missingNamespace) | ||
| { | ||
| missingNamespace = null; | ||
|
|
||
| // Check if this is a directive attribute (starts with '@') | ||
| var attributeName = attributeBlock.Name.GetContent(); | ||
| if (attributeName is not ['@', ..]) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Get all tag helpers, not just those in scope, since we want to suggest adding a using | ||
| var tagHelpers = codeDocument.GetTagHelpers(); | ||
| if (tagHelpers is null) | ||
| { | ||
| return false; | ||
| } | ||
DustinCampbell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // For attributes with parameters (e.g., @bind:after), extract just the base attribute name | ||
| var baseAttributeName = attributeName; | ||
| var colonIndex = attributeName.IndexOf(':'); | ||
| if (colonIndex > 0) | ||
| { | ||
| baseAttributeName = attributeName[..colonIndex]; | ||
| } | ||
|
|
||
| // Search for matching bound attribute descriptors in all available tag helpers | ||
| foreach (var tagHelper in tagHelpers) | ||
| { | ||
| if (!tagHelper.IsAttributeDescriptor()) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| foreach (var boundAttribute in tagHelper.BoundAttributes) | ||
| { | ||
| // No need to worry about multiple matches, because Razor syntax has no way to disambiguate anyway. | ||
| // Currently only compiler can create directive attribute tag helpers anyway. | ||
| if (boundAttribute.IsDirectiveAttribute && | ||
| boundAttribute.Name == baseAttributeName) | ||
| { | ||
| if (boundAttribute.Parent.TypeNamespace is { } typeNamespace) | ||
| { | ||
| missingNamespace = typeNamespace; | ||
| return true; | ||
| } | ||
|
|
||
| // This is unexpected, but if for some reason we can't find a namespace, there is no point looking further | ||
| break; | ||
| } | ||
| } | ||
| } | ||
DustinCampbell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return false; | ||
| } | ||
| } | ||
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
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
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.
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.
@davidwengier Do we normally not include the end position in these tests?
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.
The first guard clause in this checks for a selection, and doesn't offer the action if there is one, so end and start will always be the same. I don't know if that is best practice for code actions, but I think most, if not all, of the Razor ones do it.
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.
Sorry, that wasn't clear of me, I meant the end position of the Name.span. This will trigger if the caret is here
<a []@foo="test" />
but not here
<a @foo[] = "test" />
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.
Oh, nice catch, thanks!