Skip to content

Commit afc1a9c

Browse files
Support imported languages
1 parent 1bb2a54 commit afc1a9c

File tree

8 files changed

+38782
-38695
lines changed

8 files changed

+38782
-38695
lines changed

.eslintrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
"jest.config.ts",
9898
"docusaurus.config.mts",
9999
"mdx-components.tsx",
100-
"prepare-assets.ts",
101100
"typings/**"
102101
],
103102
"extends": ["plugin:@typescript-eslint/disable-type-checked"]

packages/cursorless-org-docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"write-heading-ids": "docusaurus write-heading-ids",
4343
"compile": "tsc --build",
4444
"watch": "tsc --build --watch",
45-
"prepare-assets": "my-ts-node scripts/prepare-assets.ts",
45+
"prepare-assets": "my-ts-node src/scripts/prepare-assets.ts",
4646
"clean": "pnpm clear && rm -rf ./out tsconfig.tsbuildinfo ./dist ./build"
4747
},
4848
"dependencies": {

packages/cursorless-org-docs/src/docs/user/languages/components/Code.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ function getDecorations(
8585
}
8686

8787
// Use a fallback language for languages that are not supported by Shiki
88+
// https://shiki.style/languages
8889
function getFallbackLanguage(languageId: string): string {
8990
switch (languageId) {
91+
case "javascriptreact":
92+
return "jsx";
93+
case "typescriptreact":
94+
return "tsx";
9095
case "scm":
9196
return "scheme";
9297
default:

packages/cursorless-org-docs/src/docs/user/languages/components/ScopeVisualizer.tsx

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
import { Range } from "@cursorless/common";
22
import React, { useState } from "react";
3-
import scopeTestsExport from "../../../../../static/scopeTests.json";
3+
import scopeTestsJson from "../../../../../static/scopeTests.json";
44
import { Code, type Highlight } from "./Code";
5+
import type { Fixture, ScopeTestsJson } from "./types";
56

67
type RangeType = "content" | "removal";
78

8-
interface Fixture {
9-
name: string;
10-
facet: string;
11-
languageId: string;
12-
code: string;
13-
scopes: Scope[];
14-
}
15-
16-
interface Scope {
17-
domain?: string;
18-
targets: Target[];
19-
}
20-
21-
interface Target {
22-
content: string;
23-
removal?: string;
24-
insertionDelimiter?: string;
25-
}
26-
27-
const scopeTests = scopeTestsExport as Fixture[];
9+
const scopeTests = scopeTestsJson as ScopeTestsJson;
2810

2911
interface Props {
3012
languageId: string;
3113
}
3214

3315
export function ScopeVisualizer({ languageId }: Props) {
34-
const [fixtures] = useState(
35-
scopeTests.filter((s) => s.languageId === languageId),
36-
);
16+
const [fixtures] = useState(getFixtures(languageId));
3717
const [rangeType, setRangeType] = useState<RangeType>("content");
3818
const [renderWhitespace, setRenderWhitespace] = useState(false);
3919

@@ -56,22 +36,25 @@ export function ScopeVisualizer({ languageId }: Props) {
5636
Render whitespace
5737
</label>
5838

59-
{fixtures.map((f) => renderFixture(f, rangeType, renderWhitespace))}
39+
{fixtures.map((f) =>
40+
renderFixture(languageId, rangeType, renderWhitespace, f),
41+
)}
6042
</div>
6143
);
6244
}
6345

6446
function renderFixture(
65-
fixture: Fixture,
47+
languageId: string,
6648
rangeType: RangeType,
6749
renderWhitespace: boolean,
50+
fixture: Fixture,
6851
) {
6952
const highlights = getHighlights(fixture, rangeType);
7053
return (
7154
<div key={fixture.name}>
7255
{fixture.facet}
7356
<Code
74-
languageId={fixture.languageId}
57+
languageId={languageId}
7558
renderWhitespace={renderWhitespace}
7659
highlights={highlights}
7760
>
@@ -154,3 +137,10 @@ function getOverlap(a: Range, b: Range): Range | null {
154137
? intersection
155138
: null;
156139
}
140+
141+
function getFixtures(languageId: string): Fixture[] {
142+
const languageIds = new Set<string>(
143+
scopeTests.imports[languageId] ?? [languageId],
144+
);
145+
return scopeTests.fixtures.filter((f) => languageIds.has(f.languageId));
146+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export interface ScopeTestsJson {
2+
imports: Record<string, string[]>;
3+
fixtures: Fixture[];
4+
}
5+
6+
export interface Fixture {
7+
name: string;
8+
facet: string;
9+
languageId: string;
10+
code: string;
11+
scopes: Scope[];
12+
}
13+
14+
export interface Scope {
15+
domain?: string;
16+
targets: Target[];
17+
}
18+
19+
export interface Target {
20+
content: string;
21+
removal?: string;
22+
insertionDelimiter?: string;
23+
}

packages/cursorless-org-docs/scripts/prepare-assets.ts renamed to packages/cursorless-org-docs/src/scripts/prepare-assets.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
import {
2+
getPackagePath,
3+
getScopeTestLanguagesRecursively,
24
getScopeTestPaths,
35
type ScopeTestPath,
4-
getPackagePath,
56
} from "@cursorless/node-common";
67
import * as fs from "node:fs";
78
import * as path from "node:path";
8-
9-
interface Fixture {
10-
name: string;
11-
facet: string;
12-
languageId: string;
13-
code: string;
14-
scopes: Scope[];
15-
}
16-
17-
interface Scope {
18-
domain?: string;
19-
targets: Target[];
20-
}
21-
22-
interface Target {
23-
content?: string;
24-
removal?: string;
25-
insertionDelimiter?: string;
26-
}
9+
import type {
10+
Fixture,
11+
Scope,
12+
ScopeTestsJson,
13+
Target,
14+
} from "../docs/user/languages/components/types";
2715

2816
const fixtures: Fixture[] = [];
2917

18+
const importedLanguages = getScopeTestLanguagesRecursively();
19+
3020
for (const test of getScopeTestPaths()) {
3121
const fixture = parseTest(test);
3222
if (fixture != null) {
3323
fixtures.push(fixture);
3424
}
3525
}
3626

37-
saveFixtures(fixtures);
27+
const result: ScopeTestsJson = {
28+
imports: importedLanguages,
29+
fixtures,
30+
};
31+
32+
saveJson(result);
3833

3934
function parseTest(test: ScopeTestPath) {
4035
const fixture = fs
@@ -54,7 +49,7 @@ function parseTest(test: ScopeTestPath) {
5449
const unprocessedTypes: string[] = [];
5550
let currentScopeIndex = "1";
5651
let currentTargetIndex = "1";
57-
let currentTarget: Target = {};
52+
let currentTarget: Target = { content: "" };
5853
let currentScope: Scope = { targets: [currentTarget] };
5954

6055
function processLine(type: string, value: string) {
@@ -107,11 +102,11 @@ function parseTest(test: ScopeTestPath) {
107102
scopes.push(currentScope);
108103
currentScopeIndex = scopeIndex;
109104
currentTargetIndex = "1";
110-
currentTarget = {};
105+
currentTarget = { content: "" };
111106
currentScope = { targets: [currentTarget] };
112107
} else if (targetIndex != null && targetIndex !== currentTargetIndex) {
113108
currentTargetIndex = targetIndex;
114-
currentTarget = {};
109+
currentTarget = { content: "" };
115110
currentScope.targets.push(currentTarget);
116111
}
117112

@@ -132,6 +127,13 @@ function parseTest(test: ScopeTestPath) {
132127

133128
scopes.push(currentScope);
134129

130+
if (scopes.some((s) => s.targets.some((t) => !t.content))) {
131+
throw Error(`Scope fixture ${test.path} contains targets without content.`);
132+
}
133+
if (scopes.some((s) => s.targets.length === 0)) {
134+
throw Error(`Scope fixture ${test.path} contains empty scopes.`);
135+
}
136+
135137
const result: Fixture = {
136138
name: test.name,
137139
languageId: test.languageId,
@@ -173,12 +175,12 @@ function parseLine(line: string) {
173175
return { scopeIndex, targetIndex, type, value };
174176
}
175177

176-
function saveFixtures(fixtures: Fixture[]) {
178+
function saveJson(result: ScopeTestsJson) {
177179
const assetPath = path.join(
178180
getPackagePath("cursorless-org-docs"),
179181
"static",
180182
"scopeTests.json",
181183
);
182-
const content = JSON.stringify(fixtures, null, 2) + "\n";
184+
const content = JSON.stringify(result, null, 2) + "\n";
183185
fs.writeFileSync(assetPath, content, "utf8");
184186
}

0 commit comments

Comments
 (0)