Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.AspNetCore.Razor.LanguageServer.ColorPresentation;

[RazorLanguageServerEndpoint(CustomMessageNames.ColorPresentationMethodName)]
[RazorLanguageServerEndpoint(Methods.TextDocumentColorPresentationName)]
internal sealed class ColorPresentationEndpoint(IClientConnection clientConnection) : IRazorRequestHandler<ColorPresentationParams, LspColorPresentation[]>
{
private readonly IClientConnection _clientConnection = clientConnection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,4 @@ internal static class CustomMessageNames

// Called to get C# diagnostics from Roslyn when publishing diagnostics for VS Code
public const string RazorCSharpPullDiagnosticsEndpointName = "razor/csharpPullDiagnostics";

public const string ColorPresentationMethodName = "textDocument/colorPresentation";
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,6 +29,7 @@ ValueTask<Response> GetCompletionAsync(
VSInternalCompletionContext completionContext,
RazorCompletionOptions razorCompletionOptions,
HashSet<string> existingHtmlCompletions,
Guid correlationId,
CancellationToken cancellationToken);

ValueTask<VSInternalCompletionItem> ResolveCompletionItemAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using System.Linq;
Expand All @@ -16,6 +17,7 @@
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Completion;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.Telemetry;
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Response = Microsoft.CodeAnalysis.Razor.Remote.RemoteResponse<Roslyn.LanguageServer.Protocol.RazorVSInternalCompletionList?>;
Expand All @@ -35,6 +37,7 @@ protected override IRemoteCompletionService CreateService(in ServiceArgs args)
private readonly IClientCapabilitiesService _clientCapabilitiesService = args.ExportProvider.GetExportedValue<IClientCapabilitiesService>();
private readonly CompletionTriggerAndCommitCharacters _triggerAndCommitCharacters = args.ExportProvider.GetExportedValue<CompletionTriggerAndCommitCharacters>();
private readonly IRazorFormattingService _formattingService = args.ExportProvider.GetExportedValue<IRazorFormattingService>();
private readonly ITelemetryReporter _telemetryReporter = args.ExportProvider.GetExportedValue<ITelemetryReporter>();

public ValueTask<CompletionPositionInfo?> GetPositionInfoAsync(
JsonSerializableRazorPinnedSolutionInfoWrapper solutionInfo,
Expand Down Expand Up @@ -84,6 +87,7 @@ public ValueTask<Response> GetCompletionAsync(
VSInternalCompletionContext completionContext,
RazorCompletionOptions razorCompletionOptions,
HashSet<string> existingHtmlCompletions,
Guid correlationId,
CancellationToken cancellationToken)
=> RunServiceAsync(
solutionInfo,
Expand All @@ -94,6 +98,7 @@ public ValueTask<Response> GetCompletionAsync(
completionContext,
razorCompletionOptions,
existingHtmlCompletions,
correlationId,
cancellationToken),
cancellationToken);

Expand All @@ -103,6 +108,7 @@ private async ValueTask<Response> GetCompletionAsync(
VSInternalCompletionContext completionContext,
RazorCompletionOptions razorCompletionOptions,
HashSet<string> existingDelegatedCompletions,
Guid correlationId,
CancellationToken cancellationToken)
{
var documentPositionInfo = positionInfo.DocumentPositionInfo;
Expand Down Expand Up @@ -138,6 +144,7 @@ private async ValueTask<Response> GetCompletionAsync(
mappedPosition,
completionContext,
razorCompletionOptions,
correlationId,
cancellationToken)
.ConfigureAwait(false);

Expand Down Expand Up @@ -179,6 +186,7 @@ private async ValueTask<Response> GetCompletionAsync(
Position mappedPosition,
CompletionContext completionContext,
RazorCompletionOptions razorCompletionOptions,
Guid correlationId,
CancellationToken cancellationToken)
{
var clientCapabilities = _clientCapabilitiesService.ClientCapabilities;
Expand All @@ -189,14 +197,19 @@ private async ValueTask<Response> GetCompletionAsync(
}

var mappedLinePosition = mappedPosition.ToLinePosition();
var completionList = await ExternalAccess.Razor.Cohost.Handlers.Completion.GetCompletionListAsync(
generatedDocument,
mappedLinePosition,
completionContext,
clientCapabilities.SupportsVisualStudioExtensions,
completionSetting,
cancellationToken)
.ConfigureAwait(false);

VSInternalCompletionList? completionList = null;
using (_telemetryReporter.TrackLspRequest(Methods.TextDocumentCompletionName, "Razor.ExternalAccess", TelemetryThresholds.CompletionSubLSPTelemetryThreshold, correlationId))
{
completionList = await ExternalAccess.Razor.Cohost.Handlers.Completion.GetCompletionListAsync(
generatedDocument,
mappedLinePosition,
completionContext,
clientCapabilities.SupportsVisualStudioExtensions,
completionSetting,
cancellationToken)
.ConfigureAwait(false);
}

if (completionList is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.Telemetry;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

Expand All @@ -31,15 +30,13 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
internal sealed class CohostCodeActionsEndpoint(
IRemoteServiceInvoker remoteServiceInvoker,
IClientCapabilitiesService clientCapabilitiesService,
IHtmlDocumentSynchronizer htmlDocumentSynchronizer,
LSPRequestInvoker requestInvoker,
IHtmlRequestInvoker requestInvoker,
ITelemetryReporter telemetryReporter)
: AbstractRazorCohostDocumentRequestHandler<VSCodeActionParams, SumType<Command, CodeAction>[]?>, IDynamicRegistrationProvider
{
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
private readonly IClientCapabilitiesService _clientCapabilitiesService = clientCapabilitiesService;
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly IHtmlRequestInvoker _requestInvoker = requestInvoker;
private readonly ITelemetryReporter _telemetryReporter = telemetryReporter;

protected override bool MutatesSolutionState => false;
Expand Down Expand Up @@ -115,38 +112,20 @@ private async Task<RazorVSInternalCodeAction[]> GetCSharpCodeActionsAsync(TextDo

private async Task<RazorVSInternalCodeAction[]> GetHtmlCodeActionsAsync(TextDocument razorDocument, VSCodeActionParams request, Guid correlationId, CancellationToken cancellationToken)
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)
var result = await _requestInvoker.MakeHtmlLspRequestAsync<VSCodeActionParams, RazorVSInternalCodeAction[]>(
razorDocument,
Methods.TextDocumentCodeActionName,
request,
TelemetryThresholds.CodeActionSubLSPTelemetryThreshold,
correlationId,
cancellationToken).ConfigureAwait(false);

if (result is null)
{
return [];
}

// We don't want to create a new request, and risk losing data, so we just tweak the Uri and
// set it back again at the end
var oldTdi = request.TextDocument;
try
{
request.TextDocument = new VSTextDocumentIdentifier { Uri = htmlDocument.Uri };

using var _ = _telemetryReporter.TrackLspRequest(Methods.TextDocumentCodeActionName, RazorLSPConstants.HtmlLanguageServerName, TelemetryThresholds.CodeActionSubLSPTelemetryThreshold, correlationId);
var result = await _requestInvoker.ReinvokeRequestOnServerAsync<VSCodeActionParams, RazorVSInternalCodeAction[]?>(
htmlDocument.Buffer,
Methods.TextDocumentCodeActionName,
RazorLSPConstants.HtmlLanguageServerName,
request,
cancellationToken).ConfigureAwait(false);

if (result?.Response is null)
{
return [];
}

return result.Response;
}
finally
{
request.TextDocument = oldTdi;
}
return result;
}

internal TestAccessor GetTestAccessor() => new(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.Razor.Settings;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
Expand All @@ -31,15 +30,13 @@ internal sealed class CohostCodeActionsResolveEndpoint(
IRemoteServiceInvoker remoteServiceInvoker,
IClientCapabilitiesService clientCapabilitiesService,
IClientSettingsManager clientSettingsManager,
IHtmlDocumentSynchronizer htmlDocumentSynchronizer,
LSPRequestInvoker requestInvoker)
IHtmlRequestInvoker requestInvoker)
: AbstractRazorCohostDocumentRequestHandler<CodeAction, CodeAction?>, IDynamicRegistrationProvider
{
private readonly IRemoteServiceInvoker _remoteServiceInvoker = remoteServiceInvoker;
private readonly IClientCapabilitiesService _clientCapabilitiesService = clientCapabilitiesService;
private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager;
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly IHtmlRequestInvoker _requestInvoker = requestInvoker;

protected override bool MutatesSolutionState => false;

Expand Down Expand Up @@ -125,25 +122,18 @@ private async Task<CodeAction> ResolvedHtmlCodeActionAsync(TextDocument razorDoc

try
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)
{
return codeAction;
}

var result = await _requestInvoker.ReinvokeRequestOnServerAsync<CodeAction, CodeAction>(
htmlDocument.Buffer,
var result = await _requestInvoker.MakeHtmlLspRequestAsync<CodeAction, CodeAction>(
razorDocument,
Methods.CodeActionResolveName,
RazorLSPConstants.HtmlLanguageServerName,
codeAction,
cancellationToken).ConfigureAwait(false);

if (result?.Response is null)
if (result is null)
{
return codeAction;
}

return result.Response;
return result;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@
using Microsoft.AspNetCore.Razor;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

#pragma warning disable RS0030 // Do not use banned APIs
[Shared]
[CohostEndpoint(CustomMessageNames.ColorPresentationMethodName)]
[CohostEndpoint(Methods.TextDocumentColorPresentationName)]
[ExportCohostStatelessLspService(typeof(CohostColorPresentationEndpoint))]
[method: ImportingConstructor]
#pragma warning restore RS0030 // Do not use banned APIs
internal class CohostColorPresentationEndpoint(
IHtmlDocumentSynchronizer htmlDocumentSynchronizer,
LSPRequestInvoker requestInvoker)
internal sealed class CohostColorPresentationEndpoint(
IHtmlRequestInvoker requestInvoker)
: AbstractRazorCohostDocumentRequestHandler<ColorPresentationParams, ColorPresentation[]?>
{
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly IHtmlRequestInvoker _requestInvoker = requestInvoker;

protected override bool MutatesSolutionState => false;

Expand All @@ -38,22 +34,11 @@ internal class CohostColorPresentationEndpoint(

private async Task<ColorPresentation[]?> HandleRequestAsync(ColorPresentationParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)
{
return null;
}

request.TextDocument = request.TextDocument.WithUri(htmlDocument.Uri);

var result = await _requestInvoker.ReinvokeRequestOnServerAsync<ColorPresentationParams, ColorPresentation[]>(
htmlDocument.Buffer,
CustomMessageNames.ColorPresentationMethodName,
RazorLSPConstants.HtmlLanguageServerName,
return await _requestInvoker.MakeHtmlLspRequestAsync<ColorPresentationParams, ColorPresentation[]>(
razorDocument,
Methods.TextDocumentColorPresentationName,
request,
cancellationToken).ConfigureAwait(false);

return result?.Response;
}

internal TestAccessor GetTestAccessor() => new(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.AspNetCore.Razor;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

Expand All @@ -19,13 +18,11 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
[ExportCohostStatelessLspService(typeof(CohostDocumentColorEndpoint))]
[method: ImportingConstructor]
#pragma warning restore RS0030 // Do not use banned APIs
internal class CohostDocumentColorEndpoint(
IHtmlDocumentSynchronizer htmlDocumentSynchronizer,
LSPRequestInvoker requestInvoker)
internal sealed class CohostDocumentColorEndpoint(
IHtmlRequestInvoker requestInvoker)
: AbstractRazorCohostDocumentRequestHandler<DocumentColorParams, ColorInformation[]?>, IDynamicRegistrationProvider
{
private readonly IHtmlDocumentSynchronizer _htmlDocumentSynchronizer = htmlDocumentSynchronizer;
private readonly LSPRequestInvoker _requestInvoker = requestInvoker;
private readonly IHtmlRequestInvoker _requestInvoker = requestInvoker;

protected override bool MutatesSolutionState => false;

Expand Down Expand Up @@ -53,22 +50,11 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie

private async Task<ColorInformation[]?> HandleRequestAsync(DocumentColorParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)
{
return null;
}

request.TextDocument = request.TextDocument.WithUri(htmlDocument.Uri);

var result = await _requestInvoker.ReinvokeRequestOnServerAsync<DocumentColorParams, ColorInformation[]?>(
htmlDocument.Buffer,
return await _requestInvoker.MakeHtmlLspRequestAsync<DocumentColorParams, ColorInformation[]>(
razorDocument,
Methods.TextDocumentDocumentColorName,
RazorLSPConstants.HtmlLanguageServerName,
request,
cancellationToken).ConfigureAwait(false);

return result?.Response;
}

internal TestAccessor GetTestAccessor() => new(this);
Expand Down
Loading