From cd47b49e278abe77fb9cc9a07eb784a77688f2bc Mon Sep 17 00:00:00 2001 From: David Wengier Date: Fri, 4 Oct 2024 11:19:47 +1000 Subject: [PATCH] Fix code action requests with FUSE Also reported in https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2268945 --- .../Razor/GenerateMethodCodeActionResolver.cs | 2 +- .../Formatting/RazorFormattingService.cs | 22 ++++++++++++------- .../Telemetry/TelemetryReporter.cs | 3 --- 3 files changed, 15 insertions(+), 12 deletions(-) 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 026cf9c36ec..8df517e9037 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 @@ -215,7 +215,7 @@ private async Task GenerateMethodInCodeBlockAsync( var formattedChange = await _razorFormattingService.TryGetCSharpCodeActionEditAsync( documentContext, - result.SelectAsArray(code.Source.Text.GetTextChange), + result.SelectAsArray(code.GetCSharpSourceText().GetTextChange), formattingOptions, cancellationToken).ConfigureAwait(false); diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs index 23110dab756..838dd78cab3 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/RazorFormattingService.cs @@ -126,7 +126,7 @@ public Task> GetCSharpOnTypeFormattingChangesAsync(Do triggerCharacter, [_csharpOnTypeFormattingPass, .. _validationPasses], collapseChanges: false, - automaticallyAddUsings: false, + isCodeActionFormattingRequest: false, cancellationToken: cancellationToken); public Task> GetHtmlOnTypeFormattingChangesAsync(DocumentContext documentContext, ImmutableArray htmlChanges, RazorFormattingOptions options, int hostDocumentIndex, char triggerCharacter, CancellationToken cancellationToken) @@ -138,7 +138,7 @@ public Task> GetHtmlOnTypeFormattingChangesAsync(Docu triggerCharacter, [_htmlOnTypeFormattingPass, .. _validationPasses], collapseChanges: false, - automaticallyAddUsings: false, + isCodeActionFormattingRequest: false, cancellationToken: cancellationToken); public async Task TryGetSingleCSharpEditAsync(DocumentContext documentContext, TextChange csharpEdit, RazorFormattingOptions options, CancellationToken cancellationToken) @@ -151,7 +151,7 @@ public Task> GetHtmlOnTypeFormattingChangesAsync(Docu triggerCharacter: '\0', [_csharpOnTypeFormattingPass, .. _validationPasses], collapseChanges: false, - automaticallyAddUsings: false, + isCodeActionFormattingRequest: false, cancellationToken: cancellationToken).ConfigureAwait(false); return razorChanges.SingleOrDefault(); } @@ -166,7 +166,7 @@ public Task> GetHtmlOnTypeFormattingChangesAsync(Docu triggerCharacter: '\0', [_csharpOnTypeFormattingPass], collapseChanges: true, - automaticallyAddUsings: true, + isCodeActionFormattingRequest: true, cancellationToken: cancellationToken).ConfigureAwait(false); return razorChanges.SingleOrDefault(); } @@ -183,7 +183,7 @@ public Task> GetHtmlOnTypeFormattingChangesAsync(Docu triggerCharacter: '\0', [_csharpOnTypeFormattingPass], collapseChanges: true, - automaticallyAddUsings: false, + isCodeActionFormattingRequest: false, cancellationToken: cancellationToken).ConfigureAwait(false); razorChanges = UnwrapCSharpSnippets(razorChanges); @@ -211,7 +211,7 @@ private async Task> ApplyFormattedChangesAsync( char triggerCharacter, ImmutableArray formattingPasses, bool collapseChanges, - bool automaticallyAddUsings, + bool isCodeActionFormattingRequest, CancellationToken cancellationToken) { // If we only received a single edit, let's always return a single edit back. @@ -219,13 +219,19 @@ private async Task> ApplyFormattedChangesAsync( collapseChanges |= generatedDocumentChanges.Length == 1; var documentSnapshot = documentContext.Snapshot; - var codeDocument = await _codeDocumentProvider.GetCodeDocumentAsync(documentSnapshot).ConfigureAwait(false); + + // Code actions were computed on the regular document, which with FUSE could be a runtime document. We have to make + // sure for code actions specifically we are formatting that same document, or TextChange spans may not line up + var codeDocument = isCodeActionFormattingRequest + ? await documentSnapshot.GetGeneratedOutputAsync(forceDesignTimeGeneratedOutput: false).ConfigureAwait(false) + : await _codeDocumentProvider.GetCodeDocumentAsync(documentSnapshot).ConfigureAwait(false); + var context = FormattingContext.CreateForOnTypeFormatting( documentSnapshot, codeDocument, options, _codeDocumentProvider, - automaticallyAddUsings, + automaticallyAddUsings: isCodeActionFormattingRequest, hostDocumentIndex, triggerCharacter); var result = generatedDocumentChanges; diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Telemetry/TelemetryReporter.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Telemetry/TelemetryReporter.cs index 8b268f4b9ec..c9cee632176 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Telemetry/TelemetryReporter.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Telemetry/TelemetryReporter.cs @@ -10,8 +10,6 @@ using Microsoft.AspNetCore.Razor.Telemetry; using Microsoft.AspNetCore.Razor; using System.IO; - - #if DEBUG using System.Linq; #endif @@ -268,7 +266,6 @@ public TelemetryScope TrackLspRequest(string lspMethodName, string languageServe new("eventscope.correlationid", correlationId)); } - /// /// Returns values that should be set to (failureParameter1, failureParameter2) when reporting a fault. /// Those values represent the blamed stackframe module and method name.