-
Notifications
You must be signed in to change notification settings - Fork 35
/
Tree.tsx
141 lines (134 loc) · 4.04 KB
/
Tree.tsx
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
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { FC, useState } from "react";
import * as ReactDOM from "react-dom/client";
import { preprocessAstNodeObject, PropertyNode, ValueNode } from "./preprocess.js";
import { clsx } from "clsx";
import { AstNode, AstNodeLocator } from "langium";
export let treeRoot: ReactDOM.Root;
export function render(root: AstNode, locator: AstNodeLocator) {
const location = document.getElementById("ast-body")!;
const data = preprocessAstNodeObject(root, locator);
if (!treeRoot) {
// create a fresh root, if not already present
treeRoot = ReactDOM.createRoot(location);
}
treeRoot.render(
<ul>
<TreeNode root={data} hidden={false} />
</ul>
);
}
interface TreeProps {
root: ValueNode;
hidden: boolean;
}
const TreeContent: FC<TreeProps> = ({ root, hidden }) => {
switch (root.kind) {
case "undefined":
return (
<span className="undefined">
undefined
</span>
);
case "boolean":
case "number":
case "string":
return (
<span className="literal">
{hidden
? "..."
: root.kind === "string"
? '"' + root.value + '"'
: root.value.toString()}
</span>
);
case "object":
return (
<>
<span className="object">
{hidden ? (
<span className="opening-brace">{...}</span>
) : (
<>
<div className="opening-brace">{</div>
<ul className="object-body">
{root.properties.map((p, index) => (
<Property
key={index}
p={p}
comma={index !== root.properties.length - 1}
/>
))}
</ul>
<span className="closing-brace">}</span>
</>
)}
</span>
</>
);
case "array":
if(root.children.length === 0) {
return <span className="opening-brace">{"[]"}</span>
}
if(hidden) {
return <span className="opening-brace">{"[...]"}</span>
}
return (
<>
<div className="opening-brace">[</div>
<ul className="object-body">
{root.children.map((c, index) => (
<li
key={index}
className={clsx("entry toggable", {
closed: hidden,
})}
>
<TreeContent root={c} hidden={false} />
{index !== root.children.length - 1 && (
<span className="comma">, </span>
)}
</li>
))}
</ul>
<span className="closing-brace inline">]</span>
</>
);
case "reference":
return (
<>
{hidden ? <span className="link">{"Reference(...)"}</span> :
<span className="link">Reference('{root.$text}')</span>}
</>
);
}
return <div>???</div>;
};
const TreeNode: FC<TreeProps> = ({ root, hidden }) => {
return (
<li className="inline entry">
<TreeContent root={root} hidden={hidden} />
</li>
);
};
function Property({ p, comma }: { p: PropertyNode; comma: boolean }) {
const [open, setOpen] = useState(true);
return (
<li className={clsx("entry toggable", { closed: !open })}>
<span className={"value"}>
<span className="property" onClick={() => setOpen((p) => !p)}>
{p.name}
</span>
<span className="colon">: </span>
<ul className="inline">
<TreeNode root={p.type} hidden={!open} />
</ul>
</span>
{comma && <span className="comma">, </span>}
</li>
);
}