Skip to content
Merged
Changes from all 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 @@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.VisualStudio.Threading;
using Roslyn.LanguageServer.Protocol;
using Roslyn.Utilities;

Expand Down Expand Up @@ -66,23 +67,25 @@ public Task OnInitializedAsync(ClientCapabilities clientCapabilities, RequestCon

void InitializeRazor()
{
this.InitializeRazor(clientCapabilities, context, _disposalTokenSource.Token);
this.InitializeRazorAsync(clientCapabilities, context, _disposalTokenSource.Token).ReportNonFatalErrorAsync();
}
}

private void InitializeRazor(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken)
private async Task InitializeRazorAsync(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken)
{
// The LSP server will dispose us when the server exits, but VS could decide to activate us later.
// If a new instance of the server is created, a new instance of this class will be created and the
// UIContext will already be active, so this method will be immediately called on the new instance.
if (cancellationToken.IsCancellationRequested) return;

await TaskScheduler.Default.SwitchTo(alwaysYield: true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SwitchTo(alwaysYield: true);

Just so I'm clear, the SwitchTo isn't necessary as the concern is about leaving the main thread, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, or maybe you just don't want to block the calling thread, even if it's a threadpool thread. I see.

Copy link
Member Author

@davidwengier davidwengier Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not necessary in the real world, but in theory the UI context could have been activated before we got here (in future if the conditions for the context change), or we could be in VS Code (in future) and I thought getting out of the way of the LSP servers initialize handler is probably kind regardless.


// We use a string to pass capabilities to/from Razor to avoid version issues with the Protocol DLL
var serializedClientCapabilities = JsonSerializer.Serialize(clientCapabilities, ProtocolConversions.LspJsonSerializerOptions);
var razorCohostClientLanguageServerManager = new RazorCohostClientLanguageServerManager(clientLanguageServerManager!);

var requestContext = new RazorCohostRequestContext(context);
dynamicRegistrationService!.Value.RegisterAsync(serializedClientCapabilities, requestContext, cancellationToken).ReportNonFatalErrorAsync();
await dynamicRegistrationService!.Value.RegisterAsync(serializedClientCapabilities, requestContext, cancellationToken).ConfigureAwait(false);
}
}
}
Loading