From c2bca03d57098570ee77dce56eefc254b803b68c Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 15 May 2024 21:28:07 +1000 Subject: [PATCH] Remove unnecessary indirection --- .../IOutOfProcSemanticTokensService.cs | 18 ------ .../CohostSemanticTokensRangeEndpoint.cs | 46 +++++++++++--- .../Remote/OutOfProcSemanticTokensService.cs | 62 ------------------- 3 files changed, 37 insertions(+), 89 deletions(-) delete mode 100644 src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/IOutOfProcSemanticTokensService.cs delete mode 100644 src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Remote/OutOfProcSemanticTokensService.cs diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/IOutOfProcSemanticTokensService.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/IOutOfProcSemanticTokensService.cs deleted file mode 100644 index 1e8052df70f..00000000000 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/IOutOfProcSemanticTokensService.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Text; - -namespace Microsoft.CodeAnalysis.Razor.Workspaces; - -internal interface IOutOfProcSemanticTokensService -{ - ValueTask GetSemanticTokensDataAsync( - TextDocument razorDocument, - LinePositionSpan span, - Guid correlationId, - CancellationToken cancellationToken); -} diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostSemanticTokensRangeEndpoint.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostSemanticTokensRangeEndpoint.cs index 4fed19fa39d..555452d9104 100644 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostSemanticTokensRangeEndpoint.cs +++ b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/LanguageClient/Cohost/CohostSemanticTokensRangeEndpoint.cs @@ -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; @@ -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, 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(); @@ -66,19 +70,43 @@ internal sealed class CohostSemanticTokensRangeEndpoint( protected override async Task 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( + 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; + } } } diff --git a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Remote/OutOfProcSemanticTokensService.cs b/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Remote/OutOfProcSemanticTokensService.cs deleted file mode 100644 index a6f0af985c0..00000000000 --- a/src/Razor/src/Microsoft.VisualStudio.LanguageServices.Razor/Remote/OutOfProcSemanticTokensService.cs +++ /dev/null @@ -1,62 +0,0 @@ -// 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.ComponentModel.Composition; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Razor.Logging; -using Microsoft.CodeAnalysis.Razor.Remote; -using Microsoft.CodeAnalysis.Razor.Workspaces; -using Microsoft.CodeAnalysis.Text; -using Microsoft.VisualStudio.Razor.Settings; - -namespace Microsoft.VisualStudio.Razor.Remote; - -[Export(typeof(IOutOfProcSemanticTokensService))] -[method: ImportingConstructor] -internal class OutOfProcSemanticTokensService(IRemoteClientProvider remoteClientProvider, IClientSettingsManager clientSettingsManager, ILoggerFactory loggerFactory) : IOutOfProcSemanticTokensService -{ - private readonly IRemoteClientProvider _remoteClientProvider = remoteClientProvider; - private readonly IClientSettingsManager _clientSettingsManager = clientSettingsManager; - private readonly ILogger _logger = loggerFactory.GetOrCreateLogger(); - - public async ValueTask GetSemanticTokensDataAsync(TextDocument razorDocument, LinePositionSpan span, Guid correlationId, CancellationToken cancellationToken) - { - // We're being overly defensive here because the OOP host can return null for the client/session/operation - // when it's disconnected (user stops the process). - // - // This will change in the future to an easier to consume API but for VS RTM this is what we have. - var remoteClient = await _remoteClientProvider.TryGetClientAsync(cancellationToken).ConfigureAwait(false); - - if (remoteClient is null) - { - _logger.LogWarning($"Couldn't get remote client"); - // Could not resolve - return null; - } - - try - { - var colorBackground = _clientSettingsManager.GetClientSettings().AdvancedSettings.ColorBackground; - - var data = await remoteClient.TryInvokeAsync( - razorDocument.Project.Solution, - (service, solutionInfo, cancellationToken) => service.GetSemanticTokensDataAsync(solutionInfo, razorDocument.Id, span, colorBackground, correlationId, cancellationToken), - cancellationToken).ConfigureAwait(false); - - if (!data.HasValue) - { - return null; - } - - return data.Value; - } - catch (Exception ex) - { - _logger.LogError(ex, $"Error calling remote"); - return null; - } - } -}