-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LSP] Modify semanticTokens isFinalized
logic
#60484
Changes from 12 commits
c6d83aa
2f569b8
eae6fa3
1828ce3
20cf491
4740f18
8606db0
fe6307f
ef16c05
d3274e2
3af6c20
f96cf89
fb287ff
9bc049f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Classification; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
@@ -23,6 +25,7 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens | |
internal class SemanticTokensRangeHandler : AbstractStatelessRequestHandler<LSP.SemanticTokensRangeParams, LSP.SemanticTokens> | ||
{ | ||
private readonly IGlobalOptionService _globalOptions; | ||
private readonly Dictionary<ProjectId, Task<Compilation?>> _projectIdToCompilation = new(); | ||
|
||
public override bool MutatesSolutionState => false; | ||
public override bool RequiresLSPSolution => true; | ||
|
@@ -48,13 +51,20 @@ public SemanticTokensRangeHandler(IGlobalOptionService globalOptions) | |
Contract.ThrowIfNull(request.TextDocument, "TextDocument is null."); | ||
Contract.ThrowIfNull(context.Document, "Document is null."); | ||
|
||
var options = _globalOptions.GetClassificationOptions(context.Document.Project.Language); | ||
var project = context.Document.Project; | ||
var options = _globalOptions.GetClassificationOptions(project.Language); | ||
|
||
// Razor uses isFinalized to determine whether to cache tokens. We should be able to | ||
// remove it altogether once Roslyn implements workspace/semanticTokens/refresh: | ||
// https://github.com/dotnet/roslyn/issues/60441 | ||
var isFinalized = !context.Document.IsRazorDocument() || | ||
await IsDataFinalizedAsync(project, cancellationToken).ConfigureAwait(false); | ||
|
||
// The results from the range handler should not be cached since we don't want to cache | ||
// partial token results. In addition, a range request is only ever called with a whole | ||
// document request, so caching range results is unnecessary since the whole document | ||
// handler will cache the results anyway. | ||
var (tokensData, isFinalized) = await SemanticTokensHelpers.ComputeSemanticTokensDataAsync( | ||
var tokensData = await SemanticTokensHelpers.ComputeSemanticTokensDataAsync( | ||
context.Document, | ||
SemanticTokensHelpers.TokenTypeToIndex, | ||
request.Range, | ||
|
@@ -64,5 +74,35 @@ public SemanticTokensRangeHandler(IGlobalOptionService globalOptions) | |
|
||
return new RoslynSemanticTokens { Data = tokensData, IsFinalized = isFinalized }; | ||
} | ||
|
||
private async Task<bool> IsDataFinalizedAsync(Project project, CancellationToken cancellationToken) | ||
{ | ||
// We use a combination of IsFullyLoaded + the completed project compilation as the metric | ||
// for isFinalized. It may not be completely accurate but this is only a a temporary fix until | ||
// workspace/semanticTokens/refresh is implemented. | ||
if (_projectIdToCompilation.TryGetValue(project.Id, out var compilationTask) && compilationTask.IsCompleted) | ||
{ | ||
// We don't want to hang on to the compilation since this can be very expensive, | ||
// but we do want to mark the compilation as being successfully retrieved. | ||
if (compilationTask.Result is not null) | ||
{ | ||
_projectIdToCompilation[project.Id] = Task.FromResult<Compilation?>(null); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
var workspaceStatusService = project.Solution.Workspace.Services.GetRequiredService<IWorkspaceStatusService>(); | ||
var isFullyLoaded = await workspaceStatusService.IsFullyLoadedAsync(cancellationToken).ConfigureAwait(false); | ||
if (isFullyLoaded && !_projectIdToCompilation.ContainsKey(project.Id)) | ||
{ | ||
// If the project's compilation isn't yet available, kick off a task in the background to | ||
// hopefully make it available faster since we'll need it later to compute tokens. | ||
var newCompilationTask = project.GetCompilationAsync(cancellationToken); | ||
_projectIdToCompilation.Add(project.Id, newCompilationTask); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can technically race and lead to multiple calls to
I think you'll want to guard access to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! Thanks |
||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❔ Do we want to use frozen partial semantics with
isFinalized
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I wouldn't think so? I believe we want to call
TryGetCompilation
on the complete document. The only other place the frozen document would be used withisFinalized
is when retrieving the WorkspaceStatusService, and unless I'm mistaken it doesn't seem like frozen partial semantics would be useful for that.