-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSemanticTokensProvider.ts
More file actions
96 lines (78 loc) · 3.25 KB
/
SemanticTokensProvider.ts
File metadata and controls
96 lines (78 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { SemanticTokens, SemanticTokensParams, TextDocuments, Range } from 'vscode-languageserver'
import { TextDocument } from 'vscode-languageserver-textdocument'
import FileInfoIndex, { MatlabFunctionScopeInfo, MatlabGlobalScopeInfo } from '../../indexing/FileInfoIndex'
import DocumentIndexer from '../../indexing/DocumentIndexer'
interface VariableToken {
range: Range
typeIndex: number
}
class SemanticTokensProvider {
constructor(
protected readonly documentIndexer: DocumentIndexer,
protected readonly fileInfoIndex: FileInfoIndex
) { }
async handleSemanticTokensRequest(
params: SemanticTokensParams,
documentManager: TextDocuments<TextDocument>
): Promise<SemanticTokens | null> {
const textDocument = documentManager.get(params.textDocument.uri)
if (!textDocument) return null
await this.documentIndexer.ensureDocumentIndexIsUpdated(textDocument)
const codeInfo = this.fileInfoIndex.codeInfoCache.get(params.textDocument.uri)
if (!codeInfo) {
return { data: [] }
}
const tokens: VariableToken[] = []
this.collectVariableTokens(codeInfo.globalScopeInfo, tokens)
tokens.sort((a, b) =>
(a.range.start.line - b.range.start.line) ||
(a.range.start.character - b.range.start.character)
)
const data: number[] = []
let prevLine = 0
let prevStart = 0
for (const token of tokens) {
const line = token.range.start.line
const start = token.range.start.character
const length = token.range.end.character - token.range.start.character
const deltaLine = line - prevLine
const deltaStart = deltaLine === 0 ? start - prevStart : start
data.push(deltaLine, deltaStart, length, token.typeIndex, 0)
prevLine = line
prevStart = start
}
return { data }
}
private collectVariableTokens(
scope: MatlabGlobalScopeInfo | MatlabFunctionScopeInfo,
tokens: VariableToken[]
): void {
// Global scope, e.g. for scripts
for (const variableInfo of scope.variables.values()) {
for (const ref of variableInfo.references) {
if (ref.components.length > 0) {
const typeIndex = 0
tokens.push({ range: ref.components[0].range, typeIndex })
}
}
}
// Class scope, for class definitions and methods
const classScope = (scope as MatlabGlobalScopeInfo).classScope;
if (classScope) {
for (const nestedFunc of classScope.functionScopes.values()) {
if (nestedFunc.functionScopeInfo) {
this.collectVariableTokens(nestedFunc.functionScopeInfo, tokens);
}
}
}
// Function scopes, for nested functions
for (const nestedFunc of scope.functionScopes.values()) {
if (nestedFunc.functionScopeInfo) {
this.collectVariableTokens(nestedFunc.functionScopeInfo, tokens)
}
}
}
}
export const SEMANTIC_TOKEN_TYPES = ['variable']
export const SEMANTIC_TOKEN_MODIFIERS: string[] = []
export default SemanticTokensProvider