Skip to content

Commit 17368bc

Browse files
Cleanup and make semantic token processing and testing code more consistent (#77684)
View with whitespace off.
2 parents a4179e0 + 77a9c20 commit 17368bc

File tree

8 files changed

+930
-937
lines changed

8 files changed

+930
-937
lines changed

src/Features/Lsif/Generator/Generator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,19 +466,22 @@ private static async Task GenerateSemanticTokensAsync(
466466
IdFactory idFactory,
467467
LsifDocument documentVertex)
468468
{
469+
var cancellationToken = CancellationToken.None;
470+
469471
// Compute colorization data.
470472
//
471473
// Unlike the mainline LSP scenario, where we control both the syntactic colorizer (in-proc syntax tagger)
472474
// and the semantic colorizer (LSP semantic tokens) LSIF is more likely to be consumed by clients which may
473475
// have different syntactic classification behavior than us, resulting in missing colors. To avoid this, we
474476
// include syntax tokens in the generated data.
477+
var text = await document.GetTextAsync(cancellationToken);
475478
var data = await SemanticTokensHelpers.ComputeSemanticTokensDataAsync(
476479
// Just get the pure-lsp semantic tokens here.
477480
document,
478-
spans: [],
481+
spans: [text.Lines.GetLinePositionSpan(new TextSpan(0, text.Length))],
479482
supportsVisualStudioExtensions: true,
480483
options: Classification.ClassificationOptions.Default,
481-
cancellationToken: CancellationToken.None);
484+
cancellationToken);
482485

483486
var semanticTokensResult = new SemanticTokensResult(new SemanticTokens { Data = data }, idFactory);
484487
var semanticTokensEdge = Edge.Create(Methods.TextDocumentSemanticTokensFullName, documentVertex.GetId(), semanticTokensResult.GetId(), idFactory);

src/LanguageServer/Protocol/ExternalAccess/Razor/SemanticTokensRangesHandler.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,35 @@
1212
namespace Microsoft.CodeAnalysis.LanguageServer.ExternalAccess.Razor;
1313

1414
[Method(SemanticRangesMethodName)]
15-
internal class SemanticTokensRangesHandler : ILspServiceDocumentRequestHandler<SemanticTokensRangesParams, SemanticTokens>
15+
internal sealed class SemanticTokensRangesHandler(
16+
IGlobalOptionService globalOptions,
17+
SemanticTokensRefreshQueue semanticTokensRefreshQueue)
18+
: ILspServiceDocumentRequestHandler<SemanticTokensRangesParams, SemanticTokens>
1619
{
1720
public const string SemanticRangesMethodName = "roslyn/semanticTokenRanges";
18-
private readonly IGlobalOptionService _globalOptions;
19-
private readonly SemanticTokensRefreshQueue _semanticTokenRefreshQueue;
21+
22+
private readonly IGlobalOptionService _globalOptions = globalOptions;
23+
private readonly SemanticTokensRefreshQueue _semanticTokenRefreshQueue = semanticTokensRefreshQueue;
2024

2125
public bool MutatesSolutionState => false;
2226

2327
public bool RequiresLSPSolution => true;
2428

25-
public SemanticTokensRangesHandler(
26-
IGlobalOptionService globalOptions,
27-
SemanticTokensRefreshQueue semanticTokensRefreshQueue)
28-
{
29-
_globalOptions = globalOptions;
30-
_semanticTokenRefreshQueue = semanticTokensRefreshQueue;
31-
}
32-
3329
public TextDocumentIdentifier GetTextDocumentIdentifier(SemanticTokensRangesParams request)
3430
{
3531
Contract.ThrowIfNull(request.TextDocument);
3632
return request.TextDocument;
3733
}
3834

3935
public async Task<SemanticTokens> HandleRequestAsync(
40-
SemanticTokensRangesParams request,
41-
RequestContext context,
42-
CancellationToken cancellationToken)
36+
SemanticTokensRangesParams request,
37+
RequestContext context,
38+
CancellationToken cancellationToken)
4339
{
4440
Contract.ThrowIfNull(request.TextDocument, "TextDocument is null.");
45-
if (request.Ranges.Length == 0)
46-
return new SemanticTokens { Data = [] };
4741

48-
var tokensData = await SemanticTokensHelpers.HandleRequestHelperAsync(_globalOptions, _semanticTokenRefreshQueue, request.Ranges, context, cancellationToken).ConfigureAwait(false);
42+
var tokensData = await SemanticTokensHelpers.HandleRequestHelperAsync(
43+
_globalOptions, _semanticTokenRefreshQueue, request.Ranges, context, cancellationToken).ConfigureAwait(false);
4944
return new SemanticTokens { Data = tokensData };
5045
}
5146
}

src/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensFullHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.SemanticTokensFullPa
3434
CancellationToken cancellationToken)
3535
{
3636
Contract.ThrowIfNull(request.TextDocument);
37-
// Passing an empty array of ranges will cause the helper to return tokens for the entire document.
38-
var tokensData = await SemanticTokensHelpers.HandleRequestHelperAsync(_globalOptions, _semanticTokenRefreshQueue, ranges: [], context, cancellationToken).ConfigureAwait(false);
37+
38+
// Passing an null array of ranges will cause the helper to return tokens for the entire document.
39+
var tokensData = await SemanticTokensHelpers.HandleRequestHelperAsync(
40+
_globalOptions, _semanticTokenRefreshQueue, ranges: null, context, cancellationToken).ConfigureAwait(false);
3941
return new LSP.SemanticTokens { Data = tokensData };
4042
}
4143
}

0 commit comments

Comments
 (0)