Skip to content

Commit 982ea89

Browse files
committed
Move token refresh logic to a separate function outside the server
1 parent 59f1071 commit 982ea89

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Connection } from 'vscode-languageserver'
2+
import DocumentIndexer from '../../indexing/DocumentIndexer'
3+
4+
/**
5+
* Wires semantic token invalidation to document indexing.
6+
*
7+
* When indexing completes, this schedules a debounced refresh request
8+
* so the client re-requests semantic tokens and updates highlighting.
9+
*/
10+
function setupSemanticTokenRefresh (
11+
connection: Connection,
12+
documentIndexer: DocumentIndexer
13+
): void {
14+
let refreshTimer: NodeJS.Timeout | undefined
15+
16+
documentIndexer.setOnIndexed(() => {
17+
if (refreshTimer != null) clearTimeout(refreshTimer)
18+
19+
refreshTimer = setTimeout(() => {
20+
void connection.sendRequest('workspace/semanticTokens/refresh')
21+
}, 150)
22+
})
23+
}
24+
25+
export default setupSemanticTokenRefresh

src/server.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Indexer from './indexing/Indexer'
2323
import RenameSymbolProvider from './providers/rename/RenameSymbolProvider'
2424
import HighlightSymbolProvider from './providers/highlighting/HighlightSymbolProvider'
2525
import SemanticTokensProvider, { SEMANTIC_TOKEN_TYPES, SEMANTIC_TOKEN_MODIFIERS } from './providers/semanticTokens/SemanticTokensProvider'
26+
import setupSemanticTokenRefresh from './providers/semanticTokens/setupSemanticTokenRefresh'
2627
import { RequestType } from './indexing/SymbolSearchService'
2728
import { cacheAndClearProxyEnvironmentVariables } from './utils/ProxyUtils'
2829
import MatlabDebugAdaptorServer from './debug/MatlabDebugAdaptorServer'
@@ -375,23 +376,7 @@ export async function startServer (): Promise<void> {
375376
connection.onRequest(SemanticTokensRequest.method, async (params: SemanticTokensParams) => {
376377
return await semanticTokensProvider.handleSemanticTokensRequest(params, documentManager)
377378
})
378-
379-
// Ensures that semantic tokens are refreshed after indexing,
380-
// so highlighting is updated after opening the editor.
381-
documentIndexer.setOnIndexed(() => {
382-
scheduleSemanticRefresh()
383-
})
384-
385-
let refreshTimer: NodeJS.Timeout | undefined
386-
387-
function scheduleSemanticRefresh (): void {
388-
if (refreshTimer != null) clearTimeout(refreshTimer)
389-
390-
// Delay sending the refresh notification to batch multiple indexing updates together
391-
refreshTimer = setTimeout(() => {
392-
void connection.sendRequest('workspace/semanticTokens/refresh')
393-
}, 150)
394-
}
379+
setupSemanticTokenRefresh(connection, documentIndexer)
395380
}
396381

397382
/** -------------------- Helper Functions -------------------- **/

0 commit comments

Comments
 (0)