@@ -26,8 +26,7 @@ public sealed class ConvertToGeneratedDllImportFixer : CodeFixProvider
26
26
27
27
public override FixAllProvider GetFixAllProvider ( ) => WellKnownFixAllProviders . BatchFixer ;
28
28
29
- public const string NoPreprocessorDefinesKey = "ConvertToGeneratedDllImport" ;
30
- public const string WithPreprocessorDefinesKey = "ConvertToGeneratedDllImportPreprocessor" ;
29
+ private const string ConvertToGeneratedDllImportKey = "ConvertToGeneratedDllImport" ;
31
30
32
31
private static readonly string [ ] s_preferredAttributeArgumentOrder =
33
32
{
@@ -70,33 +69,18 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
70
69
if ( ! TryGetAttribute ( methodSymbol , dllImportAttrType , out AttributeData ? dllImportAttr ) )
71
70
return ;
72
71
73
- // Register code fixes with two options for the fix - using preprocessor or not.
72
+ // Register code fix
74
73
context . RegisterCodeFix (
75
74
CodeAction . Create (
76
- Resources . ConvertToGeneratedDllImportNoPreprocessor ,
75
+ Resources . ConvertToGeneratedDllImport ,
77
76
cancelToken => ConvertToGeneratedDllImport (
78
77
context . Document ,
79
78
methodSyntax ,
80
79
methodSymbol ,
81
80
dllImportAttr ! ,
82
81
generatedDllImportAttrType ,
83
- usePreprocessorDefines : false ,
84
82
cancelToken ) ,
85
- equivalenceKey : NoPreprocessorDefinesKey ) ,
86
- context . Diagnostics ) ;
87
-
88
- context . RegisterCodeFix (
89
- CodeAction . Create (
90
- Resources . ConvertToGeneratedDllImportWithPreprocessor ,
91
- cancelToken => ConvertToGeneratedDllImport (
92
- context . Document ,
93
- methodSyntax ,
94
- methodSymbol ,
95
- dllImportAttr ! ,
96
- generatedDllImportAttrType ,
97
- usePreprocessorDefines : true ,
98
- cancelToken ) ,
99
- equivalenceKey : WithPreprocessorDefinesKey ) ,
83
+ equivalenceKey : ConvertToGeneratedDllImportKey ) ,
100
84
context . Diagnostics ) ;
101
85
}
102
86
@@ -106,7 +90,6 @@ private async Task<Document> ConvertToGeneratedDllImport(
106
90
IMethodSymbol methodSymbol ,
107
91
AttributeData dllImportAttr ,
108
92
INamedTypeSymbol generatedDllImportAttrType ,
109
- bool usePreprocessorDefines ,
110
93
CancellationToken cancellationToken )
111
94
{
112
95
DocumentEditor editor = await DocumentEditor . CreateAsync ( doc , cancellationToken ) . ConfigureAwait ( false ) ;
@@ -142,49 +125,8 @@ private async Task<Document> ConvertToGeneratedDllImport(
142
125
. WithIsExtern ( false )
143
126
. WithPartial ( true ) ) ;
144
127
145
- if ( ! usePreprocessorDefines )
146
- {
147
- // Replace the original method with the updated one
148
- editor . ReplaceNode ( methodSyntax , generatedDeclaration ) ;
149
- }
150
- else
151
- {
152
- // #if DLLIMPORTGENERATOR_ENABLED
153
- generatedDeclaration = generatedDeclaration . WithLeadingTrivia (
154
- generatedDeclaration . GetLeadingTrivia ( )
155
- . AddRange ( new [ ] {
156
- SyntaxFactory . Trivia ( SyntaxFactory . IfDirectiveTrivia ( SyntaxFactory . IdentifierName ( "DLLIMPORTGENERATOR_ENABLED" ) , isActive : true , branchTaken : true , conditionValue : true ) ) ,
157
- SyntaxFactory . ElasticMarker
158
- } ) ) ;
159
-
160
- // #else
161
- generatedDeclaration = generatedDeclaration . WithTrailingTrivia (
162
- generatedDeclaration . GetTrailingTrivia ( )
163
- . AddRange ( new [ ] {
164
- SyntaxFactory . Trivia ( SyntaxFactory . ElseDirectiveTrivia ( isActive : false , branchTaken : false ) ) ,
165
- SyntaxFactory . ElasticMarker
166
- } ) ) ;
167
-
168
- // Sort attribute arguments so that GeneratedDllImport and DllImport match
169
- MethodDeclarationSyntax updatedDeclaration = ( MethodDeclarationSyntax ) generator . ReplaceNode ( methodSyntax , dllImportSyntax , SortDllImportAttributeArguments ( dllImportSyntax , generator ) ) ;
170
-
171
- // Remove existing leading trivia - it will be on the GeneratedDllImport method
172
- updatedDeclaration = updatedDeclaration . WithLeadingTrivia ( ) ;
173
-
174
- // #endif
175
- updatedDeclaration = updatedDeclaration . WithTrailingTrivia (
176
- methodSyntax . GetTrailingTrivia ( )
177
- . AddRange ( new [ ] {
178
- SyntaxFactory . Trivia ( SyntaxFactory . EndIfDirectiveTrivia ( isActive : true ) ) ,
179
- SyntaxFactory . ElasticMarker
180
- } ) ) ;
181
-
182
- // Add the GeneratedDllImport method
183
- editor . InsertBefore ( methodSyntax , generatedDeclaration ) ;
184
-
185
- // Replace the original method with the updated DllImport method
186
- editor . ReplaceNode ( methodSyntax , updatedDeclaration ) ;
187
- }
128
+ // Replace the original method with the updated one
129
+ editor . ReplaceNode ( methodSyntax , generatedDeclaration ) ;
188
130
189
131
return editor . GetChangedDocument ( ) ;
190
132
}
0 commit comments