@@ -75,16 +75,19 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
75
75
76
76
var actionParams = CreateInitialActionParams ( context , startElementNode , @namespace ) ;
77
77
78
- var path = FilePathNormalizer . Normalize ( actionParams . Uri . GetAbsoluteOrUNCPath ( ) ) ;
79
- var directoryName = Path . GetDirectoryName ( path ) . AssumeNotNull ( ) ;
80
- var importsBuilder = new StringBuilder ( ) ;
81
- var usingsInImportsFile = GetUsingsInImportsFile ( directoryName , importsBuilder ) ;
82
-
83
78
ProcessSelection ( startElementNode , endElementNode , actionParams ) ;
84
79
85
80
var utilityScanRoot = FindNearestCommonAncestor ( startElementNode , endElementNode ) ?? startElementNode ;
81
+
82
+ // The new component usings are going to be a subset of the usings in the source razor file
83
+ var usingsInFile = context . SourceText . ToString ( )
84
+ . Split ( '\n ' )
85
+ . Where ( line => line . TrimStart ( ) . StartsWith ( "@using" ) )
86
+ . Aggregate ( new StringBuilder ( ) , ( sb , line ) => sb . AppendLine ( line ) )
87
+ . ToString ( ) ;
88
+
86
89
AddUsingDirectivesInRange ( utilityScanRoot ,
87
- usingsInImportsFile ,
90
+ usingsInFile ,
88
91
actionParams . ExtractStart ,
89
92
actionParams . ExtractEnd ,
90
93
actionParams ) ;
@@ -169,29 +172,6 @@ private static ExtractToComponentCodeActionParams CreateInitialActionParams(Razo
169
172
} ;
170
173
}
171
174
172
- // When adding usings, we must check for the first _Imports.razor file in the current directory or any parent directories.
173
- // In the new component, only add usings that are not already present in the _Imports.razor file.
174
- public static string GetUsingsInImportsFile ( string currentDirectory , StringBuilder importsBuilder )
175
- {
176
- var importsFile = Path . Combine ( currentDirectory , "_Imports.razor" ) ;
177
- if ( File . Exists ( importsFile ) )
178
- {
179
- return File . ReadAllLines ( importsFile )
180
- . Where ( line => line . TrimStart ( ) . StartsWith ( "@using" ) )
181
- . Select ( line => line . Trim ( ) )
182
- . Aggregate ( importsBuilder , ( sb , line ) => sb . AppendLine ( line ) )
183
- . ToString ( ) ;
184
- }
185
-
186
- var parentDirectory = Path . GetDirectoryName ( currentDirectory ) ;
187
- if ( parentDirectory is not null && Directory . Exists ( parentDirectory ) )
188
- {
189
- return GetUsingsInImportsFile ( parentDirectory , importsBuilder ) ;
190
- }
191
-
192
- return string . Empty ;
193
- }
194
-
195
175
/// <summary>
196
176
/// Processes a multi-point selection to determine the correct range for extraction.
197
177
/// </summary>
@@ -307,7 +287,7 @@ MarkupTagHelperAttributeSyntax or
307
287
return null ;
308
288
}
309
289
310
- private static void AddUsingDirectivesInRange ( SyntaxNode root , string usingsInImportsFile , int extractStart , int extractEnd , ExtractToComponentCodeActionParams actionParams )
290
+ private static void AddUsingDirectivesInRange ( SyntaxNode root , string usingsInFile , int extractStart , int extractEnd , ExtractToComponentCodeActionParams actionParams )
311
291
{
312
292
var components = new HashSet < string > ( ) ;
313
293
var extractSpan = new TextSpan ( extractStart , extractEnd - extractStart ) ;
@@ -316,7 +296,7 @@ private static void AddUsingDirectivesInRange(SyntaxNode root, string usingsInIm
316
296
{
317
297
if ( node is MarkupTagHelperElementSyntax { TagHelperInfo : { } tagHelperInfo } )
318
298
{
319
- AddUsingFromTagHelperInfo ( tagHelperInfo , components , usingsInImportsFile , actionParams ) ;
299
+ AddUsingFromTagHelperInfo ( tagHelperInfo , components , usingsInFile , actionParams ) ;
320
300
}
321
301
}
322
302
}
@@ -331,13 +311,9 @@ private static void AddUsingFromTagHelperInfo(TagHelperInfo tagHelperInfo, HashS
331
311
}
332
312
333
313
var typeNamespace = descriptor . GetTypeNamespace ( ) ;
334
- if ( components . Add ( typeNamespace ) )
314
+ if ( components . Add ( typeNamespace ) && usingsInImportsFile . Contains ( typeNamespace ) )
335
315
{
336
- var usingDirective = $ "@using { typeNamespace } ";
337
- if ( ! usingsInImportsFile . Contains ( usingDirective ) )
338
- {
339
- actionParams . usingDirectives . Add ( usingDirective ) ;
340
- }
316
+ actionParams . usingDirectives . Add ( $ "@using { typeNamespace } ") ;
341
317
}
342
318
}
343
319
}
0 commit comments