From 574ff332a05cf32cafb2bfa27b101b5522ae10db Mon Sep 17 00:00:00 2001 From: David Wengier Date: Wed, 27 Dec 2023 21:15:27 +1100 Subject: [PATCH] Don't start the cohost server at all if Razor doesn't want to --- .../AbstractInProcLanguageClient.cs | 2 +- .../Razor/Cohost/RazorCohostLanguageClient.cs | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/EditorFeatures/Core/LanguageServer/AbstractInProcLanguageClient.cs b/src/EditorFeatures/Core/LanguageServer/AbstractInProcLanguageClient.cs index 59deff7f61645..a93418fe75d85 100644 --- a/src/EditorFeatures/Core/LanguageServer/AbstractInProcLanguageClient.cs +++ b/src/EditorFeatures/Core/LanguageServer/AbstractInProcLanguageClient.cs @@ -166,7 +166,7 @@ protected virtual void Activate_OffUIThread() /// Signals that the extension has been loaded. The server can be started immediately, or wait for user action to start. /// To start the server, invoke the event; /// - public async Task OnLoadedAsync() + public virtual async Task OnLoadedAsync() { try { diff --git a/src/Tools/ExternalAccess/Razor/Cohost/RazorCohostLanguageClient.cs b/src/Tools/ExternalAccess/Razor/Cohost/RazorCohostLanguageClient.cs index 2e0c8a40e0a50..cdd0c54d93525 100644 --- a/src/Tools/ExternalAccess/Razor/Cohost/RazorCohostLanguageClient.cs +++ b/src/Tools/ExternalAccess/Razor/Cohost/RazorCohostLanguageClient.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Immutable; using System.ComponentModel.Composition; +using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editor.Implementation.LanguageClient; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.Host.Mef; @@ -42,18 +43,20 @@ internal sealed class RazorCohostLanguageClient( public override object? CustomMessageTarget => razorCustomMessageTarget; - public override ServerCapabilities GetCapabilities(ClientCapabilities clientCapabilities) + public override Task OnLoadedAsync() { if (razorCohostLanguageClientActivationService?.ShouldActivateCohostServer() != true) { - // A default return would still opt us in to TextDocumentSync for open files, but even that's - // wasteful if we don't want to handle anything, as we'd track document content. - return new() - { - TextDocumentSync = new TextDocumentSyncOptions { OpenClose = false, Change = TextDocumentSyncKind.None, Save = false, WillSave = false, WillSaveWaitUntil = false } - }; + // By not calling the base implementation, we avoid firing the StartAsync event, which means + // the platform will never trigger the creation of an instance of the language server. + return Task.CompletedTask; } + return base.OnLoadedAsync(); + } + + public override ServerCapabilities GetCapabilities(ClientCapabilities clientCapabilities) + { Contract.ThrowIfNull(razorCapabilitiesProvider); // We use a string to pass capabilities to/from Razor to avoid version issues with the Protocol DLL @@ -76,7 +79,7 @@ public override ServerCapabilities GetCapabilities(ClientCapabilities clientCapa /// /// If the cohost server is expected to activate then any failures are catastrophic as no razor features will work. /// - public override bool ShowNotificationOnInitializeFailed => razorCohostLanguageClientActivationService?.ShouldActivateCohostServer() == true; + public override bool ShowNotificationOnInitializeFailed => true; public override WellKnownLspServerKinds ServerKind => WellKnownLspServerKinds.RazorCohostServer; }