Skip to content

Commit 7d3c78a

Browse files
committed
PR Feedback
1 parent 8a7d0bb commit 7d3c78a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma
135135
}
136136

137137
// Correct selection to include the current node if the selection ends immediately after a closing tag.
138-
if (string.IsNullOrWhiteSpace(endOwner.ToFullString()) && endOwner.TryGetPreviousSibling(out var previousSibling))
138+
if (endOwner is MarkupTextLiteralSyntax && string.IsNullOrWhiteSpace(endOwner.ToFullString()) && endOwner.TryGetPreviousSibling(out var previousSibling))
139139
{
140140
endOwner = previousSibling;
141141
}
142142

143-
return endOwner?.FirstAncestorOrSelf<MarkupElementSyntax>();
143+
return endOwner.FirstAncestorOrSelf<MarkupElementSyntax>();
144144
}
145145

146146
private static ExtractToNewComponentCodeActionParams CreateInitialActionParams(RazorCodeActionContext context, MarkupElementSyntax startElementNode, string @namespace)
@@ -168,11 +168,13 @@ private static void ProcessMultiPointSelection(MarkupElementSyntax startElementN
168168
return;
169169
}
170170

171-
// Check if the start element is an ancestor of the end element
172-
var selectionStartHasParentElement = endElementNode.Ancestors().Any(node => node == startElementNode);
171+
var startNodeContainsEndNode = endElementNode.Ancestors().Any(node => node == startElementNode);
173172

174173
// If the start element is an ancestor, keep the original end; otherwise, use the end of the end element
175-
actionParams.ExtractEnd = selectionStartHasParentElement ? actionParams.ExtractEnd : endElementNode.Span.End;
174+
if (!startNodeContainsEndNode)
175+
{
176+
actionParams.ExtractEnd = endElementNode.Span.End;
177+
}
176178

177179
// If the start element is not an ancestor of the end element, we need to find a common parent
178180
// This conditional handles cases where the user's selection spans across different levels of the DOM.
@@ -187,7 +189,7 @@ private static void ProcessMultiPointSelection(MarkupElementSyntax startElementN
187189
// Selected text ends here <span></span>
188190
// </div>
189191
// In this case, we need to find the smallest set of complete elements that covers the entire selection.
190-
if (!selectionStartHasParentElement)
192+
if (!startNodeContainsEndNode)
191193
{
192194
// Find the closest containing sibling pair that encompasses both the start and end elements
193195
var (extractStart, extractEnd) = FindContainingSiblingPair(startElementNode, endElementNode);

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
173173
}
174174

175175
[Fact]
176-
public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentElement()
176+
public async Task Handle_MultiPointSelection_WithEndAfterElement_ReturnsCurrentElement()
177177
{
178178
// Arrange
179179
var documentPath = "c:/Test.razor";

0 commit comments

Comments
 (0)