@likec4/language-server 0.6.2 → 0.6.3

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 (63) hide show
  1. package/contrib/likec4.monarch.ts +31 -0
  2. package/contrib/likec4.tmLanguage.json +73 -0
  3. package/dist/ast.d.ts +73 -0
  4. package/dist/ast.js +133 -0
  5. package/dist/builtin.d.ts +4 -0
  6. package/dist/builtin.js +8 -0
  7. package/dist/elementRef.d.ts +6 -0
  8. package/dist/elementRef.js +39 -0
  9. package/dist/generated/ast.d.ts +359 -0
  10. package/dist/generated/ast.js +376 -0
  11. package/dist/generated/grammar.d.ts +6 -0
  12. package/dist/generated/grammar.js +2542 -0
  13. package/dist/generated/module.d.ts +9 -0
  14. package/dist/generated/module.js +26 -0
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.js +1 -0
  17. package/dist/logger.d.ts +8 -0
  18. package/dist/logger.js +20 -0
  19. package/dist/lsp/DocumentSymbolProvider.d.ts +21 -0
  20. package/dist/lsp/DocumentSymbolProvider.js +149 -0
  21. package/dist/lsp/HoverProvider.d.ts +8 -0
  22. package/dist/lsp/HoverProvider.js +54 -0
  23. package/dist/lsp/SemanticTokenProvider.d.ts +6 -0
  24. package/dist/lsp/SemanticTokenProvider.js +221 -0
  25. package/dist/lsp/index.d.ts +3 -0
  26. package/dist/lsp/index.js +3 -0
  27. package/dist/model/fqn-index.d.ts +17 -0
  28. package/dist/model/fqn-index.js +138 -0
  29. package/dist/model/index.d.ts +3 -0
  30. package/dist/model/index.js +3 -0
  31. package/dist/model/model-builder.d.ts +26 -0
  32. package/dist/model/model-builder.js +332 -0
  33. package/dist/model/model-locator.d.ts +16 -0
  34. package/dist/model/model-locator.js +108 -0
  35. package/dist/module.d.ts +20 -0
  36. package/dist/module.js +65 -0
  37. package/dist/references/index.d.ts +2 -0
  38. package/dist/references/index.js +2 -0
  39. package/dist/references/scope-computation.d.ts +10 -0
  40. package/dist/references/scope-computation.js +76 -0
  41. package/dist/references/scope-provider.d.ts +15 -0
  42. package/dist/references/scope-provider.js +110 -0
  43. package/dist/registerProtocolHandlers.d.ts +2 -0
  44. package/dist/registerProtocolHandlers.js +64 -0
  45. package/dist/shared/CodeLensProvider.d.ts +8 -0
  46. package/dist/shared/CodeLensProvider.js +35 -0
  47. package/dist/shared/WorkspaceManager.d.ts +13 -0
  48. package/dist/shared/WorkspaceManager.js +19 -0
  49. package/dist/shared/index.d.ts +2 -0
  50. package/dist/shared/index.js +2 -0
  51. package/dist/utils.d.ts +2 -0
  52. package/dist/utils.js +7 -0
  53. package/dist/validation/element.d.ts +5 -0
  54. package/dist/validation/element.js +20 -0
  55. package/dist/validation/index.d.ts +2 -0
  56. package/dist/validation/index.js +22 -0
  57. package/dist/validation/relation.d.ts +4 -0
  58. package/dist/validation/relation.js +53 -0
  59. package/dist/validation/specification.d.ts +5 -0
  60. package/dist/validation/specification.js +33 -0
  61. package/dist/validation/view.d.ts +4 -0
  62. package/dist/validation/view.js +20 -0
  63. package/package.json +3 -3
@@ -0,0 +1,31 @@
1
+ // Monarch syntax highlighting for the likec4 language.
2
+ export default {
3
+ keywords: [
4
+ 'BottomTop','LeftRight','RightLeft','TopBottom','autoLayout','browser','color','cylinder','description','element','exclude','extend','include','it','model','muted','of','person','primary','queue','rectangle','secondary','shape','specification','storage','style','tag','technology','this','title','view','views'
5
+ ],
6
+ operators: [
7
+ '*','->','.*'
8
+ ],
9
+ symbols: /\*|->|\.\*/,
10
+
11
+ tokenizer: {
12
+ initial: [
13
+ { regex: /[^\W\d_]/, action: { cases: { '@keywords': {"token":"keyword"}, '@default': {"token":"LETTER"} }} },
14
+ { regex: /[0-9]/, action: {"token":"DIGIT"} },
15
+ { regex: /[\t\r\n\v\f]/, action: {"token":"NEWLINE"} },
16
+ { regex: /"[^"]*"|'[^']*'/, action: {"token":"string"} },
17
+ { include: '@whitespace' },
18
+ { regex: /@symbols/, action: { cases: { '@operators': {"token":"operator"}, '@default': {"token":""} }} },
19
+ ],
20
+ whitespace: [
21
+ { regex: /[^\S\r\n]/, action: {"token":"white"} },
22
+ { regex: /\/\*/, action: {"token":"comment","next":"@comment"} },
23
+ { regex: /\/\/[^\n\r]*/, action: {"token":"comment"} },
24
+ ],
25
+ comment: [
26
+ { regex: /[^\/\*]+/, action: {"token":"comment"} },
27
+ { regex: /\*\//, action: {"token":"comment","next":"@pop"} },
28
+ { regex: /[\/\*]/, action: {"token":"comment"} },
29
+ ],
30
+ }
31
+ };
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "likec4",
3
+ "scopeName": "source.likec4",
4
+ "fileTypes": [
5
+ ".c4",
6
+ ".likec4",
7
+ ".like-c4"
8
+ ],
9
+ "patterns": [
10
+ {
11
+ "include": "#comments"
12
+ },
13
+ {
14
+ "name": "keyword.control.likec4",
15
+ "match": "\\b(BottomTop|LeftRight|RightLeft|TopBottom|autoLayout|browser|color|cylinder|description|element|exclude|extend|include|it|model|muted|of|person|primary|queue|rectangle|secondary|shape|specification|storage|style|tag|technology|this|title|view|views)\\b"
16
+ },
17
+ {
18
+ "name": "string.quoted.double.likec4",
19
+ "begin": "\"",
20
+ "end": "\"",
21
+ "patterns": [
22
+ {
23
+ "include": "#string-character-escape"
24
+ }
25
+ ]
26
+ },
27
+ {
28
+ "name": "string.quoted.single.likec4",
29
+ "begin": "'",
30
+ "end": "'",
31
+ "patterns": [
32
+ {
33
+ "include": "#string-character-escape"
34
+ }
35
+ ]
36
+ }
37
+ ],
38
+ "repository": {
39
+ "comments": {
40
+ "patterns": [
41
+ {
42
+ "name": "comment.block.likec4",
43
+ "begin": "/\\*",
44
+ "beginCaptures": {
45
+ "0": {
46
+ "name": "punctuation.definition.comment.likec4"
47
+ }
48
+ },
49
+ "end": "\\*/",
50
+ "endCaptures": {
51
+ "0": {
52
+ "name": "punctuation.definition.comment.likec4"
53
+ }
54
+ }
55
+ },
56
+ {
57
+ "begin": "//",
58
+ "beginCaptures": {
59
+ "1": {
60
+ "name": "punctuation.whitespace.comment.leading.likec4"
61
+ }
62
+ },
63
+ "end": "(?=$)",
64
+ "name": "comment.line.likec4"
65
+ }
66
+ ]
67
+ },
68
+ "string-character-escape": {
69
+ "name": "constant.character.escape.likec4",
70
+ "match": "\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|u\\{[0-9A-Fa-f]+\\}|[0-2][0-7]{0,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.|$)"
71
+ }
72
+ }
73
+ }
package/dist/ast.d.ts ADDED
@@ -0,0 +1,73 @@
1
+ /// <reference types="react" />
2
+ import * as ast from './generated/ast';
3
+ import type { LangiumDocument } from 'langium';
4
+ import type { LikeC4Document } from './generated/ast';
5
+ import type * as c4 from '@likec4/core/types';
6
+ export { ast };
7
+ export declare function c4hash({ c4Specification, c4Elements, c4Relations, c4Views }: LikeC4LangiumDocument): string;
8
+ export interface ParsedAstSpecification {
9
+ kinds: Record<c4.ElementKind, {
10
+ shape: c4.ElementShape;
11
+ color: c4.ThemeColor;
12
+ }>;
13
+ }
14
+ export interface ParsedAstElement {
15
+ id: c4.Fqn;
16
+ astPath: string;
17
+ kind: c4.ElementKind;
18
+ title: string;
19
+ description?: string;
20
+ technology?: string;
21
+ tags?: c4.Tag[];
22
+ shape?: c4.ElementShape;
23
+ color?: c4.ThemeColor;
24
+ }
25
+ export interface ParsedAstRelation {
26
+ id: c4.RelationID;
27
+ astPath: string;
28
+ source: c4.Fqn;
29
+ target: c4.Fqn;
30
+ title: string;
31
+ }
32
+ export interface ParsedAstElementView {
33
+ id: c4.ViewID;
34
+ astPath: string;
35
+ viewOf?: c4.Fqn;
36
+ title?: string;
37
+ description?: string;
38
+ rules: c4.ViewRule[];
39
+ }
40
+ export declare const ElementViewOps: {
41
+ writeId(node: ast.ElementView, id: c4.ViewID): ast.ElementView;
42
+ readId(node: ast.ElementView): c4.ViewID | undefined;
43
+ };
44
+ export declare const ElementOps: {
45
+ writeId(node: ast.Element, id: c4.Fqn): ast.Element;
46
+ readId(node: ast.Element): c4.Fqn | undefined;
47
+ };
48
+ export interface LikeC4LangiumDocument extends LangiumDocument<LikeC4Document> {
49
+ c4hash?: string;
50
+ c4Specification: ParsedAstSpecification;
51
+ c4Elements: ParsedAstElement[];
52
+ c4Relations: ParsedAstRelation[];
53
+ c4Views: ParsedAstElementView[];
54
+ }
55
+ export declare function cleanParsedModel(doc: LikeC4LangiumDocument): {
56
+ elements: ParsedAstElement[];
57
+ relations: ParsedAstRelation[];
58
+ views: ParsedAstElementView[];
59
+ specification: ParsedAstSpecification;
60
+ };
61
+ export declare function isLikeC4LangiumDocument(doc: LangiumDocument): doc is LikeC4LangiumDocument;
62
+ export declare function isParsedLikeC4LangiumDocument(doc: LangiumDocument): doc is LikeC4LangiumDocument;
63
+ export declare const isValidDocument: (doc: LangiumDocument) => doc is LikeC4LangiumDocument;
64
+ export declare function streamModel(doc: LikeC4LangiumDocument): Generator<ast.Element | ast.Relation, void, unknown>;
65
+ export declare function resolveRelationPoints(node: ast.Relation): {
66
+ source: ast.Element;
67
+ target: ast.Element;
68
+ };
69
+ export declare function toElementStyle(props?: ast.AStyleProperty[]): {
70
+ color?: c4.ThemeColor;
71
+ shape?: c4.ElementShape;
72
+ };
73
+ export declare function toAutoLayout(direction: ast.ViewRuleLayoutDirection): c4.ViewRuleAutoLayout['autoLayout'];
package/dist/ast.js ADDED
@@ -0,0 +1,133 @@
1
+ import * as ast from './generated/ast';
2
+ import { DocumentState } from 'langium';
3
+ import objectHash from 'object-hash';
4
+ import { elementRef } from './elementRef';
5
+ import { LikeC4LanguageMetaData } from './generated/module';
6
+ export { ast };
7
+ export function c4hash({ c4Specification, c4Elements, c4Relations, c4Views }) {
8
+ return objectHash({
9
+ c4Specification,
10
+ c4Elements,
11
+ c4Relations,
12
+ c4Views
13
+ }, {
14
+ respectType: false
15
+ });
16
+ }
17
+ const idattr = Symbol.for('idattr');
18
+ export const ElementViewOps = {
19
+ writeId(node, id) {
20
+ Object.assign(node, { [idattr]: id });
21
+ return node;
22
+ },
23
+ readId(node) {
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ return node[idattr];
26
+ }
27
+ };
28
+ export const ElementOps = {
29
+ writeId(node, id) {
30
+ Object.assign(node, { [idattr]: id });
31
+ return node;
32
+ },
33
+ readId(node) {
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ return node[idattr];
36
+ }
37
+ };
38
+ export function cleanParsedModel(doc) {
39
+ doc.c4Specification = {
40
+ kinds: {}
41
+ };
42
+ const elements = (doc.c4Elements = []);
43
+ const relations = (doc.c4Relations = []);
44
+ const views = (doc.c4Views = []);
45
+ return {
46
+ elements,
47
+ relations,
48
+ views,
49
+ specification: doc.c4Specification
50
+ };
51
+ }
52
+ export function isLikeC4LangiumDocument(doc) {
53
+ return doc.textDocument.languageId === LikeC4LanguageMetaData.languageId;
54
+ }
55
+ export function isParsedLikeC4LangiumDocument(doc) {
56
+ return (isLikeC4LangiumDocument(doc) &&
57
+ ['c4Specification', 'c4Elements', 'c4Relations', 'c4Views'].every(key => key in doc));
58
+ }
59
+ export const isValidDocument = (doc) => {
60
+ if (!isLikeC4LangiumDocument(doc))
61
+ return false;
62
+ const { state, parseResult, diagnostics } = doc;
63
+ return (state === DocumentState.Validated &&
64
+ parseResult.lexerErrors.length === 0 &&
65
+ (!diagnostics || diagnostics.every(d => d.severity !== 1)));
66
+ };
67
+ export function* streamModel(doc) {
68
+ const elements = doc.parseResult.value.model?.elements ?? [];
69
+ const traverseStack = [...elements];
70
+ let el;
71
+ while ((el = traverseStack.shift())) {
72
+ if (ast.isExtendElement(el)) {
73
+ traverseStack.push(...el.body.elements);
74
+ continue;
75
+ }
76
+ if (ast.isElement(el) && el.body) {
77
+ traverseStack.push(...el.body.elements);
78
+ }
79
+ yield el;
80
+ }
81
+ }
82
+ export function resolveRelationPoints(node) {
83
+ const target = elementRef(node.target);
84
+ if (!target) {
85
+ throw new Error('Skip relation due to invalid reference to target');
86
+ }
87
+ if (ast.isRelationWithSource(node)) {
88
+ const source = elementRef(node.source);
89
+ if (!source) {
90
+ throw new Error('Skip relation due to invalid reference to source');
91
+ }
92
+ return {
93
+ source,
94
+ target
95
+ };
96
+ }
97
+ if (!ast.isElementBody(node.$container)) {
98
+ throw new Error('Skip relation due to invalid reference to source');
99
+ }
100
+ const source = node.$container.$container;
101
+ return {
102
+ source,
103
+ target
104
+ };
105
+ }
106
+ export function toElementStyle(props) {
107
+ const result = {};
108
+ const color = props?.find(ast.isColorProperty)?.value;
109
+ if (color) {
110
+ result.color = color;
111
+ }
112
+ const shape = props?.find(ast.isShapeProperty)?.value;
113
+ if (shape) {
114
+ result.shape = shape;
115
+ }
116
+ return result;
117
+ }
118
+ export function toAutoLayout(direction) {
119
+ switch (direction) {
120
+ case 'TopBottom': {
121
+ return 'TB';
122
+ }
123
+ case 'BottomTop': {
124
+ return 'BT';
125
+ }
126
+ case 'LeftRight': {
127
+ return 'LR';
128
+ }
129
+ case 'RightLeft': {
130
+ return 'RL';
131
+ }
132
+ }
133
+ }
@@ -0,0 +1,4 @@
1
+ export declare const specification: {
2
+ uri: string;
3
+ document: string;
4
+ };
@@ -0,0 +1,8 @@
1
+ export const specification = {
2
+ uri: 'builtin:///specification.likec4',
3
+ document: `
4
+ specification {
5
+ element element
6
+ }
7
+ `.trimStart()
8
+ };
@@ -0,0 +1,6 @@
1
+ import type * as c4 from '@likec4/core/types';
2
+ import { ast } from './ast';
3
+ export declare function isElementRefHead(node: ast.ElementRef | ast.StrictElementRef): boolean;
4
+ export declare function elementRef(node: ast.ElementRef): ast.Element | undefined;
5
+ export declare function strictElementRefFqn(node: ast.StrictElementRef): c4.Fqn;
6
+ export declare function parentStrictElementRef(node: ast.StrictElementRef): c4.Fqn;
@@ -0,0 +1,39 @@
1
+ import { ast } from './ast';
2
+ import invariant from 'tiny-invariant';
3
+ import { failExpectedNever } from './utils';
4
+ export function isElementRefHead(node) {
5
+ if (ast.isElementRef(node)) {
6
+ return !ast.isElementRef(node.$container);
7
+ }
8
+ if (ast.isStrictElementRef(node)) {
9
+ return !ast.isStrictElementRef(node.$container);
10
+ }
11
+ failExpectedNever(node);
12
+ }
13
+ export function elementRef(node) {
14
+ invariant(isElementRefHead(node), 'Expected head ElementRef');
15
+ while (node.next) {
16
+ node = node.next;
17
+ }
18
+ return node.el.ref;
19
+ }
20
+ export function strictElementRefFqn(node) {
21
+ invariant(isElementRefHead(node), 'Expected head StrictElementRef');
22
+ const name = [node.el.$refText];
23
+ let child = node.next;
24
+ while (child) {
25
+ name.push(child.el.$refText);
26
+ child = child.next;
27
+ }
28
+ return name.join('.');
29
+ }
30
+ export function parentStrictElementRef(node) {
31
+ invariant(!isElementRefHead(node), 'Expected next StrictElementRef');
32
+ const path = [];
33
+ let parent = node.$container;
34
+ while (ast.isStrictElementRef(parent)) {
35
+ path.unshift(parent.el.$refText);
36
+ parent = parent.$container;
37
+ }
38
+ return path.join('.');
39
+ }