diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CodeActionResolveEndpoint.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CodeActionResolveEndpoint.cs index 4ec0b8cf40..83ae35cd3c 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CodeActionResolveEndpoint.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/CodeActionResolveEndpoint.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models; using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts; using Microsoft.AspNetCore.Razor.PooledObjects; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.Logging; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Protocol; @@ -25,11 +26,13 @@ internal sealed class CodeActionResolveEndpoint( IEnumerable razorCodeActionResolvers, IEnumerable csharpCodeActionResolvers, IEnumerable htmlCodeActionResolvers, + RazorLSPOptionsMonitor razorLSPOptionsMonitor, ILoggerFactory loggerFactory) : IRazorRequestHandler { private readonly FrozenDictionary _razorCodeActionResolvers = CreateResolverMap(razorCodeActionResolvers); private readonly FrozenDictionary _csharpCodeActionResolvers = CreateResolverMap(csharpCodeActionResolvers); private readonly FrozenDictionary _htmlCodeActionResolvers = CreateResolverMap(htmlCodeActionResolvers); + private readonly RazorLSPOptionsMonitor _razorLSPOptionsMonitor = razorLSPOptionsMonitor; private readonly ILogger _logger = loggerFactory.GetOrCreateLogger(); public bool MutatesSolutionState => false; @@ -54,6 +57,13 @@ internal sealed class CodeActionResolveEndpoint( return request; } + var options = new RazorFormattingOptions + { + TabSize = _razorLSPOptionsMonitor.CurrentValue.TabSize, + InsertSpaces = _razorLSPOptionsMonitor.CurrentValue.InsertSpaces, + CodeBlockBraceOnNextLine = _razorLSPOptionsMonitor.CurrentValue.CodeBlockBraceOnNextLine + }; + request.Data = resolutionParams.Data; switch (resolutionParams.Language) @@ -63,6 +73,7 @@ internal sealed class CodeActionResolveEndpoint( documentContext, request, resolutionParams, + options, cancellationToken).ConfigureAwait(false); case RazorLanguageKind.CSharp: return await ResolveCSharpCodeActionAsync( @@ -102,6 +113,7 @@ internal sealed class CodeActionResolveEndpoint( DocumentContext documentContext, CodeAction codeAction, RazorCodeActionResolutionParams resolutionParams, + RazorFormattingOptions options, CancellationToken cancellationToken) { if (!_razorCodeActionResolvers.TryGetValue(resolutionParams.Action, out var resolver)) @@ -117,7 +129,7 @@ internal sealed class CodeActionResolveEndpoint( return codeAction; } - var edit = await resolver.ResolveAsync(documentContext, data, cancellationToken).ConfigureAwait(false); + var edit = await resolver.ResolveAsync(documentContext, data, options, cancellationToken).ConfigureAwait(false); codeAction.Edit = edit; return codeAction; } @@ -181,8 +193,8 @@ internal sealed class CodeActionResolveEndpoint( internal readonly struct TestAccessor(CodeActionResolveEndpoint instance) { - public Task ResolveRazorCodeActionAsync(DocumentContext documentContext, CodeAction codeAction, RazorCodeActionResolutionParams resolutionParams, CancellationToken cancellationToken) - => instance.ResolveRazorCodeActionAsync(documentContext, codeAction, resolutionParams, cancellationToken); + public Task ResolveRazorCodeActionAsync(DocumentContext documentContext, CodeAction codeAction, RazorCodeActionResolutionParams resolutionParams, RazorFormattingOptions options, CancellationToken cancellationToken) + => instance.ResolveRazorCodeActionAsync(documentContext, codeAction, resolutionParams, options, cancellationToken); public Task ResolveCSharpCodeActionAsync(DocumentContext documentContext, CodeAction codeAction, RazorCodeActionResolutionParams resolutionParams, CancellationToken cancellationToken) => instance.ResolveCSharpCodeActionAsync(documentContext, codeAction, resolutionParams, cancellationToken); diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/AddUsingsCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/AddUsingsCodeActionResolver.cs index 9ecb00dc64..c8c1cdc430 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/AddUsingsCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/AddUsingsCodeActionResolver.cs @@ -19,7 +19,7 @@ internal sealed class AddUsingsCodeActionResolver : IRazorCodeActionResolver { public string Action => LanguageServerConstants.CodeActions.AddUsing; - public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { var actionParams = data.Deserialize(); if (actionParams is null) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CodeBlockService.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CodeBlockService.cs index 01869f6106..f4cff29e71 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CodeBlockService.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CodeBlockService.cs @@ -34,12 +34,12 @@ internal static class CodeBlockService /// } /// /// - /// The that contains information about indentation. + /// The that contains information about indentation. /// /// /// A that will place the formatted generated method within a @code block in the file. /// - public static TextEdit[] CreateFormattedTextEdit(RazorCodeDocument code, string templateWithMethodSignature, RazorLSPOptions options) + public static TextEdit[] CreateFormattedTextEdit(RazorCodeDocument code, string templateWithMethodSignature, RazorFormattingOptions options) { var csharpCodeBlock = code.GetSyntaxTree().Root.DescendantNodes() .Select(RazorSyntaxFacts.TryGetCSharpCodeFromCodeBlock) @@ -104,7 +104,7 @@ internal static class CodeBlockService int openBraceLineIndex, int closeBraceLineIndex, SourceLocation insertLocation, - RazorLSPOptions options, + RazorFormattingOptions options, string method) { // The absolute index and character index of the code block's location points to the end of '@code'. diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CreateComponentCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CreateComponentCodeActionResolver.cs index ff58cdea1f..5b9d907284 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CreateComponentCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/CreateComponentCodeActionResolver.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Razor.Language.Extensions; using Microsoft.AspNetCore.Razor.Language.Syntax; using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models; using Microsoft.AspNetCore.Razor.PooledObjects; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Workspaces; using Microsoft.VisualStudio.LanguageServer.Protocol; @@ -24,7 +25,7 @@ internal sealed class CreateComponentCodeActionResolver(LanguageServerFeatureOpt public string Action => LanguageServerConstants.CodeActions.CreateComponentFromTag; - public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { var actionParams = data.Deserialize(); if (actionParams is null) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs index f1c968d14e..245d2042ae 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs @@ -16,6 +16,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Formatting; using Microsoft.CodeAnalysis.Razor; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Protocol; using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions; @@ -36,7 +37,7 @@ internal sealed class ExtractToCodeBehindCodeActionResolver( public string Action => LanguageServerConstants.CodeActions.ExtractToCodeBehindAction; - public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { var actionParams = data.Deserialize(); if (actionParams is null) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs index a0a265359f..1cc26c623e 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs @@ -26,7 +26,7 @@ internal sealed class ExtractToComponentCodeActionResolver( public string Action => LanguageServerConstants.CodeActions.ExtractToNewComponentAction; - public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { if (data.ValueKind == JsonValueKind.Undefined) { diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/GenerateMethodCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/GenerateMethodCodeActionResolver.cs index e32e05d386..9e96d2a71b 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/GenerateMethodCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/GenerateMethodCodeActionResolver.cs @@ -28,12 +28,10 @@ using CSharpSyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Razor; internal sealed class GenerateMethodCodeActionResolver( - RazorLSPOptionsMonitor razorLSPOptionsMonitor, IRoslynCodeActionHelpers roslynCodeActionHelpers, IDocumentMappingService documentMappingService, IRazorFormattingService razorFormattingService) : IRazorCodeActionResolver { - private readonly RazorLSPOptionsMonitor _razorLSPOptionsMonitor = razorLSPOptionsMonitor; private readonly IRoslynCodeActionHelpers _roslynCodeActionHelpers = roslynCodeActionHelpers; private readonly IDocumentMappingService _documentMappingService = documentMappingService; private readonly IRazorFormattingService _razorFormattingService = razorFormattingService; @@ -50,7 +48,7 @@ internal sealed class GenerateMethodCodeActionResolver( public string Action => LanguageServerConstants.CodeActions.GenerateEventHandler; - public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public async Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { var actionParams = data.Deserialize(); if (actionParams is null) @@ -73,6 +71,7 @@ internal sealed class GenerateMethodCodeActionResolver( documentContext, razorNamespace: null, razorClassName, + options, cancellationToken).ConfigureAwait(false); } @@ -86,6 +85,7 @@ internal sealed class GenerateMethodCodeActionResolver( documentContext, razorNamespace, razorClassName, + options, cancellationToken).ConfigureAwait(false); } @@ -102,8 +102,8 @@ internal sealed class GenerateMethodCodeActionResolver( var classLocationLineSpan = @class.GetLocation().GetLineSpan(); var formattedMethod = FormattingUtilities.AddIndentationToMethod( templateWithMethodSignature, - _razorLSPOptionsMonitor.CurrentValue.TabSize, - _razorLSPOptionsMonitor.CurrentValue.InsertSpaces, + options.TabSize, + options.InsertSpaces, @class.SpanStart, classLocationLineSpan.StartLinePosition.Character, content); @@ -130,10 +130,11 @@ internal sealed class GenerateMethodCodeActionResolver( DocumentContext documentContext, string? razorNamespace, string? razorClassName, + RazorFormattingOptions options, CancellationToken cancellationToken) { var templateWithMethodSignature = await PopulateMethodSignatureAsync(documentContext, actionParams, cancellationToken).ConfigureAwait(false); - var edits = CodeBlockService.CreateFormattedTextEdit(code, templateWithMethodSignature, _razorLSPOptionsMonitor.CurrentValue); + var edits = CodeBlockService.CreateFormattedTextEdit(code, templateWithMethodSignature, options); // If there are 3 edits, this means that there is no existing @code block, so we have an edit for '@code {', the method stub, and '}'. // Otherwise, a singular edit means that an @code block does exist and the only edit is adding the method stub. @@ -183,9 +184,9 @@ internal sealed class GenerateMethodCodeActionResolver( { var formattingOptions = new RazorFormattingOptions() { - TabSize = _razorLSPOptionsMonitor.CurrentValue.TabSize, - InsertSpaces = _razorLSPOptionsMonitor.CurrentValue.InsertSpaces, - CodeBlockBraceOnNextLine = _razorLSPOptionsMonitor.CurrentValue.CodeBlockBraceOnNextLine + TabSize = options.TabSize, + InsertSpaces = options.InsertSpaces, + CodeBlockBraceOnNextLine = options.CodeBlockBraceOnNextLine }; var formattedChange = await _razorFormattingService.TryGetCSharpCodeActionEditAsync( diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/IRazorCodeActionResolver.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/IRazorCodeActionResolver.cs index 0763232405..6999a43bbf 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/IRazorCodeActionResolver.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/IRazorCodeActionResolver.cs @@ -4,6 +4,7 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.VisualStudio.LanguageServer.Protocol; @@ -11,5 +12,5 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions; internal interface IRazorCodeActionResolver : ICodeActionResolver { - Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken); + Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/AddUsingsCodeActionResolverTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/AddUsingsCodeActionResolverTest.cs index e2bd806f51..ac6e915e6b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/AddUsingsCodeActionResolverTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/AddUsingsCodeActionResolverTest.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Mvc.Razor.Extensions; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.VisualStudio.LanguageServer.Protocol; using Xunit; using Xunit.Abstractions; @@ -76,7 +77,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan }); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.Null(workspaceEdit); @@ -99,7 +100,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -137,7 +138,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -182,7 +183,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -221,7 +222,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -258,7 +259,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -296,7 +297,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -330,7 +331,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -368,7 +369,7 @@ public class AddUsingsCodeActionResolverTest(ITestOutputHelper testOutput) : Lan var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs index a48608358e..0a1fa4be88 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionEndToEndTest.NetFx.cs @@ -43,12 +43,10 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer private GenerateMethodCodeActionResolver[] CreateRazorCodeActionResolvers( IRoslynCodeActionHelpers roslynCodeActionHelpers, - IRazorFormattingService razorFormattingService, - RazorLSPOptionsMonitor? optionsMonitor = null) + IRazorFormattingService razorFormattingService) => [ new GenerateMethodCodeActionResolver( - optionsMonitor ?? TestRazorLSPOptionsMonitor.Create(), roslynCodeActionHelpers, new LspDocumentMappingService(FilePathService, new TestDocumentContextFactory(), LoggerFactory), razorFormattingService) @@ -1044,6 +1042,7 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer codeActionToRun, requestContext, languageServer, + optionsMonitor: null, CreateRazorCodeActionResolvers(roslynCodeActionHelpers, formattingService)); var razorEdits = new List(); @@ -1078,7 +1077,7 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer string codeAction, int childActionIndex = 0, IRazorCodeActionProvider[]? razorCodeActionProviders = null, - Func? codeActionResolversCreator = null, + Func? codeActionResolversCreator = null, RazorLSPOptionsMonitor? optionsMonitor = null, Diagnostic[]? diagnostics = null) { @@ -1091,7 +1090,7 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer string codeAction, int childActionIndex = 0, IRazorCodeActionProvider[]? razorCodeActionProviders = null, - Func? codeActionResolversCreator = null, + Func? codeActionResolversCreator = null, RazorLSPOptionsMonitor? optionsMonitor = null, Diagnostic[]? diagnostics = null) { @@ -1131,7 +1130,8 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer codeActionToRun, requestContext, languageServer, - codeActionResolversCreator?.Invoke(roslynCodeActionHelpers, formattingService, optionsMonitor) ?? []); + optionsMonitor, + codeActionResolversCreator?.Invoke(roslynCodeActionHelpers, formattingService) ?? []); var edits = new List(); foreach (var change in changes) @@ -1205,6 +1205,7 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer VSInternalCodeAction codeActionToRun, RazorRequestContext requestContext, IClientConnection clientConnection, + RazorLSPOptionsMonitor? optionsMonitor, IRazorCodeActionResolver[] razorResolvers) { var formattingService = await TestRazorFormattingService.CreateWithFullSupportAsync(LoggerFactory); @@ -1217,7 +1218,8 @@ public class CodeActionEndToEndTest(ITestOutputHelper testOutput) : SingleServer var htmlResolvers = Array.Empty(); - var resolveEndpoint = new CodeActionResolveEndpoint(razorResolvers, csharpResolvers, htmlResolvers, LoggerFactory); + optionsMonitor ??= TestRazorLSPOptionsMonitor.Create(); + var resolveEndpoint = new CodeActionResolveEndpoint(razorResolvers, csharpResolvers, htmlResolvers, optionsMonitor, LoggerFactory); var resolveResult = await resolveEndpoint.HandleRequestAsync(codeActionToRun, requestContext, DisposalToken); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionResolutionEndpointTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionResolutionEndpointTest.cs index f2757cfd58..1dfeb8359e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionResolutionEndpointTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CodeActionResolutionEndpointTest.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; using Microsoft.AspNetCore.Razor.Threading; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Razor.Protocol; using Microsoft.VisualStudio.LanguageServer.Protocol; @@ -27,6 +28,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [new MockRazorCodeActionResolver("Test")], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -61,6 +63,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [], [new MockCSharpCodeActionResolver("Test")], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -91,6 +94,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [new MockRazorCodeActionResolver("TestRazor")], csharpCodeActionResolvers: [new MockCSharpCodeActionResolver("TestCSharp")], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -121,6 +125,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -160,6 +165,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -195,6 +201,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [], csharpCodeActionResolvers: [new MockCSharpCodeActionResolver("Test")], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -234,6 +241,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [new MockRazorCodeActionResolver("Test")], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -272,6 +280,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La ], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var codeAction = new CodeAction(); var request = new RazorCodeActionResolutionParams() @@ -286,7 +295,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La }; // Act - var resolvedCodeAction = await codeActionEndpoint.GetTestAccessor().ResolveRazorCodeActionAsync(documentContext, codeAction, request, DisposalToken); + var resolvedCodeAction = await codeActionEndpoint.GetTestAccessor().ResolveRazorCodeActionAsync(documentContext, codeAction, request, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(resolvedCodeAction.Edit); @@ -304,6 +313,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La ], csharpCodeActionResolvers: [], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var codeAction = new CodeAction(); var request = new RazorCodeActionResolutionParams() @@ -318,7 +328,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La }; // Act - var resolvedCodeAction = await codeActionEndpoint.GetTestAccessor().ResolveRazorCodeActionAsync(documentContext, codeAction, request, DisposalToken); + var resolvedCodeAction = await codeActionEndpoint.GetTestAccessor().ResolveRazorCodeActionAsync(documentContext, codeAction, request, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(resolvedCodeAction.Edit); @@ -336,6 +346,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La new MockCSharpNullCodeActionResolver("B"), ], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var codeAction = new CodeAction(); var request = new RazorCodeActionResolutionParams() @@ -364,6 +375,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La new MockCSharpCodeActionResolver("B"), ], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var codeAction = new CodeAction(); var request = new RazorCodeActionResolutionParams() @@ -395,6 +407,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La new MockCSharpCodeActionResolver("D"), ], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var codeAction = new CodeAction(); var request = new RazorCodeActionResolutionParams() @@ -420,6 +433,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La razorCodeActionResolvers: [], csharpCodeActionResolvers: [new MockCSharpCodeActionResolver("Test")], htmlCodeActionResolvers: [], + TestRazorLSPOptionsMonitor.Create(), LoggerFactory); var requestParams = new RazorCodeActionResolutionParams() { @@ -452,7 +466,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La Action = action; } - public Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { return Task.FromResult(new WorkspaceEdit()); } @@ -467,7 +481,7 @@ public class CodeActionResolutionEndpointTest(ITestOutputHelper testOutput) : La Action = action; } - public Task ResolveAsync(DocumentContext documentContext, JsonElement data, CancellationToken cancellationToken) + public Task ResolveAsync(DocumentContext documentContext, JsonElement data, RazorFormattingOptions options, CancellationToken cancellationToken) { return SpecializedTasks.Null(); } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/CreateComponentCodeActionResolverTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/CreateComponentCodeActionResolverTest.cs index 7052915a37..a31fe7e31e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/CreateComponentCodeActionResolverTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/CreateComponentCodeActionResolverTest.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Models; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.VisualStudio.LanguageServer.Protocol; using Xunit; using Xunit.Abstractions; @@ -35,7 +36,7 @@ public class CreateComponentCodeActionResolverTest(ITestOutputHelper testOutput) }); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.Null(workspaceEdit); @@ -58,7 +59,7 @@ public class CreateComponentCodeActionResolverTest(ITestOutputHelper testOutput) }); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.Null(workspaceEdit); @@ -81,7 +82,7 @@ public class CreateComponentCodeActionResolverTest(ITestOutputHelper testOutput) var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -112,7 +113,7 @@ public class CreateComponentCodeActionResolverTest(ITestOutputHelper testOutput) var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToCodeBehindCodeActionResolverTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToCodeBehindCodeActionResolverTest.cs index 1b1af10b23..246e82d1ad 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToCodeBehindCodeActionResolverTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToCodeBehindCodeActionResolverTest.cs @@ -12,6 +12,7 @@ using Microsoft.AspNetCore.Razor.LanguageServer.Test; using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer; using Microsoft.AspNetCore.Razor.Test.Common.Workspaces; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Razor.Formatting; using Microsoft.CodeAnalysis.Razor.Protocol; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.LanguageServer.Protocol; @@ -46,7 +47,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(CreateExtractToCodeBehindCodeActionParams(contents, "@code", "Test")); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.Null(workspaceEdit); @@ -70,7 +71,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(CreateExtractToCodeBehindCodeActionParams(contents, "@code", "Test")); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.Null(workspaceEdit); @@ -98,7 +99,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -163,7 +164,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -236,7 +237,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -319,7 +320,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -404,7 +405,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -477,7 +478,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -542,7 +543,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -609,7 +610,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); @@ -680,7 +681,7 @@ public class ExtractToCodeBehindCodeActionResolverTest(ITestOutputHelper testOut var data = JsonSerializer.SerializeToElement(actionParams); // Act - var workspaceEdit = await resolver.ResolveAsync(documentContext, data, DisposalToken); + var workspaceEdit = await resolver.ResolveAsync(documentContext, data, new RazorFormattingOptions(), DisposalToken); // Assert Assert.NotNull(workspaceEdit); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToComponentCodeActionResolverTest.NetFx.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToComponentCodeActionResolverTest.NetFx.cs index b59ce1d296..5d4ae0b7b6 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToComponentCodeActionResolverTest.NetFx.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToComponentCodeActionResolverTest.NetFx.cs @@ -303,6 +303,7 @@ public class ExtractToComponentCodeActionResolverTest(ITestOutputHelper testOutp codeActionToRun, requestContext, languageServer, + optionsMonitor: null, [resolver] );