Skip to content

Commit 3455a0e

Browse files
committed
Refactoring of TryProcessMultiPointSelection and FindContainingSiblingPair
1 parent 68fdf30 commit 3455a0e

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

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

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,17 @@ private static bool TryProcessMultiPointSelection(MarkupSyntaxNode startElementN
306306
return true;
307307
}
308308

309-
// Check if the start element is an ancestor of the end element or vice versa
310-
var startNodeContainsEndNode = endElementNode.Ancestors().Any(node => node == startElementNode);
311-
var endNodeContainsStartNode = startElementNode.Ancestors().Any(node => node == endElementNode);
312-
313-
// If the start element is an ancestor of the end element (or vice versa), update the extraction
314-
if (endNodeContainsStartNode)
309+
// If the start node contains the end node (or vice versa), we can extract the entire range
310+
if (endElementNode.Ancestors().Contains(startElementNode))
315311
{
316-
extractStart = endElementNode.Span.Start;
312+
extractEnd = startElementNode.Span.End;
313+
return true;
317314
}
318315

319-
if (startNodeContainsEndNode)
316+
if (startElementNode.Ancestors().Contains(endElementNode))
320317
{
321-
extractEnd = startElementNode.Span.End;
318+
extractStart = endElementNode.Span.Start;
319+
return true;
322320
}
323321

324322
// If the start element is not an ancestor of the end element (or vice versa), we need to find a common parent
@@ -334,25 +332,19 @@ private static bool TryProcessMultiPointSelection(MarkupSyntaxNode startElementN
334332
// Selected text ends here <span></span>
335333
// </div>
336334
// In this case, we need to find the smallest set of complete elements that covers the entire selection.
337-
if (startElementNode != endElementNode && !(startNodeContainsEndNode || endNodeContainsStartNode))
335+
if (startElementNode != endElementNode)
338336
{
339337
// Find the closest containing sibling pair that encompasses both the start and end elements
340338
var (selectStart, selectEnd) = FindContainingSiblingPair(startElementNode, endElementNode);
341-
342-
// If we found a valid containing pair, update the extraction range
343339
if (selectStart is not null && selectEnd is not null)
344340
{
345341
extractStart = selectStart.Span.Start;
346342
extractEnd = selectEnd.Span.End;
347-
348343
return true;
349344
}
350-
// Note: If we don't find a valid pair, we keep the original extraction range
351-
}
352345

353-
if (startElementNode != endElementNode)
354-
{
355-
return true; // Will only trigger when the end of the selection does not include a code block.
346+
// Note: If we don't find a valid pair, we keep the original extraction range
347+
return true;
356348
}
357349

358350
var endLocation = GetEndLocation(actionParams.SelectEnd, codeDocument.GetSourceText());
@@ -368,18 +360,13 @@ private static bool TryProcessMultiPointSelection(MarkupSyntaxNode startElementN
368360
endCodeBlock = previousSibling.FirstAncestorOrSelf<CSharpCodeBlockSyntax>();
369361
}
370362

371-
if (endCodeBlock is null)
363+
if (endCodeBlock is not null)
372364
{
373-
// One of the cases where this triggers is when a single element is multi-pointedly selected
374-
return true;
365+
var (withCodeBlockStart, withCodeBlockEnd) = FindContainingSiblingPair(startElementNode, endCodeBlock);
366+
extractStart = withCodeBlockStart?.Span.Start ?? extractStart;
367+
extractEnd = withCodeBlockEnd?.Span.End ?? extractEnd;
375368
}
376369

377-
var (withCodeBlockStart, withCodeBlockEnd) = FindContainingSiblingPair(startElementNode, endCodeBlock);
378-
379-
// If selection ends on code block, set the extract end to the end of the code block.
380-
extractStart = withCodeBlockStart?.Span.Start ?? extractStart;
381-
extractEnd = withCodeBlockEnd?.Span.End ?? extractEnd;
382-
383370
return true;
384371
}
385372

@@ -408,15 +395,16 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
408395
if (startContainingNode is null && childSpan.Contains(startSpan))
409396
{
410397
startContainingNode = child;
411-
if (endContainingNode is not null)
412-
break; // Exit if we've found both
413398
}
414399

415400
if (childSpan.Contains(endSpan))
416401
{
417402
endContainingNode = child;
418-
if (startContainingNode is not null)
419-
break; // Exit if we've found both
403+
}
404+
405+
if (startContainingNode is not null && endContainingNode is not null)
406+
{
407+
break;
420408
}
421409
}
422410

@@ -425,21 +413,18 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
425413

426414
private static SyntaxNode? FindNearestCommonAncestor(SyntaxNode node1, SyntaxNode node2)
427415
{
428-
var current = node1;
429-
while (current is not null)
416+
for (var current = node1; current is not null; current = current.Parent)
430417
{
431-
if (CheckNode(current) && current.Span.Contains(node2.Span))
418+
if (IsValidAncestorNode(current) && current.Span.Contains(node2.Span))
432419
{
433420
return current;
434421
}
435-
436-
current = current.Parent;
437422
}
438423

439424
return null;
440425
}
441426

442-
private static bool CheckNode(SyntaxNode node) => node is MarkupElementSyntax or MarkupTagHelperElementSyntax or MarkupBlockSyntax;
427+
private static bool IsValidAncestorNode(SyntaxNode node) => node is MarkupElementSyntax or MarkupTagHelperElementSyntax or MarkupBlockSyntax;
443428

444429
private static bool IsValidNode(SyntaxNode node, bool isCodeBlock)
445430
{

0 commit comments

Comments
 (0)