Skip to content

Commit

Permalink
Revert "Reduce overhead due to lsp semantic token refresh requests. (d…
Browse files Browse the repository at this point in the history
…otnet#69690)"

This reverts commit 499a8d2.
  • Loading branch information
333fred committed Sep 5, 2023
1 parent d8b5411 commit fdcde78
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Collections;
Expand Down Expand Up @@ -45,7 +44,6 @@ internal class SemanticTokensRefreshQueue :

private readonly LspWorkspaceManager _lspWorkspaceManager;
private readonly IClientLanguageServerManager _notificationManager;
private readonly ICapabilitiesProvider _capabilitiesProvider;

private readonly IAsynchronousOperationListener _asyncListener;
private readonly CancellationTokenSource _disposalTokenSource;
Expand All @@ -63,8 +61,7 @@ public SemanticTokensRefreshQueue(
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider,
LspWorkspaceRegistrationService lspWorkspaceRegistrationService,
LspWorkspaceManager lspWorkspaceManager,
IClientLanguageServerManager notificationManager,
ICapabilitiesProvider capabilitiesProvider)
IClientLanguageServerManager notificationManager)
{
_asyncListener = asynchronousOperationListenerProvider.GetListener(FeatureAttribute.Classification);

Expand All @@ -73,14 +70,11 @@ public SemanticTokensRefreshQueue(

_lspWorkspaceManager = lspWorkspaceManager;
_notificationManager = notificationManager;
_capabilitiesProvider = capabilitiesProvider;
}

public Task OnInitializedAsync(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken _)
{
if (_semanticTokenRefreshQueue is null
&& clientCapabilities.Workspace?.SemanticTokens?.RefreshSupport is true
&& _capabilitiesProvider.GetCapabilities(clientCapabilities).SemanticTokensOptions is not null)
if (_semanticTokenRefreshQueue is null && clientCapabilities.Workspace?.SemanticTokens?.RefreshSupport is true)
{
// Only send a refresh notification to the client every 2s (if needed) in order to avoid
// sending too many notifications at once. This ensures we batch up workspace notifications,
Expand Down Expand Up @@ -147,52 +141,21 @@ private static ValueTask FilterLspTrackedDocumentsAsync(

private void OnLspSolutionChanged(object? sender, WorkspaceChangeEventArgs e)
{
Uri? documentUri = null;

if (e.DocumentId is not null)
if (e.DocumentId is not null && e.Kind is WorkspaceChangeKind.DocumentChanged)
{
var document = e.NewSolution.GetRequiredDocument(e.DocumentId);
var documentUri = document.GetURI();

// We enqueue the URI since there's a chance the client is already tracking the
// document, in which case we don't need to send a refresh notification.
// We perform the actual check when processing the batch to ensure we have the
// most up-to-date list of tracked documents.
if (e.Kind is WorkspaceChangeKind.DocumentChanged)
{
var document = e.NewSolution.GetRequiredDocument(e.DocumentId);
documentUri = document.GetURI();
}
else if (e.Kind is WorkspaceChangeKind.AdditionalDocumentChanged)
{
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))
return;
}
else if (e.Kind is WorkspaceChangeKind.DocumentReloaded)
{
var newDocument = e.NewSolution.GetRequiredDocument(e.DocumentId);
var oldDocument = e.OldSolution.GetDocument(e.DocumentId);

// If the document's attributes haven't changed, then use the document's URI for
// the call to EnqueueSemanticTokenRefreshNotification which will enable the
// tracking check before sending the WorkspaceSemanticTokensRefreshName message.
if (oldDocument?.State.Attributes is IChecksummedObject oldChecksumObject
&& newDocument.State.Attributes is IChecksummedObject newChecksumObject
&& oldChecksumObject.Checksum == newChecksumObject.Checksum)
{
documentUri = newDocument.GetURI();
}
}
EnqueueSemanticTokenRefreshNotification(documentUri);
}
else
{
EnqueueSemanticTokenRefreshNotification(documentUri: null);
}

EnqueueSemanticTokenRefreshNotification(documentUri);
}

// Duplicated from Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.LoadedProject.TreatAsIsDynamicFile
private static bool DisallowsAdditionalDocumentChangedRefreshes(string? filePath)
{
var extension = Path.GetExtension(filePath);
return extension is ".cshtml" or ".razor";
}

private void EnqueueSemanticTokenRefreshNotification(Uri? documentUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ public ILspService CreateILspService(LspServices lspServices, WellKnownLspServer
{
var notificationManager = lspServices.GetRequiredService<IClientLanguageServerManager>();
var lspWorkspaceManager = lspServices.GetRequiredService<LspWorkspaceManager>();
var capabilitiesProvider = lspServices.GetRequiredService<ICapabilitiesProvider>();

return new SemanticTokensRefreshQueue(_asyncListenerProvider, _lspWorkspaceRegistrationService, lspWorkspaceManager, notificationManager, capabilitiesProvider);
return new SemanticTokensRefreshQueue(_asyncListenerProvider, _lspWorkspaceRegistrationService, lspWorkspaceManager, notificationManager);
}
}
}

0 comments on commit fdcde78

Please sign in to comment.