Skip to content
Merged
Show file tree
Hide file tree
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 @@ -28,6 +28,8 @@ internal sealed class SemanticTokensRefreshQueue : AbstractRefreshQueue
/// </summary>
private readonly Dictionary<ProjectId, Checksum> _projectIdToLastComputedChecksum = [];

public bool AllowRazorRefresh { get; set; }

public SemanticTokensRefreshQueue(
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider,
LspWorkspaceRegistrationService lspWorkspaceRegistrationService,
Expand Down Expand Up @@ -79,7 +81,7 @@ protected override void OnLspSolutionChanged(object? sender, WorkspaceChangeEven
var document = e.NewSolution.GetRequiredAdditionalDocument(e.DocumentId);

// Changes to files with certain extensions (eg: razor) shouldn't trigger semantic a token refresh
if (DisallowsAdditionalDocumentChangedRefreshes(document.FilePath))
if (!AllowRazorRefresh && DisallowsAdditionalDocumentChangedRefreshes(document.FilePath))
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a separate boolean here, and can't just remove the disallows check?

Copy link
Member Author

Choose a reason for hiding this comment

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

Cohosting is still only optional, so I'm just being cautious until the old LSP editor is removed. With cohosting off it would mean Roslyn is asking for a refresh for a document that isn't doesn't handle.

Copy link
Member

Choose a reason for hiding this comment

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

Got it - so this is a part that can be deleted when cohosting is the only mechanism

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this can be part of the great code purge. Hopefully sometime in 2026? :D

return;
}
else if (e.Kind is WorkspaceChangeKind.DocumentReloaded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers
{
internal static class SemanticTokensRange
{
public static void RegisterRefresh(RazorCohostRequestContext requestContext)
{
var refreshQueue = requestContext.GetRequiredService<SemanticTokensRefreshQueue>();
refreshQueue.AllowRazorRefresh = true;
}

public static Task TryEnqueueRefreshComputationAsync(RazorCohostRequestContext requestContext, Project project, CancellationToken cancellationToken)
Copy link
Member

Choose a reason for hiding this comment

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

so it looks like you're allowing Razor to manually call a refresh and opting in to the Roslyn automatic refreshing done as part of the queue. Can you elaborate on why both are required (i.e. why this can't only be done on the Razor side, or only be done on the Roslyn side)?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is mirroring the Roslyn functionality its semantic tokens handler, where after every request it triggers a refresh:

// The above call to get semantic tokens may be inaccurate (because we use frozen partial semantics). Kick
// off a request to ensure that the OOP side gets a fully up to compilation for this project. Once it does
// we can optionally choose to notify our caller to do a refresh if we computed a compilation for a new
// solution snapshot.
await semanticTokensRefreshQueue.TryEnqueueRefreshComputationAsync(project, cancellationToken).ConfigureAwait(false);

Razor calls in to the method just below that, so doesn't get that behaviour, but the Razor call happens in OOP so we can't call in to the higher level. Razor could just trigger a refresh itself, but the queue does debouncing and checksum checking, and it makes sense to me that everything just funnels into the one queue.

Copy link
Member

Choose a reason for hiding this comment

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

I see. So this is something that doesn't work for Razor because the handler on our side runs in-proc. If it ran out of proc instead you could just rely on the request to compute semantic tokens to trigger the refresh as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Essentially, yes. Where you run the handler wouldn't matter, since the requests hit Razors handler, but if we called into something that knew it was OOP, and routed the Enqueue call back to in proc, then yes we could just call that.

{
var refreshQueue = requestContext.GetRequiredService<SemanticTokensRefreshQueue>();
return refreshQueue.TryEnqueueRefreshComputationAsync(project, cancellationToken);
}

public static Task<int[]> GetSemanticTokensAsync(
Document document,
ImmutableArray<LinePositionSpan> spans,
bool supportsVisualStudioExtensions,
CancellationToken cancellationToken)
{
var tokens = SemanticTokensHelpers.HandleRequestHelperAsync(
=> SemanticTokensHelpers.HandleRequestHelperAsync(
document,
spans,
supportsVisualStudioExtensions,
ClassificationOptions.Default,
cancellationToken);

// The above call to get semantic tokens may be inaccurate (because we use frozen partial semantics). Kick
// off a request to ensure that the OOP side gets a fully up to compilation for this project. Once it does
// we can optionally choose to notify our caller to do a refresh if we computed a compilation for a new
// solution snapshot.
// TODO: await semanticTokensRefreshQueue.TryEnqueueRefreshComputationAsync(project, cancellationToken).ConfigureAwait(false);
return tokens;
}
}
}
Loading