Repository navigation
Expand file tree
/
Copy pathScopeVisualizer.tsx
More file actions
343 lines (309 loc) · 8.59 KB
/
Copy pathScopeVisualizer.tsx
File metadata and controls
343 lines (309 loc) · 8.59 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { usePluginData } from "@docusaurus/useGlobalData";
import React, { useState } from "react";
import type {
ScopeSupportFacetInfo,
ScopeTypeType,
} from "@cursorless/lib-common";
import {
prettifyLanguageName,
prettifyScopeType,
scopeReferences,
serializeScopeType,
} from "@cursorless/lib-common";
import { generateDecorations } from "./calculateHighlights";
import { Code } from "./Code";
import { H2, H3, H4, H5 } from "./Header";
import "./ScopeSupport.css";
import type { FacetValue, Fixture, RangeType, ScopeTests } from "./types";
import { getFacetInfo, nameComparator, prettifyFacet } from "./util";
interface Scopes {
public: Scope[];
internal: Scope[];
}
interface Scope {
scopeTypeType: ScopeTypeType;
name: string;
facets: Facet[];
}
interface Facet {
facet: FacetValue;
name: string;
info: ScopeSupportFacetInfo;
fixtures: Fixture[];
}
interface Props {
languageId?: string;
scopeTypeType?: ScopeTypeType;
}
export function ScopeVisualizer({ languageId, scopeTypeType }: Props) {
const scopeTests = usePluginData("scope-tests-plugin") as ScopeTests;
// oxlint-disable-next-line react/hook-use-state
const [scopes] = useState(
getScopeFixtures(scopeTests, languageId, scopeTypeType),
);
const [rangeType, setRangeType] = useState<RangeType>(
scopeTypeType != null ? "blend" : "content",
);
const [renderWhitespace, setRenderWhitespace] = useState(
scopeTypeType != null,
);
if (scopes.internal.length === 0 && scopes.public.length === 0) {
return null;
}
const renderOptions = () => {
return (
<div className="mb-4">
<select
className="form-select form-select-sm d-inline-block w-auto"
value={rangeType}
onChange={(e) => setRangeType(e.currentTarget.value as RangeType)}
>
<option value="content">Content range</option>
<option value="removal">Removal range</option>
<option value="blend">Blended ranges</option>
</select>
<label className="ms-2">
<input
type="checkbox"
className="me-1"
checked={renderWhitespace}
onChange={(e) => setRenderWhitespace(e.currentTarget.checked)}
/>
Render whitespace
</label>
</div>
);
};
const renderScopes = (scopes: Scope[]) => {
return scopes.map((scope) =>
renderScope(
languageId,
scopeTypeType,
rangeType,
renderWhitespace,
scope,
),
);
};
// Specific language. Public scopes followed by (optional) internal scopes.
if (languageId != null) {
return (
<>
<p>
Below are visualizations of all our scope tests for this language.
These were created primarily for testing purposes rather than as
documentation. There are quite a few, and they may feel a bit
overwhelming from a documentation standpoint.
</p>
{renderPublicScopesHeader()}
{renderOptions()}
{renderScopes(scopes.public)}
{scopes.internal.length > 0 && (
<>
{renderInternalScopesHeader()}
{renderScopes(scopes.internal)}
</>
)}
</>
);
}
// Specific public scope
if (scopes.public.length > 0) {
return (
<>
{renderPublicScopesHeader()}
{renderOptions()}
{renderScopes(scopes.public)}
</>
);
}
// Specific internal scope
return (
<>
{renderInternalScopesHeader()}
{renderOptions()}
{renderScopes(scopes.internal)}
</>
);
}
function renderScope(
languageId: string | undefined,
scopeTypeType: ScopeTypeType | undefined,
rangeType: RangeType,
renderWhitespace: boolean,
scope: Scope,
) {
return (
<div key={scope.scopeTypeType}>
{scopeTypeType == null && <H3>{scope.name}</H3>}
{scope.facets.map((facet, index) =>
renderFacet(
languageId,
scopeTypeType,
rangeType,
renderWhitespace,
facet,
index,
),
)}
</div>
);
}
function renderPublicScopesHeader() {
return <H2>Scopes</H2>;
}
function renderInternalScopesHeader() {
return (
<>
<H2>Internal scopes</H2>
<p>
The following are internal scopes. They are not intended for user
interaction or spoken use. These scopes exist solely for internal
Cursorless functionality.
</p>
</>
);
}
function renderFacet(
languageId: string | undefined,
scopeTypeType: ScopeTypeType | undefined,
rangeType: RangeType,
renderWhitespace: boolean,
facet: Facet,
index: number,
) {
let previousLanguageId: string | undefined;
const renderFacetName = () => {
if (scopeTypeType != null) {
return (
<H3 className="facet-name" title={facet.facet}>
{facet.name}
</H3>
);
}
return (
<H4 className="facet-name" title={facet.facet}>
{`${index + 1}. ${facet.name}`}
</H4>
);
};
const renderLanguageId = (facetLanguageId: string) => {
if (scopeTypeType != null && previousLanguageId !== facetLanguageId) {
previousLanguageId = facetLanguageId;
return (
<H5
className="language-id mt-2 mb-1"
id={`${facet.name}-${facetLanguageId}`}
>
{prettifyLanguageName(facetLanguageId)}
</H5>
);
}
return null;
};
return (
<div key={facet.facet}>
{renderFacetName()}
<i>{facet.info.description}</i>
{facet.fixtures.map((fixture) => (
<React.Fragment key={fixture.name}>
{renderLanguageId(fixture.languageId)}
<Code
// oxlint-disable-next-line react_perf/jsx-no-new-object-as-prop
link={{
name: "GitHub",
url: `https://github.com/cursorless-dev/cursorless/blob/main/resources/fixtures/${fixture.name}.scope`,
}}
languageId={languageId ?? fixture.languageId}
renderWhitespace={renderWhitespace}
decorations={generateDecorations(
fixture,
rangeType,
facet.info.isIteration ?? false,
)}
>
{fixture.code}
</Code>
</React.Fragment>
))}
</div>
);
}
function getScopeFixtures(
scopeTests: ScopeTests,
languageId: string | undefined,
scopeTypeType: ScopeTypeType | undefined,
): Scopes {
const scopeMap: Partial<Record<ScopeTypeType, Scope>> = {};
const facetMap: Partial<Record<FacetValue, Facet>> = {};
const languageIds = new Set<string>(
languageId != null ? (scopeTests.imports[languageId] ?? [languageId]) : [],
);
for (const fixture of scopeTests.fixtures) {
const info = getFacetInfo(fixture.languageId, fixture.facet);
const fixtureScopeTypeType = serializeScopeType(info.scopeType);
if (
languageId != null &&
(!languageIds.has(fixture.languageId) ||
fixtureScopeTypeType.startsWith("private."))
) {
continue;
}
if (scopeTypeType != null && fixtureScopeTypeType !== scopeTypeType) {
continue;
}
if (scopeMap[fixtureScopeTypeType] == null) {
scopeMap[fixtureScopeTypeType] = {
scopeTypeType: fixtureScopeTypeType,
name: prettifyScopeType(info.scopeType),
facets: [],
};
}
if (facetMap[fixture.facet] == null) {
const facet = {
facet: fixture.facet,
name: prettifyFacet(fixture.facet),
info,
fixtures: [],
};
facetMap[fixture.facet] = facet;
scopeMap[fixtureScopeTypeType].facets.push(facet);
}
switch (fixture.languageId) {
case "javascript.core":
fixture.languageId = "javascript";
break;
case "typescript.core":
fixture.languageId = "typescript";
break;
case "javascript.jsx":
fixture.languageId = "javascriptreact";
break;
// No default
}
facetMap[fixture.facet]?.fixtures.push(fixture);
}
const result: Scopes = { public: [], internal: [] };
const sortedScopes = Object.values(scopeMap).toSorted(nameComparator);
for (const scope of sortedScopes) {
scope.facets.sort(facetComparator);
for (const f of scope.facets) {
f.fixtures.sort(nameComparator);
}
if (scopeReferences[scope.scopeTypeType].private) {
result.internal.push(scope);
} else {
result.public.push(scope);
}
}
return result;
}
export function facetComparator(a: Facet, b: Facet): number {
if (a.info.isIteration && !b.info.isIteration) {
return 1;
}
if (!a.info.isIteration && b.info.isIteration) {
return -1;
}
return nameComparator(a, b);
}