@nbt-dev/components 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/README.md +10 -0
- package/TRADEMARKS.md +49 -0
- package/dist/chunk-3ZM6YOA4.js +704 -0
- package/dist/chunk-3ZM6YOA4.js.map +7 -0
- package/dist/chunk-7B2T5ZNG.js +467 -0
- package/dist/chunk-7B2T5ZNG.js.map +7 -0
- package/dist/chunk-S7VBQE6Y.js +636 -0
- package/dist/chunk-S7VBQE6Y.js.map +7 -0
- package/dist/chunk-UPEOXMLZ.js +625 -0
- package/dist/chunk-UPEOXMLZ.js.map +7 -0
- package/dist/core/auth.d.ts +13 -0
- package/dist/core/bulk-decoder.d.ts +13 -0
- package/dist/core/config.d.ts +10 -0
- package/dist/core/data-store.d.ts +20 -0
- package/dist/core/index.d.ts +9 -0
- package/dist/core/use-bulk-stream.d.ts +24 -0
- package/dist/core/use-cartridge-info.d.ts +14 -0
- package/dist/core/utils.d.ts +2 -0
- package/dist/editor/index.d.ts +7 -0
- package/dist/editor/index.js +16 -0
- package/dist/editor/index.js.map +7 -0
- package/dist/editor/lsp-client.d.ts +57 -0
- package/dist/editor/lsp-extensions.d.ts +4 -0
- package/dist/editor/nbt-editor.d.ts +13 -0
- package/dist/editor/nbt-language.d.ts +7 -0
- package/dist/generated/bulk-protocol.d.ts +36 -0
- package/dist/graph/diagram.d.ts +5 -0
- package/dist/graph/entity-graph-utils.d.ts +92 -0
- package/dist/graph/entity-node.d.ts +9 -0
- package/dist/graph/index.d.ts +5 -0
- package/dist/graph/index.js +19 -0
- package/dist/graph/index.js.map +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +134 -0
- package/dist/index.js.map +7 -0
- package/dist/styles.css +2 -0
- package/dist/table/data-table.d.ts +9 -0
- package/dist/table/index.d.ts +3 -0
- package/dist/table/index.js +11 -0
- package/dist/table/index.js.map +7 -0
- package/dist/table/value-popover.d.ts +18 -0
- package/package.json +77 -0
- package/src/core/auth.ts +100 -0
- package/src/core/bulk-decoder.ts +178 -0
- package/src/core/config.tsx +39 -0
- package/src/core/data-store.ts +113 -0
- package/src/core/index.ts +34 -0
- package/src/core/use-bulk-stream.ts +412 -0
- package/src/core/use-cartridge-info.ts +100 -0
- package/src/core/utils.ts +6 -0
- package/src/editor/index.ts +13 -0
- package/src/editor/lsp-client.ts +227 -0
- package/src/editor/lsp-extensions.ts +191 -0
- package/src/editor/nbt-editor.tsx +142 -0
- package/src/editor/nbt-language.ts +151 -0
- package/src/generated/bulk-protocol.ts +63 -0
- package/src/graph/diagram.tsx +296 -0
- package/src/graph/entity-graph-utils.ts +423 -0
- package/src/graph/entity-node.tsx +122 -0
- package/src/graph/index.ts +19 -0
- package/src/index.ts +7 -0
- package/src/styles.css +94 -0
- package/src/table/data-table.tsx +274 -0
- package/src/table/index.ts +5 -0
- package/src/table/value-popover.tsx +230 -0
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/editor/nbt-editor.tsx
|
|
4
|
+
import React from "react";
|
|
5
|
+
import { EditorState } from "@codemirror/state";
|
|
6
|
+
import {
|
|
7
|
+
EditorView as EditorView2,
|
|
8
|
+
keymap as keymap2,
|
|
9
|
+
lineNumbers,
|
|
10
|
+
drawSelection,
|
|
11
|
+
highlightActiveLine
|
|
12
|
+
} from "@codemirror/view";
|
|
13
|
+
import {
|
|
14
|
+
defaultKeymap,
|
|
15
|
+
history,
|
|
16
|
+
historyKeymap,
|
|
17
|
+
indentWithTab
|
|
18
|
+
} from "@codemirror/commands";
|
|
19
|
+
import {
|
|
20
|
+
syntaxHighlighting,
|
|
21
|
+
defaultHighlightStyle,
|
|
22
|
+
bracketMatching
|
|
23
|
+
} from "@codemirror/language";
|
|
24
|
+
import { lintGutter } from "@codemirror/lint";
|
|
25
|
+
import { searchKeymap } from "@codemirror/search";
|
|
26
|
+
import { completionKeymap, closeBrackets } from "@codemirror/autocomplete";
|
|
27
|
+
|
|
28
|
+
// src/editor/nbt-language.ts
|
|
29
|
+
import {
|
|
30
|
+
StreamLanguage,
|
|
31
|
+
LanguageSupport,
|
|
32
|
+
indentUnit
|
|
33
|
+
} from "@codemirror/language";
|
|
34
|
+
var KEYWORDS = /* @__PURE__ */ new Set([
|
|
35
|
+
"entity",
|
|
36
|
+
"enum",
|
|
37
|
+
"struct",
|
|
38
|
+
"const",
|
|
39
|
+
"export",
|
|
40
|
+
"extends",
|
|
41
|
+
"fn",
|
|
42
|
+
"on",
|
|
43
|
+
"action",
|
|
44
|
+
"activity",
|
|
45
|
+
"task",
|
|
46
|
+
"variant",
|
|
47
|
+
"workflow",
|
|
48
|
+
"command",
|
|
49
|
+
"middleware",
|
|
50
|
+
"schedule",
|
|
51
|
+
"every",
|
|
52
|
+
"jai",
|
|
53
|
+
"migration",
|
|
54
|
+
"test",
|
|
55
|
+
"mock",
|
|
56
|
+
"assert",
|
|
57
|
+
"cartridge",
|
|
58
|
+
"service",
|
|
59
|
+
"component",
|
|
60
|
+
"app",
|
|
61
|
+
"route",
|
|
62
|
+
"layout",
|
|
63
|
+
"import",
|
|
64
|
+
"from",
|
|
65
|
+
"if",
|
|
66
|
+
"elif",
|
|
67
|
+
"else",
|
|
68
|
+
"while",
|
|
69
|
+
"for",
|
|
70
|
+
"in",
|
|
71
|
+
"return",
|
|
72
|
+
"break",
|
|
73
|
+
"continue",
|
|
74
|
+
"delete",
|
|
75
|
+
"defer",
|
|
76
|
+
"print",
|
|
77
|
+
"sleep",
|
|
78
|
+
"fail"
|
|
79
|
+
]);
|
|
80
|
+
var TYPES = /* @__PURE__ */ new Set([
|
|
81
|
+
"string",
|
|
82
|
+
"bool",
|
|
83
|
+
"ulid",
|
|
84
|
+
"document",
|
|
85
|
+
"dict",
|
|
86
|
+
"blob",
|
|
87
|
+
"DateTime",
|
|
88
|
+
"u8",
|
|
89
|
+
"u16",
|
|
90
|
+
"u32",
|
|
91
|
+
"u64",
|
|
92
|
+
"s8",
|
|
93
|
+
"s16",
|
|
94
|
+
"s32",
|
|
95
|
+
"s64",
|
|
96
|
+
"int",
|
|
97
|
+
"integer",
|
|
98
|
+
"float",
|
|
99
|
+
"float64",
|
|
100
|
+
"f32",
|
|
101
|
+
"f64",
|
|
102
|
+
"double",
|
|
103
|
+
"time",
|
|
104
|
+
"bytes",
|
|
105
|
+
"any",
|
|
106
|
+
"name",
|
|
107
|
+
"ref"
|
|
108
|
+
]);
|
|
109
|
+
function eatSingleLineString(stream, quote) {
|
|
110
|
+
while (!stream.eol()) {
|
|
111
|
+
if (stream.next() === quote) break;
|
|
112
|
+
}
|
|
113
|
+
return "string";
|
|
114
|
+
}
|
|
115
|
+
var nbtParser = {
|
|
116
|
+
name: "nbt",
|
|
117
|
+
startState: () => ({ tripleString: false }),
|
|
118
|
+
token(stream, state) {
|
|
119
|
+
if (state.tripleString) {
|
|
120
|
+
while (!stream.eol()) {
|
|
121
|
+
if (stream.match('"""')) {
|
|
122
|
+
state.tripleString = false;
|
|
123
|
+
return "string";
|
|
124
|
+
}
|
|
125
|
+
stream.next();
|
|
126
|
+
}
|
|
127
|
+
return "string";
|
|
128
|
+
}
|
|
129
|
+
if (stream.eatSpace()) return null;
|
|
130
|
+
const ch = stream.peek();
|
|
131
|
+
if (ch == null) return null;
|
|
132
|
+
if (ch === "#") {
|
|
133
|
+
stream.skipToEnd();
|
|
134
|
+
return "comment";
|
|
135
|
+
}
|
|
136
|
+
if (stream.match('"""')) {
|
|
137
|
+
state.tripleString = true;
|
|
138
|
+
while (!stream.eol()) {
|
|
139
|
+
if (stream.match('"""')) {
|
|
140
|
+
state.tripleString = false;
|
|
141
|
+
return "string";
|
|
142
|
+
}
|
|
143
|
+
stream.next();
|
|
144
|
+
}
|
|
145
|
+
return "string";
|
|
146
|
+
}
|
|
147
|
+
if (ch === '"') {
|
|
148
|
+
stream.next();
|
|
149
|
+
return eatSingleLineString(stream, '"');
|
|
150
|
+
}
|
|
151
|
+
if (ch === "'") {
|
|
152
|
+
const before = stream.string.charAt(stream.pos - 1);
|
|
153
|
+
if (!/[A-Za-z0-9_]/.test(before)) {
|
|
154
|
+
stream.next();
|
|
155
|
+
return eatSingleLineString(stream, "'");
|
|
156
|
+
}
|
|
157
|
+
stream.next();
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
if (ch === "f" && stream.string.charAt(stream.pos + 1) === '"') {
|
|
161
|
+
stream.next();
|
|
162
|
+
stream.next();
|
|
163
|
+
return eatSingleLineString(stream, '"');
|
|
164
|
+
}
|
|
165
|
+
if (ch === "@") {
|
|
166
|
+
stream.next();
|
|
167
|
+
stream.eat("@");
|
|
168
|
+
stream.eatWhile(/[A-Za-z0-9_]/);
|
|
169
|
+
return "meta";
|
|
170
|
+
}
|
|
171
|
+
if (/\d/.test(ch)) {
|
|
172
|
+
stream.match(/^\d+(\.\d+)?/);
|
|
173
|
+
return "number";
|
|
174
|
+
}
|
|
175
|
+
if (/[A-Za-z_]/.test(ch)) {
|
|
176
|
+
stream.eatWhile(/[A-Za-z0-9_]/);
|
|
177
|
+
const word = stream.current();
|
|
178
|
+
if (word === "true" || word === "false") return "atom";
|
|
179
|
+
if (KEYWORDS.has(word)) return "keyword";
|
|
180
|
+
if (TYPES.has(word)) return "typeName";
|
|
181
|
+
if (/^[A-Z]/.test(word)) return "typeName";
|
|
182
|
+
return "variableName";
|
|
183
|
+
}
|
|
184
|
+
if (/[+\-*/=<>!&|?.,:;]/.test(ch)) {
|
|
185
|
+
stream.next();
|
|
186
|
+
stream.eatWhile(/[+\-*/=<>!&|.]/);
|
|
187
|
+
return "operator";
|
|
188
|
+
}
|
|
189
|
+
stream.next();
|
|
190
|
+
return null;
|
|
191
|
+
},
|
|
192
|
+
languageData: {
|
|
193
|
+
commentTokens: { line: "#" }
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
var nbtLanguage = StreamLanguage.define(nbtParser);
|
|
197
|
+
function nbtLanguageSupport() {
|
|
198
|
+
return new LanguageSupport(nbtLanguage, [indentUnit.of(" ")]);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/editor/lsp-extensions.ts
|
|
202
|
+
import {
|
|
203
|
+
ViewPlugin,
|
|
204
|
+
hoverTooltip,
|
|
205
|
+
keymap
|
|
206
|
+
} from "@codemirror/view";
|
|
207
|
+
import {
|
|
208
|
+
autocompletion
|
|
209
|
+
} from "@codemirror/autocomplete";
|
|
210
|
+
import { setDiagnostics } from "@codemirror/lint";
|
|
211
|
+
function toLspPos(doc, offset) {
|
|
212
|
+
const line = doc.lineAt(offset);
|
|
213
|
+
return { line: line.number - 1, character: offset - line.from };
|
|
214
|
+
}
|
|
215
|
+
function fromLspPos(doc, pos) {
|
|
216
|
+
const lineNo = Math.min(Math.max(pos.line + 1, 1), doc.lines);
|
|
217
|
+
const line = doc.line(lineNo);
|
|
218
|
+
return Math.min(line.from + Math.max(pos.character, 0), line.to);
|
|
219
|
+
}
|
|
220
|
+
function toCmDiagnostics(doc, diags) {
|
|
221
|
+
return diags.map((d) => {
|
|
222
|
+
const from = fromLspPos(doc, d.range.start);
|
|
223
|
+
let to = fromLspPos(doc, d.range.end);
|
|
224
|
+
if (to <= from) to = Math.min(from + 1, doc.length);
|
|
225
|
+
const severity = d.severity === 2 ? "warning" : d.severity === 3 || d.severity === 4 ? "info" : "error";
|
|
226
|
+
return { from, to, severity, message: d.message, source: d.source ?? "nbt" };
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
function completionType(kind) {
|
|
230
|
+
switch (kind) {
|
|
231
|
+
case 3:
|
|
232
|
+
return "function";
|
|
233
|
+
case 5:
|
|
234
|
+
return "property";
|
|
235
|
+
case 13:
|
|
236
|
+
return "enum";
|
|
237
|
+
case 14:
|
|
238
|
+
return "keyword";
|
|
239
|
+
case 22:
|
|
240
|
+
return "class";
|
|
241
|
+
default:
|
|
242
|
+
return "variable";
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function lspExtensions(client, uri, onGotoDefinition) {
|
|
246
|
+
const syncPlugin = ViewPlugin.fromClass(
|
|
247
|
+
class {
|
|
248
|
+
constructor(view) {
|
|
249
|
+
this.view = view;
|
|
250
|
+
this.timer = null;
|
|
251
|
+
client.didOpen(uri, view.state.doc.toString());
|
|
252
|
+
this.unsubscribe = client.onDiagnostics(uri, (diags) => {
|
|
253
|
+
const cm = toCmDiagnostics(this.view.state.doc, diags);
|
|
254
|
+
this.view.dispatch(setDiagnostics(this.view.state, cm));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
update(u) {
|
|
258
|
+
if (!u.docChanged) return;
|
|
259
|
+
if (this.timer) clearTimeout(this.timer);
|
|
260
|
+
this.timer = setTimeout(() => {
|
|
261
|
+
this.timer = null;
|
|
262
|
+
client.didChange(uri, this.view.state.doc.toString());
|
|
263
|
+
}, 200);
|
|
264
|
+
}
|
|
265
|
+
destroy() {
|
|
266
|
+
if (this.timer) {
|
|
267
|
+
clearTimeout(this.timer);
|
|
268
|
+
client.didChange(uri, this.view.state.doc.toString());
|
|
269
|
+
}
|
|
270
|
+
this.unsubscribe();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
);
|
|
274
|
+
const completionSource = async (ctx) => {
|
|
275
|
+
const word = ctx.matchBefore(/\w*/);
|
|
276
|
+
if (!word) return null;
|
|
277
|
+
if (word.from === word.to && !ctx.explicit) {
|
|
278
|
+
const before = ctx.state.doc.sliceString(Math.max(0, ctx.pos - 1), ctx.pos);
|
|
279
|
+
if (before !== "." && before !== ":" && before !== '"') return null;
|
|
280
|
+
}
|
|
281
|
+
client.didChange(uri, ctx.state.doc.toString());
|
|
282
|
+
let result;
|
|
283
|
+
try {
|
|
284
|
+
result = await client.completion(uri, toLspPos(ctx.state.doc, ctx.pos));
|
|
285
|
+
} catch {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const items = Array.isArray(result) ? result : result?.items ?? [];
|
|
289
|
+
if (items.length === 0) return null;
|
|
290
|
+
return {
|
|
291
|
+
from: word.from,
|
|
292
|
+
options: items.map((it) => ({
|
|
293
|
+
label: it.label,
|
|
294
|
+
type: completionType(it.kind),
|
|
295
|
+
detail: it.detail
|
|
296
|
+
}))
|
|
297
|
+
};
|
|
298
|
+
};
|
|
299
|
+
const hover = hoverTooltip(async (view, pos) => {
|
|
300
|
+
let result;
|
|
301
|
+
try {
|
|
302
|
+
result = await client.hover(uri, toLspPos(view.state.doc, pos));
|
|
303
|
+
} catch {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
const value = result?.contents?.value;
|
|
307
|
+
if (!value) return null;
|
|
308
|
+
return {
|
|
309
|
+
pos,
|
|
310
|
+
create: () => {
|
|
311
|
+
const dom = document.createElement("pre");
|
|
312
|
+
dom.className = "nbt-hover-tooltip";
|
|
313
|
+
dom.style.cssText = "margin:0;padding:6px 8px;max-width:480px;white-space:pre-wrap;font-size:11px;";
|
|
314
|
+
dom.textContent = value.replace(/```\w*\n?/g, "");
|
|
315
|
+
return { dom };
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
const gotoDef = keymap.of([
|
|
320
|
+
{
|
|
321
|
+
key: "F12",
|
|
322
|
+
run: (view) => {
|
|
323
|
+
const pos = toLspPos(view.state.doc, view.state.selection.main.head);
|
|
324
|
+
client.definition(uri, pos).then((result) => {
|
|
325
|
+
const loc = Array.isArray(result) ? result[0] ?? null : result;
|
|
326
|
+
if (!loc?.uri) return;
|
|
327
|
+
if (loc.uri === uri) {
|
|
328
|
+
const off = fromLspPos(view.state.doc, loc.range.start);
|
|
329
|
+
view.dispatch({
|
|
330
|
+
selection: { anchor: off },
|
|
331
|
+
scrollIntoView: true
|
|
332
|
+
});
|
|
333
|
+
} else {
|
|
334
|
+
onGotoDefinition?.(loc.uri, loc.range.start.line);
|
|
335
|
+
}
|
|
336
|
+
}).catch(() => {
|
|
337
|
+
});
|
|
338
|
+
return true;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
]);
|
|
342
|
+
return [
|
|
343
|
+
syncPlugin,
|
|
344
|
+
autocompletion({ override: [completionSource] }),
|
|
345
|
+
hover,
|
|
346
|
+
gotoDef
|
|
347
|
+
];
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/editor/nbt-editor.tsx
|
|
351
|
+
import { jsx } from "react/jsx-runtime";
|
|
352
|
+
var nbtTheme = EditorView2.theme(
|
|
353
|
+
{
|
|
354
|
+
"&": {
|
|
355
|
+
height: "100%",
|
|
356
|
+
fontSize: "12px",
|
|
357
|
+
backgroundColor: "var(--background)",
|
|
358
|
+
color: "var(--foreground)"
|
|
359
|
+
},
|
|
360
|
+
".cm-content": { fontFamily: "ui-monospace, monospace" },
|
|
361
|
+
".cm-gutters": {
|
|
362
|
+
backgroundColor: "var(--background)",
|
|
363
|
+
color: "var(--muted-foreground)",
|
|
364
|
+
borderRight: "1px solid var(--border)"
|
|
365
|
+
},
|
|
366
|
+
".cm-activeLine": { backgroundColor: "color-mix(in srgb, var(--accent) 35%, transparent)" },
|
|
367
|
+
"&.cm-focused": { outline: "none" },
|
|
368
|
+
".cm-tooltip": {
|
|
369
|
+
backgroundColor: "var(--popover)",
|
|
370
|
+
color: "var(--popover-foreground)",
|
|
371
|
+
border: "1px solid var(--border)"
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
{ dark: true }
|
|
375
|
+
);
|
|
376
|
+
var NbtEditor = ({
|
|
377
|
+
uri,
|
|
378
|
+
value,
|
|
379
|
+
readOnly = false,
|
|
380
|
+
lsp,
|
|
381
|
+
onChange,
|
|
382
|
+
onSave,
|
|
383
|
+
onGotoDefinition
|
|
384
|
+
}) => {
|
|
385
|
+
const hostRef = React.useRef(null);
|
|
386
|
+
const viewRef = React.useRef(null);
|
|
387
|
+
const onChangeRef = React.useRef(onChange);
|
|
388
|
+
onChangeRef.current = onChange;
|
|
389
|
+
const onSaveRef = React.useRef(onSave);
|
|
390
|
+
onSaveRef.current = onSave;
|
|
391
|
+
const onGotoRef = React.useRef(onGotoDefinition);
|
|
392
|
+
onGotoRef.current = onGotoDefinition;
|
|
393
|
+
React.useEffect(() => {
|
|
394
|
+
const host = hostRef.current;
|
|
395
|
+
if (!host) return;
|
|
396
|
+
const extensions = [
|
|
397
|
+
lineNumbers(),
|
|
398
|
+
history(),
|
|
399
|
+
drawSelection(),
|
|
400
|
+
highlightActiveLine(),
|
|
401
|
+
bracketMatching(),
|
|
402
|
+
closeBrackets(),
|
|
403
|
+
lintGutter(),
|
|
404
|
+
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
|
|
405
|
+
nbtLanguageSupport(),
|
|
406
|
+
nbtTheme,
|
|
407
|
+
keymap2.of([
|
|
408
|
+
{
|
|
409
|
+
key: "Mod-s",
|
|
410
|
+
run: (view2) => {
|
|
411
|
+
onSaveRef.current?.(view2.state.doc.toString());
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
...defaultKeymap,
|
|
416
|
+
...historyKeymap,
|
|
417
|
+
...searchKeymap,
|
|
418
|
+
...completionKeymap,
|
|
419
|
+
indentWithTab
|
|
420
|
+
]),
|
|
421
|
+
EditorView2.updateListener.of((u) => {
|
|
422
|
+
if (u.docChanged) onChangeRef.current?.(u.state.doc.toString());
|
|
423
|
+
}),
|
|
424
|
+
EditorState.readOnly.of(readOnly)
|
|
425
|
+
];
|
|
426
|
+
if (lsp && !readOnly) {
|
|
427
|
+
extensions.push(
|
|
428
|
+
lspExtensions(lsp, uri, (defUri, line) => onGotoRef.current?.(defUri, line))
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
const view = new EditorView2({
|
|
432
|
+
state: EditorState.create({ doc: value, extensions }),
|
|
433
|
+
parent: host
|
|
434
|
+
});
|
|
435
|
+
viewRef.current = view;
|
|
436
|
+
return () => {
|
|
437
|
+
view.destroy();
|
|
438
|
+
viewRef.current = null;
|
|
439
|
+
};
|
|
440
|
+
}, [uri, readOnly, lsp]);
|
|
441
|
+
return /* @__PURE__ */ jsx("div", { ref: hostRef, className: "h-full min-h-0 overflow-hidden" });
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// src/editor/lsp-client.ts
|
|
445
|
+
var NbtLspClient = class {
|
|
446
|
+
constructor(url) {
|
|
447
|
+
this.rootUri = null;
|
|
448
|
+
this.ws = null;
|
|
449
|
+
this.nextId = 1;
|
|
450
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
451
|
+
this.diagHandlers = /* @__PURE__ */ new Map();
|
|
452
|
+
// Open buffers, kept for replay across reconnects.
|
|
453
|
+
this.docs = /* @__PURE__ */ new Map();
|
|
454
|
+
this.ready = false;
|
|
455
|
+
this.queue = [];
|
|
456
|
+
this.closed = false;
|
|
457
|
+
this.retryMs = 500;
|
|
458
|
+
this.url = url;
|
|
459
|
+
this.connect();
|
|
460
|
+
}
|
|
461
|
+
dispose() {
|
|
462
|
+
this.closed = true;
|
|
463
|
+
this.ws?.close();
|
|
464
|
+
this.pending.forEach((p) => p.reject(new Error("lsp client disposed")));
|
|
465
|
+
this.pending.clear();
|
|
466
|
+
}
|
|
467
|
+
connect() {
|
|
468
|
+
if (this.closed) return;
|
|
469
|
+
const ws = new WebSocket(this.url);
|
|
470
|
+
this.ws = ws;
|
|
471
|
+
ws.onopen = () => {
|
|
472
|
+
this.retryMs = 500;
|
|
473
|
+
if (this.rootUri) this.sendInitialize(this.rootUri);
|
|
474
|
+
};
|
|
475
|
+
ws.onmessage = (ev) => {
|
|
476
|
+
if (typeof ev.data !== "string") return;
|
|
477
|
+
this.onMessage(ev.data);
|
|
478
|
+
};
|
|
479
|
+
ws.onclose = () => {
|
|
480
|
+
this.ready = false;
|
|
481
|
+
this.ws = null;
|
|
482
|
+
for (const p of this.pending.values())
|
|
483
|
+
p.reject(new Error("lsp connection closed"));
|
|
484
|
+
this.pending.clear();
|
|
485
|
+
if (!this.closed) {
|
|
486
|
+
setTimeout(() => this.connect(), this.retryMs);
|
|
487
|
+
this.retryMs = Math.min(this.retryMs * 2, 1e4);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
// Called once by the host with file://<projectRoot>; also re-sent after every
|
|
492
|
+
// reconnect, followed by didOpen replays for tracked buffers.
|
|
493
|
+
initialize(rootUri) {
|
|
494
|
+
this.rootUri = rootUri;
|
|
495
|
+
if (this.ws?.readyState === WebSocket.OPEN) this.sendInitialize(rootUri);
|
|
496
|
+
}
|
|
497
|
+
sendInitialize(rootUri) {
|
|
498
|
+
const id = this.nextId++;
|
|
499
|
+
this.pending.set(id, {
|
|
500
|
+
resolve: () => {
|
|
501
|
+
this.sendRaw({ jsonrpc: "2.0", method: "initialized", params: {} });
|
|
502
|
+
this.ready = true;
|
|
503
|
+
for (const [uri, d] of this.docs) {
|
|
504
|
+
this.sendRaw({
|
|
505
|
+
jsonrpc: "2.0",
|
|
506
|
+
method: "textDocument/didOpen",
|
|
507
|
+
params: {
|
|
508
|
+
textDocument: {
|
|
509
|
+
uri,
|
|
510
|
+
languageId: "nbt",
|
|
511
|
+
version: d.version,
|
|
512
|
+
text: d.text
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
const q = this.queue;
|
|
518
|
+
this.queue = [];
|
|
519
|
+
for (const m of q) this.ws?.send(m);
|
|
520
|
+
},
|
|
521
|
+
reject: () => {
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
this.ws?.send(
|
|
525
|
+
JSON.stringify({
|
|
526
|
+
jsonrpc: "2.0",
|
|
527
|
+
id,
|
|
528
|
+
method: "initialize",
|
|
529
|
+
params: { rootUri }
|
|
530
|
+
})
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
sendRaw(msg) {
|
|
534
|
+
const s = JSON.stringify(msg);
|
|
535
|
+
if (this.ready && this.ws?.readyState === WebSocket.OPEN) this.ws.send(s);
|
|
536
|
+
else this.queue.push(s);
|
|
537
|
+
}
|
|
538
|
+
onMessage(data) {
|
|
539
|
+
let msg;
|
|
540
|
+
try {
|
|
541
|
+
msg = JSON.parse(data);
|
|
542
|
+
} catch {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (msg.id != null && (msg.result !== void 0 || msg.error)) {
|
|
546
|
+
const p = this.pending.get(msg.id);
|
|
547
|
+
if (!p) return;
|
|
548
|
+
this.pending.delete(msg.id);
|
|
549
|
+
if (msg.error) p.reject(new Error(msg.error.message ?? "lsp error"));
|
|
550
|
+
else p.resolve(msg.result);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (msg.method === "textDocument/publishDiagnostics") {
|
|
554
|
+
const uri = msg.params?.uri;
|
|
555
|
+
const handler = this.diagHandlers.get(uri);
|
|
556
|
+
if (handler) handler(msg.params?.diagnostics ?? []);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
request(method, params) {
|
|
560
|
+
const id = this.nextId++;
|
|
561
|
+
const p = new Promise((resolve, reject) => {
|
|
562
|
+
this.pending.set(id, { resolve, reject });
|
|
563
|
+
});
|
|
564
|
+
this.sendRaw({ jsonrpc: "2.0", id, method, params });
|
|
565
|
+
return p;
|
|
566
|
+
}
|
|
567
|
+
onDiagnostics(uri, handler) {
|
|
568
|
+
this.diagHandlers.set(uri, handler);
|
|
569
|
+
return () => {
|
|
570
|
+
if (this.diagHandlers.get(uri) === handler) this.diagHandlers.delete(uri);
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
didOpen(uri, text) {
|
|
574
|
+
const existing = this.docs.get(uri);
|
|
575
|
+
if (existing) {
|
|
576
|
+
this.didChange(uri, text);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
this.docs.set(uri, { text, version: 1 });
|
|
580
|
+
this.sendRaw({
|
|
581
|
+
jsonrpc: "2.0",
|
|
582
|
+
method: "textDocument/didOpen",
|
|
583
|
+
params: { textDocument: { uri, languageId: "nbt", version: 1, text } }
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
didChange(uri, text) {
|
|
587
|
+
const d = this.docs.get(uri);
|
|
588
|
+
if (!d) return this.didOpen(uri, text);
|
|
589
|
+
if (d.text === text) return;
|
|
590
|
+
d.text = text;
|
|
591
|
+
d.version += 1;
|
|
592
|
+
this.sendRaw({
|
|
593
|
+
jsonrpc: "2.0",
|
|
594
|
+
method: "textDocument/didChange",
|
|
595
|
+
params: {
|
|
596
|
+
textDocument: { uri, version: d.version },
|
|
597
|
+
contentChanges: [{ text }]
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
didClose(uri) {
|
|
602
|
+
if (!this.docs.delete(uri)) return;
|
|
603
|
+
this.sendRaw({
|
|
604
|
+
jsonrpc: "2.0",
|
|
605
|
+
method: "textDocument/didClose",
|
|
606
|
+
params: { textDocument: { uri } }
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
completion(uri, pos) {
|
|
610
|
+
return this.request("textDocument/completion", {
|
|
611
|
+
textDocument: { uri },
|
|
612
|
+
position: pos
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
hover(uri, pos) {
|
|
616
|
+
return this.request("textDocument/hover", {
|
|
617
|
+
textDocument: { uri },
|
|
618
|
+
position: pos
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
definition(uri, pos) {
|
|
622
|
+
return this.request("textDocument/definition", {
|
|
623
|
+
textDocument: { uri },
|
|
624
|
+
position: pos
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
export {
|
|
630
|
+
nbtLanguage,
|
|
631
|
+
nbtLanguageSupport,
|
|
632
|
+
lspExtensions,
|
|
633
|
+
NbtEditor,
|
|
634
|
+
NbtLspClient
|
|
635
|
+
};
|
|
636
|
+
//# sourceMappingURL=chunk-S7VBQE6Y.js.map
|