dot-language-support 1.7.0 → 2.0.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.
Files changed (111) hide show
  1. package/.github/workflows/CD.yml +3 -3
  2. package/.github/workflows/CI.yml +2 -2
  3. package/README.md +5 -4
  4. package/jest.config.mjs +19 -0
  5. package/lib/{binder.d.ts → cjs/binder.d.ts} +1 -1
  6. package/lib/{binder.js → cjs/binder.js} +34 -34
  7. package/lib/{checker.d.ts → cjs/checker.d.ts} +1 -1
  8. package/lib/{checker.js → cjs/checker.js} +28 -28
  9. package/lib/{core.d.ts → cjs/core.d.ts} +0 -0
  10. package/lib/{core.js → cjs/core.js} +0 -0
  11. package/lib/{error.d.ts → cjs/error.d.ts} +1 -1
  12. package/lib/{error.js → cjs/error.js} +0 -0
  13. package/lib/cjs/index.d.ts +5 -0
  14. package/lib/{index.js → cjs/index.js} +5 -5
  15. package/lib/{parser.d.ts → cjs/parser.d.ts} +1 -1
  16. package/lib/{parser.js → cjs/parser.js} +11 -11
  17. package/lib/{scanner.d.ts → cjs/scanner.d.ts} +1 -1
  18. package/lib/{scanner.js → cjs/scanner.js} +67 -67
  19. package/lib/{service → cjs/service}/codeAction.d.ts +2 -2
  20. package/lib/{service → cjs/service}/codeAction.js +34 -34
  21. package/lib/{service → cjs/service}/colorProvider.d.ts +3 -3
  22. package/lib/{service → cjs/service}/colorProvider.js +3 -3
  23. package/lib/{service → cjs/service}/command/ChangeAllOtherEdgeOpsAndFixGraphCommand.d.ts +3 -3
  24. package/lib/{service → cjs/service}/command/ChangeAllOtherEdgeOpsAndFixGraphCommand.js +6 -6
  25. package/lib/{service → cjs/service}/command/ChangeEdgeOpCommand.d.ts +3 -3
  26. package/lib/{service → cjs/service}/command/ChangeEdgeOpCommand.js +3 -3
  27. package/lib/{service → cjs/service}/command/ConsolidateDescendantsCommand.d.ts +3 -3
  28. package/lib/{service → cjs/service}/command/ConsolidateDescendantsCommand.js +7 -7
  29. package/lib/{service → cjs/service}/command/RemoveSemicolons.d.ts +3 -3
  30. package/lib/{service → cjs/service}/command/RemoveSemicolons.js +4 -4
  31. package/lib/{service → cjs/service}/command/common.d.ts +2 -2
  32. package/lib/{service → cjs/service}/command/common.js +6 -6
  33. package/lib/{service → cjs/service}/completion.d.ts +2 -2
  34. package/lib/{service → cjs/service}/completion.js +25 -25
  35. package/lib/{service → cjs/service}/hover.d.ts +2 -2
  36. package/lib/{service → cjs/service}/hover.js +38 -38
  37. package/lib/{service → cjs/service}/languageFacts.d.ts +0 -0
  38. package/lib/{service → cjs/service}/languageFacts.js +0 -0
  39. package/lib/{service → cjs/service}/polyfill.d.ts +0 -0
  40. package/lib/{service → cjs/service}/polyfill.js +0 -0
  41. package/lib/{service → cjs/service}/reference.d.ts +2 -2
  42. package/lib/{service → cjs/service}/reference.js +9 -9
  43. package/lib/{service → cjs/service}/rename.d.ts +2 -2
  44. package/lib/{service → cjs/service}/rename.js +11 -11
  45. package/lib/{service → cjs/service}/service.d.ts +2 -2
  46. package/lib/cjs/service/service.js +39 -0
  47. package/lib/{service → cjs/service}/util.d.ts +2 -2
  48. package/lib/{service → cjs/service}/util.js +5 -5
  49. package/lib/{service → cjs/service}/validation.d.ts +2 -2
  50. package/lib/{service → cjs/service}/validation.js +3 -3
  51. package/lib/cjs/tester.d.ts +1 -0
  52. package/lib/cjs/tester.js +23 -0
  53. package/lib/{types.d.ts → cjs/types.d.ts} +0 -0
  54. package/lib/{types.js → cjs/types.js} +0 -0
  55. package/lib/{visitor.d.ts → cjs/visitor.d.ts} +1 -1
  56. package/lib/{visitor.js → cjs/visitor.js} +17 -17
  57. package/lib/esm/binder.d.ts +2 -0
  58. package/lib/esm/binder.js +296 -0
  59. package/lib/esm/checker.d.ts +14 -0
  60. package/lib/esm/checker.js +169 -0
  61. package/lib/esm/core.d.ts +1 -0
  62. package/lib/esm/core.js +10 -0
  63. package/lib/esm/error.d.ts +3 -0
  64. package/lib/esm/error.js +10 -0
  65. package/lib/esm/index.d.ts +5 -0
  66. package/lib/esm/index.js +6 -0
  67. package/lib/esm/parser.d.ts +84 -0
  68. package/lib/esm/parser.js +700 -0
  69. package/lib/esm/scanner.d.ts +52 -0
  70. package/lib/esm/scanner.js +581 -0
  71. package/lib/esm/service/codeAction.d.ts +12 -0
  72. package/lib/esm/service/codeAction.js +208 -0
  73. package/lib/esm/service/colorProvider.d.ts +6 -0
  74. package/lib/esm/service/colorProvider.js +69 -0
  75. package/lib/esm/service/command/ChangeAllOtherEdgeOpsAndFixGraphCommand.d.ts +10 -0
  76. package/lib/esm/service/command/ChangeAllOtherEdgeOpsAndFixGraphCommand.js +38 -0
  77. package/lib/esm/service/command/ChangeEdgeOpCommand.d.ts +10 -0
  78. package/lib/esm/service/command/ChangeEdgeOpCommand.js +32 -0
  79. package/lib/esm/service/command/ConsolidateDescendantsCommand.d.ts +10 -0
  80. package/lib/esm/service/command/ConsolidateDescendantsCommand.js +83 -0
  81. package/lib/esm/service/command/RemoveSemicolons.d.ts +10 -0
  82. package/lib/esm/service/command/RemoveSemicolons.js +37 -0
  83. package/lib/esm/service/command/common.d.ts +31 -0
  84. package/lib/esm/service/command/common.js +22 -0
  85. package/lib/esm/service/completion.d.ts +4 -0
  86. package/lib/esm/service/completion.js +137 -0
  87. package/lib/esm/service/hover.d.ts +4 -0
  88. package/lib/esm/service/hover.js +119 -0
  89. package/lib/esm/service/languageFacts.d.ts +683 -0
  90. package/lib/esm/service/languageFacts.js +997 -0
  91. package/lib/esm/service/polyfill.d.ts +16 -0
  92. package/lib/esm/service/polyfill.js +2 -0
  93. package/lib/esm/service/reference.d.ts +5 -0
  94. package/lib/esm/service/reference.js +66 -0
  95. package/lib/esm/service/rename.d.ts +4 -0
  96. package/lib/esm/service/rename.js +45 -0
  97. package/lib/esm/service/service.d.ts +28 -0
  98. package/lib/esm/service/service.js +35 -0
  99. package/lib/esm/service/util.d.ts +11 -0
  100. package/lib/esm/service/util.js +44 -0
  101. package/lib/esm/service/validation.d.ts +4 -0
  102. package/lib/esm/service/validation.js +20 -0
  103. package/lib/esm/tester.d.ts +1 -0
  104. package/lib/esm/tester.js +21 -0
  105. package/lib/esm/types.d.ts +396 -0
  106. package/lib/esm/types.js +71 -0
  107. package/lib/esm/visitor.d.ts +2 -0
  108. package/lib/esm/visitor.js +74 -0
  109. package/package.json +13 -10
  110. package/lib/index.d.ts +0 -5
  111. package/lib/service/service.js +0 -39
@@ -0,0 +1,16 @@
1
+ import type { Range, TextEdit } from "vscode-languageserver-types";
2
+ export interface ColorInformation {
3
+ range: Range;
4
+ color: Color;
5
+ }
6
+ export interface Color {
7
+ readonly red: number;
8
+ readonly green: number;
9
+ readonly blue: number;
10
+ readonly alpha: number;
11
+ }
12
+ export interface ColorPresentation {
13
+ label: string;
14
+ textEdit?: TextEdit;
15
+ additionalTextEdits?: TextEdit[];
16
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=polyfill.js.map
@@ -0,0 +1,5 @@
1
+ import type { Position, ReferenceContext, Location } from "vscode-languageserver-types";
2
+ import type { SourceFile } from "../types.js";
3
+ import { type DocumentLike } from "../index.js";
4
+ export declare function findReferences(doc: DocumentLike, sourceFile: SourceFile, position: Position, context: ReferenceContext): Location[];
5
+ export declare function findDefinition(doc: DocumentLike, sourceFile: SourceFile, position: Position): Location | undefined;
@@ -0,0 +1,66 @@
1
+ import { isIdentifierNode } from "../index.js";
2
+ import { findNodeAtOffset } from "../checker.js";
3
+ import { syntaxNodesToRanges, syntaxNodeToRange } from "./util.js";
4
+ export function findReferences(doc, sourceFile, position, context) {
5
+ if (!sourceFile.symbols)
6
+ throw "sourceFile is not bound";
7
+ const g = sourceFile.graph;
8
+ if (!g)
9
+ return [];
10
+ const offset = doc.offsetAt(position);
11
+ const node = findNodeAtOffset(g, offset);
12
+ if (!node)
13
+ return [];
14
+ if (isIdentifierNode(node)) {
15
+ const nodeSymbol = node.symbol;
16
+ if (!nodeSymbol)
17
+ throw "node.symbol is not bound";
18
+ const refs = nodeSymbol.references || [];
19
+ let symbolRefs;
20
+ if (context.includeDeclaration) {
21
+ symbolRefs = [nodeSymbol.firstMention, ...refs];
22
+ }
23
+ else {
24
+ if (nodeSymbol.firstMention === node) {
25
+ symbolRefs = refs;
26
+ }
27
+ else {
28
+ symbolRefs = [
29
+ nodeSymbol.firstMention,
30
+ ...refs.filter(r => r !== node),
31
+ ];
32
+ }
33
+ }
34
+ const ranges = syntaxNodesToRanges(doc, sourceFile, symbolRefs);
35
+ const uri = doc.uri;
36
+ return ranges.map(range => {
37
+ return { uri, range };
38
+ });
39
+ }
40
+ debugger;
41
+ return [];
42
+ }
43
+ export function findDefinition(doc, sourceFile, position) {
44
+ if (!sourceFile.symbols)
45
+ throw "sourceFile is not bound";
46
+ const g = sourceFile.graph;
47
+ if (!g)
48
+ return undefined;
49
+ const offset = doc.offsetAt(position);
50
+ const node = findNodeAtOffset(g, offset);
51
+ if (!node)
52
+ return undefined;
53
+ if (isIdentifierNode(node)) {
54
+ const nodeSymbol = node.symbol;
55
+ if (!nodeSymbol)
56
+ throw "node.symbol is not bound";
57
+ const firstMention = nodeSymbol.firstMention;
58
+ if (!firstMention)
59
+ return undefined;
60
+ const range = syntaxNodeToRange(doc, sourceFile, firstMention);
61
+ return { uri: doc.uri, range };
62
+ }
63
+ debugger;
64
+ return undefined;
65
+ }
66
+ //# sourceMappingURL=reference.js.map
@@ -0,0 +1,4 @@
1
+ import { Position, WorkspaceEdit } from "vscode-languageserver-types";
2
+ import { SourceFile } from "../types.js";
3
+ import { DocumentLike } from "../index.js";
4
+ export declare function renameSymbol(doc: DocumentLike, sourceFile: SourceFile, position: Position, newName: string): WorkspaceEdit | undefined;
@@ -0,0 +1,45 @@
1
+ import { TextEdit } from "vscode-languageserver-types";
2
+ import { SyntaxKind } from "../types.js";
3
+ import { findNodeAtOffset } from "../checker.js";
4
+ import { isIdentifierNode } from "../index.js";
5
+ import { syntaxNodesToRanges } from "./util.js";
6
+ export function renameSymbol(doc, sourceFile, position, newName) {
7
+ if (!sourceFile.symbols)
8
+ throw "sourceFile is not bound";
9
+ if (!newName)
10
+ return undefined;
11
+ const g = sourceFile.graph;
12
+ if (!g)
13
+ return undefined;
14
+ const offset = doc.offsetAt(position);
15
+ const node = findNodeAtOffset(g, offset);
16
+ if (!node)
17
+ return undefined;
18
+ const parent = node.parent;
19
+ if (isIdentifierNode(node) && isRenamableIdentifier(node) && !!parent && isRenameableNode(parent)) {
20
+ const nodeSymbol = node.symbol;
21
+ if (!nodeSymbol)
22
+ throw "node.symbol is not bound";
23
+ const r = nodeSymbol.references;
24
+ const refs = r ? [nodeSymbol.firstMention, ...r] : [nodeSymbol.firstMention];
25
+ const ranges = syntaxNodesToRanges(doc, sourceFile, refs);
26
+ const uri = doc.uri;
27
+ const res = {
28
+ changes: {
29
+ [uri]: ranges.map(r => TextEdit.replace(r, newName)),
30
+ }
31
+ };
32
+ return res;
33
+ }
34
+ debugger;
35
+ return undefined;
36
+ }
37
+ function isRenameableNode(node) {
38
+ return node.kind === SyntaxKind.NodeId
39
+ || node.kind === SyntaxKind.DirectedGraph
40
+ || node.kind === SyntaxKind.UndirectedGraph;
41
+ }
42
+ function isRenamableIdentifier(node) {
43
+ return node.kind !== SyntaxKind.QuotedTextIdentifier;
44
+ }
45
+ //# sourceMappingURL=rename.js.map
@@ -0,0 +1,28 @@
1
+ import type * as lst from "vscode-languageserver-types";
2
+ import type { TextDocument } from "vscode-languageserver-textdocument";
3
+ import { ColorInformation, Color, ColorPresentation } from "./polyfill.js";
4
+ import { SourceFile, Omit } from "../types.js";
5
+ export interface DocumentLike {
6
+ positionAt(offset: number): lst.Position;
7
+ offsetAt(position: lst.Position): number;
8
+ readonly uri: string;
9
+ }
10
+ export interface CommandApplication {
11
+ label?: string;
12
+ edit: lst.WorkspaceEdit;
13
+ }
14
+ export interface LanguageService {
15
+ parseDocument(doc: TextDocument | string): SourceFile;
16
+ validateDocument(doc: DocumentLike, sourceFile: SourceFile): lst.Diagnostic[];
17
+ hover(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.Hover | undefined;
18
+ findReferences(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position, context: lst.ReferenceContext): lst.Location[];
19
+ findDefinition(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.Location | undefined;
20
+ renameSymbol(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position, newName: string): lst.WorkspaceEdit | undefined;
21
+ getCompletions(doc: DocumentLike, sourceFile: SourceFile, position: lst.Position): lst.CompletionItem[];
22
+ getDocumentColors(doc: DocumentLike, sourceFile: SourceFile): ColorInformation[] | undefined;
23
+ getColorRepresentations(doc: DocumentLike, sourceFile: SourceFile, color: Color, range: lst.Range): ColorPresentation[] | undefined;
24
+ getCodeActions(doc: DocumentLike, sourceFile: SourceFile, range: lst.Range, context: lst.CodeActionContext): lst.Command[] | undefined;
25
+ executeCommand(doc: DocumentLike, sourceFile: SourceFile, command: Omit<lst.Command, "title">): CommandApplication | undefined;
26
+ getAvailableCommands(): string[];
27
+ }
28
+ export declare function createService(): LanguageService;
@@ -0,0 +1,35 @@
1
+ import { Parser } from "../index.js";
2
+ import { bindSourceFile } from "../binder.js";
3
+ import { hover } from "./hover.js";
4
+ import { validateDocument } from "./validation.js";
5
+ import { findReferences, findDefinition } from "./reference.js";
6
+ import { renameSymbol } from "./rename.js";
7
+ import { getCompletions } from "./completion.js";
8
+ import { checkSourceFile } from "../checker.js";
9
+ import { getCodeActions, executeCommand, getAvailableCommands } from "./codeAction.js";
10
+ import { getDocumentColors, getColorRepresentations } from "./colorProvider.js";
11
+ function parseDocument(doc) {
12
+ const parser = new Parser();
13
+ const content = typeof doc === "string" ? doc : doc.getText();
14
+ const sourceFile = parser.parse(content);
15
+ bindSourceFile(sourceFile);
16
+ checkSourceFile(sourceFile);
17
+ return sourceFile;
18
+ }
19
+ export function createService() {
20
+ return {
21
+ parseDocument,
22
+ validateDocument,
23
+ hover,
24
+ findReferences,
25
+ findDefinition,
26
+ renameSymbol,
27
+ getCompletions,
28
+ getDocumentColors,
29
+ getColorRepresentations,
30
+ getCodeActions,
31
+ executeCommand,
32
+ getAvailableCommands,
33
+ };
34
+ }
35
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1,11 @@
1
+ import type { Range } from "vscode-languageserver-types";
2
+ import { SourceFile, SyntaxNode } from "../types.js";
3
+ import type { DocumentLike } from "../index.js";
4
+ export declare function getStart(sourceFile: SourceFile, node: SyntaxNode): number;
5
+ export declare function syntaxNodesToRanges(doc: DocumentLike, sourceFile: SourceFile, nodes: SyntaxNode[]): Range[];
6
+ export declare function syntaxNodeToRange(doc: DocumentLike, sourceFile: SourceFile, node: SyntaxNode): {
7
+ start: import("vscode-languageserver-types").Position;
8
+ end: import("vscode-languageserver-types").Position;
9
+ };
10
+ export declare function escapeIdentifierText(text: string): string;
11
+ export declare function assertNever(v: never): never;
@@ -0,0 +1,44 @@
1
+ import { SyntaxKind } from "../types.js";
2
+ import { skipTrivia, isIdentifierStart } from "../scanner.js";
3
+ export function getStart(sourceFile, node) {
4
+ return getTokenPosOfNode(sourceFile, node);
5
+ }
6
+ function getTokenPosOfNode(sourceFile, node) {
7
+ if (nodeIsMissing(node))
8
+ return node.pos;
9
+ return skipTrivia(sourceFile.content, node.pos);
10
+ }
11
+ function nodeIsMissing(node) {
12
+ return node === undefined
13
+ ? true
14
+ : node.pos === node.end && node.pos >= 0 && node.kind !== SyntaxKind.EndOfFileToken;
15
+ }
16
+ export function syntaxNodesToRanges(doc, sourceFile, nodes) {
17
+ return nodes.map(node => syntaxNodeToRange(doc, sourceFile, node));
18
+ }
19
+ export function syntaxNodeToRange(doc, sourceFile, node) {
20
+ const start = getStart(sourceFile, node);
21
+ return {
22
+ start: doc.positionAt(start),
23
+ end: doc.positionAt(node.end),
24
+ };
25
+ }
26
+ export function escapeIdentifierText(text) {
27
+ if (text === "")
28
+ return quote("");
29
+ if (text.includes("\"") || text.includes("\n")) {
30
+ const esc = text
31
+ .replace(/"/, "\\\"")
32
+ .replace(/\n/, "\\\n");
33
+ return quote(esc);
34
+ }
35
+ const ch = text.charCodeAt(0);
36
+ if (!isIdentifierStart(ch) || text.includes(" "))
37
+ return quote(text);
38
+ return text;
39
+ }
40
+ const quote = (s) => "\"" + s + "\"";
41
+ export function assertNever(v) {
42
+ throw new Error("Should not have reached this. Value: " + (v ?? ""));
43
+ }
44
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1,4 @@
1
+ import * as lst from "vscode-languageserver-types";
2
+ import { SourceFile } from "../types.js";
3
+ import { DocumentLike } from "../index.js";
4
+ export declare function validateDocument(doc: DocumentLike, sourceFile: SourceFile): lst.Diagnostic[];
@@ -0,0 +1,20 @@
1
+ import { formatError, diagnosicSource } from "../error.js";
2
+ function convertDiagnostic(document, source) {
3
+ return {
4
+ range: {
5
+ start: document.positionAt(source.start),
6
+ end: document.positionAt(source.end),
7
+ },
8
+ severity: source.category,
9
+ code: formatError(source.code),
10
+ source: diagnosicSource,
11
+ message: source.message,
12
+ };
13
+ }
14
+ export function validateDocument(doc, sourceFile) {
15
+ const diagnostics = sourceFile.diagnostics;
16
+ if (!diagnostics || diagnostics.length <= 0)
17
+ return [];
18
+ return diagnostics.map(d => convertDiagnostic(doc, d));
19
+ }
20
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import { TextDocument } from "vscode-languageserver-textdocument";
2
+ import { Parser } from "./parser.js";
3
+ import { bindSourceFile } from "./binder.js";
4
+ import { checkSourceFile } from "./checker.js";
5
+ import { getCompletions } from "./service/completion.js";
6
+ const text = `graph {
7
+ node_name_a -- node_name_b [color=blue,
8
+ ];
9
+ }`;
10
+ function main() {
11
+ const parser = new Parser();
12
+ const sf = parser.parse(text);
13
+ bindSourceFile(sf);
14
+ checkSourceFile(sf);
15
+ const doc = TextDocument.create("inmemory://model.json", "DOT", 0, text);
16
+ const requestOffset = text.indexOf("color=blue,\n\t") + "color=blue,\n\t".length;
17
+ const completions = getCompletions(doc, sf, doc.positionAt(requestOffset));
18
+ console.dir(completions);
19
+ }
20
+ main();
21
+ //# sourceMappingURL=tester.js.map