@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
package/dist/module.js ADDED
@@ -0,0 +1,65 @@
1
+ import { createDefaultModule, createDefaultSharedModule, EmptyFileSystem, inject } from 'langium';
2
+ import { LikeC4GeneratedModule, LikeC4GeneratedSharedModule } from './generated/module';
3
+ import { LikeC4DocumentSymbolProvider, LikeC4HoverProvider, LikeC4SemanticTokenProvider } from './lsp';
4
+ import { FqnIndex, LikeC4ModelBuilder, LikeC4ModelLocator } from './model';
5
+ import { LikeC4ScopeComputation, LikeC4ScopeProvider } from './references';
6
+ import { registerProtocolHandlers } from './registerProtocolHandlers';
7
+ import { LikeC4CodeLensProvider, LikeC4WorkspaceManager } from './shared';
8
+ import { registerValidationChecks } from './validation';
9
+ function bind(Type) {
10
+ return (services) => new Type(services);
11
+ }
12
+ export const LikeC4Module = {
13
+ likec4: {
14
+ FqnIndex: bind(FqnIndex),
15
+ ModelBuilder: bind(LikeC4ModelBuilder),
16
+ ModelLocator: bind(LikeC4ModelLocator)
17
+ // Model: bind(LikeC4Model),
18
+ // Model: bind(LikeC4Model),
19
+ // SpecIndex: bind(LikeC4SpecIndex),
20
+ // Validator: bind(LikeC4Validator)
21
+ },
22
+ lsp: {
23
+ DocumentSymbolProvider: bind(LikeC4DocumentSymbolProvider),
24
+ SemanticTokenProvider: bind(LikeC4SemanticTokenProvider),
25
+ HoverProvider: bind(LikeC4HoverProvider)
26
+ },
27
+ //
28
+ // // Formatter: bind(LikeC4Formatter),
29
+ //
30
+ // },
31
+ references: {
32
+ ScopeComputation: bind(LikeC4ScopeComputation),
33
+ ScopeProvider: bind(LikeC4ScopeProvider)
34
+ }
35
+ };
36
+ const LikeC4SharedModule = {
37
+ ...LikeC4GeneratedSharedModule,
38
+ workspace: {
39
+ WorkspaceManager: services => new LikeC4WorkspaceManager(services)
40
+ },
41
+ lsp: {
42
+ CodeLensProvider: services => new LikeC4CodeLensProvider(services)
43
+ }
44
+ };
45
+ export function createLanguageServices(context) {
46
+ // const connection = context.connection
47
+ // if (connection) {
48
+ // logger.log = connection.console.log.bind(connection.console)
49
+ // logger.info = connection.console.info.bind(connection.console)
50
+ // logger.warn = connection.console.warn.bind(connection.console)
51
+ // logger.error = connection.console.error.bind(connection.console)
52
+ // logger.debug = connection.tracer.log.bind(connection.tracer)
53
+ // logger.trace = connection.tracer.log.bind(connection.tracer)
54
+ // }
55
+ const moduleContext = {
56
+ ...EmptyFileSystem,
57
+ ...context
58
+ };
59
+ const shared = inject(createDefaultSharedModule(moduleContext), LikeC4SharedModule);
60
+ const likec4 = inject(createDefaultModule({ shared }), LikeC4GeneratedModule, LikeC4Module);
61
+ shared.ServiceRegistry.register(likec4);
62
+ registerValidationChecks(likec4);
63
+ registerProtocolHandlers(likec4);
64
+ return { shared, likec4 };
65
+ }
@@ -0,0 +1,2 @@
1
+ export * from './scope-computation';
2
+ export * from './scope-provider';
@@ -0,0 +1,2 @@
1
+ export * from './scope-computation';
2
+ export * from './scope-provider';
@@ -0,0 +1,10 @@
1
+ import { DefaultScopeComputation, MultiMap, type AstNodeDescription, type PrecomputedScopes } from 'langium';
2
+ import type { CancellationToken } from 'vscode-languageserver-protocol';
3
+ import { ast, type LikeC4LangiumDocument } from '../ast';
4
+ type ElementsContainer = ast.Model | ast.ElementBody | ast.ExtendElementBody;
5
+ export declare class LikeC4ScopeComputation extends DefaultScopeComputation {
6
+ computeExports(document: LikeC4LangiumDocument, _cancelToken: CancellationToken): Promise<AstNodeDescription[]>;
7
+ computeLocalScopes(document: LikeC4LangiumDocument, _cancelToken: CancellationToken): Promise<PrecomputedScopes>;
8
+ protected processContainer(container: ElementsContainer, scopes: PrecomputedScopes, document: LikeC4LangiumDocument): MultiMap<string, AstNodeDescription>;
9
+ }
10
+ export {};
@@ -0,0 +1,76 @@
1
+ import { DefaultScopeComputation, MultiMap } from 'langium';
2
+ import { ast } from '../ast';
3
+ export class LikeC4ScopeComputation extends DefaultScopeComputation {
4
+ // constructor(services: LikeC4Services) {
5
+ // super(services)
6
+ // }
7
+ computeExports(document, _cancelToken) {
8
+ const { specification, model, views } = document.parseResult.value;
9
+ const docExports = [];
10
+ if (specification) {
11
+ for (const { kind } of specification.elementKinds) {
12
+ docExports.push(this.descriptions.createDescription(kind, kind.name, document));
13
+ }
14
+ for (const { tag } of specification.tags) {
15
+ docExports.push(this.descriptions.createDescription(tag, tag.name, document));
16
+ docExports.push(this.descriptions.createDescription(tag, '#' + tag.name, document));
17
+ }
18
+ }
19
+ if (model) {
20
+ for (const elAst of model.elements) {
21
+ if (ast.isElement(elAst)) {
22
+ docExports.push(this.descriptions.createDescription(elAst, elAst.name, document));
23
+ }
24
+ }
25
+ }
26
+ if (views) {
27
+ for (const viewAst of views.views) {
28
+ if ('name' in viewAst) {
29
+ docExports.push(this.descriptions.createDescription(viewAst, viewAst.name, document));
30
+ }
31
+ }
32
+ }
33
+ return Promise.resolve(docExports);
34
+ }
35
+ async computeLocalScopes(document, _cancelToken) {
36
+ const root = document.parseResult.value;
37
+ const scopes = new MultiMap();
38
+ if (root.model) {
39
+ const nested = this.processContainer(root.model, scopes, document);
40
+ scopes.addAll(root, nested.values());
41
+ }
42
+ return Promise.resolve(scopes);
43
+ }
44
+ processContainer(container, scopes, document) {
45
+ const localScope = new MultiMap();
46
+ const nestedScopes = new MultiMap();
47
+ for (const el of container.elements) {
48
+ if (ast.isRelation(el)) {
49
+ continue;
50
+ }
51
+ let subcontainer;
52
+ if (ast.isElement(el)) {
53
+ localScope.add(el.name, this.descriptions.createDescription(el, el.name, document));
54
+ subcontainer = el.body;
55
+ }
56
+ else if (ast.isExtendElement(el)) {
57
+ subcontainer = el.body;
58
+ }
59
+ if (subcontainer && subcontainer.elements.length > 0) {
60
+ const nested = this.processContainer(subcontainer, scopes, document);
61
+ for (const [nestedName, desc] of nested) {
62
+ nestedScopes.add(nestedName, desc);
63
+ }
64
+ }
65
+ }
66
+ for (const [name, descriptions] of nestedScopes.entriesGroupedByKey()) {
67
+ // If name is unique for current scope
68
+ if (!localScope.has(name) && descriptions.length === 1) {
69
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
+ localScope.add(name, descriptions[0]);
71
+ }
72
+ }
73
+ scopes.addAll(container, localScope.values());
74
+ return localScope;
75
+ }
76
+ }
@@ -0,0 +1,15 @@
1
+ import type { AstNode } from 'langium';
2
+ import { DefaultScopeProvider, type ReferenceInfo, type Scope } from 'langium';
3
+ import type { LikeC4Services } from '../module';
4
+ export declare class LikeC4ScopeProvider extends DefaultScopeProvider {
5
+ private fqnIndex;
6
+ constructor(services: LikeC4Services);
7
+ private scopeElementRef;
8
+ private scopeElementView;
9
+ getScope(context: ReferenceInfo): Scope;
10
+ protected computeScope(node: AstNode, referenceType: string): Scope;
11
+ /**
12
+ * Create a global scope filtered for the given reference type.
13
+ */
14
+ protected getGlobalScope(referenceType: string): Scope;
15
+ }
@@ -0,0 +1,110 @@
1
+ import { DONE_RESULT, DefaultScopeProvider, EMPTY_SCOPE, StreamImpl, StreamScope, getDocument, stream, EMPTY_STREAM } from 'langium';
2
+ import { ast } from '../ast';
3
+ import { elementRef, isElementRefHead, parentStrictElementRef, strictElementRefFqn } from '../elementRef';
4
+ export class LikeC4ScopeProvider extends DefaultScopeProvider {
5
+ fqnIndex;
6
+ constructor(services) {
7
+ super(services);
8
+ this.fqnIndex = services.likec4.FqnIndex;
9
+ }
10
+ scopeElementRef(ref) {
11
+ const parentNode = ref.$container;
12
+ if (!ast.isElementRef(parentNode)) {
13
+ throw new Error('Expected be inside ElementRef');
14
+ }
15
+ return new StreamImpl(() => {
16
+ const parent = parentNode.el.ref;
17
+ const fqn = parent && this.fqnIndex.get(parent);
18
+ if (fqn) {
19
+ return this.fqnIndex.uniqueDescedants(fqn).iterator();
20
+ }
21
+ return null;
22
+ }, iterator => {
23
+ if (iterator) {
24
+ return iterator.next();
25
+ }
26
+ return DONE_RESULT;
27
+ });
28
+ }
29
+ scopeElementView({ viewOf }) {
30
+ if (!viewOf) {
31
+ return EMPTY_STREAM;
32
+ }
33
+ return new StreamImpl(() => {
34
+ const target = elementRef(viewOf);
35
+ const fqn = target && this.fqnIndex.get(target);
36
+ if (fqn) {
37
+ return this.fqnIndex.uniqueDescedants(fqn).iterator();
38
+ }
39
+ return null;
40
+ }, iterator => {
41
+ if (iterator) {
42
+ return iterator.next();
43
+ }
44
+ return DONE_RESULT;
45
+ });
46
+ }
47
+ getScope(context) {
48
+ try {
49
+ const referenceType = this.reflection.getReferenceType(context);
50
+ const node = context.container;
51
+ // const path = this.services.workspace.AstNodeLocator.getAstNodePath(node)
52
+ if (referenceType === ast.Element) {
53
+ if (ast.isStrictElementRef(node)) {
54
+ if (isElementRefHead(node)) {
55
+ return this.getGlobalScope(referenceType);
56
+ }
57
+ const parent = parentStrictElementRef(node);
58
+ return new StreamScope(this.fqnIndex.directChildrenOf(parent));
59
+ }
60
+ if (ast.isElementRef(node) && !isElementRefHead(node)) {
61
+ return new StreamScope(this.scopeElementRef(node));
62
+ }
63
+ }
64
+ return this.computeScope(node, referenceType);
65
+ }
66
+ catch (e) {
67
+ console.error(e);
68
+ // logger.error(e)
69
+ return EMPTY_SCOPE;
70
+ }
71
+ }
72
+ computeScope(node, referenceType) {
73
+ const scopes = [];
74
+ const doc = getDocument(node);
75
+ const precomputed = doc.precomputedScopes;
76
+ const byReferenceType = (desc) => this.reflection.isSubtype(desc.type, referenceType);
77
+ if (precomputed) {
78
+ const elements = precomputed.get(node).filter(byReferenceType);
79
+ if (elements.length > 0) {
80
+ scopes.push(stream(elements));
81
+ }
82
+ let container = node.$container;
83
+ while (container) {
84
+ const elements = precomputed.get(container).filter(byReferenceType);
85
+ if (elements.length > 0) {
86
+ scopes.push(stream(elements));
87
+ }
88
+ if (referenceType === ast.Element) {
89
+ if (ast.isExtendElementBody(container)) {
90
+ const extendsOf = strictElementRefFqn(container.$container.element);
91
+ scopes.push(this.fqnIndex.uniqueDescedants(extendsOf));
92
+ }
93
+ if (ast.isViewRule(container)) {
94
+ scopes.push(this.scopeElementView(container.$container));
95
+ }
96
+ }
97
+ container = container.$container;
98
+ }
99
+ }
100
+ return scopes.reduceRight((outerScope, elements) => {
101
+ return this.createScope(elements, outerScope);
102
+ }, this.getGlobalScope(referenceType));
103
+ }
104
+ /**
105
+ * Create a global scope filtered for the given reference type.
106
+ */
107
+ getGlobalScope(referenceType) {
108
+ return new StreamScope(this.indexManager.allElements(referenceType));
109
+ }
110
+ }
@@ -0,0 +1,2 @@
1
+ import type { LikeC4Services } from './module';
2
+ export declare function registerProtocolHandlers(services: LikeC4Services): void;
@@ -0,0 +1,64 @@
1
+ import { logger } from './logger';
2
+ import { buildDocuments, fetchLikeC4Model, locateElement, locateRelation, locateView } from '@likec4/language-protocol';
3
+ export function registerProtocolHandlers(services) {
4
+ const connection = services.shared.lsp.Connection;
5
+ if (!connection) {
6
+ return;
7
+ }
8
+ const modelBuilder = services.likec4.ModelBuilder;
9
+ const modelLocator = services.likec4.ModelLocator;
10
+ const LangiumDocuments = services.shared.workspace.LangiumDocuments;
11
+ connection.onRequest(fetchLikeC4Model, async (_cancelToken) => {
12
+ let model;
13
+ try {
14
+ model = modelBuilder.buildModel() ?? null;
15
+ }
16
+ catch (e) {
17
+ model = null;
18
+ logger.error(e);
19
+ }
20
+ return Promise.resolve({
21
+ model: model ?? null
22
+ });
23
+ });
24
+ connection.onRequest(buildDocuments, async (docs, cancelToken) => {
25
+ const changed = [];
26
+ for (const d of docs) {
27
+ const uri = d;
28
+ if (LangiumDocuments.hasDocument(uri)) {
29
+ changed.push(uri);
30
+ }
31
+ else {
32
+ logger.error(`LangiumDocuments does not have document: ${uri.toString()}`);
33
+ }
34
+ }
35
+ logger.debug(`Received request to rebuild: [
36
+ ${changed.map(d => d.toString()).join('\n ')}
37
+ ]`);
38
+ await services.shared.workspace.DocumentBuilder.update(changed, [], cancelToken);
39
+ });
40
+ connection.onRequest(locateElement, async ({ element, property }, _cancelToken) => {
41
+ try {
42
+ return Promise.resolve(modelLocator.locateElement(element, property ?? 'name'));
43
+ }
44
+ catch (e) {
45
+ return Promise.reject(e);
46
+ }
47
+ });
48
+ connection.onRequest(locateRelation, ({ id }, _cancelToken) => {
49
+ try {
50
+ return Promise.resolve(modelLocator.locateRelation(id));
51
+ }
52
+ catch (e) {
53
+ return Promise.reject(e);
54
+ }
55
+ });
56
+ connection.onRequest(locateView, ({ id }, _cancelToken) => {
57
+ try {
58
+ return Promise.resolve(modelLocator.locateView(id));
59
+ }
60
+ catch (e) {
61
+ return Promise.reject(e);
62
+ }
63
+ });
64
+ }
@@ -0,0 +1,8 @@
1
+ import type { LangiumDocument, LangiumSharedServices, MaybePromise } from 'langium';
2
+ import type { CodeLensProvider } from 'langium/lib/lsp/code-lens-provider';
3
+ import type { CancellationToken, CodeLens, CodeLensParams } from 'vscode-languageserver-protocol';
4
+ export declare class LikeC4CodeLensProvider implements CodeLensProvider {
5
+ private services;
6
+ constructor(services: LangiumSharedServices);
7
+ provideCodeLens(doc: LangiumDocument, _params: CodeLensParams, _cancelToken?: CancellationToken): MaybePromise<CodeLens[] | undefined>;
8
+ }
@@ -0,0 +1,35 @@
1
+ import { ElementViewOps, isParsedLikeC4LangiumDocument } from '../ast';
2
+ export class LikeC4CodeLensProvider {
3
+ services;
4
+ constructor(services) {
5
+ this.services = services;
6
+ //
7
+ }
8
+ provideCodeLens(doc, _params, _cancelToken) {
9
+ if (!isParsedLikeC4LangiumDocument(doc)) {
10
+ return;
11
+ }
12
+ return doc.parseResult.value.views?.views.flatMap(ast => {
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ const viewId = ElementViewOps.readId(ast);
15
+ const range = ast.$cstNode?.range;
16
+ if (!range || !viewId) {
17
+ return [];
18
+ }
19
+ return {
20
+ range: {
21
+ start: range.start,
22
+ end: {
23
+ line: range.start.line,
24
+ character: range.start.character + 4
25
+ }
26
+ },
27
+ command: {
28
+ command: 'likec4.open-preview',
29
+ arguments: [viewId],
30
+ title: 'open preview'
31
+ }
32
+ };
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,13 @@
1
+ import type { LangiumDocument, LangiumDocumentFactory, LangiumSharedServices } from 'langium';
2
+ import { DefaultWorkspaceManager } from 'langium';
3
+ import type { WorkspaceFolder } from 'vscode-languageserver-protocol';
4
+ export declare class LikeC4WorkspaceManager extends DefaultWorkspaceManager {
5
+ protected readonly documentFactory: LangiumDocumentFactory;
6
+ constructor(services: LangiumSharedServices);
7
+ /**
8
+ * Load all additional documents that shall be visible in the context of the given workspace
9
+ * folders and add them to the collector. This can be used to include built-in libraries of
10
+ * your language, which can be either loaded from provided files or constructed in memory.
11
+ */
12
+ protected loadAdditionalDocuments(folders: WorkspaceFolder[], collector: (document: LangiumDocument) => void): Promise<void>;
13
+ }
@@ -0,0 +1,19 @@
1
+ import { DefaultWorkspaceManager } from 'langium';
2
+ import { URI } from 'vscode-uri';
3
+ import * as builtin from '../builtin';
4
+ export class LikeC4WorkspaceManager extends DefaultWorkspaceManager {
5
+ documentFactory;
6
+ constructor(services) {
7
+ super(services);
8
+ this.documentFactory = services.workspace.LangiumDocumentFactory;
9
+ }
10
+ /**
11
+ * Load all additional documents that shall be visible in the context of the given workspace
12
+ * folders and add them to the collector. This can be used to include built-in libraries of
13
+ * your language, which can be either loaded from provided files or constructed in memory.
14
+ */
15
+ loadAdditionalDocuments(folders, collector) {
16
+ collector(this.documentFactory.fromString(builtin.specification.document, URI.parse(builtin.specification.uri)));
17
+ return Promise.resolve();
18
+ }
19
+ }
@@ -0,0 +1,2 @@
1
+ export * from './CodeLensProvider';
2
+ export * from './WorkspaceManager';
@@ -0,0 +1,2 @@
1
+ export * from './CodeLensProvider';
2
+ export * from './WorkspaceManager';
@@ -0,0 +1,2 @@
1
+ export declare function failExpectedNever(arg: never): never;
2
+ export declare function ignoreNeverInRuntime(arg: never): void;
package/dist/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ export function failExpectedNever(arg) {
2
+ throw new Error(`Unexpected value: ${JSON.stringify(arg)}`);
3
+ }
4
+ export function ignoreNeverInRuntime(arg) {
5
+ console.warn(`Unexpected and ignored value: ${JSON.stringify(arg)}`);
6
+ // throw new Error(`Unexpected value: ${arg}`);
7
+ }
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ import type { ValidationCheck } from 'langium';
3
+ import type { ast } from '../ast';
4
+ import type { LikeC4Services } from '../module';
5
+ export declare const elementChecks: (services: LikeC4Services) => ValidationCheck<ast.Element>;
@@ -0,0 +1,20 @@
1
+ export const elementChecks = (services) => {
2
+ const fqnIndex = services.likec4.FqnIndex;
3
+ return (el, accept) => {
4
+ const fqn = fqnIndex.get(el);
5
+ if (!fqn) {
6
+ accept('error', 'Not indexed', {
7
+ node: el,
8
+ property: 'name'
9
+ });
10
+ return;
11
+ }
12
+ const withSameFqn = fqnIndex.byFqn(fqn);
13
+ if (withSameFqn.length > 1) {
14
+ accept('error', `Duplicate element name ${el.name !== fqn ? el.name + ' (' + fqn + ')' : el.name}`, {
15
+ node: el,
16
+ property: 'name'
17
+ });
18
+ }
19
+ };
20
+ };
@@ -0,0 +1,2 @@
1
+ import type { LikeC4Services } from '../module';
2
+ export declare function registerValidationChecks(services: LikeC4Services): void;
@@ -0,0 +1,22 @@
1
+ import { elementChecks } from './element';
2
+ import { relationChecks } from './relation';
3
+ import { elementKindChecks, tagChecks } from './specification';
4
+ import { viewChecks } from './view';
5
+ export function registerValidationChecks(services) {
6
+ const registry = services.validation.ValidationRegistry;
7
+ // const checks: ValidationChecks = {
8
+ // Element: validator.checkElementNameDuplicates,
9
+ // Tag: validator.checkTagDuplicates,
10
+ // ElementKind: elementKindChecks(services),
11
+ // ElementStyleProperty: validator.checkElementStyleProperty,
12
+ // View: validator.checkViewNameDuplicates,
13
+ // ColorStyleProperty: validator.checkColorStyleProperty,
14
+ // }
15
+ registry.register({
16
+ ElementView: viewChecks(services),
17
+ Element: elementChecks(services),
18
+ ElementKind: elementKindChecks(services),
19
+ Relation: relationChecks(services),
20
+ Tag: tagChecks(services)
21
+ });
22
+ }
@@ -0,0 +1,4 @@
1
+ import type { ValidationCheck } from 'langium';
2
+ import type { ast } from '../ast';
3
+ import type { LikeC4Services } from '../module';
4
+ export declare const relationChecks: (services: LikeC4Services) => ValidationCheck<ast.Relation>;
@@ -0,0 +1,53 @@
1
+ import { resolveRelationPoints } from '../ast';
2
+ import { isSameHierarchy } from '@likec4/core/utils';
3
+ export const relationChecks = (services) => {
4
+ const fqnIndex = services.likec4.FqnIndex;
5
+ return (el, accept) => {
6
+ try {
7
+ const coupling = resolveRelationPoints(el);
8
+ const target = fqnIndex.get(coupling.target);
9
+ if (!target) {
10
+ return accept('error', 'Invalid target', {
11
+ node: el,
12
+ property: 'target'
13
+ });
14
+ }
15
+ const source = fqnIndex.get(coupling.source);
16
+ if (!source) {
17
+ return accept('error', 'Invalid source', {
18
+ node: el
19
+ });
20
+ }
21
+ if (isSameHierarchy(source, target)) {
22
+ return accept('error', 'Invalid relation (same hierarchy)', {
23
+ node: el
24
+ });
25
+ }
26
+ }
27
+ catch (e) {
28
+ if (e instanceof Error) {
29
+ return accept('error', e.message, {
30
+ node: el
31
+ });
32
+ }
33
+ accept('error', 'Invalid relation', {
34
+ node: el
35
+ });
36
+ }
37
+ // const fqn = fqnIndex.get(el)
38
+ // if (!fqn) {
39
+ // accept('error', 'Not indexed', {
40
+ // node: el,
41
+ // property: 'name',
42
+ // })
43
+ // return
44
+ // }
45
+ // const withSameFqn = fqnIndex.byFqn(fqn)
46
+ // if (withSameFqn.length > 1) {
47
+ // accept('error', `Duplicate element name ${el.name !== fqn ? el.name +' (' + fqn + ')' : el.name}`, {
48
+ // node: el,
49
+ // property: 'name',
50
+ // })
51
+ // }
52
+ };
53
+ };
@@ -0,0 +1,5 @@
1
+ import type { ValidationCheck } from 'langium';
2
+ import { ast } from '../ast';
3
+ import type { LikeC4Services } from '../module';
4
+ export declare const elementKindChecks: (services: LikeC4Services) => ValidationCheck<ast.ElementKind>;
5
+ export declare const tagChecks: (services: LikeC4Services) => ValidationCheck<ast.Tag>;
@@ -0,0 +1,33 @@
1
+ import { ast } from '../ast';
2
+ export const elementKindChecks = (services) => {
3
+ const index = services.shared.workspace.IndexManager;
4
+ return (node, accept) => {
5
+ const sameKinds = index
6
+ .allElements(ast.ElementKind)
7
+ .filter(n => n.name === node.name)
8
+ .limit(2)
9
+ .count();
10
+ if (sameKinds > 1) {
11
+ accept('error', `Duplicate element kind '${node.name}'`, {
12
+ node: node,
13
+ property: 'name'
14
+ });
15
+ }
16
+ };
17
+ };
18
+ export const tagChecks = (services) => {
19
+ const index = services.shared.workspace.IndexManager;
20
+ return (node, accept) => {
21
+ const sameKinds = index
22
+ .allElements(ast.Tag)
23
+ .filter(n => n.name === node.name)
24
+ .limit(2)
25
+ .count();
26
+ if (sameKinds > 1) {
27
+ accept('error', `Duplicate tag '${node.name}'`, {
28
+ node: node,
29
+ property: 'name'
30
+ });
31
+ }
32
+ };
33
+ };
@@ -0,0 +1,4 @@
1
+ import type { ValidationCheck } from 'langium';
2
+ import { ast } from '../ast';
3
+ import type { LikeC4Services } from '../module';
4
+ export declare const viewChecks: (services: LikeC4Services) => ValidationCheck<ast.ElementView>;
@@ -0,0 +1,20 @@
1
+ import { ast } from '../ast';
2
+ export const viewChecks = (services) => {
3
+ const index = services.shared.workspace.IndexManager;
4
+ return (el, accept) => {
5
+ if (!el.name) {
6
+ return;
7
+ }
8
+ const anotherViews = index
9
+ .allElements(ast.View)
10
+ .filter(n => n.name === el.name)
11
+ .limit(2)
12
+ .count();
13
+ if (anotherViews > 1) {
14
+ accept('error', `Duplicate view '${el.name}'`, {
15
+ node: el,
16
+ property: 'name'
17
+ });
18
+ }
19
+ };
20
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@likec4/language-server",
3
3
  "description": "LikeC4 Language Server",
4
- "version": "0.6.2",
4
+ "version": "0.6.3",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "bugs": "https://github.com/likec4/likec4/issues",
@@ -66,8 +66,8 @@
66
66
  "test:watch": "vitest"
67
67
  },
68
68
  "dependencies": {
69
- "@likec4/core": "0.6.2",
70
- "@likec4/language-protocol": "0.6.2",
69
+ "@likec4/core": "0.6.3",
70
+ "@likec4/language-protocol": "0.6.3",
71
71
  "@mobily/ts-belt": "^3.13.1",
72
72
  "nanoid": "^4.0.2",
73
73
  "object-hash": "^3.0.0",