yuhwa 1.0.2 → 1.2.0
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/README.md +13 -12
- package/client.d.ts +9 -1
- package/dist/astro.d.ts +2 -2
- package/dist/astro.js +8 -1
- package/dist/browser.d.ts +11 -5
- package/dist/browser.js +27 -7
- package/dist/cli.js +612 -49
- package/dist/editor/assets/index-BphHLTvA.js +689 -0
- package/dist/editor/assets/lsp.worker-sZtzIA6-.js +30 -0
- package/dist/editor/index.html +5 -4
- package/dist/figure-CA2hy0xh.d.ts +48 -0
- package/dist/{index-Dl845tUQ.d.ts → index-CnnoZYl0.d.ts} +76 -7
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/ironmark.d.ts +4 -2
- package/dist/ironmark.js +18 -7
- package/dist/llms.txt +26 -17
- package/dist/lsp-browser.d.ts +2 -2
- package/dist/lsp-browser.js +1 -1
- package/dist/lsp-node.js +208 -212
- package/dist/lsp-server.d.ts +1 -1
- package/dist/lsp-server.js +1 -1
- package/dist/lsp.d.ts +44 -2
- package/dist/lsp.js +1 -1
- package/dist/markdown-it.d.ts +2 -1
- package/dist/markdown-it.js +7 -6
- package/dist/node-Bzn2uKXZ.js +224 -0
- package/dist/remark.d.ts +2 -1
- package/dist/remark.js +16 -5
- package/dist/{server-DJC_fQMe.d.ts → server--jHjJjOS.d.ts} +2 -2
- package/dist/{server-DmQuHQrG.js → server-Dxo7OG8K.js} +19 -2
- package/dist/{service-BYS9Qfkq.js → service-B1fpOg_L.js} +348 -44
- package/dist/{service-ClZ8clSQ.d.ts → service-Brvpt7Do.d.ts} +3 -1
- package/dist/{shared-qo_r6RUN.js → shared-Ddw88O9D.js} +3069 -742
- package/dist/{src-wk7tBpWs.js → src-DkQj7aIV.js} +2842 -744
- package/dist/style.css +14 -0
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +19 -12
- package/dist/vuepress.d.ts +3 -1
- package/dist/vuepress.js +7 -2
- package/dist/yuhwa.iife.js +91 -75
- package/dist/zoom-CCSUbX1X.d.ts +49 -0
- package/dist/zoom-QeqB6QlH.js +275 -0
- package/dist/zoom-script-CVdAzMhX.js +51 -0
- package/dist/zoom.d.ts +35 -0
- package/dist/zoom.js +19 -0
- package/package.json +10 -2
- package/dist/editor/assets/index-BQLsumas.js +0 -587
- package/dist/editor/assets/lsp.worker-RlPj9blG.js +0 -26
- package/dist/figure-BZHIKnxv.d.ts +0 -27
- package/dist/node-Bneu5eJV.js +0 -108
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Dt as TONES, Ft as themeNames, G as blockBraces, H as KNOWN_SETTINGS, I as optionKind, It as table, J as lex, Q as serializeProps, Tt as THEME_KEYS, U as PANELS, W as SETTING_VALUES, X as parse, _ as isMermaid, at as ENUMS, ct as NUMBERS, ft as SHAPES, gt as SPAN_KEYS, it as EDGE_PROPS, lt as PANEL_PROPS, nt as BOOLS, ot as GROUP_PROPS, q as joinPath, rt as COLORS, st as NODE_PROPS, ut as PLACEMENT_PROPS, vt as parseSpanStyle, xt as spanKey } from "./src-DkQj7aIV.js";
|
|
2
2
|
// src/lsp/format.ts
|
|
3
3
|
/**
|
|
4
4
|
* Formatting and text edits, without a parser of their own beyond `lex`.
|
|
@@ -10,6 +10,7 @@ import { B as KNOWN_SETTINGS, G as joinPath, H as SETTING_VALUES, J as parse, K
|
|
|
10
10
|
* as a whole and keep their inner indentation. Lines of a `def` body are moved
|
|
11
11
|
* the same way, since they may hold such bodies.
|
|
12
12
|
*/
|
|
13
|
+
const INDENT = " ";
|
|
13
14
|
const lead = (l) => l.match(/^\s*/)[0].length;
|
|
14
15
|
const leadTabs = (l) => l.match(/^[ \t]*/)[0].length;
|
|
15
16
|
/** line ranges `[open, close]` whose inner lines are kept as they are */
|
|
@@ -163,6 +164,180 @@ function applyChange(text, change) {
|
|
|
163
164
|
const b = Math.max(a, offsetAt(text, change.range.end));
|
|
164
165
|
return text.slice(0, a) + change.text + text.slice(b);
|
|
165
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* The indent for a line broken at `offset`: the current line's own, one step
|
|
169
|
+
* deeper when the text before the break opens a block. Indentation-shaped
|
|
170
|
+
* bodies (mind maps, tree views) keep their depth, so Enter starts a sibling.
|
|
171
|
+
*/
|
|
172
|
+
function newlineIndent(text, offset) {
|
|
173
|
+
const before = text.slice(text.lastIndexOf("\n", offset - 1) + 1, offset);
|
|
174
|
+
const own = before.match(/^[ \t]*/)[0];
|
|
175
|
+
let open = 0;
|
|
176
|
+
for (const b of blockBraces(lex(before).tokens)) open = Math.max(0, open + (b === "{" ? 1 : -1));
|
|
177
|
+
return open ? own + INDENT : own;
|
|
178
|
+
}
|
|
179
|
+
/** The indent for a `}` typed on the line holding `offset`: that of its opener. */
|
|
180
|
+
function closeIndent(text, offset) {
|
|
181
|
+
let depth = 1;
|
|
182
|
+
for (let end = text.lastIndexOf("\n", offset - 1); end >= 0;) {
|
|
183
|
+
const start = end > 0 ? text.lastIndexOf("\n", end - 1) + 1 : 0;
|
|
184
|
+
const line = text.slice(start, text[end - 1] === "\r" ? end - 1 : end);
|
|
185
|
+
for (const b of blockBraces(lex(line).tokens).reverse()) {
|
|
186
|
+
depth += b === "}" ? 1 : -1;
|
|
187
|
+
if (!depth) return line.match(/^[ \t]*/)[0];
|
|
188
|
+
}
|
|
189
|
+
end = start - 1;
|
|
190
|
+
}
|
|
191
|
+
return "";
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* What a line broken at `offset` starts with: its indent, plus the bullet of
|
|
195
|
+
* an outline item (`- ` or `* `), so a list in a mind map or tree view goes on.
|
|
196
|
+
*/
|
|
197
|
+
function newlinePrefix(text, offset) {
|
|
198
|
+
const indent = newlineIndent(text, offset);
|
|
199
|
+
const before = text.slice(text.lastIndexOf("\n", offset - 1) + 1, offset);
|
|
200
|
+
return indent + ((indent === before.match(/^[ \t]*/)[0] ? before.match(/^[ \t]*([-*] +)\S/)?.[1] : void 0) ?? "");
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Enter with the selection `from`..`to`: the new line keeps the depth (one step
|
|
204
|
+
* deeper after `{`) and continues a bullet. Enter on an empty bullet ends the
|
|
205
|
+
* list, and Enter between `{` and `}` puts the `}` on a line of its own.
|
|
206
|
+
*/
|
|
207
|
+
function enterEdit(text, from, to) {
|
|
208
|
+
const lineStart = text.lastIndexOf("\n", from - 1) + 1;
|
|
209
|
+
const before = text.slice(lineStart, from);
|
|
210
|
+
const own = before.match(/^[ \t]*/)[0];
|
|
211
|
+
const nl = text.indexOf("\n", to);
|
|
212
|
+
const rest = text.slice(to, nl < 0 ? text.length : nl);
|
|
213
|
+
if (/^[ \t]*[-*] *$/.test(before) && !rest.trim()) return {
|
|
214
|
+
start: lineStart,
|
|
215
|
+
end: to,
|
|
216
|
+
insert: own
|
|
217
|
+
};
|
|
218
|
+
const insert = "\n" + newlinePrefix(text, from);
|
|
219
|
+
if (newlineIndent(text, from) !== own && /^[ \t]*\}/.test(rest)) return {
|
|
220
|
+
start: from,
|
|
221
|
+
end: to,
|
|
222
|
+
insert: insert + "\n" + own,
|
|
223
|
+
caret: from + insert.length
|
|
224
|
+
};
|
|
225
|
+
return {
|
|
226
|
+
start: from,
|
|
227
|
+
end: to,
|
|
228
|
+
insert
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Tab and Shift+Tab: indent (or outdent by one step) every line the selection
|
|
233
|
+
* `from`..`to` touches, wherever the caret sits on it. Mind maps and tree
|
|
234
|
+
* views nest by indentation, so Tab on `- item` moves the item, not its text.
|
|
235
|
+
* Returns the new text and selection.
|
|
236
|
+
*/
|
|
237
|
+
function shiftLines(text, from, to, out = false) {
|
|
238
|
+
const start = text.lastIndexOf("\n", from - 1) + 1;
|
|
239
|
+
const last = to > from && text[to - 1] === "\n" ? to - 1 : to;
|
|
240
|
+
const nl = text.indexOf("\n", last);
|
|
241
|
+
const end = nl < 0 ? text.length : nl;
|
|
242
|
+
const lines = text.slice(start, end).split("\n");
|
|
243
|
+
const shifts = [];
|
|
244
|
+
const body = [];
|
|
245
|
+
let at = start;
|
|
246
|
+
for (const line of lines) {
|
|
247
|
+
const lead = line.match(/^ */)[0].length;
|
|
248
|
+
const delta = out ? -Math.min(2, lead) : lines.length > 1 && !line.trim() ? 0 : 2;
|
|
249
|
+
shifts.push([at, delta]);
|
|
250
|
+
body.push(delta > 0 ? INDENT + line : line.slice(-delta));
|
|
251
|
+
at += line.length + 1;
|
|
252
|
+
}
|
|
253
|
+
const map = (p, anchor) => {
|
|
254
|
+
let q = p;
|
|
255
|
+
for (const [s, d] of shifts) if (d > 0 && (p > s || p === s && !anchor)) q += d;
|
|
256
|
+
else if (d < 0 && p >= s) q -= Math.min(-d, p - s);
|
|
257
|
+
return q;
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
text: text.slice(0, start) + body.join("\n") + text.slice(end),
|
|
261
|
+
from: map(from, from < to),
|
|
262
|
+
to: map(to, false)
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Edits after a character was typed at `pos`: a newline re-indents the new
|
|
267
|
+
* line and continues a bullet, a `}` alone on its line lines up with its opener.
|
|
268
|
+
*/
|
|
269
|
+
function onTypeEdits(text, pos, ch) {
|
|
270
|
+
const start = offsetAt(text, {
|
|
271
|
+
line: pos.line,
|
|
272
|
+
character: 0
|
|
273
|
+
});
|
|
274
|
+
const nl = text.indexOf("\n", start);
|
|
275
|
+
const line = text.slice(start, nl < 0 ? text.length : nl).replace(/\r$/, "");
|
|
276
|
+
let end = line.match(/^[ \t]*/)[0].length;
|
|
277
|
+
let want;
|
|
278
|
+
if (ch === "\n" && pos.line > 0) {
|
|
279
|
+
want = newlinePrefix(text, start - (text[start - 2] === "\r" ? 2 : 1));
|
|
280
|
+
const bullet = line.slice(end).match(/^[-*] +/)?.[0];
|
|
281
|
+
if (bullet && want.endsWith(bullet)) end += bullet.length;
|
|
282
|
+
} else if (ch === "}" && /^[ \t]*\}$/.test(line.slice(0, pos.character))) want = closeIndent(text, offsetAt(text, pos));
|
|
283
|
+
if (want === void 0 || want === line.slice(0, end)) return [];
|
|
284
|
+
return [{
|
|
285
|
+
range: {
|
|
286
|
+
start: {
|
|
287
|
+
line: pos.line,
|
|
288
|
+
character: 0
|
|
289
|
+
},
|
|
290
|
+
end: {
|
|
291
|
+
line: pos.line,
|
|
292
|
+
character: end
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
newText: want
|
|
296
|
+
}];
|
|
297
|
+
}
|
|
298
|
+
// src/core/tidy.ts
|
|
299
|
+
/**
|
|
300
|
+
* Hygiene rules for `yuhwa lint`. The diagram renders fine without fixing
|
|
301
|
+
* these, so `check` stays quiet about them: unused classes, variables and
|
|
302
|
+
* components, and edges written twice. A file that draws nothing is a library
|
|
303
|
+
* for importers, so its definitions count as used.
|
|
304
|
+
*/
|
|
305
|
+
/** `toks`: per-line tokens the caller already has; an empty line entry is lexed here */
|
|
306
|
+
function tidy(source, d, toks) {
|
|
307
|
+
const out = [];
|
|
308
|
+
const warn = (code, line, message) => out.push({
|
|
309
|
+
line: line + 1,
|
|
310
|
+
message,
|
|
311
|
+
severity: "warning",
|
|
312
|
+
code
|
|
313
|
+
});
|
|
314
|
+
const library = !d.nodes.size && !d.edges.length && !d.panels.length;
|
|
315
|
+
const exported = new Set(d.exports ?? []);
|
|
316
|
+
const used = d.used ?? /* @__PURE__ */ new Set();
|
|
317
|
+
if (!library) {
|
|
318
|
+
const classes = Object.entries(d.classLines ?? {});
|
|
319
|
+
const applied = new Set(classes.length ? source.split(/\r?\n/).flatMap((l, li) => (toks?.[li]?.length ? toks[li] : lex(l).tokens).filter((t, i) => i && t.kind === "class").map((t) => t.value)) : []);
|
|
320
|
+
for (const [c, line] of classes) if (!exported.has("." + c) && !applied.has(c)) warn("unused-class", line, `Class .${c} is never used: apply it (node .${c}) or remove it`);
|
|
321
|
+
for (const [v, line] of Object.entries(d.varLines ?? {})) if (!exported.has("$" + v) && !used.has("$" + v)) warn("unused-variable", line, `Variable $${v} is never used: use it ($${v}) or remove it`);
|
|
322
|
+
for (const [f, line] of Object.entries(d.defLines ?? {})) if (!exported.has(f) && !used.has(f)) warn("unused-component", line, `Component ${f}() is never called: call it or remove it`);
|
|
323
|
+
}
|
|
324
|
+
const seen = /* @__PURE__ */ new Map();
|
|
325
|
+
for (const e of d.edges) {
|
|
326
|
+
if (!e.ref || e.ref.generated) continue;
|
|
327
|
+
const key = [
|
|
328
|
+
e.from,
|
|
329
|
+
e.to,
|
|
330
|
+
e.label ?? "",
|
|
331
|
+
e.line,
|
|
332
|
+
e.startHead,
|
|
333
|
+
e.endHead
|
|
334
|
+
].join("\0");
|
|
335
|
+
const first = seen.get(key);
|
|
336
|
+
if (first === void 0) seen.set(key, e.ref.line);
|
|
337
|
+
else if (first !== e.ref.line) warn("duplicate-edge", e.ref.line, `Edge ${e.from} -> ${e.to} is already drawn on line ${first + 1}: remove one`);
|
|
338
|
+
}
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
166
341
|
// src/lsp/service.ts
|
|
167
342
|
/**
|
|
168
343
|
* The yuhwa language service: plain functions from source text (and a
|
|
@@ -173,6 +348,8 @@ const SEVERITY = {
|
|
|
173
348
|
error: 1,
|
|
174
349
|
warning: 2
|
|
175
350
|
};
|
|
351
|
+
const HINT = 4;
|
|
352
|
+
const UNNECESSARY = 1;
|
|
176
353
|
const ITEM = {
|
|
177
354
|
Function: 3,
|
|
178
355
|
Variable: 6,
|
|
@@ -202,15 +379,6 @@ const HEAD_KEYWORDS = /* @__PURE__ */ new Set([
|
|
|
202
379
|
]);
|
|
203
380
|
/** `tone` is the old name of `color`: still read, never offered */
|
|
204
381
|
const DEPRECATED_PROPS = /* @__PURE__ */ new Set(["tone"]);
|
|
205
|
-
const COLOR_PROPS = /* @__PURE__ */ new Set([
|
|
206
|
-
"fill",
|
|
207
|
-
"stroke",
|
|
208
|
-
"color",
|
|
209
|
-
"text",
|
|
210
|
-
"sub-color",
|
|
211
|
-
"label-color",
|
|
212
|
-
"label-bg"
|
|
213
|
-
]);
|
|
214
382
|
const SIDES = [
|
|
215
383
|
"top",
|
|
216
384
|
"right",
|
|
@@ -218,13 +386,13 @@ const SIDES = [
|
|
|
218
386
|
"left",
|
|
219
387
|
"auto"
|
|
220
388
|
];
|
|
221
|
-
const ARROWS = {
|
|
389
|
+
const ARROWS = table({
|
|
222
390
|
"-": "solid line",
|
|
223
391
|
"--": "dashed line",
|
|
224
392
|
"..": "dotted line",
|
|
225
393
|
"=": "thick line",
|
|
226
394
|
"==": "thick line"
|
|
227
|
-
};
|
|
395
|
+
});
|
|
228
396
|
const ALL_PROPS = [.../* @__PURE__ */ new Set([
|
|
229
397
|
...NODE_PROPS,
|
|
230
398
|
...EDGE_PROPS,
|
|
@@ -254,6 +422,9 @@ const NAME_TYPE = {
|
|
|
254
422
|
panel: "type",
|
|
255
423
|
class: "class"
|
|
256
424
|
};
|
|
425
|
+
/** props whose value is the id of the node placed against */
|
|
426
|
+
const PLACEMENT = new Set(PLACEMENT_PROPS.filter((p) => p !== "rank"));
|
|
427
|
+
const PLACED = new RegExp(`(?:^\\[|[,;])\\s*(${[...PLACEMENT].join("|")})\\s*:\\s*([\\p{L}_][\\p{L}\\p{N}_-]*)(?=\\s*(?:[,;\\]]|$))`, "gu");
|
|
257
428
|
const LAYOUT_WORDS = /* @__PURE__ */ new Set([
|
|
258
429
|
"at",
|
|
259
430
|
"size",
|
|
@@ -277,6 +448,8 @@ const range = (line, start, end) => ({
|
|
|
277
448
|
end: pos(line, end)
|
|
278
449
|
});
|
|
279
450
|
const keyOf = (t) => t.kind === "class" ? "." + t.value : t.value;
|
|
451
|
+
/** a statement's tokens without a leading `export` */
|
|
452
|
+
const unexported = (t) => t[0]?.value === "export" && t[0].kind === "ident" && t.length > 1 ? t.slice(1) : t;
|
|
280
453
|
const isKeywordHead = (t) => t[0].kind === "ident" && (HEAD_KEYWORDS.has(t[0].value) || PANELS.has(t[0].value) && t[t.length - 1].value === "{") && !!t[1] && t[1].kind !== "arrow" && t[1].value !== ":" && t[1].value !== ",";
|
|
281
454
|
function settingValues(key, d) {
|
|
282
455
|
if (key === "theme") return themeNames(d?.themes);
|
|
@@ -297,26 +470,97 @@ function inThemeBlock(a, line) {
|
|
|
297
470
|
return t[t[0]?.value === "export" ? 1 : 0]?.value === "theme";
|
|
298
471
|
}
|
|
299
472
|
function propValues(key, panel, d) {
|
|
300
|
-
const
|
|
301
|
-
if (
|
|
473
|
+
const opt = panel ? PANELS.get(panel)?.options?.[key] : void 0;
|
|
474
|
+
if (opt !== void 0) {
|
|
475
|
+
const o = optionKind(opt);
|
|
476
|
+
switch (o.kind) {
|
|
477
|
+
case "select": return [...o.values];
|
|
478
|
+
case "flag": return ["true", "false"];
|
|
479
|
+
case "color": return TONES;
|
|
480
|
+
case "number":
|
|
481
|
+
case "text": return [];
|
|
482
|
+
default: return o;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
302
485
|
if (panel && key === "frame") return ["false"];
|
|
303
486
|
if (panel && key === "theme") return themeNames(d?.themes);
|
|
304
487
|
if (panel && key === "border") return ["none", ...TONES];
|
|
305
|
-
if (panel && (key === "background" || key === "fill" || key === "stroke")) return TONES;
|
|
306
488
|
if (ENUMS[key]) return ENUMS[key];
|
|
307
489
|
if (BOOLS.includes(key)) return ["true", "false"];
|
|
308
|
-
if (
|
|
490
|
+
if (COLORS.includes(key)) return TONES;
|
|
309
491
|
return [];
|
|
310
492
|
}
|
|
311
|
-
|
|
493
|
+
/** The open span spec (`{size: 18, red`) the cursor is typing, if any. */
|
|
494
|
+
function openSpan(before) {
|
|
495
|
+
const m = before.match(/\{([^{}|"\n]*)$/);
|
|
496
|
+
return m && /^[\w\s,:#.()%-]*$/.test(m[1]) ? m[1] : void 0;
|
|
497
|
+
}
|
|
498
|
+
/** The span spec (`{…|`) whose keys cover column `ch`, with its offset. */
|
|
499
|
+
function spanAt(line, ch) {
|
|
500
|
+
for (const m of line.matchAll(/\{([^{}|\n]+?)[ ]?\|/g)) {
|
|
501
|
+
const at = m.index + 1;
|
|
502
|
+
if (ch >= at && ch <= at + m[1].length && parseSpanStyle(m[1])) return {
|
|
503
|
+
spec: m[1],
|
|
504
|
+
at
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function propInfo(key, panel) {
|
|
509
|
+
const opt = panel ? PANELS.get(panel)?.options?.[key] : void 0;
|
|
510
|
+
if (opt !== void 0) {
|
|
511
|
+
const o = optionKind(opt);
|
|
512
|
+
if (o.kind === "select") return `one of: ${o.values.join(", ")}`;
|
|
513
|
+
if (o.kind === "flag") return "true or false";
|
|
514
|
+
if (o.kind === "number") return "a number";
|
|
515
|
+
if (o.kind === "color") return "a palette name or any CSS colour (#hex, rgb(), hsl())";
|
|
516
|
+
return "text";
|
|
517
|
+
}
|
|
518
|
+
if (panel && key === "frame") return "`false` drops the box";
|
|
519
|
+
if (panel && key === "theme") return "a theme name";
|
|
520
|
+
if (panel && key === "border") return "`none`, a palette name or any CSS colour";
|
|
312
521
|
const values = propValues(key);
|
|
313
522
|
if (BOOLS.includes(key)) return "true or false";
|
|
314
523
|
if (NUMBERS.includes(key)) return "a number";
|
|
315
524
|
if (ENUMS[key]) return `one of: ${values.join(", ")}`;
|
|
316
525
|
if (DEPRECATED_PROPS.has(key)) return "old name of `color`";
|
|
317
|
-
if (
|
|
526
|
+
if (COLORS.includes(key)) return "a palette name or any CSS colour (#hex, rgb(), hsl())";
|
|
318
527
|
return "text";
|
|
319
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* The declaration that `line` is indented under, whose props its `key: value`
|
|
531
|
+
* lines set (as `parse` does), or -1.
|
|
532
|
+
*/
|
|
533
|
+
function declAbove(a, line) {
|
|
534
|
+
const indent = (l) => a.lines[l].match(/^\s*/)[0].length;
|
|
535
|
+
for (let l = line - 1; l >= 0; l--) {
|
|
536
|
+
const t = a.toks[l];
|
|
537
|
+
if (!t.length || t[0].kind === "ident" && t[1]?.value === ":") continue;
|
|
538
|
+
return t[0].value !== "}" && indent(l) < indent(line) ? l : -1;
|
|
539
|
+
}
|
|
540
|
+
return -1;
|
|
541
|
+
}
|
|
542
|
+
/** Props an indented line under the declaration on `line` can set. */
|
|
543
|
+
function declProps(a, line) {
|
|
544
|
+
const t = unexported(a.toks[line]);
|
|
545
|
+
if (t[0].kind === "class") return ALL_PROPS;
|
|
546
|
+
if (t[0].value === "group") return GROUP_PROPS;
|
|
547
|
+
return t.some((x) => x.kind === "arrow") ? EDGE_PROPS : NODE_PROPS;
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Where a name quoted in a diagnostic sits on its line: a whole word, so `shap`
|
|
551
|
+
* is not found inside `shape`, and with `props` the one inside `[...]`, so the
|
|
552
|
+
* value in `red [shape: red]` wins over the node id.
|
|
553
|
+
*/
|
|
554
|
+
function findQuoted(code, q, props) {
|
|
555
|
+
const re = new RegExp(`(?<![\\w-])${q.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![\\w-])`, "g");
|
|
556
|
+
let first = -1;
|
|
557
|
+
for (const m of code.matchAll(re)) {
|
|
558
|
+
const depth = [...code.slice(0, m.index)].reduce((n, c) => n + (c === "[" ? 1 : c === "]" ? -1 : 0), 0);
|
|
559
|
+
if (props && depth > 0) return m.index;
|
|
560
|
+
if (first < 0) first = m.index;
|
|
561
|
+
}
|
|
562
|
+
return first >= 0 ? first : code.indexOf(q);
|
|
563
|
+
}
|
|
320
564
|
/** relative import path from one file to another */
|
|
321
565
|
function relPath(from, to) {
|
|
322
566
|
const a = (from ?? "").split("/").slice(0, -1);
|
|
@@ -344,7 +588,7 @@ var YuhwaService = class {
|
|
|
344
588
|
file,
|
|
345
589
|
resolve: this.opts.resolve
|
|
346
590
|
});
|
|
347
|
-
const lines = text.split(
|
|
591
|
+
const lines = text.split(/\r?\n/);
|
|
348
592
|
const opaque = lines.map(() => false);
|
|
349
593
|
const body = (r) => {
|
|
350
594
|
if (r) for (let i = r[0] + 1; i < r[1]; i++) opaque[i] = true;
|
|
@@ -397,12 +641,26 @@ var YuhwaService = class {
|
|
|
397
641
|
else if (x.kind === "punct" && x.value === ":" && arrow) ids = false;
|
|
398
642
|
if (x.kind === "class") add(li, x);
|
|
399
643
|
else if (ids && known(x) && !(k === 0 && skipHead)) add(li, x);
|
|
644
|
+
else if (x.kind === "props") {
|
|
645
|
+
const bare = lines[li].slice(x.start, x.end).replace(/(["'])(?:\\.|(?!\1).)*\1?/g, (q) => " ".repeat(q.length));
|
|
646
|
+
for (const m of bare.matchAll(PLACED)) {
|
|
647
|
+
const start = x.start + m.index + m[0].length - m[2].length;
|
|
648
|
+
if (kinds.get(m[2]) === "node") occ.push({
|
|
649
|
+
key: m[2],
|
|
650
|
+
line: li,
|
|
651
|
+
start,
|
|
652
|
+
end: start + m[2].length
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
}
|
|
400
656
|
}
|
|
657
|
+
if (PLACEMENT.has(head) && t[1]?.value === ":" && known(t[2]) && t.length === 3) add(li, t[2]);
|
|
401
658
|
}
|
|
402
659
|
const decl = /* @__PURE__ */ new Map();
|
|
660
|
+
const first = /* @__PURE__ */ new Map();
|
|
661
|
+
for (const x of occ) if (!first.has(`${x.line}\0${x.key}`)) first.set(`${x.line}\0${x.key}`, x);
|
|
403
662
|
const declare = (key, line) => {
|
|
404
|
-
|
|
405
|
-
const o = occ.find((x) => x.key === key && x.line === line);
|
|
663
|
+
const o = line === void 0 ? void 0 : first.get(`${line}\0${key}`);
|
|
406
664
|
if (o) decl.set(key, o);
|
|
407
665
|
};
|
|
408
666
|
for (const n of diagram.nodes.values()) declare(n.id, declLine(n));
|
|
@@ -434,15 +692,25 @@ var YuhwaService = class {
|
|
|
434
692
|
}
|
|
435
693
|
diagnostics(text, file) {
|
|
436
694
|
const a = this.analyze(text, file);
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
695
|
+
const at = (line) => Math.min(Math.max(line - 1, 0), a.lines.length - 1);
|
|
696
|
+
const found = a.diagram.diagnostics.map((d) => ({
|
|
697
|
+
range: this.narrow(a, at(d.line), d.message),
|
|
698
|
+
message: d.message,
|
|
699
|
+
severity: SEVERITY[d.severity],
|
|
700
|
+
source: "yuhwa"
|
|
701
|
+
}));
|
|
702
|
+
for (const d of tidy(text, a.diagram, a.toks)) {
|
|
703
|
+
const li = at(d.line);
|
|
704
|
+
const l = a.lines[li];
|
|
705
|
+
found.push({
|
|
706
|
+
range: range(li, l.length - l.trimStart().length, l.trimEnd().length),
|
|
441
707
|
message: d.message,
|
|
442
|
-
severity:
|
|
443
|
-
source: "yuhwa"
|
|
444
|
-
|
|
445
|
-
|
|
708
|
+
severity: HINT,
|
|
709
|
+
source: "yuhwa lint",
|
|
710
|
+
...d.code?.startsWith("unused-") ? { tags: [UNNECESSARY] } : {}
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
return found;
|
|
446
714
|
}
|
|
447
715
|
/** Messages name only a line; point at the quoted word they complain about when it is there. */
|
|
448
716
|
narrow(a, li, message) {
|
|
@@ -450,14 +718,14 @@ var YuhwaService = class {
|
|
|
450
718
|
const { codeEnd } = lex(line);
|
|
451
719
|
const start = Math.max(0, line.search(/\S/));
|
|
452
720
|
const end = Math.max(start, line.slice(0, codeEnd).trimEnd().length);
|
|
453
|
-
const quoted = /^\S+\.yuhwa:\d+:/i.test(message) ? void 0 : message.match(/"([^"\n]+)"/)?.[1];
|
|
454
|
-
const at = quoted ? line.
|
|
455
|
-
return at >= 0
|
|
721
|
+
const quoted = /^\S+\.(yu|yuhwa):\d+:/i.test(message) ? void 0 : message.match(/"([^"\n]+)"/)?.[1];
|
|
722
|
+
const at = quoted ? findQuoted(line.slice(0, codeEnd), quoted, /prop|unknown|expects|did you mean/i.test(message)) : -1;
|
|
723
|
+
return at >= 0 ? range(li, at, at + quoted.length) : range(li, start, end);
|
|
456
724
|
}
|
|
457
725
|
/** Quick fixes: apply a diagnostic's `did you mean "…"` suggestion. */
|
|
458
726
|
fixes(text, diagnostics) {
|
|
459
727
|
const out = [];
|
|
460
|
-
const lines = text.split(
|
|
728
|
+
const lines = text.split(/\r?\n/);
|
|
461
729
|
for (const d of diagnostics) {
|
|
462
730
|
const m = (typeof d.message === "string" ? d.message : d.message.value).match(/"([^"\n]+)".*\(did you mean "([^"\n]+)"\?\)/);
|
|
463
731
|
if (!m) continue;
|
|
@@ -495,8 +763,22 @@ var YuhwaService = class {
|
|
|
495
763
|
const values = (vs) => vs.map((v) => item(v, ITEM.Value));
|
|
496
764
|
const themeKeyItems = () => Object.entries(THEME_KEYS).map(([k, { info }]) => item(k, ITEM.Property, info, `${k}: `));
|
|
497
765
|
const nodes = () => [...d.nodes.values()].map((n) => item(n.id, ITEM.Variable, n.label !== n.id ? n.label : void 0));
|
|
498
|
-
|
|
766
|
+
const ends = () => [...nodes(), ...d.panels.map((x) => item(x.id, ITEM.Module, x.kind))];
|
|
499
767
|
const toks = lex(line).tokens;
|
|
768
|
+
const spec = openSpan(before);
|
|
769
|
+
const brace = before.length - (spec?.length ?? 0) - 1;
|
|
770
|
+
const inString = toks.some((t) => t.kind === "string" && t.start < brace && brace < t.end);
|
|
771
|
+
if (spec !== void 0 && (inString || a.opaque[p.line])) {
|
|
772
|
+
const seg = spec.split(",").pop();
|
|
773
|
+
const colon = seg.indexOf(":");
|
|
774
|
+
if (colon >= 0) {
|
|
775
|
+
const key = seg.slice(0, colon).trim().toLowerCase();
|
|
776
|
+
const k = spanKey(key);
|
|
777
|
+
return values(k ? SPAN_KEYS[k].values : []);
|
|
778
|
+
}
|
|
779
|
+
return Object.entries(SPAN_KEYS).map(([k, { info, flag }]) => item(k, ITEM.Property, info, flag ? k : `${k}: `));
|
|
780
|
+
}
|
|
781
|
+
if (a.opaque[p.line]) return [];
|
|
500
782
|
const tok = toks.find((t) => t.start < p.character && p.character < t.end);
|
|
501
783
|
const open = toks.find((t) => (t.kind === "props" || t.kind === "string") && t.start < p.character && p.character === t.end && t.end === line.length);
|
|
502
784
|
const inside = tok ?? (open && !/["'\]]$/.test(line.slice(open.start + 1)) ? open : void 0);
|
|
@@ -523,14 +805,15 @@ var YuhwaService = class {
|
|
|
523
805
|
}
|
|
524
806
|
if (kind === "theme") return themeKeyItems();
|
|
525
807
|
const keys = kind === "edge" ? EDGE_PROPS : kind === "group" ? GROUP_PROPS : kind === "class" ? ALL_PROPS : kind.startsWith("panel:") ? [...Object.keys(PANELS.get(kind.slice(6))?.options ?? {}), ...PANEL_PROPS] : NODE_PROPS;
|
|
526
|
-
|
|
808
|
+
const panel = kind.startsWith("panel:") ? kind.slice(6) : void 0;
|
|
809
|
+
return [...new Set(keys)].filter((k) => !DEPRECATED_PROPS.has(k)).map((k) => item(k, ITEM.Property, propInfo(k, panel), `${k}: `));
|
|
527
810
|
}
|
|
528
811
|
if (inside) return [];
|
|
529
812
|
if (/(^|[\s,:])\.$/.test(before.slice(0, wordStart))) return Object.entries(d.classes).map(([c, props]) => item(c, ITEM.Class, serializeProps(props) || void 0));
|
|
530
813
|
const kv = before.match(/^(\s*)(?:export\s+)?([\w-]+)\s*:\s*[\p{L}\p{N}_-]*$/u);
|
|
531
814
|
if (kv && !toks.some((t) => t.kind === "arrow")) {
|
|
532
815
|
const key = kv[2].toLowerCase();
|
|
533
|
-
if (
|
|
816
|
+
if (a.depth[p.line] === 0 && declAbove(a, p.line) < 0) return values(settingValues(key, d));
|
|
534
817
|
if (inThemeBlock(a, p.line)) return values(themeValues(key, d));
|
|
535
818
|
return values(propValues(key, void 0, d));
|
|
536
819
|
}
|
|
@@ -538,8 +821,7 @@ var YuhwaService = class {
|
|
|
538
821
|
const lineStart = !before.slice(0, wordStart).trim();
|
|
539
822
|
if (p.line > ls && p.line < le) {
|
|
540
823
|
if (lineStart) return [
|
|
541
|
-
...
|
|
542
|
-
...d.panels.map((x) => item(x.id, ITEM.Module, x.kind)),
|
|
824
|
+
...ends(),
|
|
543
825
|
...[...new Set(d.edges.map((e) => e.key))].map((k) => item(k, ITEM.Variable, "edge")),
|
|
544
826
|
item("rank", ITEM.Keyword)
|
|
545
827
|
];
|
|
@@ -556,8 +838,11 @@ var YuhwaService = class {
|
|
|
556
838
|
}
|
|
557
839
|
if (lineStart && inThemeBlock(a, p.line)) return themeKeyItems();
|
|
558
840
|
if (lineStart) {
|
|
559
|
-
const out =
|
|
560
|
-
|
|
841
|
+
const out = ends();
|
|
842
|
+
const decl = a.depth[p.line] === 0 ? declAbove(a, p.line) : -1;
|
|
843
|
+
if (decl >= 0) {
|
|
844
|
+
for (const k of new Set(declProps(a, decl))) if (!DEPRECATED_PROPS.has(k)) out.push(item(k, ITEM.Property, propInfo(k), `${k}: `));
|
|
845
|
+
} else if (a.depth[p.line] === 0) {
|
|
561
846
|
out.push(...[...KNOWN_SETTINGS].map((k) => item(k, ITEM.Property, "setting", `${k}: `)));
|
|
562
847
|
for (const k of HEAD_KEYWORDS) out.push(item(k, ITEM.Keyword));
|
|
563
848
|
for (const [k, kind] of PANELS) out.push(item(k, ITEM.Module, kind.syntax ?? "panel"));
|
|
@@ -572,10 +857,11 @@ var YuhwaService = class {
|
|
|
572
857
|
else if (t.kind === "punct" && t.value === ":" && arrow) return [];
|
|
573
858
|
}
|
|
574
859
|
if (toks[0]?.value === "def" || toks[0]?.value === "import") return [];
|
|
575
|
-
return
|
|
860
|
+
return ends();
|
|
576
861
|
}
|
|
577
862
|
/** what a `[...]` block on this line styles */
|
|
578
863
|
statementKind(toks, props) {
|
|
864
|
+
toks = unexported(toks);
|
|
579
865
|
const head = toks[0];
|
|
580
866
|
if (head.kind === "class") return "class";
|
|
581
867
|
if (head.value === "group") return "group";
|
|
@@ -625,6 +911,16 @@ var YuhwaService = class {
|
|
|
625
911
|
}
|
|
626
912
|
return md(`**class** \`${name}\` is not defined`, o);
|
|
627
913
|
}
|
|
914
|
+
const span = spanAt(a.lines[p.line] ?? "", p.character);
|
|
915
|
+
if (span) {
|
|
916
|
+
const st = parseSpanStyle(span.spec);
|
|
917
|
+
const parts = Object.entries(st).map(([k, v]) => `${k}: ${v}`);
|
|
918
|
+
const keys = Object.entries(SPAN_KEYS).map(([k, { info }]) => `- \`${k}\`: ${info}`).join("\n");
|
|
919
|
+
return md(`**style span** \`${parts.join(", ")}\`\n\nstyles the text after \`|\`. Keys:\n\n${keys}`, {
|
|
920
|
+
start: span.at,
|
|
921
|
+
end: span.at + span.spec.length
|
|
922
|
+
});
|
|
923
|
+
}
|
|
628
924
|
const t = this.tokAt(a, p);
|
|
629
925
|
if (!t) return null;
|
|
630
926
|
if (t.kind === "class") return md(`**class** \`.${t.value}\` is not defined`, t);
|
|
@@ -635,7 +931,7 @@ var YuhwaService = class {
|
|
|
635
931
|
return md(`**edge** \`${t.value}\`: ${ARROWS[m[2]]}, ${heads}`, t);
|
|
636
932
|
}
|
|
637
933
|
const toks = a.toks[p.line];
|
|
638
|
-
if (t === toks[0] && t.kind === "ident" && toks[1]?.value === ":" && a.depth[p.line] === 0) {
|
|
934
|
+
if (t === toks[0] && t.kind === "ident" && toks[1]?.value === ":" && a.depth[p.line] === 0 && declAbove(a, p.line) < 0) {
|
|
639
935
|
const key = t.value.toLowerCase();
|
|
640
936
|
if (!KNOWN_SETTINGS.has(key)) return null;
|
|
641
937
|
const vs = settingValues(key, a.diagram);
|
|
@@ -651,7 +947,10 @@ var YuhwaService = class {
|
|
|
651
947
|
if (p.character < at || p.character > at + part.length) continue;
|
|
652
948
|
const key = part.split(":")[0].trim().toLowerCase();
|
|
653
949
|
const start = at + part.indexOf(part.trim());
|
|
654
|
-
|
|
950
|
+
const kind = this.statementKind(toks, t);
|
|
951
|
+
const panel = kind.startsWith("panel:") ? kind.slice(6) : void 0;
|
|
952
|
+
const known = panel ? key in (PANELS.get(panel)?.options ?? {}) || PANEL_PROPS.includes(key) : ALL_PROPS.includes(key);
|
|
953
|
+
return key && known ? md(`**prop** \`${key}\`: ${propInfo(key, panel)}`, {
|
|
655
954
|
start,
|
|
656
955
|
end: start + key.length
|
|
657
956
|
}) : null;
|
|
@@ -821,6 +1120,7 @@ var YuhwaService = class {
|
|
|
821
1120
|
const name = isClass && !newName.startsWith(".") ? "." + newName : newName;
|
|
822
1121
|
const t = lex(name).tokens;
|
|
823
1122
|
if (t.length !== 1 || t[0].end !== name.length || t[0].kind !== (isClass ? "class" : "ident") || name[0] === "$") return { error: `"${newName}" is not a valid ${isClass ? "class name" : "id"}` };
|
|
1123
|
+
if (HEAD_KEYWORDS.has(name) || PANELS.has(name)) return { error: `"${name}" is a keyword` };
|
|
824
1124
|
if (name !== o.key && a.kinds.has(name)) return { error: `"${name}" already exists` };
|
|
825
1125
|
return a.occ.filter((x) => x.key === o.key).map((x) => ({
|
|
826
1126
|
range: range(x.line, x.start, x.end),
|
|
@@ -904,6 +1204,10 @@ var YuhwaService = class {
|
|
|
904
1204
|
format(text) {
|
|
905
1205
|
return diffEdits(text, format(text));
|
|
906
1206
|
}
|
|
1207
|
+
/** Re-indent after a newline or `}` was typed at `pos`. */
|
|
1208
|
+
onType(text, pos, ch) {
|
|
1209
|
+
return onTypeEdits(text, pos, ch);
|
|
1210
|
+
}
|
|
907
1211
|
folding(text, file) {
|
|
908
1212
|
const a = this.analyze(text, file);
|
|
909
1213
|
const out = a.blocks.filter(([s, e]) => e - 1 > s).map(([s, e]) => ({
|
|
@@ -927,4 +1231,4 @@ var YuhwaService = class {
|
|
|
927
1231
|
return out.sort((x, y) => x.startLine - y.startLine);
|
|
928
1232
|
}
|
|
929
1233
|
};
|
|
930
|
-
export {
|
|
1234
|
+
export { diffEdits as a, newlineIndent as c, onTypeEdits as d, positionAt as f, closeIndent as i, newlinePrefix as l, YuhwaService as n, enterEdit as o, shiftLines as p, applyChange as r, format as s, SEMANTIC_LEGEND as t, offsetAt as u };
|
|
@@ -2,7 +2,7 @@ import { A as Hover, C as DocumentHighlight, D as FoldingRange, H as Position, K
|
|
|
2
2
|
interface ServiceOptions {
|
|
3
3
|
/** Source of an imported file by its resolved path, as for `parse`; undefined when missing. */
|
|
4
4
|
resolve?: (path: string) => string | undefined;
|
|
5
|
-
/** Paths of the `.
|
|
5
|
+
/** Paths of the `.yu` files an import may name, for completing import paths. */
|
|
6
6
|
files?: () => string[];
|
|
7
7
|
}
|
|
8
8
|
/** Where a definition lives: `file` is set when it is another file than the one asked about. */
|
|
@@ -66,6 +66,8 @@ declare class YuhwaService {
|
|
|
66
66
|
symbols(text: string, file?: string): DocumentSymbol[];
|
|
67
67
|
/** Edits that format the document: indentation, line ends, blank lines. */
|
|
68
68
|
format(text: string): TextEdit[];
|
|
69
|
+
/** Re-indent after a newline or `}` was typed at `pos`. */
|
|
70
|
+
onType(text: string, pos: Position, ch: string): TextEdit[];
|
|
69
71
|
folding(text: string, file?: string): FoldingRange[];
|
|
70
72
|
}
|
|
71
73
|
export { YuhwaService as a, Target as i, SEMANTIC_LEGEND as n, ServiceOptions as r, Fix as t };
|