Skip to content

Commit

Permalink
[Cohost] Remove unnecessary indirection in semantic tokens (#10369)
Browse files Browse the repository at this point in the history
Being the first endpoint created, semantic tokens had a little bit of
over design. I did not YAGNI 😁
  • Loading branch information
davidwengier authored May 15, 2024
2 parents 6e83c9e + c2bca03 commit e2128d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 89 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Razor.LanguageClient.Extensions;
using Microsoft.VisualStudio.Razor.Settings;
using Newtonsoft.Json;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
Expand All @@ -26,13 +28,15 @@ namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;
[method: ImportingConstructor]
#pragma warning restore RS0030 // Do not use banned APIs
internal sealed class CohostSemanticTokensRangeEndpoint(
IOutOfProcSemanticTokensService semanticTokensInfoService,
IRemoteClientProvider remoteClientProvider,
IClientSettingsManager clientSettingsManager,
ISemanticTokensLegendService semanticTokensLegendService,
ITelemetryReporter telemetryReporter,
ILoggerFactory loggerFactory)
: AbstractRazorCohostDocumentRequestHandler<SemanticTokensRangeParams, SemanticTokens?>, IDynamicRegistrationProvider
{
private readonly IOutOfProcSemanticTokensService _semanticTokensInfoService = semanticTokensInfoService;
private readonly IRemoteClientProvider _remoteClientProvider = remoteClientProvider;
private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager;
private readonly ISemanticTokensLegendService _semanticTokensLegendService = semanticTokensLegendService;
private readonly ITelemetryReporter _telemetryReporter = telemetryReporter;
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<CohostSemanticTokensRangeEndpoint>();
Expand Down Expand Up @@ -66,19 +70,43 @@ internal sealed class CohostSemanticTokensRangeEndpoint(

protected override async Task<SemanticTokens?> HandleRequestAsync(SemanticTokensRangeParams request, RazorCohostRequestContext context, CancellationToken cancellationToken)
{
var correlationId = Guid.NewGuid();
using var _ = _telemetryReporter.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, RazorLSPConstants.CohostLanguageServerName, correlationId);
var razorDocument = context.TextDocument.AssumeNotNull();
var span = request.Range.ToLinePositionSpan();

var data = await _semanticTokensInfoService.GetSemanticTokensDataAsync(context.TextDocument.AssumeNotNull(), request.Range.ToLinePositionSpan(), correlationId, cancellationToken);
var remoteClient = await _remoteClientProvider.TryGetClientAsync(cancellationToken).ConfigureAwait(false);

if (data is null)
if (remoteClient is null)
{
_logger.LogWarning($"Couldn't get remote client");
return null;
}

return new SemanticTokens
try
{
Data = data
};
var colorBackground = _clientSettingsManager.GetClientSettings().AdvancedSettings.ColorBackground;

var correlationId = Guid.NewGuid();
using var _ = _telemetryReporter.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, RazorLSPConstants.CohostLanguageServerName, correlationId);

var data = await remoteClient.TryInvokeAsync<IRemoteSemanticTokensService, int[]?>(
razorDocument.Project.Solution,
(service, solutionInfo, cancellationToken) => service.GetSemanticTokensDataAsync(solutionInfo, razorDocument.Id, span, colorBackground, correlationId, cancellationToken),
cancellationToken).ConfigureAwait(false);

if (data.Value is not { } tokens)
{
return null;
}

return new SemanticTokens
{
Data = tokens
};
}
catch (Exception ex)
{
_logger.LogError(ex, $"Error calling remote");
return null;
}
}
}

This file was deleted.

0 comments on commit e2128d0

Please sign in to comment.