@@ -25,93 +25,64 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)
2525 context . RegisterCodeFix (
2626 CodeAction . Create (
2727 title : Title ,
28- createChangedDocument : c => ReplaceIsDefinedWithGenerated ( context . Document , context . Diagnostics , c ) ,
28+ createChangedDocument : c => FixAllAsync ( context . Document , context . Diagnostics , c ) ,
2929 equivalenceKey : Title ) ,
3030 context . Diagnostics ) ;
3131 }
3232
3333 return Task . CompletedTask ;
3434 }
3535
36- protected sealed override Task < Document > FixAllAsync ( Document document , ImmutableArray < Diagnostic > diagnostics , CancellationToken cancellationToken )
37- => ReplaceIsDefinedWithGenerated ( document , diagnostics , cancellationToken ) ;
38-
39- private static async Task < Document > ReplaceIsDefinedWithGenerated (
40- Document document ,
41- ImmutableArray < Diagnostic > diagnostics ,
36+ protected override Task FixWithEditor ( DocumentEditor editor , Diagnostic diagnostic ,
37+ INamedTypeSymbol extensionTypeSymbol ,
4238 CancellationToken cancellationToken )
4339 {
44- var editor = await DocumentEditor . CreateAsync ( document , cancellationToken ) . ConfigureAwait ( false ) ;
45- if ( editor is null )
40+ // Find the invocation node at the diagnostic location
41+ var node = editor . OriginalRoot . FindNode ( diagnostic . Location . SourceSpan ) ;
42+ if ( node is not InvocationExpressionSyntax invocation )
4643 {
47- return document ;
44+ return Task . CompletedTask ;
4845 }
4946
50- var generator = editor . Generator ;
51- var semanticModel = editor . SemanticModel ;
52-
53- foreach ( var diagnostic in diagnostics )
47+ // Get the symbol to determine which pattern we're dealing with
48+ var symbolInfo = editor . SemanticModel . GetSymbolInfo ( invocation , cancellationToken ) ;
49+ if ( symbolInfo . Symbol is not IMethodSymbol methodSymbol )
5450 {
55- cancellationToken . ThrowIfCancellationRequested ( ) ;
56- if ( ! diagnostic . Properties . TryGetValue ( AnalyzerHelpers . ExtensionTypeNameProperty , out var extensionTypeName )
57- || extensionTypeName is null )
58- {
59- continue ;
60- }
61-
62- // Find the invocation node at the diagnostic location
63- var node = editor . OriginalRoot . FindNode ( diagnostic . Location . SourceSpan ) ;
64- if ( node is not InvocationExpressionSyntax invocation )
65- {
66- continue ;
67- }
68-
69- // Get the symbol to determine which pattern we're dealing with
70- var symbolInfo = semanticModel . GetSymbolInfo ( invocation ) ;
71- if ( symbolInfo . Symbol is not IMethodSymbol methodSymbol )
72- {
73- continue ;
74- }
75-
76- ArgumentSyntax ? valueArgument = null ;
51+ return Task . CompletedTask ;
52+ }
7753
78- // Determine which argument is the value to check
79- if ( methodSymbol is { IsGenericMethod : true , TypeArguments . Length : 1 } )
80- {
81- // Pattern: Enum.IsDefined<TEnum>(value)
82- if ( invocation . ArgumentList . Arguments . Count >= 1 )
83- {
84- valueArgument = invocation . ArgumentList . Arguments [ 0 ] ;
85- }
86- }
87- else if ( methodSymbol . Parameters . Length == 2
88- && invocation . ArgumentList . Arguments . Count == 2 )
89- {
90- // Pattern: Enum.IsDefined(typeof(TEnum), value)
91- valueArgument = invocation . ArgumentList . Arguments [ 1 ] ;
92- }
54+ ArgumentSyntax ? valueArgument = null ;
9355
94- if ( valueArgument is null )
95- {
96- continue ;
97- }
98-
99- var type = semanticModel . Compilation . GetTypeByMetadataName ( extensionTypeName ) ;
100- if ( type is null )
56+ // Determine which argument is the value to check
57+ if ( methodSymbol is { IsGenericMethod : true , TypeArguments . Length : 1 } )
58+ {
59+ // Pattern: Enum.IsDefined<TEnum>(value)
60+ if ( invocation . ArgumentList . Arguments . Count >= 1 )
10161 {
102- continue ;
62+ valueArgument = invocation . ArgumentList . Arguments [ 0 ] ;
10363 }
64+ }
65+ else if ( methodSymbol . Parameters . Length == 2
66+ && invocation . ArgumentList . Arguments . Count == 2 )
67+ {
68+ // Pattern: Enum.IsDefined(typeof(TEnum), value)
69+ valueArgument = invocation . ArgumentList . Arguments [ 1 ] ;
70+ }
10471
105- // Create new invocation: ExtensionsClass.IsDefined(value)
106- var newInvocation = generator . InvocationExpression (
107- generator . MemberAccessExpression ( generator . TypeExpression ( type ) , "IsDefined" ) ,
108- valueArgument . Expression )
109- . WithTriviaFrom ( invocation )
110- . WithAdditionalAnnotations ( Simplifier . AddImportsAnnotation , Simplifier . Annotation ) ;
111-
112- editor . ReplaceNode ( invocation , newInvocation ) ;
72+ if ( valueArgument is null )
73+ {
74+ return Task . CompletedTask ;
11375 }
11476
115- return editor . GetChangedDocument ( ) ;
77+ // Create new invocation: ExtensionsClass.IsDefined(value)
78+ var generator = editor . Generator ;
79+ var newInvocation = generator . InvocationExpression (
80+ generator . MemberAccessExpression ( generator . TypeExpression ( extensionTypeSymbol ) , "IsDefined" ) ,
81+ valueArgument . Expression )
82+ . WithTriviaFrom ( invocation )
83+ . WithAdditionalAnnotations ( Simplifier . AddImportsAnnotation , Simplifier . Annotation ) ;
84+
85+ editor . ReplaceNode ( invocation , newInvocation ) ;
86+ return Task . CompletedTask ;
11687 }
117- }
88+ }
0 commit comments