Skip to content

Commit

Permalink
Support for delegating auto-insert to C#
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgav committed Aug 9, 2024
1 parent 1b80b83 commit 2587d1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Microsoft.CodeAnalysis.Razor.Protocol.AutoInsert.RemoteInsertTextEdit?>;
using RoslynFormattingOptions = Roslyn.LanguageServer.Protocol.FormattingOptions;

namespace Microsoft.CodeAnalysis.Remote.Razor;

Expand All @@ -29,6 +32,8 @@ private readonly IAutoInsertService _autoInsertService
= args.ExportProvider.GetExportedValue<IAutoInsertService>();
private readonly IRazorDocumentMappingService _documentMappingService

Check failure on line 33 in src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

View check run for this annotation

Azure Pipelines / razor-tooling-ci (Build macOS release)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs#L33

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs(33,22): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IRazorDocumentMappingService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

View check run for this annotation

Azure Pipelines / razor-tooling-ci (Build macOS debug)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs#L33

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs(33,22): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IRazorDocumentMappingService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

View check run for this annotation

Azure Pipelines / razor-tooling-ci (Build Linux release)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs#L33

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs(33,22): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IRazorDocumentMappingService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

View check run for this annotation

Azure Pipelines / razor-tooling-ci (Build Linux debug)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs#L33

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs(33,22): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IRazorDocumentMappingService' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 33 in src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs

View check run for this annotation

Azure Pipelines / razor-tooling-ci

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs#L33

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/AutoInsert/RemoteAutoInsertService.cs(33,22): error CS0246: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'IRazorDocumentMappingService' could not be found (are you missing a using directive or an assembly reference?)
= args.ExportProvider.GetExportedValue<IRazorDocumentMappingService>();
private readonly IFilePathService _filePathService =
args.ExportProvider.GetExportedValue<IFilePathService>();

public ValueTask<Response> TryResolveInsertionAsync(
RazorPinnedSolutionInfoWrapper solutionInfo,
Expand Down Expand Up @@ -82,7 +87,25 @@ private async ValueTask<Response> 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;
}
Expand Down

0 comments on commit 2587d1f

Please sign in to comment.