@@ -7,7 +7,7 @@ import { URI, UriComponents } from 'vs/base/common/uri';
7
7
import { mixin } from 'vs/base/common/objects' ;
8
8
import type * as vscode from 'vscode' ;
9
9
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' ;
11
11
import { ISingleEditOperation } from 'vs/editor/common/model' ;
12
12
import * as modes from 'vs/editor/common/modes' ;
13
13
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments' ;
@@ -676,6 +676,13 @@ class SemanticTokensPreviousResult {
676
676
) { }
677
677
}
678
678
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
+
679
686
export class DocumentSemanticTokensAdapter {
680
687
681
688
private readonly _previousResults : Map < number , SemanticTokensPreviousResult > ;
@@ -696,13 +703,14 @@ export class DocumentSemanticTokensAdapter {
696
703
return this . _provider . provideDocumentSemanticTokensEdits ( doc , previousResult . resultId , token ) ;
697
704
}
698
705
return this . _provider . provideDocumentSemanticTokens ( doc , token ) ;
699
- } ) . then ( value => {
706
+ } ) . then ( ( value : ProvidedSemanticTokens | ProvidedSemanticTokensEdits | null | undefined ) => {
700
707
if ( previousResult ) {
701
708
this . _previousResults . delete ( previousResultId ) ;
702
709
}
703
710
if ( ! value ) {
704
711
return null ;
705
712
}
713
+ value = DocumentSemanticTokensAdapter . _fixProvidedSemanticTokens ( value ) ;
706
714
return this . _send ( DocumentSemanticTokensAdapter . _convertToEdits ( previousResult , value ) , value ) ;
707
715
} ) ;
708
716
}
@@ -711,12 +719,40 @@ export class DocumentSemanticTokensAdapter {
711
719
this . _previousResults . delete ( semanticColoringResultId ) ;
712
720
}
713
721
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 ) ;
716
739
}
717
740
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 ;
720
756
}
721
757
722
758
private static _convertToEdits ( previousResult : SemanticTokensPreviousResult | null | undefined , newResult : vscode . SemanticTokens | vscode . SemanticTokensEdits ) : vscode . SemanticTokens | vscode . SemanticTokensEdits {
0 commit comments