Skip to content

Commit 4297e6f

Browse files
committed
PR Feedback
1 parent 4bab4dd commit 4297e6f

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,15 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma
119119
return null;
120120
}
121121

122-
var endLocation = GetEndLocation(selectionEnd, context.CodeDocument);
123-
if (!endLocation.HasValue)
124-
{
125-
return null;
126-
}
127-
128-
var endOwner = syntaxTree.Root.FindInnermostNode(endLocation.Value.AbsoluteIndex, true);
122+
var endAbsoluteIndex = context.SourceText.GetRequiredAbsoluteIndex(selectionEnd);
123+
var endOwner = syntaxTree.Root.FindInnermostNode(endAbsoluteIndex, true);
129124
if (endOwner is null)
130125
{
131126
return null;
132127
}
133128

134129
// Correct selection to include the current node if the selection ends immediately after a closing tag.
135-
if (endOwner is MarkupTextLiteralSyntax && string.IsNullOrWhiteSpace(endOwner.ToFullString()) && endOwner.TryGetPreviousSibling(out var previousSibling))
130+
if (endOwner is MarkupTextLiteralSyntax && endOwner.ContainsOnlyWhitespace() && endOwner.TryGetPreviousSibling(out var previousSibling))
136131
{
137132
endOwner = previousSibling;
138133
}
@@ -178,13 +173,14 @@ private static void ProcessSelection(MarkupElementSyntax startElementNode, Marku
178173
// This conditional handles cases where the user's selection spans across different levels of the DOM.
179174
// For example:
180175
// <div>
181-
// <span>
182-
// Selected text starts here<p>Some text</p>
176+
// {|result:<span>
177+
// {|selection:<p>Some text</p>
183178
// </span>
184179
// <span>
185180
// <p>More text</p>
186181
// </span>
187-
// Selected text ends here <span></span>
182+
// <span>
183+
// </span>|}|}
188184
// </div>
189185
// In this case, we need to find the smallest set of complete elements that covers the entire selection.
190186

@@ -200,16 +196,6 @@ private static void ProcessSelection(MarkupElementSyntax startElementNode, Marku
200196
// Note: If we don't find a valid pair, we keep the original extraction range
201197
}
202198

203-
private static SourceLocation? GetEndLocation(Position selectionEnd, RazorCodeDocument codeDocument)
204-
{
205-
if (!codeDocument.Source.Text.TryGetSourceLocation(selectionEnd, out var location))
206-
{
207-
return null;
208-
}
209-
210-
return location;
211-
}
212-
213199
private static bool TryGetNamespace(RazorCodeDocument codeDocument, [NotNullWhen(returnValue: true)] out string? @namespace)
214200
// If the compiler can't provide a computed namespace it will fallback to "__GeneratedComponent" or
215201
// similar for the NamespaceNode. This would end up with extracting to a wrong namespace

0 commit comments

Comments
 (0)