Skip to content

Commit c9a3e72

Browse files
Copilotdavidwengier
andcommitted
Fix namespace extraction to handle generic types and improve heuristics
- Use AddUsingsCodeActionResolver.GetNamespaceFromFQN to properly extract namespace from types with generics - Improve heuristics to detect Web namespace by checking for EventArgs and EventCallback types - Add check for ".Web>" pattern to catch generic parameters Tests improved from 5/9 to 6/9 passing. @OnClick, @onchange, @OnClick with existing usings, @Bind:after all now pass. @Bind and @bind-value tests need investigation - they're finding System namespace or no match. Co-authored-by: davidwengier <[email protected]>
1 parent 38d9cb0 commit c9a3e72

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/UnboundDirectiveAttributeAddUsingCodeActionProvider.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,25 @@ private static bool TryGetMissingDirectiveAttributeNamespace(
118118
var typeName = boundAttribute.TypeName;
119119

120120
// Apply heuristics to determine the namespace
121-
if (typeName.Contains(".Web.") || typeName.EndsWith(".Web.EventHandlers"))
121+
// Check for Web namespace indicators (event args types are defined there)
122+
if (typeName.Contains(".Web.") || typeName.Contains(".Web>") ||
123+
typeName.Contains("EventArgs") || typeName.Contains("EventCallback"))
122124
{
123125
missingNamespace = "Microsoft.AspNetCore.Components.Web";
124126
return true;
125127
}
126-
else if (typeName.Contains(".Forms."))
128+
else if (typeName.Contains(".Forms.") || typeName.Contains(".Forms>"))
127129
{
128130
missingNamespace = "Microsoft.AspNetCore.Components.Forms";
129131
return true;
130132
}
131133
else
132134
{
133-
// Extract namespace from type name (everything before the last dot)
134-
var lastDotIndex = typeName.LastIndexOf('.');
135-
if (lastDotIndex > 0)
135+
// Extract namespace from type name using the existing method
136+
var extractedNamespace = AddUsingsCodeActionResolver.GetNamespaceFromFQN(typeName);
137+
if (!string.IsNullOrEmpty(extractedNamespace))
136138
{
137-
missingNamespace = typeName[..lastDotIndex];
139+
missingNamespace = extractedNamespace;
138140
return true;
139141
}
140142
}

0 commit comments

Comments
 (0)