@@ -306,19 +306,17 @@ private static bool TryProcessMultiPointSelection(MarkupSyntaxNode startElementN
306
306
return true ;
307
307
}
308
308
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 ) )
315
311
{
316
- extractStart = endElementNode . Span . Start ;
312
+ extractEnd = startElementNode . Span . End ;
313
+ return true ;
317
314
}
318
315
319
- if ( startNodeContainsEndNode )
316
+ if ( startElementNode . Ancestors ( ) . Contains ( endElementNode ) )
320
317
{
321
- extractEnd = startElementNode . Span . End ;
318
+ extractStart = endElementNode . Span . Start ;
319
+ return true ;
322
320
}
323
321
324
322
// 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
334
332
// Selected text ends here <span></span>
335
333
// </div>
336
334
// 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 )
338
336
{
339
337
// Find the closest containing sibling pair that encompasses both the start and end elements
340
338
var ( selectStart , selectEnd ) = FindContainingSiblingPair ( startElementNode , endElementNode ) ;
341
-
342
- // If we found a valid containing pair, update the extraction range
343
339
if ( selectStart is not null && selectEnd is not null )
344
340
{
345
341
extractStart = selectStart . Span . Start ;
346
342
extractEnd = selectEnd . Span . End ;
347
-
348
343
return true ;
349
344
}
350
- // Note: If we don't find a valid pair, we keep the original extraction range
351
- }
352
345
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 ;
356
348
}
357
349
358
350
var endLocation = GetEndLocation ( actionParams . SelectEnd , codeDocument . GetSourceText ( ) ) ;
@@ -368,18 +360,13 @@ private static bool TryProcessMultiPointSelection(MarkupSyntaxNode startElementN
368
360
endCodeBlock = previousSibling . FirstAncestorOrSelf < CSharpCodeBlockSyntax > ( ) ;
369
361
}
370
362
371
- if ( endCodeBlock is null )
363
+ if ( endCodeBlock is not null )
372
364
{
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 ;
375
368
}
376
369
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
-
383
370
return true ;
384
371
}
385
372
@@ -408,15 +395,16 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
408
395
if ( startContainingNode is null && childSpan . Contains ( startSpan ) )
409
396
{
410
397
startContainingNode = child ;
411
- if ( endContainingNode is not null )
412
- break ; // Exit if we've found both
413
398
}
414
399
415
400
if ( childSpan . Contains ( endSpan ) )
416
401
{
417
402
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 ;
420
408
}
421
409
}
422
410
@@ -425,21 +413,18 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy
425
413
426
414
private static SyntaxNode ? FindNearestCommonAncestor ( SyntaxNode node1 , SyntaxNode node2 )
427
415
{
428
- var current = node1 ;
429
- while ( current is not null )
416
+ for ( var current = node1 ; current is not null ; current = current . Parent )
430
417
{
431
- if ( CheckNode ( current ) && current . Span . Contains ( node2 . Span ) )
418
+ if ( IsValidAncestorNode ( current ) && current . Span . Contains ( node2 . Span ) )
432
419
{
433
420
return current ;
434
421
}
435
-
436
- current = current . Parent ;
437
422
}
438
423
439
424
return null ;
440
425
}
441
426
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 ;
443
428
444
429
private static bool IsValidNode ( SyntaxNode node , bool isCodeBlock )
445
430
{
0 commit comments