-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqualified_reference_name.ts
More file actions
39 lines (36 loc) · 1.1 KB
/
Copy pathqualified_reference_name.ts
File metadata and controls
39 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Reference } from "langium";
import {
NamedElement,
isControlUnit,
isVarDecl,
} from "../../../../language/generated/ast.js";
export function getQualifiedReferenceName(
ref: Reference<NamedElement>
): string {
const [isInControlUnit, unitName] = isControlUnitVariable(ref);
if (isInControlUnit && unitName && isVarDecl(ref.ref)) {
return `${unitName}_${getReferenceName(ref)}`;
}
return getReferenceName(ref);
}
export function getReferenceName(ref: Reference<NamedElement>): string {
return (
ref?.$refText ??
(console.warn("Unresolved reference:", JSON.stringify(ref, null, 2)),
"UNRESOLVED_REF")
);
}
/**
* Check if a referenced element belongs to a control unit
* @param ref Reference to check
* @return Tuple where the first element indicates if it's in a control unit,
* and the second element is the control unit name or null
*/
function isControlUnitVariable(
ref: Reference<NamedElement>
): [boolean, string | null] {
const container = ref?.ref?.$container;
return container && isControlUnit(container)
? [true, container.name]
: [false, null];
}