Skip to content

Commit e0495a0

Browse files
authored
refactor: code cleanup and simplification (#162)
1 parent 854da8f commit e0495a0

22 files changed

+215
-634
lines changed

docs/adding-languages.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Add an SVG logo into the `public/languages/` directory.
2020
1. Import the type for the new language options from `src/hooks/use-explorer.ts`.
2121
1. Add an entry in the `languages` variable for the new language.
2222
1. Export a new variable describing the available modes for the new language (such as `jsonModes` or `cssModes`).
23-
1. Default the default code for the new language (such as `defaultJsonCode` or `defaultCssCode`).
23+
1. Define the default code for the new language (such as `defaultJsonCode` or `defaultCssCode`).
2424
1. Add an entry in the `defaultCode` variable for the new language's default code.
2525
1. Export a variable containing the default options for the new language (such as `defaultJsonOptions` or `defaultCssOptions`).
2626
1. Add an entry to the `esquerySelectorPlaceholder` constant for the new language(must match Language enum), using an appropriate example selector.
@@ -32,32 +32,15 @@ Now import the default options for the new language from `src/lib/const.ts`.
3232
## Step 5: Update `src/components/options.tsx`
3333

3434
1. Import the new language mode type from `src/hooks/use-explorer` (such as `JsonMode` or `CssMode`).
35-
1. Import the available modes for the new language from `src/hooks/const` (such as `jsonModes` or `cssModes`).
35+
1. Import the available modes for the new language from `src/lib/const.ts` (such as `jsonModes` or `cssModes`).
3636
1. Create an options panel for the new language (such as `JsonPanel` or `CssPanel`).
3737
1. Update the `Panel` variable to use the new options panel.
3838

39-
## Step 6: Update `src/components/editor.ts`
39+
## Step 6: Update `src/components/editor.tsx`
4040

4141
1. Install the appropriate CodeMirror plugin for the new language.
4242
1. Update the `languageExtensions` variable to include the new language CodeMirror plugin.
4343

44-
## Step 7: Add AST components
44+
## Step 7: Wire the AST
4545

46-
In the `src/components/ast` directory, create two files:
47-
48-
1. `{new language}-ast.tsx`
49-
1. `{new language}-ast-tree-item.tsx`
50-
51-
Replace `{new language}` with the name of the language. You can copy existing files for other languages to get started.
52-
53-
In `{new language}-ast-tree-item.tsx`, update the name of the options type and the exported component to match the new language.
54-
55-
Next, install the ESLint language plugin for the new language.
56-
57-
In `{new language}-ast.tsx`:
58-
59-
1. Update language references to point to the new language.
60-
1. Import the ESLint language plugin to parse the code.
61-
1. Set the `defaultValue` in the accordion to the name of the root node for the new language AST.
62-
63-
Last, update `src/components/index.tsx` to import `{new language}-ast.tsx` and update `Ast` to include the new language.
46+
Add a parsing case in `src/hooks/use-ast.ts` for the new language that produces `{ ok: true, ast }`.

src/components/ast/css-ast-tree-item.tsx renamed to src/components/ast/ast-tree-item.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import { TreeEntry } from "../tree-entry";
77
import type { FC } from "react";
88
import { mergeClassNames } from "@/lib/utils";
99

10-
type ASTNode = {
10+
export type ASTNode = {
1111
readonly type: string;
1212
readonly [key: string]: unknown;
1313
};
1414

15-
export type CssAstTreeItemProperties = {
15+
export type ASTTreeItemProperties = {
1616
readonly index: number;
1717
readonly data: ASTNode;
1818
readonly esqueryMatchedNodes: ASTNode[];
1919
};
2020

21-
export const CssAstTreeItem: FC<CssAstTreeItemProperties> = ({
21+
export const ASTTreeItem: FC<ASTTreeItemProperties> = ({
2222
data,
2323
index,
2424
esqueryMatchedNodes,
Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,22 @@
1+
import type { FC } from "react";
12
import { useExplorer } from "@/hooks/use-explorer";
2-
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
33
import { astViewOptions } from "@/lib/const";
4-
import { mergeClassNames } from "@/lib/utils";
5-
import type { FC } from "react";
4+
import { ViewModeToggle } from "@/components/view-mode-toggle";
65

76
export const AstViewMode: FC = () => {
87
const { viewModes, setViewModes } = useExplorer();
98
const { astView } = viewModes;
109

11-
const handleValueChange = (value: string) => {
12-
if (!value) {
13-
return;
14-
}
15-
16-
setViewModes({ ...viewModes, astView: value as "tree" | "json" });
17-
};
18-
1910
return (
20-
<ToggleGroup
21-
type="single"
11+
<ViewModeToggle
2212
value={astView}
23-
onValueChange={handleValueChange}
24-
className="border rounded-md"
25-
>
26-
{astViewOptions.map(option => (
27-
<ToggleGroupItem
28-
key={option.value}
29-
value={option.value}
30-
className={mergeClassNames(
31-
"border -m-px flex items-center gap-1.5",
32-
option.value === astView
33-
? "!bg-background"
34-
: "border-transparent hover:bg-transparent text-muted-foreground",
35-
)}
36-
>
37-
<option.icon size={16} />
38-
{option.label}
39-
</ToggleGroupItem>
40-
))}
41-
</ToggleGroup>
13+
options={astViewOptions}
14+
onValueChange={value =>
15+
setViewModes({
16+
...viewModes,
17+
astView: value as "tree" | "json",
18+
})
19+
}
20+
/>
4221
);
4322
};

src/components/ast/css-ast.tsx

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/components/ast/html-ast-tree-item.tsx

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/components/ast/html-ast.tsx

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/components/ast/index.tsx

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
1-
import { useExplorer } from "@/hooks/use-explorer";
2-
import { JavascriptAst } from "./javascript-ast";
3-
import { JsonAst } from "./json-ast";
4-
import { CssAst } from "./css-ast";
5-
import { MarkdownAst } from "./markdown-ast";
6-
import { HtmlAst } from "./html-ast";
71
import type { FC } from "react";
2+
import { useAST } from "@/hooks/use-ast";
3+
import { useExplorer } from "@/hooks/use-explorer";
4+
import { Accordion } from "@/components/ui/accordion";
5+
import { Editor } from "@/components/editor";
6+
import { ErrorState } from "@/components/error-boundary";
7+
import { ASTTreeItem } from "@/components/ast/ast-tree-item";
8+
import type { ASTNode } from "@/components/ast/ast-tree-item";
9+
import { parseError } from "@/lib/parse-error";
810

9-
export const Ast: FC = () => {
10-
const { language } = useExplorer();
11+
export const AST: FC = () => {
12+
const result = useAST();
13+
const { viewModes, language } = useExplorer();
14+
const { astView } = viewModes;
1115

12-
switch (language) {
13-
case "markdown":
14-
return <MarkdownAst />;
15-
case "json":
16-
return <JsonAst />;
17-
case "css":
18-
return <CssAst />;
19-
case "html":
20-
return <HtmlAst />;
21-
default:
22-
return <JavascriptAst />;
16+
if (!result.ok) {
17+
const message = parseError(result.errors[0]);
18+
return <ErrorState message={message} />;
2319
}
20+
21+
const ast = JSON.stringify(result.ast, null, 2);
22+
23+
if (astView === "tree") {
24+
if (result.ast === null) {
25+
return null;
26+
}
27+
28+
return (
29+
<Accordion
30+
type="multiple"
31+
className="px-8 pb-4 font-mono space-y-3 min-w-max"
32+
key={`${language}`}
33+
defaultValue={[`0-${(result.ast as ASTNode).type}`]}
34+
>
35+
<ASTTreeItem
36+
data={result.ast as ASTNode}
37+
index={0}
38+
esqueryMatchedNodes={
39+
result.esqueryMatchedNodes as ASTNode[]
40+
}
41+
/>
42+
</Accordion>
43+
);
44+
}
45+
46+
return <Editor readOnly value={ast} />;
2447
};

src/components/ast/javascript-ast-tree-item.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)