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 369d125b888..0c971e2685b 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 @@ -9,7 +9,6 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -using ICSharpCode.Decompiler.CSharp.Syntax; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.Components; using Microsoft.AspNetCore.Razor.Language.Extensions; @@ -108,6 +107,7 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma { return true; } + return context.Location.AbsoluteIndex > startElementNode.StartTag.Span.End && context.Location.AbsoluteIndex < startElementNode.EndTag.SpanStart; } @@ -128,7 +128,6 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma } var endOwner = syntaxTree.Root.FindInnermostNode(endLocation.Value.AbsoluteIndex, true); - if (endOwner is null) { return null; @@ -206,10 +205,11 @@ private static void ProcessMultiPointSelection(MarkupElementSyntax startElementN private static SourceLocation? GetEndLocation(Position selectionEnd, RazorCodeDocument codeDocument, ILogger logger) { - if (!selectionEnd.TryGetSourceLocation(codeDocument.GetSourceText(), logger, out var location)) + if (!selectionEnd.TryGetSourceLocation(codeDocument.Source.Text, logger, out var location)) { return null; } + return location; } @@ -236,7 +236,7 @@ private static (SyntaxNode? Start, SyntaxNode? End) FindContainingSiblingPair(Sy var startSpan = startNode.Span; var endSpan = endNode.Span; - foreach (var child in nearestCommonAncestor.ChildNodes().Where(node => node.Kind == SyntaxKind.MarkupElement)) + foreach (var child in nearestCommonAncestor.ChildNodes().Where(static node => node.Kind == SyntaxKind.MarkupElement)) { var childSpan = child.Span; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs index c1f443df593..f4dee81574f 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs @@ -155,6 +155,8 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty() var location = new SourceLocation(cursorPosition, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents); + AddMultiPointSelectionToContext(context, selectionSpan); + var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory); // Act @@ -162,6 +164,11 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty() // Assert Assert.NotEmpty(commandOrCodeActionContainer); + var codeAction = Assert.Single(commandOrCodeActionContainer); + var razorCodeActionResolutionParams = ((JsonElement)codeAction.Data!).Deserialize(); + Assert.NotNull(razorCodeActionResolutionParams); + var actionParams = ((JsonElement)razorCodeActionResolutionParams.Data).Deserialize(); + Assert.NotNull(actionParams); } [Fact] @@ -201,7 +208,7 @@ public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentEl var location = new SourceLocation(cursorPosition, -1, -1); var context = CreateRazorCodeActionContext(request, location, documentPath, contents); - AddMultiPointSelectionToContext(ref context, selectionSpan); + AddMultiPointSelectionToContext(context, selectionSpan); var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory); @@ -249,10 +256,9 @@ private static RazorCodeActionContext CreateRazorCodeActionContext(VSCodeActionP return context; } - - private static void AddMultiPointSelectionToContext(ref RazorCodeActionContext context, TextSpan selectionSpan) + private static void AddMultiPointSelectionToContext(RazorCodeActionContext context, TextSpan selectionSpan) { - var sourceText = context.CodeDocument.GetSourceText(); + var sourceText = context.CodeDocument.Source.Text; var startLinePosition = sourceText.Lines.GetLinePosition(selectionSpan.Start); var startPosition = new Position(startLinePosition.Line, startLinePosition.Character); diff --git a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/FileUtilities.cs b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/FileUtilities.cs index fd73c5bccb4..829fb250647 100644 --- a/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/FileUtilities.cs +++ b/src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/FileUtilities.cs @@ -19,10 +19,7 @@ internal static class FileUtilities public static string GenerateUniquePath(string path, string extension) { // Add check for rooted path in the future, currently having issues in platforms other than Windows. - //if (!Path.IsPathRooted(path)) - //{ - // throw new ArgumentException("The path is not rooted.", nameof(path)); - //} + // See: https://github.com/dotnet/razor/issues/10684 var directoryName = Path.GetDirectoryName(path).AssumeNotNull(); var baseFileName = Path.GetFileNameWithoutExtension(path);