Skip to content

Commit b85f1b5

Browse files
authored
Support multi-target references (#1509)
1 parent b9d3652 commit b85f1b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+976
-305
lines changed

examples/arithmetics/src/language-server/generated/grammar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ export const ArithmeticsGrammar = (): Grammar => loadedArithmeticsGrammar ?? (lo
370370
"type": {
371371
"$ref": "#/types@0"
372372
},
373-
"deprecatedSyntax": false
373+
"deprecatedSyntax": false,
374+
"isMulti": false
374375
}
375376
},
376377
{

examples/domainmodel/src/language-server/domain-model-rename-refactoring.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ export class DomainModelRenameProvider extends DefaultRenameProvider {
3131
const offset = document.textDocument.offsetAt(params.position);
3232
const leafNode = CstUtils.findDeclarationNodeAtOffset(rootNode, offset, this.grammarConfig.nameRegexp);
3333
if (!leafNode) return undefined;
34-
const targetNode = this.references.findDeclaration(leafNode);
35-
if (!targetNode) return undefined;
36-
if (isNamed(targetNode)) targetNode.name = params.newName;
37-
const location = this.getNodeLocation(targetNode);
38-
if (location) {
39-
const change = TextEdit.replace(location.range, params.newName);
40-
const uri = location.uri;
41-
if (uri) {
42-
if (changes[uri]) {
43-
changes[uri].push(change);
44-
} else {
45-
changes[uri] = [change];
34+
const targetNodes = this.references.findDeclarations(leafNode);
35+
if (!targetNodes.length) return undefined;
36+
for (const node of targetNodes) {
37+
if (isNamed(node)) node.name = params.newName;
38+
const location = this.getNodeLocation(node);
39+
if (location) {
40+
const change = TextEdit.replace(location.range, params.newName);
41+
const uri = location.uri;
42+
if (uri) {
43+
if (changes[uri]) {
44+
changes[uri].push(change);
45+
} else {
46+
changes[uri] = [change];
47+
}
4648
}
4749
}
4850
}
49-
51+
const targetNode = targetNodes[0];
5052
for (const node of AstUtils.streamAst(targetNode)) {
5153
const qn = this.buildQualifiedName(node);
5254
if (qn) {

examples/domainmodel/src/language-server/domain-model.langium

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ DataType:
1818
'datatype' name=ID;
1919

2020
Entity:
21-
'entity' name=ID ('extends' superType=[Entity:QualifiedName])? '{'
21+
'entity' name=ID ('extends' superType=[+Entity:QualifiedName])? '{'
2222
(features+=Feature)*
2323
'}';
2424

examples/domainmodel/src/language-server/generated/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface Entity extends langium.AstNode {
8888
readonly $type: 'Entity';
8989
features: Array<Feature>;
9090
name: string;
91-
superType?: langium.Reference<Entity>;
91+
superType?: langium.MultiReference<Entity>;
9292
}
9393

9494
export const Entity = {

examples/domainmodel/src/language-server/generated/grammar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
196196
"operator": "=",
197197
"terminal": {
198198
"$type": "CrossReference",
199+
"isMulti": true,
199200
"type": {
200201
"$ref": "#/rules@5"
201202
},
@@ -287,7 +288,8 @@ export const DomainModelGrammar = (): Grammar => loadedDomainModelGrammar ?? (lo
287288
},
288289
"arguments": []
289290
},
290-
"deprecatedSyntax": false
291+
"deprecatedSyntax": false,
292+
"isMulti": false
291293
}
292294
}
293295
]

examples/requirements/src/language-server/generated/grammar.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ export const RequirementsGrammar = (): Grammar => loadedRequirementsGrammar ?? (
162162
"type": {
163163
"$ref": "#/rules@1"
164164
},
165-
"deprecatedSyntax": false
165+
"deprecatedSyntax": false,
166+
"isMulti": false
166167
}
167168
},
168169
{
@@ -181,7 +182,8 @@ export const RequirementsGrammar = (): Grammar => loadedRequirementsGrammar ?? (
181182
"type": {
182183
"$ref": "#/rules@1"
183184
},
184-
"deprecatedSyntax": false
185+
"deprecatedSyntax": false,
186+
"isMulti": false
185187
}
186188
}
187189
],
@@ -410,7 +412,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
410412
},
411413
"arguments": []
412414
},
413-
"deprecatedSyntax": false
415+
"deprecatedSyntax": false,
416+
"isMulti": false
414417
}
415418
},
416419
{
@@ -436,7 +439,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
436439
},
437440
"arguments": []
438441
},
439-
"deprecatedSyntax": false
442+
"deprecatedSyntax": false,
443+
"isMulti": false
440444
}
441445
}
442446
],
@@ -462,7 +466,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
462466
"type": {
463467
"$ref": "#/rules@3"
464468
},
465-
"deprecatedSyntax": false
469+
"deprecatedSyntax": false,
470+
"isMulti": false
466471
}
467472
},
468473
{
@@ -481,7 +486,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
481486
"type": {
482487
"$ref": "#/rules@3"
483488
},
484-
"deprecatedSyntax": false
489+
"deprecatedSyntax": false,
490+
"isMulti": false
485491
}
486492
}
487493
],
@@ -645,7 +651,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
645651
"type": {
646652
"$ref": "#/rules@3"
647653
},
648-
"deprecatedSyntax": false
654+
"deprecatedSyntax": false,
655+
"isMulti": false
649656
}
650657
},
651658
{
@@ -664,7 +671,8 @@ export const TestsGrammar = (): Grammar => loadedTestsGrammar ?? (loadedTestsGra
664671
"type": {
665672
"$ref": "#/rules@3"
666673
},
667-
"deprecatedSyntax": false
674+
"deprecatedSyntax": false,
675+
"isMulti": false
668676
}
669677
}
670678
],

examples/statemachine/src/language-server/generated/grammar.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
9696
"type": {
9797
"$ref": "#/rules@3"
9898
},
99-
"deprecatedSyntax": false
99+
"deprecatedSyntax": false,
100+
"isMulti": false
100101
},
101102
"$comment": "/** The starting state for the machine */"
102103
},
@@ -201,7 +202,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
201202
"type": {
202203
"$ref": "#/rules@2"
203204
},
204-
"deprecatedSyntax": false
205+
"deprecatedSyntax": false,
206+
"isMulti": false
205207
},
206208
"cardinality": "+"
207209
},
@@ -252,7 +254,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
252254
"type": {
253255
"$ref": "#/rules@1"
254256
},
255-
"deprecatedSyntax": false
257+
"deprecatedSyntax": false,
258+
"isMulti": false
256259
},
257260
"$comment": "/** The event triggering the transition */"
258261
},
@@ -269,7 +272,8 @@ export const StatemachineGrammar = (): Grammar => loadedStatemachineGrammar ?? (
269272
"type": {
270273
"$ref": "#/rules@3"
271274
},
272-
"deprecatedSyntax": false
275+
"deprecatedSyntax": false,
276+
"isMulti": false
273277
},
274278
"$comment": "/** The target state */"
275279
}

packages/langium-cli/src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function mapGrammarElements(grammars: Grammar[], visited: Set<string> = new Set(
161161
.concat(grammar.types)
162162
.concat(grammar.interfaces)
163163
);
164-
const importedGrammars = grammar.imports.map(e => resolveImport(documents, e)).filter((e): e is Grammar => e !== undefined);
164+
const importedGrammars = grammar.imports.map(e => resolveImport(documents, e)).filter(e => e !== undefined);
165165
mapGrammarElements(importedGrammars, visited, map);
166166
}
167167
}

packages/langium-cli/src/generator/ast-generator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* This program and the accompanying materials are made available under the
44
* terms of the MIT License, which is available in the project root.
55
******************************************************************************/
6+
67
import type { Grammar, LangiumCoreServices } from 'langium';
78
import { EOL, type Generated, expandToNode, joinToNode, toString } from 'langium/generate';
89
import type { AstTypes, Property, PropertyDefaultValue } from 'langium/grammar';

packages/langium-cli/src/generator/grammar-serializer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
******************************************************************************/
66

77
/* eslint-disable @stylistic/indent */
8-
import type { Grammar, LangiumCoreServices, Reference } from 'langium';
8+
import type { Grammar, LangiumCoreServices } from 'langium';
99
import { expandToNode, joinToNode, normalizeEOL, toString } from 'langium/generate';
1010
import type { URI } from 'vscode-uri';
1111
import type { LangiumConfig } from '../package-types.js';
@@ -31,9 +31,9 @@ export function serializeGrammar(services: LangiumCoreServices, grammars: Gramma
3131
grammar => {
3232
const production = config.mode === 'production';
3333
const delimiter = production ? "'" : '`';
34-
const uriConverter = (uri: URI, ref: Reference) => {
34+
const uriConverter = (uri: URI) => {
3535
// We expect the grammar to be self-contained after the transformations we've done before
36-
throw new Error(`Unexpected reference to symbol '${ref.$refText}' in document: ${uri.toString()}`);
36+
throw new Error(`Unexpected reference to element in document: ${uri.toString()}`);
3737
};
3838
const serializedGrammar = services.serializer.JsonSerializer.serialize(grammar, {
3939
space: production ? undefined : 2,

0 commit comments

Comments
 (0)