@@ -63,14 +63,14 @@ import * as vscode from 'vscode';
6363 * *NOTE*: If the provider cannot temporarily compute semantic tokens, it can indicate this by throwing an error with the message 'Busy'.
6464 */
6565export function rangesByName ( data : vscode . SemanticTokens , legend : vscode . SemanticTokensLegend , editor : vscode . TextEditor , highlightGlobals : boolean ) : Map < string , vscode . Range [ ] > {
66- const accumulator : Map < string , vscode . Range [ ] > = new Map ( ) ;
67- // const accumulator: Record<string, vscode.Range[]> = {};
66+ const accumulatorParam : Map < string , vscode . Range [ ] > = new Map ( ) ;
67+ const accumulatorVar : Map < string , vscode . Range [ ] > = new Map ( ) ;
68+ const accumulatorWithDec : Map < string , boolean > = new Map ( ) ;
6869 const recordSize = 5 ;
6970 let line = 0 ;
7071 let column = 0 ;
7172
7273 for ( let i = 0 ; i < data . data . length ; i += recordSize ) {
73- console . log ( legend ) ;
7474 const [ deltaLine , deltaColumn , length , kindIndex , modifierIndex ] = data . data . slice ( i , i + recordSize ) ;
7575 column = deltaLine === 0 ? column : 0 ;
7676 line += deltaLine ;
@@ -79,13 +79,53 @@ export function rangesByName(data: vscode.SemanticTokens, legend: vscode.Semanti
7979 const name = editor . document . getText ( range ) ;
8080 const kind = legend . tokenTypes [ kindIndex ] ;
8181 const modifiers = legend . tokenModifiers . filter ( ( _ , index ) => ( modifierIndex & ( 1 << index ) ) !== 0 ) ;
82- if ( [ 'variable' , 'parameter' ] . includes ( kind ) && name . length > 2 && ( highlightGlobals || ! modifiers . includes ( 'global' ) ) ) {
83- if ( accumulator . has ( name ) ) {
84- accumulator . get ( name ) ! . push ( range ) ;
82+ if ( highlightGlobals || ! modifiers . includes ( 'global' ) && name . length > 2 ) {
83+ if ( 'variable' === kind ) {
84+ if ( accumulatorVar . has ( name ) ) {
85+ accumulatorVar . get ( name ) ! . push ( range ) ;
86+ } else {
87+ accumulatorVar . set ( name , [ range ] ) ;
88+ }
89+ }
90+ else if ( kind === 'parameter' && ! modifiers . includes ( 'label' ) ) {
91+ // Check for declaration to not highlight labels in python since the modifier
92+ // label is not declared by pylance.
93+ // See https://github.com/microsoft/pylance-release/issues/2196
94+ if ( ! accumulatorWithDec . has ( name ) ) {
95+ accumulatorWithDec . set ( name , false ) ;
96+ }
97+ if ( modifiers . includes ( 'declaration' ) ) {
98+ accumulatorWithDec . set ( name , true ) ;
99+ }
100+
101+ if ( accumulatorParam . has ( name ) ) {
102+ accumulatorParam . get ( name ) ! . push ( range ) ;
103+ } else {
104+ accumulatorParam . set ( name , [ range ] ) ;
105+ }
106+ }
107+ }
108+ }
109+
110+ // The cpp language server does not implement the declaration modifier
111+ // hence we colorize everything
112+ if ( editor . document . languageId === "cpp" ) {
113+ for ( let [ name , isDeclared ] of accumulatorWithDec ) {
114+ accumulatorWithDec . set ( name , true ) ;
115+ }
116+ }
117+ for ( let [ name , isDeclared ] of accumulatorWithDec ) {
118+ if ( ! isDeclared ) {
119+ accumulatorParam . delete ( name ) ;
120+ }
121+ else {
122+ if ( accumulatorVar . has ( name ) ) {
123+ accumulatorVar . set ( name ,
124+ accumulatorVar . get ( name ) ! . concat ( accumulatorParam . get ( name ) ! ) ) ;
85125 } else {
86- accumulator . set ( name , [ range ] ) ;
126+ accumulatorVar . set ( name , accumulatorParam . get ( name ) ! ) ;
87127 }
88128 }
89129 }
90- return accumulator ;
130+ return accumulatorVar ;
91131}
0 commit comments