Skip to content

Commit cba6cf1

Browse files
committed
Base component dependency functionality and fixed range selection bug
FROM MERGE
1 parent 1b6e035 commit cba6cf1

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Models/ExtractToNewComponentCodeActionParams.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Text.Json.Serialization;
67

78
namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models;
@@ -10,10 +11,16 @@ internal sealed class ExtractToNewComponentCodeActionParams
1011
{
1112
[JsonPropertyName("uri")]
1213
public required Uri Uri { get; set; }
14+
1315
[JsonPropertyName("extractStart")]
1416
public int ExtractStart { get; set; }
17+
1518
[JsonPropertyName("extractEnd")]
1619
public int ExtractEnd { get; set; }
20+
1721
[JsonPropertyName("namespace")]
1822
public required string Namespace { get; set; }
23+
24+
[JsonPropertyName("dependencies")]
25+
public required List<string> Dependencies { get; set; }
1926
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,37 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
257257

258258
return null;
259259
}
260+
private static HashSet<SyntaxNode> IdentifyComponentsInRange(SyntaxNode root, int extractStart, int extractEnd)
261+
{
262+
var components = new HashSet<SyntaxNode>();
263+
var extractSpan = new TextSpan(extractStart, extractEnd - extractStart);
264+
265+
foreach (var node in root.DescendantNodes())
266+
{
267+
if (node is MarkupTagHelperElementSyntax markupElement &&
268+
extractSpan.Contains(markupElement.Span))
269+
{
270+
components.Add(markupElement);
271+
}
272+
}
273+
274+
return components;
275+
}
276+
277+
private static List<string> GetAllUsingStatements(SyntaxNode root)
278+
{
279+
return root.DescendantNodes()
280+
.OfType<CSharpCodeBlockSyntax>()
281+
.SelectMany(cSharpBlock => cSharpBlock.ChildNodes())
282+
.OfType<RazorDirectiveSyntax>()
283+
.Select(directive => directive.ToFullString().TrimStart())
284+
.Where(directiveString => directiveString.StartsWith("@using"))
285+
.Select(directiveString => directiveString.Trim())
286+
.ToList();
287+
}
288+
289+
//private static bool HasUnsupportedChildren(Language.Syntax.SyntaxNode node)
290+
//{
291+
// return node.DescendantNodes().Any(static n => n is MarkupBlockSyntax or CSharpTransitionSyntax or RazorCommentBlockSyntax);
292+
//}
260293
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ internal sealed class ExtractToNewComponentCodeActionResolver(
9090
}
9191

9292
var componentName = Path.GetFileNameWithoutExtension(componentPath);
93-
var newComponentContent = text.GetSubTextString(new TextSpan(actionParams.ExtractStart, actionParams.ExtractEnd - actionParams.ExtractStart)).Trim();
93+
var newComponentContent = string.Empty;
94+
95+
newComponentContent += string.Join(Environment.NewLine, actionParams.Dependencies);
96+
if (actionParams.Dependencies.Count > 0)
97+
{
98+
newComponentContent += Environment.NewLine + Environment.NewLine; // Ensure there's a newline after the dependencies if any exist.
99+
}
100+
101+
newComponentContent = newComponentContent + text.GetSubTextString(new CodeAnalysis.Text.TextSpan(actionParams.ExtractStart, actionParams.ExtractEnd - actionParams.ExtractStart)).Trim();
94102

95103
var start = componentDocument.Source.Text.Lines.GetLinePosition(actionParams.ExtractStart);
96104
var end = componentDocument.Source.Text.Lines.GetLinePosition(actionParams.ExtractEnd);
@@ -100,7 +108,9 @@ internal sealed class ExtractToNewComponentCodeActionResolver(
100108
End = new Position(end.Line, end.Character)
101109
};
102110

103-
var componentDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = actionParams.Uri };
111+
112+
113+
var componentDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = actionParams.Uri };
104114
var newComponentDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier { Uri = newComponentUri };
105115

106116
var documentChanges = new SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[]

0 commit comments

Comments
 (0)