-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathHoverProcessor.ts
More file actions
153 lines (135 loc) · 5.79 KB
/
Copy pathHoverProcessor.ts
File metadata and controls
153 lines (135 loc) · 5.79 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { isBrsFile, isFunctionType, isXmlFile } from '../../astUtils/reflection';
import type { BrsFile } from '../../files/BrsFile';
import type { XmlFile } from '../../files/XmlFile';
import type { Hover, ProvideHoverEvent, Callable } from '../../interfaces';
import type { Token } from '../../lexer/Token';
import { TokenKind } from '../../lexer/TokenKind';
import { BrsTranspileState } from '../../parser/BrsTranspileState';
import { ParseMode } from '../../parser/Parser';
import util from '../../util';
export class HoverProcessor {
public constructor(
public event: ProvideHoverEvent
) {
}
public process() {
let hover: Hover | undefined;
if (isBrsFile(this.event.file)) {
hover = this.getBrsFileHover(this.event.file);
} else if (isXmlFile(this.event.file)) {
hover = this.getXmlFileHover(this.event.file);
}
//if we got a result, "return" it
if (hover) {
//assign the hover to the event
this.event.hovers.push(hover);
}
}
private buildContentsWithDocs(text: string, startingToken: Token) {
const parts = [text];
const docs = this.getTokenDocumentation((this.event.file as BrsFile).parser.tokens, startingToken);
if (docs) {
parts.push('***', docs);
}
return parts.join('\n');
}
private buildCallableContents(callable: Callable, fence: (code: string) => string) {
const parts = [fence(callable.type.toString())];
// Use shortDescription and documentation for all callables (both global and user-defined)
const docs = [];
if (callable.shortDescription) {
docs.push(callable.shortDescription);
}
if (callable.documentation) {
docs.push(callable.documentation);
}
if (docs.length > 0) {
parts.push('***', docs.join('\n\n'));
}
return parts.join('\n');
}
private getBrsFileHover(file: BrsFile): Hover | undefined {
const scope = this.event.scopes[0];
const fence = (code: string) => util.mdFence(code, 'brightscript');
//get the token at the position
let token = file.getTokenAt(this.event.position);
let hoverTokenTypes = [
TokenKind.Identifier,
TokenKind.Function,
TokenKind.EndFunction,
TokenKind.Sub,
TokenKind.EndSub
];
//throw out invalid tokens and the wrong kind of tokens
if (!token || !hoverTokenTypes.includes(token.kind)) {
return undefined;
}
const expression = file.getClosestExpression(this.event.position);
if (expression?.range) {
let containingNamespace = file.getNamespaceStatementForPosition(expression.range.start)?.getName(ParseMode.BrighterScript);
const fullName = util.getAllDottedGetParts(expression)?.map(x => x.text).join('.');
//find a constant with this name
const constant = scope?.getConstFileLink(fullName, containingNamespace);
if (constant) {
const constantValue = util.sourceNodeFromTranspileResult(null, null, null, constant.item.value.transpile(new BrsTranspileState(file))).toString();
return {
contents: this.buildContentsWithDocs(fence(`const ${constant.item.fullName} = ${constantValue}`), constant.item.tokens.const),
range: token.range
};
}
}
let lowerTokenText = token.text.toLowerCase();
//look through local variables first
{
//get the function scope for this position (if exists)
let functionScope = file.getFunctionScopeAtPosition(this.event.position);
if (functionScope) {
//find any variable with this name
for (const varDeclaration of functionScope.variableDeclarations) {
//we found a variable declaration with this token text!
if (varDeclaration.name.toLowerCase() === lowerTokenText) {
let typeText: string;
if (isFunctionType(varDeclaration.type)) {
typeText = varDeclaration.type.toString();
} else {
typeText = `${varDeclaration.name} as ${varDeclaration.type.toString()}`;
}
return {
range: token.range,
//append the variable name to the front for scope
contents: fence(typeText)
};
}
}
for (const labelStatement of functionScope.labelStatements) {
if (labelStatement.name.toLocaleLowerCase() === lowerTokenText) {
return {
range: token.range,
contents: fence(`${labelStatement.name}: label`)
};
}
}
}
}
//look through all callables in relevant scopes
for (let scope of this.event.scopes) {
let callable = scope.getCallableByName(lowerTokenText);
if (callable) {
return {
range: token.range,
contents: this.buildCallableContents(callable, fence)
};
}
}
}
/**
* Combine all the documentation found before a token (i.e. comment tokens)
*/
private getTokenDocumentation(tokens: Token[], token?: Token) {
return util.getTokenDocumentation(tokens, token);
}
private getXmlFileHover(file: XmlFile): Hover | undefined {
//TODO add xml hovers
return undefined;
}
}