Skip to content

Commit

Permalink
Cohosting Rename (#10721)
Browse files Browse the repository at this point in the history
Part of #9519
Fixes #10688

Tried not to go on too many side quests with this one, but I guess
review commit-at-a-time if you want to double check :)
  • Loading branch information
davidwengier authored Aug 13, 2024
2 parents 7542d94 + 4bbedc6 commit a01016a
Show file tree
Hide file tree
Showing 35 changed files with 1,086 additions and 491 deletions.
1 change: 1 addition & 0 deletions eng/targets/Services.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<ServiceHubService Include="Microsoft.VisualStudio.Razor.SignatureHelp" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteSignatureHelpService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.DocumentHighlight" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteDocumentHighlightService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.InlayHint" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteInlayHintService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Rename" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteRenameService+Factory" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
Expand All @@ -27,7 +28,7 @@ public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
{
if (codeAction.Edit is not null)
{
await RemapAndFixHtmlCodeActionEditAsync(_editMappingService, context.CodeDocument, codeAction, cancellationToken).ConfigureAwait(false);
await RemapAndFixHtmlCodeActionEditAsync(_editMappingService, context.DocumentSnapshot, codeAction, cancellationToken).ConfigureAwait(false);

results.Add(codeAction);
}
Expand All @@ -40,14 +41,15 @@ public async Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(
return results.ToImmutable();
}

public static async Task RemapAndFixHtmlCodeActionEditAsync(IEditMappingService editMappingService, RazorCodeDocument codeDocument, CodeAction codeAction, CancellationToken cancellationToken)
public static async Task RemapAndFixHtmlCodeActionEditAsync(IEditMappingService editMappingService, IDocumentSnapshot documentSnapshot, CodeAction codeAction, CancellationToken cancellationToken)
{
Assumes.NotNull(codeAction.Edit);

codeAction.Edit = await editMappingService.RemapWorkspaceEditAsync(codeAction.Edit, cancellationToken).ConfigureAwait(false);
codeAction.Edit = await editMappingService.RemapWorkspaceEditAsync(documentSnapshot, codeAction.Edit, cancellationToken).ConfigureAwait(false);

if (codeAction.Edit.TryGetDocumentChanges(out var documentEdits) == true)
if (codeAction.Edit.TryGetTextDocumentEdits(out var documentEdits))
{
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync().ConfigureAwait(false);
var htmlSourceText = codeDocument.GetHtmlSourceText();

foreach (var edit in documentEdits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public async override Task<CodeAction> ResolveAsync(
return codeAction;
}

var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
await DefaultHtmlCodeActionProvider.RemapAndFixHtmlCodeActionEditAsync(_editMappingService, codeDocument, resolvedCodeAction, cancellationToken).ConfigureAwait(false);
await DefaultHtmlCodeActionProvider.RemapAndFixHtmlCodeActionEditAsync(_editMappingService, documentContext.Snapshot, resolvedCodeAction, cancellationToken).ConfigureAwait(false);

return resolvedCodeAction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,17 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer.DocumentPresentation;

internal abstract class AbstractTextDocumentPresentationEndpointBase<TParams> : IRazorRequestHandler<TParams, WorkspaceEdit?>, ICapabilitiesProvider
where TParams : IPresentationParams
internal abstract class AbstractTextDocumentPresentationEndpointBase<TParams>(
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IFilePathService filePathService,
ILogger logger) : IRazorRequestHandler<TParams, WorkspaceEdit?>, ICapabilitiesProvider
where TParams : IPresentationParams
{
private readonly IDocumentMappingService _documentMappingService;
private readonly IClientConnection _clientConnection;
private readonly IFilePathService _filePathService;
private readonly ILogger _logger;

protected AbstractTextDocumentPresentationEndpointBase(
IDocumentMappingService documentMappingService,
IClientConnection clientConnection,
IFilePathService filePathService,
ILogger logger)
{
_documentMappingService = documentMappingService;
_clientConnection = clientConnection;
_filePathService = filePathService;
_logger = logger;
}
private readonly IDocumentMappingService _documentMappingService = documentMappingService;
private readonly IClientConnection _clientConnection = clientConnection;
private readonly IFilePathService _filePathService = filePathService;
private readonly ILogger _logger = logger;

public abstract string EndpointName { get; }

Expand Down Expand Up @@ -128,36 +120,6 @@ protected AbstractTextDocumentPresentationEndpointBase(
return edit;
}

private static bool TryGetDocumentChanges(WorkspaceEdit workspaceEdit, [NotNullWhen(true)] out TextDocumentEdit[]? documentChanges)
{
if (workspaceEdit.DocumentChanges?.Value is TextDocumentEdit[] documentEditArray)
{
documentChanges = documentEditArray;
return true;
}

if (workspaceEdit.DocumentChanges?.Value is SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>[] sumTypeArray)
{
using var documentEdits = new PooledArrayBuilder<TextDocumentEdit>();
foreach (var sumType in sumTypeArray)
{
if (sumType.Value is TextDocumentEdit textDocumentEdit)
{
documentEdits.Add(textDocumentEdit);
}
}

if (documentEdits.Count > 0)
{
documentChanges = documentEdits.ToArray();
return true;
}
}

documentChanges = null;
return false;
}

private Dictionary<string, TextEdit[]> MapChanges(Dictionary<string, TextEdit[]> changes, bool mapRanges, RazorCodeDocument codeDocument)
{
var remappedChanges = new Dictionary<string, TextEdit[]>();
Expand Down Expand Up @@ -216,7 +178,7 @@ private TextDocumentEdit[] MapDocumentChanges(TextDocumentEdit[] documentEdits,
Uri = razorDocumentUri,
Version = hostDocumentVersion
},
Edits = remappedEdits.ToArray()
Edits = [.. remappedEdits]
});
}

Expand Down Expand Up @@ -247,10 +209,10 @@ private TextEdit[] MapTextEdits(bool mapRanges, RazorCodeDocument codeDocument,

private WorkspaceEdit? MapWorkspaceEdit(WorkspaceEdit workspaceEdit, bool mapRanges, RazorCodeDocument codeDocument, int hostDocumentVersion)
{
if (TryGetDocumentChanges(workspaceEdit, out var documentChanges))
if (workspaceEdit.TryGetTextDocumentEdits(out var documentEdits))
{
// The LSP spec says, we should prefer `DocumentChanges` property over `Changes` if available.
var remappedEdits = MapDocumentChanges(documentChanges, mapRanges, codeDocument, hostDocumentVersion);
var remappedEdits = MapDocumentChanges(documentEdits, mapRanges, codeDocument, hostDocumentVersion);
return new WorkspaceEdit()
{
DocumentChanges = remappedEdits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static void AddDocumentManagementServices(this IServiceCollection service
services.AddSingleton<IRazorStartupService>((services) => (RazorProjectService)services.GetRequiredService<IRazorProjectService>());
services.AddSingleton<IRazorStartupService, OpenDocumentGenerator>();
services.AddSingleton<IDocumentMappingService, LspDocumentMappingService>();
services.AddSingleton<IEditMappingService, EditMappingService>();
services.AddSingleton<IEditMappingService, LspEditMappingService>();
services.AddSingleton<RazorFileChangeDetectorManager>();
services.AddSingleton<IOnInitialized>(sp => sp.GetRequiredService<RazorFileChangeDetectorManager>());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer;

internal class LspEditMappingService(
IDocumentMappingService documentMappingService,
IFilePathService filePathService,
IDocumentContextFactory documentContextFactory) : AbstractEditMappingService(documentMappingService, filePathService)
{
private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;

protected override bool TryGetVersionedDocumentContext(IDocumentSnapshot contextDocumentSnapshot, Uri razorDocumentUri, VSProjectContext? projectContext, [NotNullWhen(true)] out VersionedDocumentContext? documentContext)
{
if (!_documentContextFactory.TryCreateForOpenDocument(razorDocumentUri, projectContext, out documentContext))
{
return false;
}

return true;
}

protected override bool TryGetDocumentContext(IDocumentSnapshot contextDocumentSnapshot, Uri razorDocumentUri, [NotNullWhen(true)] out DocumentContext? documentContext)
{
if (!_documentContextFactory.TryCreate(razorDocumentUri, out documentContext))
{
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.FoldingRanges;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Rename;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -177,11 +178,13 @@ static void AddHandlers(IServiceCollection services, LanguageServerFeatureOption
services.AddHandlerWithCapabilities<ImplementationEndpoint>();
services.AddHandlerWithCapabilities<OnAutoInsertEndpoint>();

services.AddHandlerWithCapabilities<RenameEndpoint>();
services.AddHandlerWithCapabilities<DefinitionEndpoint>();

if (!featureOptions.UseRazorCohostServer)
{
services.AddSingleton<IRenameService, RenameService>();
services.AddHandlerWithCapabilities<RenameEndpoint>();

services.AddHandlerWithCapabilities<DocumentHighlightEndpoint>();
services.AddHandlerWithCapabilities<SignatureHelpEndpoint>();
services.AddHandlerWithCapabilities<LinkedEditingRangeEndpoint>();
Expand Down
Loading

0 comments on commit a01016a

Please sign in to comment.