Skip to content

Commit d83031b

Browse files
committed
Another approach for refining new component usings
1 parent 3d9614c commit d83031b

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionProvider.cs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,19 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
7575

7676
var actionParams = CreateInitialActionParams(context, startElementNode, @namespace);
7777

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-
8378
ProcessSelection(startElementNode, endElementNode, actionParams);
8479

8580
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+
8689
AddUsingDirectivesInRange(utilityScanRoot,
87-
usingsInImportsFile,
90+
usingsInFile,
8891
actionParams.ExtractStart,
8992
actionParams.ExtractEnd,
9093
actionParams);
@@ -169,29 +172,6 @@ private static ExtractToComponentCodeActionParams CreateInitialActionParams(Razo
169172
};
170173
}
171174

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-
195175
/// <summary>
196176
/// Processes a multi-point selection to determine the correct range for extraction.
197177
/// </summary>
@@ -307,7 +287,7 @@ MarkupTagHelperAttributeSyntax or
307287
return null;
308288
}
309289

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)
311291
{
312292
var components = new HashSet<string>();
313293
var extractSpan = new TextSpan(extractStart, extractEnd - extractStart);
@@ -316,7 +296,7 @@ private static void AddUsingDirectivesInRange(SyntaxNode root, string usingsInIm
316296
{
317297
if (node is MarkupTagHelperElementSyntax { TagHelperInfo: { } tagHelperInfo })
318298
{
319-
AddUsingFromTagHelperInfo(tagHelperInfo, components, usingsInImportsFile, actionParams);
299+
AddUsingFromTagHelperInfo(tagHelperInfo, components, usingsInFile, actionParams);
320300
}
321301
}
322302
}
@@ -331,13 +311,9 @@ private static void AddUsingFromTagHelperInfo(TagHelperInfo tagHelperInfo, HashS
331311
}
332312

333313
var typeNamespace = descriptor.GetTypeNamespace();
334-
if (components.Add(typeNamespace))
314+
if (components.Add(typeNamespace) && usingsInImportsFile.Contains(typeNamespace))
335315
{
336-
var usingDirective = $"@using {typeNamespace}";
337-
if (!usingsInImportsFile.Contains(usingDirective))
338-
{
339-
actionParams.usingDirectives.Add(usingDirective);
340-
}
316+
actionParams.usingDirectives.Add($"@using {typeNamespace}");
341317
}
342318
}
343319
}

0 commit comments

Comments
 (0)