From 2587d1fef69053f352046ce47ad00ee08a75181b Mon Sep 17 00:00:00 2001 From: "Alex Gavrilov (DEV PROD)" Date: Fri, 9 Aug 2024 00:15:35 -0700 Subject: [PATCH] Support for delegating auto-insert to C# --- .../AutoInsert/RemoteInsertTextEdit.cs | 7 ++++++ .../AutoInsert/RemoteAutoInsertService.cs | 25 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/AutoInsert/RemoteInsertTextEdit.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/AutoInsert/RemoteInsertTextEdit.cs index 364758a6a0a..3c40a5ab0fa 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/AutoInsert/RemoteInsertTextEdit.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Protocol/AutoInsert/RemoteInsertTextEdit.cs @@ -24,6 +24,13 @@ public static RemoteInsertTextEdit FromLspInsertTextEdit(InsertTextEdit edit) edit.TextEdit.NewText, edit.InsertTextFormat); + public static RemoteInsertTextEdit FromVsPlatformAutoInsertResponse( + VSInternalDocumentOnAutoInsertResponseItem response) + => new( + response.TextEdit.Range.ToLinePositionSpan(), + response.TextEdit.NewText, + response.TextEditFormat); + public static VSInternalDocumentOnAutoInsertResponseItem ToLspInsertTextEdit(RemoteInsertTextEdit edit) => new() { diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs index bb38f0a3c60..00dfe5b6209 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs @@ -4,15 +4,18 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.ExternalAccess.Razor; +using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers; using Microsoft.CodeAnalysis.Razor.AutoInsert; using Microsoft.CodeAnalysis.Razor.DocumentMapping; using Microsoft.CodeAnalysis.Razor.Protocol; using Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert; +using Microsoft.CodeAnalysis.Razor.Workspaces; using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem; using Microsoft.CodeAnalysis.Text; using Microsoft.VisualStudio.LanguageServer.Protocol; using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse; +using RoslynFormattingOptions = Roslyn.LanguageServer.Protocol.FormattingOptions; namespace Microsoft.CodeAnalysis.Remote.Razor; @@ -29,6 +32,8 @@ private readonly IAutoInsertService _autoInsertService = args.ExportProvider.GetExportedValue(); private readonly IRazorDocumentMappingService _documentMappingService = args.ExportProvider.GetExportedValue(); + private readonly IFilePathService _filePathService = + args.ExportProvider.GetExportedValue(); public ValueTask TryResolveInsertionAsync( RazorPinnedSolutionInfoWrapper solutionInfo, @@ -82,7 +87,25 @@ private async ValueTask TryResolveInsertionAsync( : Response.NoFurtherHandling; } - // TODO: handle C# case + // C# case + + var csharpDocument = codeDocument.GetCSharpDocument(); + if (_documentMappingService.TryMapToGeneratedDocumentPosition(csharpDocument, index, out var mappedPosition, out _)) + { + var generatedDocument = await remoteDocumentContext.GetGeneratedDocumentAsync(_filePathService, cancellationToken).ConfigureAwait(false); + // TODO: use correct options rather than default + var formattingOptions = new RoslynFormattingOptions(); + var autoInsertResponseItem = await OnAutoInsert.GetOnAutoInsertResponseAsync( + generatedDocument, + mappedPosition, + character, + formattingOptions, + cancellationToken + ); + return autoInsertResponseItem is not null + ? Response.Results(RemoteInsertTextEdit.FromVsPlatformAutoInsertResponse(autoInsertResponseItem)) + : Response.NoFurtherHandling; + } return Response.NoFurtherHandling; }