diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToNewComponentCodeActionProvider.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToNewComponentCodeActionProvider.cs index 72dd5dec289..6ae53437ba7 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToNewComponentCodeActionProvider.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToNewComponentCodeActionProvider.cs @@ -119,20 +119,15 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma return null; } - var endLocation = GetEndLocation(selectionEnd, context.CodeDocument); - if (!endLocation.HasValue) - { - return null; - } - - var endOwner = syntaxTree.Root.FindInnermostNode(endLocation.Value.AbsoluteIndex, true); + var endAbsoluteIndex = context.SourceText.GetRequiredAbsoluteIndex(selectionEnd); + var endOwner = syntaxTree.Root.FindInnermostNode(endAbsoluteIndex, true); if (endOwner is null) { return null; } // Correct selection to include the current node if the selection ends immediately after a closing tag. - if (endOwner is MarkupTextLiteralSyntax && string.IsNullOrWhiteSpace(endOwner.ToFullString()) && endOwner.TryGetPreviousSibling(out var previousSibling)) + if (endOwner is MarkupTextLiteralSyntax && endOwner.ContainsOnlyWhitespace() && endOwner.TryGetPreviousSibling(out var previousSibling)) { endOwner = previousSibling; } @@ -178,13 +173,14 @@ private static void ProcessSelection(MarkupElementSyntax startElementNode, Marku // This conditional handles cases where the user's selection spans across different levels of the DOM. // For example: //
- // - // Selected text starts here

Some text

+ // {|result: + // {|selection:

Some text

//
// //

More text

//
- // Selected text ends here + // + // |}|} //
// In this case, we need to find the smallest set of complete elements that covers the entire selection. @@ -200,16 +196,6 @@ private static void ProcessSelection(MarkupElementSyntax startElementNode, Marku // Note: If we don't find a valid pair, we keep the original extraction range } - private static SourceLocation? GetEndLocation(Position selectionEnd, RazorCodeDocument codeDocument) - { - if (!codeDocument.Source.Text.TryGetSourceLocation(selectionEnd, out var location)) - { - return null; - } - - return location; - } - private static bool TryGetNamespace(RazorCodeDocument codeDocument, [NotNullWhen(returnValue: true)] out string? @namespace) // If the compiler can't provide a computed namespace it will fallback to "__GeneratedComponent" or // similar for the NamespaceNode. This would end up with extracting to a wrong namespace