Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -110,10 +110,16 @@ internal abstract partial class AbstractInProcLanguageClient(

public event AsyncEventHandler<EventArgs>? StartAsync;

public event AsyncEventHandler<EventArgs>? StopAsync;

/// <summary>
/// Unused, implementing <see cref="ILanguageClient"/>
/// Stops the server if it has been started.
/// </summary>
public event AsyncEventHandler<EventArgs>? StopAsync { add { } remove { } }
/// <remarks>
/// Per the documentation on <see cref="ILanguageClient.StopAsync"/>, the event is ignored if the server has not been started.
/// </remarks>
public Task StopServerAsync()
=> StopAsync?.InvokeAsync(this, EventArgs.Empty) ?? Task.CompletedTask;

public async Task<Connection?> ActivateAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,25 @@ internal sealed class AlwaysActiveLanguageClientEventListener(
/// </summary>
public void StartListening(Workspace workspace)
{
// Trigger a fire and forget request to the VS LSP client to load our ILanguageClient.
Load();
_ = workspace.RegisterWorkspaceChangedHandler(Workspace_WorkspaceChanged);
}

private void Workspace_WorkspaceChanged(WorkspaceChangeEventArgs e)
{
if (e.Kind == WorkspaceChangeKind.SolutionAdded)
{
// Normally VS will load the language client when an editor window is created for one of our content types,
// but we want to load it as soon as a solution is loaded so workspace diagnostics work, and so 3rd parties
// like Razor can use dynamic registration.
Load();
}
else if (e.Kind is WorkspaceChangeKind.SolutionRemoved)
{
// VS will unload the language client when the solution is closed, but sometimes its a little slow to do so,
// and we can end up trying to load it, above, before it has been asked to unload. We wait for the previous
// instance to shutdown when we load, but we have to ensure that it at least gets signaled to do so first.
Unload();
}
}

public void StopListening(Workspace workspace)
Expand All @@ -56,7 +73,6 @@ private void Load()

async Task LoadAsync()
{

// Explicitly switch to the bg so that if this causes any expensive work (like mef loads) it
// doesn't block the UI thread. Note, we always yield because sometimes our caller starts
// on the threadpool thread but is indirectly blocked on by the UI thread.
Expand All @@ -71,6 +87,14 @@ await _languageClientBroker.Value.LoadAsync(new LanguageClientMetadata(
}
}

private void Unload()
{
// We just want to signal that an unload should happen, in case the above call to Load comes in quick.
// We don't want to wait for it to complete, not do we care about errors that may occur during the unload.
// The language client/server does its own error reporting as necessary.
_languageClient.StopServerAsync().Forget();
}

/// <summary>
/// The <see cref="ILanguageClientBroker.LoadAsync(ILanguageClientMetadata, ILanguageClient)"/>
/// requires that we pass the <see cref="ILanguageClientMetadata"/> along with the language client instance.
Expand Down
Loading