generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathview.ts
179 lines (159 loc) · 4.22 KB
/
view.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
import { render } from "./render.ts";
import { update } from "./update.ts";
import { type Bindings } from "./bindings.ts";
import { assertUnreachable } from "../utils/assertUnreachable.ts";
import type { NvimBuffer } from "../nvim/buffer.ts";
import { type Position0Indexed } from "../nvim/window.ts";
import type { Nvim } from "nvim-node";
export function pos(row: number, col: number) {
return { row, col } as Position0Indexed;
}
export interface MountPoint {
nvim: Nvim;
buffer: NvimBuffer;
startPos: Position0Indexed;
endPos: Position0Indexed;
}
export type View<P> = (props: P) => VDOMNode;
export type StringVDOMNode = {
type: "string";
content: string;
bindings?: Bindings | undefined;
};
export type ComponentVDOMNode = {
type: "node";
children: VDOMNode[];
template: TemplateStringsArray;
bindings?: Bindings;
};
export type ArrayVDOMNode = {
type: "array";
children: VDOMNode[];
bindings?: Bindings;
};
export type VDOMNode = StringVDOMNode | ComponentVDOMNode | ArrayVDOMNode;
export type MountedStringNode = {
type: "string";
content: string;
startPos: Position0Indexed;
endPos: Position0Indexed;
bindings?: Bindings | undefined;
};
export type MountedComponentNode = {
type: "node";
template: TemplateStringsArray;
children: MountedVDOM[];
startPos: Position0Indexed;
endPos: Position0Indexed;
bindings?: Bindings | undefined;
};
export type MountedArrayNode = {
type: "array";
children: MountedVDOM[];
startPos: Position0Indexed;
endPos: Position0Indexed;
bindings?: Bindings | undefined;
};
export type MountedVDOM =
| MountedStringNode
| MountedComponentNode
| MountedArrayNode;
export function prettyPrintMountedNode(root: MountedVDOM) {
const stack: { node: MountedVDOM; indent: number }[] = [
{ node: root, indent: 0 },
];
const output: string[] = [];
while (stack.length) {
const { node, indent } = stack.pop()!;
let body = "";
switch (node.type) {
case "string":
body = JSON.stringify(node.content);
break;
case "node":
case "array": {
for (
let childIdx = node.children.length - 1;
childIdx >= 0;
childIdx--
) {
stack.push({ node: node.children[childIdx], indent: indent + 2 });
}
break;
}
default:
assertUnreachable(node);
}
const bindings = node.bindings
? `{${Object.keys(node.bindings).join(", ")}}`
: "";
output.push(
`${" ".repeat(indent)}${prettyPrintPos(node.startPos)}-${prettyPrintPos(node.endPos)} (${node.type}) ${bindings} ${body}`,
);
}
return output.join("\n");
}
function prettyPrintPos(pos: Position0Indexed) {
return `[${pos.row}, ${pos.col}]`;
}
export type MountedView<P> = {
render(props: P): Promise<void>;
unmount(): void;
/** for testing */
_getMountedNode(): MountedVDOM;
};
export async function mountView<P>({
view,
mount,
props,
}: {
view: View<P>;
mount: MountPoint;
props: P;
}): Promise<MountedView<P>> {
let mountedNode = await render({ vdom: view(props), mount });
return {
async render(props) {
const next = view(props);
mountedNode = await update({
currentRoot: mountedNode,
nextRoot: next,
mount,
});
},
unmount() {
// TODO
},
_getMountedNode: () => mountedNode,
};
}
export function d(
template: TemplateStringsArray,
...values: (VDOMNode[] | VDOMNode | string)[]
): VDOMNode {
const children: VDOMNode[] = [];
if (template[0].length) {
children.push({ type: "string", content: template[0] });
}
for (let i = 0; i < values.length; i++) {
if (typeof values[i] == "string") {
children.push({ type: "string", content: values[i] as string });
} else if (Array.isArray(values[i])) {
children.push({ type: "array", children: values[i] as VDOMNode[] });
} else {
children.push(values[i] as VDOMNode);
}
if (template[i + 1].length > 0) {
children.push({ type: "string", content: template[i + 1] });
}
}
return { type: "node", children: children, template: template };
}
/** Replace the bindings for this node
*/
export function withBindings(node: VDOMNode, bindings: Bindings) {
return {
...node,
bindings,
};
}