@standarx/graph 1.0.0 → 1.1.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.
@@ -0,0 +1,3 @@
1
+ import type { Range } from "./format";
2
+ import type { CodeAction } from "./types";
3
+ export declare function codeActions(source: string, range: Range): CodeAction[];
@@ -0,0 +1,14 @@
1
+ import type { Range as Span } from "../text/errors";
2
+ import { type Item } from "./analysis";
3
+ import type { Diagnostic } from "./types";
4
+ export type Finding = {
5
+ diagnostic: Diagnostic;
6
+ span: Span;
7
+ suggestion?: {
8
+ item: Item;
9
+ target: Item;
10
+ mentions: Span[];
11
+ };
12
+ };
13
+ export declare function collect(source: string): Finding[];
14
+ export declare function diagnostics(source: string): Diagnostic[];
@@ -1,6 +1,8 @@
1
+ export { codeActions } from "./actions";
1
2
  export { complete } from "./complete";
3
+ export { diagnostics } from "./diagnostics";
2
4
  export { format, formatText } from "./format";
3
5
  export { hover } from "./hover";
4
6
  export { definition, prepareRename, references, rename } from "./navigation";
5
7
  export { documentSymbols, foldingRanges } from "./symbols";
6
- export type { CompletionItem, CompletionItemKind, DocumentSymbol, FoldingRange, Hover, Location, Position, Range, SymbolKind, TextEdit, WorkspaceEdit, } from "./types";
8
+ export type { CodeAction, CompletionItem, CompletionItemKind, Diagnostic, DocumentSymbol, FoldingRange, Hover, Location, Position, Range, SymbolKind, TextEdit, WorkspaceEdit, } from "./types";
@@ -11,14 +11,16 @@ import {
11
11
  OPERATOR_WORDS,
12
12
  OPERATOR_RULES,
13
13
  operatorOf,
14
- parseSyntaxPartial
15
- } from "../cli-njgyf690.js";
14
+ parseSyntaxPartial,
15
+ parse2
16
+ } from "../cli-nhmh6yxk.js";
16
17
  import {
17
18
  SHAPES2,
18
19
  DirectionSchema2,
19
20
  PSEUDO2,
20
21
  TREE_LAYOUTS2
21
- } from "../cli-kzp16pt5.js";
22
+ } from "../cli-m29a3wgx.js";
23
+ import"../cli-z3ccx2y4.js";
22
24
 
23
25
  // src/text/syntax.ts
24
26
  function parseTree(source) {
@@ -283,6 +285,177 @@ function occurrenceAt(analysis, offset) {
283
285
  return touching;
284
286
  }
285
287
 
288
+ // src/language/diagnostics.ts
289
+ function distance(a, b, max) {
290
+ const left = Array.from(a);
291
+ const right = Array.from(b);
292
+ if (Math.abs(left.length - right.length) > max)
293
+ return max + 1;
294
+ let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
295
+ for (let row = 1;row <= left.length; row++) {
296
+ const current = [row];
297
+ let low = row;
298
+ for (let column = 1;column <= right.length; column++) {
299
+ const cost = left[row - 1] === right[column - 1] ? 0 : 1;
300
+ const value = Math.min(previous[column] + 1, current[column - 1] + 1, previous[column - 1] + cost);
301
+ current.push(value);
302
+ if (value < low)
303
+ low = value;
304
+ }
305
+ if (low > max)
306
+ return max + 1;
307
+ previous = current;
308
+ }
309
+ return previous[right.length];
310
+ }
311
+ function closest(item, candidates) {
312
+ const limit = Math.min(2, Math.floor(item.id.length / 3));
313
+ if (limit < 1)
314
+ return;
315
+ let best;
316
+ let bestDistance = limit + 1;
317
+ for (const candidate of candidates) {
318
+ if (candidate.kind !== item.kind || !candidate.declared)
319
+ continue;
320
+ const value = distance(item.id, candidate.id, limit);
321
+ if (value < bestDistance) {
322
+ best = candidate;
323
+ bestDistance = value;
324
+ }
325
+ }
326
+ return best;
327
+ }
328
+ function collect(source) {
329
+ const analysis = analyze(source);
330
+ const { conv } = analysis;
331
+ const text = conv.text;
332
+ const starts = [0];
333
+ for (let index = 0;index < text.length; index++) {
334
+ if (text.charCodeAt(index) === 10)
335
+ starts.push(index + 1);
336
+ }
337
+ const at = (line, column) => Math.min((starts[line - 1] ?? text.length) + column - 1, text.length);
338
+ const range = (span) => ({ start: conv.position(span.start), end: conv.position(span.end) });
339
+ const findings = [];
340
+ const result = parse2(source);
341
+ if (!result.ok) {
342
+ for (const error of result.errors) {
343
+ const span = { start: at(error.line, error.column), end: at(error.endLine, error.endColumn) };
344
+ findings.push({
345
+ diagnostic: { range: range(span), severity: "error", code: error.code, message: error.message },
346
+ span
347
+ });
348
+ }
349
+ }
350
+ for (const item of analysis.items) {
351
+ if (item.declared)
352
+ continue;
353
+ const target = closest(item, analysis.items);
354
+ if (!target)
355
+ continue;
356
+ const mentions = analysis.occurrences.filter((occurrence) => occurrence.item === item).map((o) => o.range);
357
+ for (const span of mentions) {
358
+ findings.push({
359
+ diagnostic: {
360
+ range: range(span),
361
+ severity: "hint",
362
+ code: "H_SIMILAR_ID",
363
+ message: `"${item.id}" is never declared; did you mean "${target.id}"?`
364
+ },
365
+ span,
366
+ suggestion: { item, target, mentions }
367
+ });
368
+ }
369
+ }
370
+ const errors = findings.filter((finding) => finding.diagnostic.severity === "error");
371
+ const hints = findings.filter((finding) => finding.diagnostic.severity === "hint").sort((a, b) => a.span.start - b.span.start);
372
+ return [...errors, ...hints];
373
+ }
374
+ function diagnostics(source) {
375
+ return collect(source).map((finding) => finding.diagnostic);
376
+ }
377
+
378
+ // src/language/actions.ts
379
+ var BARE = new RegExp(`^(?:${IDENT.source})$`, "u");
380
+ var RESERVED = new Set(["to", "with", "async", "reply", "via", "initial", "final", "do", "end", "group"]);
381
+ function idText(id) {
382
+ if (BARE.test(id) && !RESERVED.has(id))
383
+ return id;
384
+ return `\`${id.replace(/[\`]/g, "$&")}\``;
385
+ }
386
+ function errorCount(text) {
387
+ const result = parse2(text);
388
+ return result.ok ? 0 : result.errors.length;
389
+ }
390
+ function apply(text, changes) {
391
+ let result = text;
392
+ for (const change of [...changes].sort((a, b) => b.start - a.start)) {
393
+ result = result.slice(0, change.start) + change.newText + result.slice(change.end);
394
+ }
395
+ return result;
396
+ }
397
+ function before(a, b) {
398
+ return a.line < b.line || a.line === b.line && a.character < b.character;
399
+ }
400
+ function touches(inner, outer) {
401
+ return !before(outer.end, inner.start) && !before(inner.end, outer.start);
402
+ }
403
+ function lineStart(text, offset) {
404
+ return text.lastIndexOf(`
405
+ `, offset - 1) + 1;
406
+ }
407
+ function removeLine(text, offset) {
408
+ const start = lineStart(text, offset);
409
+ const newline = text.indexOf(`
410
+ `, offset);
411
+ if (newline >= 0)
412
+ return { start, end: newline + 1 };
413
+ return { start: start > 0 ? start - 1 : start, end: text.length };
414
+ }
415
+ function codeActions(source, range) {
416
+ const analysis = analyze(source);
417
+ const text = analysis.conv.text;
418
+ const baseline = errorCount(text);
419
+ const actions = [];
420
+ const offer = (finding, title, changes) => {
421
+ if (errorCount(apply(text, changes)) > baseline)
422
+ return;
423
+ actions.push({
424
+ title,
425
+ kind: "quickfix",
426
+ diagnostic: finding.diagnostic,
427
+ edit: {
428
+ changes: changes.map((change) => ({
429
+ range: { start: analysis.conv.position(change.start), end: analysis.conv.position(change.end) },
430
+ newText: change.newText
431
+ }))
432
+ }
433
+ });
434
+ };
435
+ for (const finding of collect(source)) {
436
+ if (!touches(finding.diagnostic.range, range))
437
+ continue;
438
+ const { code } = finding.diagnostic;
439
+ const { span } = finding;
440
+ if (code === "E_STRING") {
441
+ const quote = text[span.start];
442
+ if (quote === undefined)
443
+ continue;
444
+ offer(finding, "Close the string", [{ start: span.end, end: span.end, newText: quote }]);
445
+ } else if (code === "E_DUPLICATE") {
446
+ offer(finding, "Remove the duplicate declaration", [{ ...removeLine(text, span.start), newText: "" }]);
447
+ } else if (code === "H_SIMILAR_ID" && finding.suggestion) {
448
+ const { target, mentions, item } = finding.suggestion;
449
+ offer(finding, `Replace with "${target.id}"`, mentions.map((mention) => ({ ...mention, newText: idText(target.id) })));
450
+ const first = mentions[0].start;
451
+ const start = lineStart(text, first);
452
+ const indent = /^[ \t]*/.exec(text.slice(start, first))[0];
453
+ offer(finding, `Declare "${item.id}"`, [{ start, end: start, newText: `${indent}${idText(item.id)}
454
+ ` }]);
455
+ }
456
+ }
457
+ return actions;
458
+ }
286
459
  // src/language/docs.ts
287
460
  var KIND_DOCS = {
288
461
  graph: "Graph diagram: nodes, groups and edges laid out automatically.",
@@ -1130,7 +1303,7 @@ function hover(source, position) {
1130
1303
  return;
1131
1304
  }
1132
1305
  // src/language/navigation.ts
1133
- var BARE = new RegExp(`^(?:${IDENT.source})$`, "u");
1306
+ var BARE2 = new RegExp(`^(?:${IDENT.source})$`, "u");
1134
1307
  function at(analysis, position) {
1135
1308
  return occurrenceAt(analysis, analysis.conv.offset(position));
1136
1309
  }
@@ -1161,7 +1334,7 @@ function prepareRename(source, position) {
1161
1334
  function rename(source, position, newName) {
1162
1335
  const analysis = analyze(source);
1163
1336
  const found = at(analysis, position);
1164
- if (!found || !BARE.test(newName))
1337
+ if (!found || !BARE2.test(newName))
1165
1338
  return;
1166
1339
  if (analysis.items.some((item) => item !== found.item && item.id === newName))
1167
1340
  return;
@@ -1372,8 +1545,10 @@ function foldingRanges(source) {
1372
1545
  return result.sort((a, b) => a.startLine - b.startLine || a.endLine - b.endLine);
1373
1546
  }
1374
1547
  export {
1548
+ codeActions,
1375
1549
  complete,
1376
1550
  definition,
1551
+ diagnostics,
1377
1552
  documentSymbols,
1378
1553
  foldingRanges,
1379
1554
  format,
@@ -32,3 +32,15 @@ export type CompletionItem = {
32
32
  documentation?: string;
33
33
  insertText?: string;
34
34
  };
35
+ export type Diagnostic = {
36
+ range: Range;
37
+ severity: "error" | "hint";
38
+ code: string;
39
+ message: string;
40
+ };
41
+ export type CodeAction = {
42
+ title: string;
43
+ kind: "quickfix";
44
+ diagnostic: Diagnostic;
45
+ edit: WorkspaceEdit;
46
+ };
@@ -1,15 +1,14 @@
1
1
  import {
2
2
  layout2
3
- } from "../cli-eqq7a3wz.js";
3
+ } from "../cli-abzdy8pn.js";
4
4
  import"../cli-txrzzr2q.js";
5
- import"../cli-xhdxp3cj.js";
6
- import"../cli-kzp16pt5.js";
5
+ import"../cli-m29a3wgx.js";
7
6
  import {
8
7
  measureText2
9
8
  } from "../cli-gh5166k6.js";
10
9
  import {
11
10
  VERSION2
12
- } from "../cli-nkm2j980.js";
11
+ } from "../cli-z3ccx2y4.js";
13
12
  export {
14
13
  VERSION2 as VERSION,
15
14
  layout2 as layout,