-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathBrsFileSemanticTokensProcessor.ts
More file actions
119 lines (106 loc) · 5.06 KB
/
BrsFileSemanticTokensProcessor.ts
File metadata and controls
119 lines (106 loc) · 5.06 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { Range } from 'vscode-languageserver-protocol';
import { SemanticTokenModifiers } from 'vscode-languageserver-protocol';
import { SemanticTokenTypes } from 'vscode-languageserver-protocol';
import { isCallExpression, isCustomType, isNamespaceStatement, isNewExpression } from '../../astUtils/reflection';
import type { BrsFile } from '../../files/BrsFile';
import type { OnGetSemanticTokensEvent } from '../../interfaces';
import type { Locatable } from '../../lexer/Token';
import { ParseMode } from '../../parser/Parser';
import type { NamespaceStatement } from '../../parser/Statement';
import util from '../../util';
export class BrsFileSemanticTokensProcessor {
public constructor(
public event: OnGetSemanticTokensEvent<BrsFile>
) {
}
public process() {
this.handleClasses();
this.handleConstDeclarations();
this.iterateExpressions();
}
private handleConstDeclarations() {
for (const stmt of this.event.file.parser.references.constStatements) {
this.addToken(stmt.tokens.name, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]);
}
}
private handleClasses() {
const classes = [] as Array<{ className: string; namespaceName: string; range: Range }>;
//classes used in function param types
for (const func of this.event.file.parser.references.functionExpressions) {
for (const parm of func.parameters) {
if (isCustomType(parm.type)) {
const namespace = parm.findAncestor<NamespaceStatement>(isNamespaceStatement);
classes.push({
className: parm.typeToken.text,
namespaceName: namespace?.getName(ParseMode.BrighterScript),
range: parm.typeToken.range
});
}
}
}
for (const cls of classes) {
if (
cls.className.length > 0 &&
//only highlight classes that are in scope
this.event.scopes.some(x => x.hasClass(cls.className, cls.namespaceName))
) {
const tokens = util.splitGetRange('.', cls.className, cls.range);
this.addTokens(tokens.reverse(), SemanticTokenTypes.class, SemanticTokenTypes.namespace);
}
}
}
/**
* Add tokens for each locatable item in the list.
* Each locatable is paired with a token type. If there are more locatables than token types, all remaining locatables are given the final token type
*/
private addTokens(locatables: Locatable[], ...semanticTokenTypes: SemanticTokenTypes[]) {
for (let i = 0; i < locatables.length; i++) {
const locatable = locatables[i];
//skip items that don't have a location
if (locatable?.range) {
this.addToken(
locatables[i],
//use the type at the index, or the last type if missing
semanticTokenTypes[i] ?? semanticTokenTypes[semanticTokenTypes.length - 1]
);
}
}
}
private addToken(locatable: Locatable, type: SemanticTokenTypes, modifiers: SemanticTokenModifiers[] = []) {
this.event.semanticTokens.push({
range: locatable.range,
tokenType: type,
tokenModifiers: modifiers
});
}
private iterateExpressions() {
const scope = this.event.scopes[0];
for (let expression of this.event.file.parser.references.expressions) {
//lift the callee from call expressions to handle namespaced function calls
if (isCallExpression(expression)) {
expression = expression.callee;
} else if (isNewExpression(expression)) {
expression = expression.call.callee;
}
const tokens = util.getAllDottedGetParts(expression);
const processedNames: string[] = [];
for (const token of tokens ?? []) {
processedNames.push(token.text?.toLowerCase());
const entityName = processedNames.join('.');
if (scope.getEnumMemberMap().has(entityName)) {
this.addToken(token, SemanticTokenTypes.enumMember);
} else if (scope.getEnumMap().has(entityName)) {
this.addToken(token, SemanticTokenTypes.enum);
} else if (scope.getClassMap().has(entityName)) {
this.addToken(token, SemanticTokenTypes.class);
} else if (scope.getCallableByName(entityName)) {
this.addToken(token, SemanticTokenTypes.function);
} else if (scope.namespaceLookup.has(entityName)) {
this.addToken(token, SemanticTokenTypes.namespace);
} else if (scope.getConstFileLink(entityName)) {
this.addToken(token, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly, SemanticTokenModifiers.static]);
}
}
}
}
}