Skip to content

Commit 4229a3f

Browse files
committed
Fixes #96664: Accept number[] besides Uint32Array for semantic tokens
1 parent fd5d9a3 commit 4229a3f

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/vs/workbench/api/common/extHostLanguageFeatures.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { URI, UriComponents } from 'vs/base/common/uri';
77
import { mixin } from 'vs/base/common/objects';
88
import type * as vscode from 'vscode';
99
import * as typeConvert from 'vs/workbench/api/common/extHostTypeConverters';
10-
import { Range, Disposable, CompletionList, SnippetString, CodeActionKind, SymbolInformation, DocumentSymbol, SemanticTokensEdits } from 'vs/workbench/api/common/extHostTypes';
10+
import { Range, Disposable, CompletionList, SnippetString, CodeActionKind, SymbolInformation, DocumentSymbol, SemanticTokensEdits, SemanticTokens, SemanticTokensEdit } from 'vs/workbench/api/common/extHostTypes';
1111
import { ISingleEditOperation } from 'vs/editor/common/model';
1212
import * as modes from 'vs/editor/common/modes';
1313
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
@@ -676,6 +676,13 @@ class SemanticTokensPreviousResult {
676676
) { }
677677
}
678678

679+
type RelaxedSemanticTokens = { readonly resultId?: string; readonly data: number[]; };
680+
type RelaxedSemanticTokensEdit = { readonly start: number; readonly deleteCount: number; readonly data?: number[]; };
681+
type RelaxedSemanticTokensEdits = { readonly resultId?: string; readonly edits: RelaxedSemanticTokensEdit[]; };
682+
683+
type ProvidedSemanticTokens = vscode.SemanticTokens | RelaxedSemanticTokens;
684+
type ProvidedSemanticTokensEdits = vscode.SemanticTokensEdits | RelaxedSemanticTokensEdits;
685+
679686
export class DocumentSemanticTokensAdapter {
680687

681688
private readonly _previousResults: Map<number, SemanticTokensPreviousResult>;
@@ -696,13 +703,14 @@ export class DocumentSemanticTokensAdapter {
696703
return this._provider.provideDocumentSemanticTokensEdits(doc, previousResult.resultId, token);
697704
}
698705
return this._provider.provideDocumentSemanticTokens(doc, token);
699-
}).then(value => {
706+
}).then((value: ProvidedSemanticTokens | ProvidedSemanticTokensEdits | null | undefined) => {
700707
if (previousResult) {
701708
this._previousResults.delete(previousResultId);
702709
}
703710
if (!value) {
704711
return null;
705712
}
713+
value = DocumentSemanticTokensAdapter._fixProvidedSemanticTokens(value);
706714
return this._send(DocumentSemanticTokensAdapter._convertToEdits(previousResult, value), value);
707715
});
708716
}
@@ -711,12 +719,40 @@ export class DocumentSemanticTokensAdapter {
711719
this._previousResults.delete(semanticColoringResultId);
712720
}
713721

714-
private static _isSemanticTokens(v: vscode.SemanticTokens | vscode.SemanticTokensEdits): v is vscode.SemanticTokens {
715-
return v && !!((v as vscode.SemanticTokens).data);
722+
private static _fixProvidedSemanticTokens(v: ProvidedSemanticTokens | ProvidedSemanticTokensEdits): vscode.SemanticTokens | vscode.SemanticTokensEdits {
723+
if (DocumentSemanticTokensAdapter._isSemanticTokens(v)) {
724+
if (DocumentSemanticTokensAdapter._isCorrectSemanticTokens(v)) {
725+
return v;
726+
}
727+
return new SemanticTokens(new Uint32Array(v.data), v.resultId);
728+
} else if (DocumentSemanticTokensAdapter._isSemanticTokensEdits(v)) {
729+
if (DocumentSemanticTokensAdapter._isCorrectSemanticTokensEdits(v)) {
730+
return v;
731+
}
732+
return new SemanticTokensEdits(v.edits.map(edit => new SemanticTokensEdit(edit.start, edit.deleteCount, edit.data ? new Uint32Array(edit.data) : edit.data)), v.resultId);
733+
}
734+
return v;
735+
}
736+
737+
private static _isSemanticTokens(v: ProvidedSemanticTokens | ProvidedSemanticTokensEdits): v is ProvidedSemanticTokens {
738+
return v && !!((v as ProvidedSemanticTokens).data);
716739
}
717740

718-
private static _isSemanticTokensEdits(v: vscode.SemanticTokens | vscode.SemanticTokensEdits): v is vscode.SemanticTokensEdits {
719-
return v && Array.isArray((v as vscode.SemanticTokensEdits).edits);
741+
private static _isCorrectSemanticTokens(v: ProvidedSemanticTokens): v is vscode.SemanticTokens {
742+
return (v.data instanceof Uint32Array);
743+
}
744+
745+
private static _isSemanticTokensEdits(v: ProvidedSemanticTokens | ProvidedSemanticTokensEdits): v is ProvidedSemanticTokensEdits {
746+
return v && Array.isArray((v as ProvidedSemanticTokensEdits).edits);
747+
}
748+
749+
private static _isCorrectSemanticTokensEdits(v: ProvidedSemanticTokensEdits): v is vscode.SemanticTokensEdits {
750+
for (const edit of v.edits) {
751+
if (!(edit.data instanceof Uint32Array)) {
752+
return false;
753+
}
754+
}
755+
return true;
720756
}
721757

722758
private static _convertToEdits(previousResult: SemanticTokensPreviousResult | null | undefined, newResult: vscode.SemanticTokens | vscode.SemanticTokensEdits): vscode.SemanticTokens | vscode.SemanticTokensEdits {

0 commit comments

Comments
 (0)