Add UnboundDirectiveAttributeAddUsingCodeActionProvider for directive attributes#12404
Add UnboundDirectiveAttributeAddUsingCodeActionProvider for directive attributes#12404davidwengier merged 19 commits intomainfrom
Conversation
…qualified component tags Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
…raction Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
|
@copilot the prompt asked about directive attributes, your plan mentions directive attributes, but what you've implemented offers to insert usings for component names. That is not what we want. Please try again. |
…ective attributes Replace the incorrect AddUsingsCodeActionProvider (for component tags) with the correct implementation that handles unbound directive attributes like @OnClick, @bind-Value, etc. - Detects MarkupTagHelperDirectiveAttributeSyntax nodes that are unbound - Checks if TagHelperAttributeInfo.Bound == false && IsDirectiveAttribute == true - Searches TagHelperDocumentContext for matching BoundAttributeDescriptors - Applies heuristics to determine missing namespace (e.g., Microsoft.AspNetCore.Components.Web for event handlers) - Verifies namespace is not already imported before offering code action - Registered in both LanguageServer and Remote services Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Fixed in commit 4f69d7f. I've replaced the incorrect implementation with
|
davidwengier
left a comment
There was a problem hiding this comment.
@copilot please add tests in a new test file in the CodeActions folder of the Microsoft.VisualStudioCode.RazorExtension.Test project. The other test files in that folder will tell you the patterns to use.
...src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/AddUsingsCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
…AttributeAddUsingTests - Remove AddUsingsCodeActionProvider.cs (was solving wrong problem - component tags instead of directive attributes) - Add UnboundDirectiveAttributeAddUsingTests.cs with test cases for unbound directive attributes - Tests are currently failing and need further investigation to understand test setup Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
Added UnboundDirectiveAttributeAddUsingTests.cs in commit 3b52a62. The test file follows the patterns from other CodeActions tests and includes test cases for:
However, the tests are currently failing. The provider doesn't appear to be offering code actions in the test scenarios. This may be because directive attributes in the tests are already bound (if the test framework includes necessary namespaces by default). I need to investigate the test setup further to understand why the provider isn't being triggered. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
davidwengier
left a comment
There was a problem hiding this comment.
@copilot your tests are failing, please investigate and fix
...is.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
...is.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
- Remove lines 67-83: checking if namespace is already imported is redundant because if it was, the attribute would be bound - Change ".Dummy" to ".Component" and add clarifying comment explaining why a suffix is needed - Remove unused System.Linq and Microsoft.AspNetCore.Razor.Language.Legacy using statements Note: Tests still failing - need to investigate when directive attributes are actually unbound in the component model Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
The tests are failing because no code actions are being offered (Available: empty). The provider isn't being triggered. I believe the issue is that in standard Razor Components, directive attributes like I need clarification on the intended scenario: when would directive attributes like Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot I've pushed a fix to the test infra so your tests now have a chance of working. The issue you have is that the code in the provider is trying to find a |
...is.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
Replace GetTagHelpers() with null check pattern with TryGetTagHelpers() for cleaner code and better null handling. All tests pass (9/9 passing). Co-authored-by: DustinCampbell <116161+DustinCampbell@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…matching - Use MemoryExtensions.AsSpan() to work with ReadOnlySpan<char> instead of allocating substring - Use MemoryExtensions.SequenceEqual() for span comparison - Add using System for MemoryExtensions - Reduces memory allocations when checking attribute names with parameters (e.g., @Bind:after) All tests pass (9/9 passing). Co-authored-by: DustinCampbell <116161+DustinCampbell@users.noreply.github.com>
...is.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs
Outdated
Show resolved
Hide resolved
…d call Changed from MemoryExtensions.SequenceEqual(boundAttribute.Name.AsSpan(), baseAttributeName) to boundAttribute.Name.AsSpan().SequenceEqual(baseAttributeName) for better readability. Build verified successful. All tests pass (9/9 passing). Co-authored-by: DustinCampbell <116161+DustinCampbell@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Part of #9747
Summary
Successfully implemented
UnboundDirectiveAttributeAddUsingCodeActionProviderto offer @using directives for unbound directive attributes.Test Results: All Passing ✅
Passing Tests:
Implementation Features
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.