@@ -4,7 +4,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument'
44import FileInfoIndex , { MatlabFunctionScopeInfo , MatlabGlobalScopeInfo } from '../../indexing/FileInfoIndex'
55import DocumentIndexer from '../../indexing/DocumentIndexer'
66
7- interface VariableToken {
7+ interface SemanticToken {
88 range : Range
99 typeIndex : number
1010}
@@ -20,25 +20,21 @@ class SemanticTokensProvider {
2020 params : SemanticTokensParams ,
2121 documentManager : TextDocuments < TextDocument >
2222 ) : Promise < SemanticTokens | null > {
23- // This request will be called constantly, should not connect to MATLAB just because it was called
23+ // This provider will be called constantly, should not connect to MATLAB just because it was called
2424 const matlabConnection = await this . matlabLifecycleManager . getMatlabConnection ( false )
25- if ( matlabConnection == null ) {
26- // If MATLAB is not connected, fall back to textmate
27- return null
28- }
25+ // If MATLAB is not connected, fall back to default highlighting
26+ if ( matlabConnection == null ) return null
2927
3028 const textDocument = documentManager . get ( params . textDocument . uri )
3129 if ( textDocument == null ) return null
3230
3331 await this . documentIndexer . ensureDocumentIndexIsUpdated ( textDocument )
3432
3533 const codeInfo = this . fileInfoIndex . codeInfoCache . get ( params . textDocument . uri )
36- if ( codeInfo == null ) {
37- return { data : [ ] }
38- }
34+ if ( codeInfo == null ) return null
3935
40- const tokens : VariableToken [ ] = [ ]
41- this . collectVariableTokens ( codeInfo . globalScopeInfo , tokens )
36+ const tokens : SemanticToken [ ] = [ ]
37+ this . collectSemanticTokens ( codeInfo . globalScopeInfo , tokens )
4238
4339 // Sort tokens by their position in the document (line and character)
4440 // This is necessary to encode them using relative positions
@@ -70,9 +66,15 @@ class SemanticTokensProvider {
7066 return { data }
7167 }
7268
73- private collectVariableTokens (
69+ /**
70+ * Recursively collects semantic tokens for a given scope and its nested scopes.
71+ * Tokens are appended to 'tokens' in-place.
72+ * @param scope The scope from which semantic tokens should be collected
73+ * @param tokens The array to which collected semantic tokens are appended
74+ */
75+ private collectSemanticTokens (
7476 scope : MatlabGlobalScopeInfo | MatlabFunctionScopeInfo ,
75- tokens : VariableToken [ ]
77+ tokens : SemanticToken [ ]
7678 ) : void {
7779 // Variables: highlight only the first component as variable
7880 for ( const item of scope . variables . values ( ) ) {
@@ -93,15 +95,15 @@ class SemanticTokensProvider {
9395 if ( classScope != null ) {
9496 for ( const nestedFunc of classScope . functionScopes . values ( ) ) {
9597 if ( nestedFunc . functionScopeInfo != null ) {
96- this . collectVariableTokens ( nestedFunc . functionScopeInfo , tokens ) ;
98+ this . collectSemanticTokens ( nestedFunc . functionScopeInfo , tokens ) ;
9799 }
98100 }
99101 }
100102
101103 // Function scopes
102104 for ( const nestedFunc of scope . functionScopes . values ( ) ) {
103105 if ( nestedFunc . functionScopeInfo != null ) {
104- this . collectVariableTokens ( nestedFunc . functionScopeInfo , tokens )
106+ this . collectSemanticTokens ( nestedFunc . functionScopeInfo , tokens )
105107 }
106108 }
107109 }
0 commit comments