-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorationSetup.ts
281 lines (253 loc) · 9.87 KB
/
decorationSetup.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { onUpdatedAST } from "./extension";
import { AST_NODE_TYPES } from "@typescript-eslint/typescript-estree";
import { DeclObj, ScopeObj, parseClosed } from "./parseClosed";
import * as vscode from "vscode";
const variableDeclaration = ({});
const variableDeclarationUnused = ({ backgroundColor: `hsla(320, 75, 40, 0)` });
const variableDeclarationHasCloses = ({
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableDeclarationHasCloses0 = ({
before: {
contentText: " ",
backgroundColor: "hsla(280, 75%, 40%, 1)",
width: "10px",
height: "10px",
margin: "0px 2px 0px 2px",
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableDeclarationHasCloses1 = ({
before: {
contentText: " ",
backgroundColor: "hsla(60, 75%, 40%, 1)",
width: "10px",
height: "10px",
margin: "0px 2px 0px 2px",
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableDeclarationHasCloses2 = ({
before: {
contentText: " ",
backgroundColor: "hsla(200, 75%, 40%, 1)",
width: "10px",
height: "10px",
margin: "0px 2px 0px 2px",
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableDeclarationHasCloses3 = ({
before: {
contentText: " ",
backgroundColor: "hsla(0, 75%, 40%, 1)",
width: "10px",
height: "10px",
margin: "0px 2px 0px 2px",
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableDeclarationHasCloses4 = ({
before: {
contentText: " ",
backgroundColor: "hsla(0, 75%, 40%, 1)",
width: "10px",
height: "10px",
margin: "0px 2px 0px 2px",
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
// Order is order of matching precedence
const variableUsedGlobal = ({
border: "1px solid hsla(0, 0%, 70%, 0.4)",
color: "hsla(0, 0%, 80%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableUsedSameScope = ({});
const variableUsedFunctionScope = ({});
const variableUsedRootScope = ({
border: "1px transparent",
outline: "1px solid hsla(280, 75%, 40%, 0.6)",
//backgroundColor: "blue",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
});
const variableUsedOtherScope0 = ({
border: "2px solid hsla(280, 75%, 40%, 0.4)",
backgroundColor: "hsla(280, 75%, 40%, 0.3)",
color: "hsla(280, 75%, 80%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
});
const variableUsedOtherScope1 = ({
border: "2px solid hsla(60, 75%, 40%, 0.4)",
backgroundColor: "hsla(60, 75%, 40%, 0.3)",
color: "hsla(60, 75%, 80%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
});
const variableUsedOtherScope2 = ({
border: "2px solid hsla(200, 75%, 40%, 0.4)",
backgroundColor: "hsla(200, 75%, 40%, 0.3)",
color: "hsla(200, 75%, 80%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
});
const variableUsedOtherScope3 = ({
border: "2px solid hsla(0, 75%, 40%, 0.4)",
backgroundColor: "hsla(0, 75%, 40%, 0.3)",
color: "hsla(0, 75%, 80%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
});
const variableUsedOtherScope4 = ({
border: "2px solid hsla(0, 75%, 40%, 1)",
backgroundColor: "hsla(0, 75%, 40%, 0.6)",
color: "hsla(0, 75%, 50%, 1)",
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed
});
function getVariableUsedOtherScope(index: number) {
if (index === 0) return variableUsedOtherScope0;
if (index === 1) return variableUsedOtherScope1;
if (index === 2) return variableUsedOtherScope2;
if (index === 3) return variableUsedOtherScope3;
return variableUsedOtherScope4;
}
function getCloseColoring(index: number) {
if (index === 0) return variableDeclarationHasCloses0;
if (index === 1) return variableDeclarationHasCloses1;
if (index === 2) return variableDeclarationHasCloses2;
if (index === 3) return variableDeclarationHasCloses3;
return variableDeclarationHasCloses4;
}
onUpdatedAST(({ setDecoration, traverse, doc, ast }) => {
function onDeclare(obj: DeclObj, scope: ScopeObj) {
declarations.set(obj, new Set());
}
function onAccess(
declScope: ScopeObj | undefined,
varName: string,
varPos: number,
curScope: ScopeObj,
curFncScope: ScopeObj,
declObj: DeclObj | undefined,
): void {
let range = new vscode.Range(doc.positionAt(varPos), doc.positionAt(varPos + varName.length));
if (!declScope) {
setDecoration({
range,
hoverMessage: `Closed variable from global scope`,
...variableUsedGlobal
});
return;
}
if (!declObj) {
throw new Error(`not declObj passed while there was a declScope passed. This is unexpected`);
}
let declFncScope = declScope.type === "brace" && declScope.parentFncScope || declScope;
// Ugh, the root function/brace scopes means it might not be the same scope object. But... if the
// pos start is the same, it has to be the same scope... right?
if (declScope.posStart === curScope.posStart) {
setDecoration({
range,
hoverMessage: `Closed variable from same scope`,
...variableUsedSameScope
});
return;
} else if (declFncScope === curFncScope) {
setDecoration({
range,
hoverMessage: `Closed variable from function scope`,
...variableUsedFunctionScope
});
return;
} else if (declScope.parentScope === undefined) {
setDecoration({
range,
hoverMessage: `Closed variable from module scope`,
...variableUsedRootScope
});
return;
} else {
let scopeName = declScope.scopeName;
let functionScope = curScope.type === "function" ? curScope : curScope.parentFncScope;
if (!functionScope) {
throw new Error(`Internal error, if the declaration for this variable is in a function, but we are the module scope... how does that even work?`);
}
let curInFncScope = closedFromParent.get(functionScope);
if (!curInFncScope) {
curInFncScope = new Map();
closedFromParent.set(functionScope, curInFncScope);
}
// I've given up on naming at this point
let curInFncScope2 = curInFncScope.get(declFncScope);
if (!curInFncScope2) {
curInFncScope2 = [];
curInFncScope.set(declFncScope, curInFncScope2);
}
curInFncScope2.push({ range, scopeName, decl: declObj });
//let declScopeStart = declScope.declNode?.loc.start;
//addDecoration(variableUsedOtherScope, varFixedRange, `Closed variable from parent scope "${scopeName}" ${declScopeStart?.line}:${declScopeStart?.column}`);
return;
}
}
// key is current function scope
let closedFromParent: Map<ScopeObj,
// key is declFncScope
Map<ScopeObj, {
range: vscode.Range;
scopeName: string;
decl: DeclObj;
}[]>
> = new Map();
// Set of child scope close colorings
let declarations: Map<DeclObj, Set<number>> = new Map();
parseClosed(doc.getText(), ast, onDeclare, onAccess);
for (let [functionScope, declScopes] of closedFromParent) {
let declScopesSorted = Array.from(declScopes.entries()).sort((b, a) => a[0].posStart - b[0].posStart);
for (let i = 0; i < declScopesSorted.length; i++) {
let [declScope, vars] = declScopesSorted[i];
for (let { range, scopeName, decl } of vars) {
let declScopeStart = declScope.posLineCol;
setDecoration({
range,
hoverMessage: `Closed variable from parent scope "${scopeName}", ${i + 1}/${declScopesSorted.length} scopes closed. Scope declared at ${declScopeStart.line}:${declScopeStart.column}`,
...getVariableUsedOtherScope(i),
});
let declScopeColorings = declarations.get(decl);
if (!declScopeColorings) {
throw new Error(`Impossible, declaration not found`);
}
declScopeColorings.add(i);
}
}
}
for (let [obj, closedColorings] of declarations) {
// Anything before the start of the document is an implicit declaration (this, arguments, etc)
if (obj.varPos < 0) continue;
let countUsed = obj.uses.size;
let range = new vscode.Range(doc.positionAt(obj.varPos), doc.positionAt(obj.varPos + obj.varName.length));
if (closedColorings.size > 0) {
let closedColoringsSorted = Array.from(closedColorings.values()).sort((b, a) => a - b);
setDecoration({
range,
hoverMessage: `Declaration closed upon. Total uses (including non-closes), are: ${countUsed} times.`,
...variableDeclarationHasCloses
})
for (let closeColoring of closedColoringsSorted) {
setDecoration({
range,
...getCloseColoring(closeColoring)
});
}
}
else if (countUsed === 0) {
setDecoration({
range,
hoverMessage: `Unused declaration`,
...variableDeclarationUnused
});
} else {
setDecoration({
range,
hoverMessage: `Declaration used ${countUsed} times`,
...variableDeclaration
});
}
}
});