@zenstackhq/language 3.0.0-beta.2 → 3.0.0-beta.20
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.
- package/dist/index.cjs +1577 -280
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +84 -20
- package/dist/index.d.ts +84 -20
- package/dist/index.js +1528 -234
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +106 -28
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +5 -5
- package/dist/utils.d.ts +5 -5
- package/dist/utils.js +100 -23
- package/dist/utils.js.map +1 -1
- package/package.json +11 -7
- package/res/stdlib.zmodel +43 -71
package/dist/index.js
CHANGED
|
@@ -7,16 +7,19 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
// src/
|
|
11
|
-
import { isAstNode, URI as
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
10
|
+
// src/document.ts
|
|
11
|
+
import { isAstNode, URI as URI4 } from "langium";
|
|
12
|
+
import fs4 from "fs";
|
|
13
|
+
import path4 from "path";
|
|
14
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
16
15
|
|
|
17
16
|
// src/generated/ast.ts
|
|
18
17
|
import * as langium from "langium";
|
|
19
18
|
var AbstractDeclaration = "AbstractDeclaration";
|
|
19
|
+
function isAbstractDeclaration(item) {
|
|
20
|
+
return reflection.isInstance(item, AbstractDeclaration);
|
|
21
|
+
}
|
|
22
|
+
__name(isAbstractDeclaration, "isAbstractDeclaration");
|
|
20
23
|
var ConfigExpr = "ConfigExpr";
|
|
21
24
|
var Expression = "Expression";
|
|
22
25
|
function isExpression(item) {
|
|
@@ -43,6 +46,10 @@ function isAttribute(item) {
|
|
|
43
46
|
}
|
|
44
47
|
__name(isAttribute, "isAttribute");
|
|
45
48
|
var AttributeArg = "AttributeArg";
|
|
49
|
+
function isAttributeArg(item) {
|
|
50
|
+
return reflection.isInstance(item, AttributeArg);
|
|
51
|
+
}
|
|
52
|
+
__name(isAttributeArg, "isAttributeArg");
|
|
46
53
|
var AttributeParam = "AttributeParam";
|
|
47
54
|
var AttributeParamType = "AttributeParamType";
|
|
48
55
|
var BinaryExpr = "BinaryExpr";
|
|
@@ -61,6 +68,10 @@ function isConfigArrayExpr(item) {
|
|
|
61
68
|
}
|
|
62
69
|
__name(isConfigArrayExpr, "isConfigArrayExpr");
|
|
63
70
|
var ConfigField = "ConfigField";
|
|
71
|
+
function isConfigField(item) {
|
|
72
|
+
return reflection.isInstance(item, ConfigField);
|
|
73
|
+
}
|
|
74
|
+
__name(isConfigField, "isConfigField");
|
|
64
75
|
var ConfigInvocationArg = "ConfigInvocationArg";
|
|
65
76
|
var ConfigInvocationExpr = "ConfigInvocationExpr";
|
|
66
77
|
var DataField = "DataField";
|
|
@@ -105,10 +116,22 @@ function isEnumField(item) {
|
|
|
105
116
|
__name(isEnumField, "isEnumField");
|
|
106
117
|
var FieldInitializer = "FieldInitializer";
|
|
107
118
|
var FunctionDecl = "FunctionDecl";
|
|
119
|
+
function isFunctionDecl(item) {
|
|
120
|
+
return reflection.isInstance(item, FunctionDecl);
|
|
121
|
+
}
|
|
122
|
+
__name(isFunctionDecl, "isFunctionDecl");
|
|
108
123
|
var FunctionParam = "FunctionParam";
|
|
109
124
|
var FunctionParamType = "FunctionParamType";
|
|
110
125
|
var GeneratorDecl = "GeneratorDecl";
|
|
126
|
+
function isGeneratorDecl(item) {
|
|
127
|
+
return reflection.isInstance(item, GeneratorDecl);
|
|
128
|
+
}
|
|
129
|
+
__name(isGeneratorDecl, "isGeneratorDecl");
|
|
111
130
|
var InternalAttribute = "InternalAttribute";
|
|
131
|
+
function isInternalAttribute(item) {
|
|
132
|
+
return reflection.isInstance(item, InternalAttribute);
|
|
133
|
+
}
|
|
134
|
+
__name(isInternalAttribute, "isInternalAttribute");
|
|
112
135
|
var InvocationExpr = "InvocationExpr";
|
|
113
136
|
function isInvocationExpr(item) {
|
|
114
137
|
return reflection.isInstance(item, InvocationExpr);
|
|
@@ -125,6 +148,10 @@ function isModel(item) {
|
|
|
125
148
|
}
|
|
126
149
|
__name(isModel, "isModel");
|
|
127
150
|
var ModelImport = "ModelImport";
|
|
151
|
+
function isModelImport(item) {
|
|
152
|
+
return reflection.isInstance(item, ModelImport);
|
|
153
|
+
}
|
|
154
|
+
__name(isModelImport, "isModelImport");
|
|
128
155
|
var NullExpr = "NullExpr";
|
|
129
156
|
function isNullExpr(item) {
|
|
130
157
|
return reflection.isInstance(item, NullExpr);
|
|
@@ -146,6 +173,10 @@ function isPlugin(item) {
|
|
|
146
173
|
}
|
|
147
174
|
__name(isPlugin, "isPlugin");
|
|
148
175
|
var PluginField = "PluginField";
|
|
176
|
+
function isPluginField(item) {
|
|
177
|
+
return reflection.isInstance(item, PluginField);
|
|
178
|
+
}
|
|
179
|
+
__name(isPluginField, "isPluginField");
|
|
149
180
|
var Procedure = "Procedure";
|
|
150
181
|
var ProcedureParam = "ProcedureParam";
|
|
151
182
|
var ReferenceArg = "ReferenceArg";
|
|
@@ -1012,6 +1043,26 @@ var ZModelAstReflection = class extends langium.AbstractAstReflection {
|
|
|
1012
1043
|
};
|
|
1013
1044
|
var reflection = new ZModelAstReflection();
|
|
1014
1045
|
|
|
1046
|
+
// src/ast.ts
|
|
1047
|
+
var BinaryExprOperatorPriority = {
|
|
1048
|
+
//LogicalExpr
|
|
1049
|
+
"||": 1,
|
|
1050
|
+
"&&": 1,
|
|
1051
|
+
//EqualityExpr
|
|
1052
|
+
"==": 2,
|
|
1053
|
+
"!=": 2,
|
|
1054
|
+
//ComparisonExpr
|
|
1055
|
+
">": 3,
|
|
1056
|
+
"<": 3,
|
|
1057
|
+
">=": 3,
|
|
1058
|
+
"<=": 3,
|
|
1059
|
+
in: 4,
|
|
1060
|
+
//CollectionPredicateExpr
|
|
1061
|
+
"^": 5,
|
|
1062
|
+
"?": 5,
|
|
1063
|
+
"!": 5
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1015
1066
|
// src/constants.ts
|
|
1016
1067
|
var SUPPORTED_PROVIDERS = [
|
|
1017
1068
|
"sqlite",
|
|
@@ -1042,8 +1093,11 @@ var ExpressionContext = /* @__PURE__ */ function(ExpressionContext2) {
|
|
|
1042
1093
|
}({});
|
|
1043
1094
|
|
|
1044
1095
|
// src/module.ts
|
|
1045
|
-
import { inject } from "langium";
|
|
1096
|
+
import { DocumentState as DocumentState2, inject, URI as URI3 } from "langium";
|
|
1046
1097
|
import { createDefaultModule, createDefaultSharedModule } from "langium/lsp";
|
|
1098
|
+
import { NodeFileSystem } from "langium/node";
|
|
1099
|
+
import path3 from "path";
|
|
1100
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
1047
1101
|
|
|
1048
1102
|
// src/generated/grammar.ts
|
|
1049
1103
|
import { loadGrammarFromJson } from "langium";
|
|
@@ -5104,15 +5158,12 @@ var ZModelGeneratedModule = {
|
|
|
5104
5158
|
parser: {}
|
|
5105
5159
|
};
|
|
5106
5160
|
|
|
5107
|
-
// src/validators/attribute-application-validator.ts
|
|
5108
|
-
import { AstUtils as AstUtils2 } from "langium";
|
|
5109
|
-
import pluralize from "pluralize";
|
|
5110
|
-
|
|
5111
5161
|
// src/utils.ts
|
|
5112
|
-
import { invariant } from "@zenstackhq/common-helpers";
|
|
5113
5162
|
import { AstUtils, URI } from "langium";
|
|
5114
5163
|
import fs from "fs";
|
|
5164
|
+
import { createRequire } from "module";
|
|
5115
5165
|
import path from "path";
|
|
5166
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
5116
5167
|
function hasAttribute(decl, name) {
|
|
5117
5168
|
return !!getAttribute(decl, name);
|
|
5118
5169
|
}
|
|
@@ -5192,10 +5243,6 @@ function isRelationshipField(field) {
|
|
|
5192
5243
|
return isDataModel(field.type.reference?.ref);
|
|
5193
5244
|
}
|
|
5194
5245
|
__name(isRelationshipField, "isRelationshipField");
|
|
5195
|
-
function isFutureExpr(node) {
|
|
5196
|
-
return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
|
|
5197
|
-
}
|
|
5198
|
-
__name(isFutureExpr, "isFutureExpr");
|
|
5199
5246
|
function isDelegateModel(node) {
|
|
5200
5247
|
return isDataModel(node) && hasAttribute(node, "@@delegate");
|
|
5201
5248
|
}
|
|
@@ -5213,8 +5260,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
|
|
|
5213
5260
|
return result;
|
|
5214
5261
|
}
|
|
5215
5262
|
seen.add(decl);
|
|
5216
|
-
|
|
5217
|
-
|
|
5263
|
+
const bases = [
|
|
5264
|
+
...decl.mixins,
|
|
5265
|
+
...isDataModel(decl) && decl.baseModel ? [
|
|
5266
|
+
decl.baseModel
|
|
5267
|
+
] : []
|
|
5268
|
+
];
|
|
5269
|
+
bases.forEach((base) => {
|
|
5270
|
+
const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
|
|
5218
5271
|
if (baseDecl) {
|
|
5219
5272
|
if (!includeDelegate && isDelegateModel(baseDecl)) {
|
|
5220
5273
|
return;
|
|
@@ -5354,7 +5407,7 @@ function getFunctionExpressionContext(funcDecl) {
|
|
|
5354
5407
|
}
|
|
5355
5408
|
__name(getFunctionExpressionContext, "getFunctionExpressionContext");
|
|
5356
5409
|
function isCheckInvocation(node) {
|
|
5357
|
-
return isInvocationExpr(node) && node.function.ref?.name === "check"
|
|
5410
|
+
return isInvocationExpr(node) && node.function.ref?.name === "check";
|
|
5358
5411
|
}
|
|
5359
5412
|
__name(isCheckInvocation, "isCheckInvocation");
|
|
5360
5413
|
function resolveTransitiveImports(documents, model) {
|
|
@@ -5424,17 +5477,17 @@ function getAllDeclarationsIncludingImports(documents, model) {
|
|
|
5424
5477
|
}
|
|
5425
5478
|
__name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
|
|
5426
5479
|
function getAuthDecl(decls) {
|
|
5427
|
-
let authModel = decls.find((
|
|
5480
|
+
let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
|
|
5428
5481
|
if (!authModel) {
|
|
5429
|
-
authModel = decls.find((
|
|
5482
|
+
authModel = decls.find((d) => d.name === "User");
|
|
5430
5483
|
}
|
|
5431
5484
|
return authModel;
|
|
5432
5485
|
}
|
|
5433
5486
|
__name(getAuthDecl, "getAuthDecl");
|
|
5434
|
-
function
|
|
5435
|
-
return isInvocationExpr(node) && node.function.ref?.name === "
|
|
5487
|
+
function isBeforeInvocation(node) {
|
|
5488
|
+
return isInvocationExpr(node) && node.function.ref?.name === "before";
|
|
5436
5489
|
}
|
|
5437
|
-
__name(
|
|
5490
|
+
__name(isBeforeInvocation, "isBeforeInvocation");
|
|
5438
5491
|
function isCollectionPredicate(node) {
|
|
5439
5492
|
return isBinaryExpr(node) && [
|
|
5440
5493
|
"?",
|
|
@@ -5489,12 +5542,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
|
|
|
5489
5542
|
seen.add(decl);
|
|
5490
5543
|
const fields = [];
|
|
5491
5544
|
for (const mixin of decl.mixins) {
|
|
5492
|
-
|
|
5493
|
-
|
|
5545
|
+
if (mixin.ref) {
|
|
5546
|
+
fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
|
|
5547
|
+
}
|
|
5494
5548
|
}
|
|
5495
5549
|
if (isDataModel(decl) && decl.baseModel) {
|
|
5496
|
-
|
|
5497
|
-
|
|
5550
|
+
if (decl.baseModel.ref) {
|
|
5551
|
+
fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
|
|
5552
|
+
}
|
|
5498
5553
|
}
|
|
5499
5554
|
fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
|
|
5500
5555
|
return fields;
|
|
@@ -5507,12 +5562,14 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
|
|
|
5507
5562
|
seen.add(decl);
|
|
5508
5563
|
const attributes = [];
|
|
5509
5564
|
for (const mixin of decl.mixins) {
|
|
5510
|
-
|
|
5511
|
-
|
|
5565
|
+
if (mixin.ref) {
|
|
5566
|
+
attributes.push(...getAllAttributes(mixin.ref, seen));
|
|
5567
|
+
}
|
|
5512
5568
|
}
|
|
5513
5569
|
if (isDataModel(decl) && decl.baseModel) {
|
|
5514
|
-
|
|
5515
|
-
|
|
5570
|
+
if (decl.baseModel.ref) {
|
|
5571
|
+
attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
|
|
5572
|
+
}
|
|
5516
5573
|
}
|
|
5517
5574
|
attributes.push(...decl.attributes);
|
|
5518
5575
|
return attributes;
|
|
@@ -5527,6 +5584,71 @@ function getDocument(node) {
|
|
|
5527
5584
|
return result;
|
|
5528
5585
|
}
|
|
5529
5586
|
__name(getDocument, "getDocument");
|
|
5587
|
+
function getPluginDocuments(model, schemaPath) {
|
|
5588
|
+
const result = [];
|
|
5589
|
+
for (const decl of model.declarations.filter(isPlugin)) {
|
|
5590
|
+
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
5591
|
+
if (!providerField) {
|
|
5592
|
+
continue;
|
|
5593
|
+
}
|
|
5594
|
+
const provider = getLiteral(providerField.value);
|
|
5595
|
+
if (!provider) {
|
|
5596
|
+
continue;
|
|
5597
|
+
}
|
|
5598
|
+
let pluginModelFile;
|
|
5599
|
+
let providerPath = path.resolve(path.dirname(schemaPath), provider);
|
|
5600
|
+
if (fs.existsSync(providerPath)) {
|
|
5601
|
+
if (fs.statSync(providerPath).isDirectory()) {
|
|
5602
|
+
providerPath = path.join(providerPath, "index.js");
|
|
5603
|
+
}
|
|
5604
|
+
pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
|
|
5605
|
+
if (!fs.existsSync(pluginModelFile)) {
|
|
5606
|
+
pluginModelFile = findUp([
|
|
5607
|
+
PLUGIN_MODULE_NAME
|
|
5608
|
+
], path.dirname(providerPath));
|
|
5609
|
+
}
|
|
5610
|
+
}
|
|
5611
|
+
if (!pluginModelFile) {
|
|
5612
|
+
if (typeof import.meta.resolve === "function") {
|
|
5613
|
+
try {
|
|
5614
|
+
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5615
|
+
pluginModelFile = fileURLToPath(resolvedUrl);
|
|
5616
|
+
} catch {
|
|
5617
|
+
}
|
|
5618
|
+
}
|
|
5619
|
+
}
|
|
5620
|
+
if (!pluginModelFile) {
|
|
5621
|
+
try {
|
|
5622
|
+
const require2 = createRequire(pathToFileURL(schemaPath));
|
|
5623
|
+
pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5624
|
+
} catch {
|
|
5625
|
+
}
|
|
5626
|
+
}
|
|
5627
|
+
if (pluginModelFile && fs.existsSync(pluginModelFile)) {
|
|
5628
|
+
result.push(pluginModelFile);
|
|
5629
|
+
}
|
|
5630
|
+
}
|
|
5631
|
+
return result;
|
|
5632
|
+
}
|
|
5633
|
+
__name(getPluginDocuments, "getPluginDocuments");
|
|
5634
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
5635
|
+
if (!names.some((name) => !!name)) {
|
|
5636
|
+
return void 0;
|
|
5637
|
+
}
|
|
5638
|
+
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
|
|
5639
|
+
if (multiple === false && target) {
|
|
5640
|
+
return path.join(cwd, target);
|
|
5641
|
+
}
|
|
5642
|
+
if (target) {
|
|
5643
|
+
result.push(path.join(cwd, target));
|
|
5644
|
+
}
|
|
5645
|
+
const up = path.resolve(cwd, "..");
|
|
5646
|
+
if (up === cwd) {
|
|
5647
|
+
return multiple && result.length > 0 ? result : void 0;
|
|
5648
|
+
}
|
|
5649
|
+
return findUp(names, up, multiple, result);
|
|
5650
|
+
}
|
|
5651
|
+
__name(findUp, "findUp");
|
|
5530
5652
|
function findRootNode(node) {
|
|
5531
5653
|
while (node.$container) {
|
|
5532
5654
|
node = node.$container;
|
|
@@ -5536,6 +5658,8 @@ function findRootNode(node) {
|
|
|
5536
5658
|
__name(findRootNode, "findRootNode");
|
|
5537
5659
|
|
|
5538
5660
|
// src/validators/attribute-application-validator.ts
|
|
5661
|
+
import { AstUtils as AstUtils2 } from "langium";
|
|
5662
|
+
import pluralize from "pluralize";
|
|
5539
5663
|
function _ts_decorate(decorators, target, key, desc) {
|
|
5540
5664
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5541
5665
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -5649,6 +5773,7 @@ var AttributeApplicationValidator = class {
|
|
|
5649
5773
|
});
|
|
5650
5774
|
}
|
|
5651
5775
|
}
|
|
5776
|
+
// TODO: design a way to let plugin register validation
|
|
5652
5777
|
_checkModelLevelPolicy(attr, accept) {
|
|
5653
5778
|
const kind = getStringLiteral(attr.args[0]?.value);
|
|
5654
5779
|
if (!kind) {
|
|
@@ -5661,11 +5786,61 @@ var AttributeApplicationValidator = class {
|
|
|
5661
5786
|
"create",
|
|
5662
5787
|
"read",
|
|
5663
5788
|
"update",
|
|
5789
|
+
"post-update",
|
|
5664
5790
|
"delete",
|
|
5665
5791
|
"all"
|
|
5666
5792
|
], attr, accept);
|
|
5667
|
-
|
|
5793
|
+
if ((kind === "create" || kind === "all") && attr.args[1]?.value) {
|
|
5794
|
+
this.rejectNonOwnedRelationInExpression(attr.args[1].value, accept);
|
|
5795
|
+
}
|
|
5796
|
+
if (kind !== "post-update" && attr.args[1]?.value) {
|
|
5797
|
+
const beforeCall = AstUtils2.streamAst(attr.args[1]?.value).find(isBeforeInvocation);
|
|
5798
|
+
if (beforeCall) {
|
|
5799
|
+
accept("error", `"before()" is only allowed in "post-update" policy rules`, {
|
|
5800
|
+
node: beforeCall
|
|
5801
|
+
});
|
|
5802
|
+
}
|
|
5803
|
+
}
|
|
5668
5804
|
}
|
|
5805
|
+
rejectNonOwnedRelationInExpression(expr, accept) {
|
|
5806
|
+
const contextModel = AstUtils2.getContainerOfType(expr, isDataModel);
|
|
5807
|
+
if (!contextModel) {
|
|
5808
|
+
return;
|
|
5809
|
+
}
|
|
5810
|
+
if (AstUtils2.streamAst(expr).some((node) => {
|
|
5811
|
+
if (!isDataFieldReference(node)) {
|
|
5812
|
+
return false;
|
|
5813
|
+
}
|
|
5814
|
+
if (node.target.ref?.$container !== contextModel) {
|
|
5815
|
+
return false;
|
|
5816
|
+
}
|
|
5817
|
+
const field = node.target.ref;
|
|
5818
|
+
if (!isRelationshipField(field)) {
|
|
5819
|
+
return false;
|
|
5820
|
+
}
|
|
5821
|
+
if (isAuthOrAuthMemberAccess(node)) {
|
|
5822
|
+
return false;
|
|
5823
|
+
}
|
|
5824
|
+
const startNode = isCollectionPredicate(node.$container) && node.$container.left === node ? node.$container : node;
|
|
5825
|
+
const collectionPredicate = AstUtils2.getContainerOfType(startNode.$container, isCollectionPredicate);
|
|
5826
|
+
if (collectionPredicate && isAuthOrAuthMemberAccess(collectionPredicate.left)) {
|
|
5827
|
+
return false;
|
|
5828
|
+
}
|
|
5829
|
+
const relationAttr = field.attributes.find((attr) => attr.decl.ref?.name === "@relation");
|
|
5830
|
+
if (!relationAttr) {
|
|
5831
|
+
return true;
|
|
5832
|
+
}
|
|
5833
|
+
if (!relationAttr.args.some((arg) => arg.name === "fields")) {
|
|
5834
|
+
return true;
|
|
5835
|
+
}
|
|
5836
|
+
return false;
|
|
5837
|
+
})) {
|
|
5838
|
+
accept("error", `non-owned relation fields are not allowed in "create" rules`, {
|
|
5839
|
+
node: expr
|
|
5840
|
+
});
|
|
5841
|
+
}
|
|
5842
|
+
}
|
|
5843
|
+
// TODO: design a way to let plugin register validation
|
|
5669
5844
|
_checkFieldLevelPolicy(attr, accept) {
|
|
5670
5845
|
const kind = getStringLiteral(attr.args[0]?.value);
|
|
5671
5846
|
if (!kind) {
|
|
@@ -5680,8 +5855,8 @@ var AttributeApplicationValidator = class {
|
|
|
5680
5855
|
"all"
|
|
5681
5856
|
], attr, accept);
|
|
5682
5857
|
const expr = attr.args[1]?.value;
|
|
5683
|
-
if (expr && AstUtils2.streamAst(expr).some((node) =>
|
|
5684
|
-
accept("error", `"
|
|
5858
|
+
if (expr && AstUtils2.streamAst(expr).some((node) => isBeforeInvocation(node))) {
|
|
5859
|
+
accept("error", `"before()" is not allowed in field-level policy rules`, {
|
|
5685
5860
|
node: expr
|
|
5686
5861
|
});
|
|
5687
5862
|
}
|
|
@@ -5693,7 +5868,6 @@ var AttributeApplicationValidator = class {
|
|
|
5693
5868
|
});
|
|
5694
5869
|
}
|
|
5695
5870
|
}
|
|
5696
|
-
this.rejectEncryptedFields(attr, accept);
|
|
5697
5871
|
}
|
|
5698
5872
|
_checkValidate(attr, accept) {
|
|
5699
5873
|
const condition = attr.args[0]?.value;
|
|
@@ -5703,8 +5877,9 @@ var AttributeApplicationValidator = class {
|
|
|
5703
5877
|
});
|
|
5704
5878
|
}
|
|
5705
5879
|
}
|
|
5706
|
-
|
|
5880
|
+
_checkConstraint(attr, accept) {
|
|
5707
5881
|
const fields = attr.args[0]?.value;
|
|
5882
|
+
const attrName = attr.decl.ref?.name;
|
|
5708
5883
|
if (!fields) {
|
|
5709
5884
|
accept("error", `expects an array of field references`, {
|
|
5710
5885
|
node: attr.args[0]
|
|
@@ -5713,7 +5888,7 @@ var AttributeApplicationValidator = class {
|
|
|
5713
5888
|
}
|
|
5714
5889
|
if (isArrayExpr(fields)) {
|
|
5715
5890
|
if (fields.items.length === 0) {
|
|
5716
|
-
accept("error",
|
|
5891
|
+
accept("error", `\`${attrName}\` expects at least one field reference`, {
|
|
5717
5892
|
node: fields
|
|
5718
5893
|
});
|
|
5719
5894
|
return;
|
|
@@ -5732,7 +5907,7 @@ var AttributeApplicationValidator = class {
|
|
|
5732
5907
|
return;
|
|
5733
5908
|
}
|
|
5734
5909
|
if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
|
|
5735
|
-
accept("error", `Cannot use fields inherited from a polymorphic base model in
|
|
5910
|
+
accept("error", `Cannot use fields inherited from a polymorphic base model in \`${attrName}\``, {
|
|
5736
5911
|
node: item
|
|
5737
5912
|
});
|
|
5738
5913
|
}
|
|
@@ -5743,15 +5918,6 @@ var AttributeApplicationValidator = class {
|
|
|
5743
5918
|
});
|
|
5744
5919
|
}
|
|
5745
5920
|
}
|
|
5746
|
-
rejectEncryptedFields(attr, accept) {
|
|
5747
|
-
AstUtils2.streamAllContents(attr).forEach((node) => {
|
|
5748
|
-
if (isDataFieldReference(node) && hasAttribute(node.target.ref, "@encrypted")) {
|
|
5749
|
-
accept("error", `Encrypted fields cannot be used in policy rules`, {
|
|
5750
|
-
node
|
|
5751
|
-
});
|
|
5752
|
-
}
|
|
5753
|
-
});
|
|
5754
|
-
}
|
|
5755
5921
|
validatePolicyKinds(kind, candidates, attr, accept) {
|
|
5756
5922
|
const items = kind.split(",").map((x) => x.trim());
|
|
5757
5923
|
items.forEach((item) => {
|
|
@@ -5794,15 +5960,16 @@ _ts_decorate([
|
|
|
5794
5960
|
_ts_metadata("design:returntype", void 0)
|
|
5795
5961
|
], AttributeApplicationValidator.prototype, "_checkValidate", null);
|
|
5796
5962
|
_ts_decorate([
|
|
5797
|
-
check("@@unique"),
|
|
5798
5963
|
check("@@id"),
|
|
5964
|
+
check("@@index"),
|
|
5965
|
+
check("@@unique"),
|
|
5799
5966
|
_ts_metadata("design:type", Function),
|
|
5800
5967
|
_ts_metadata("design:paramtypes", [
|
|
5801
5968
|
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
5802
5969
|
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
5803
5970
|
]),
|
|
5804
5971
|
_ts_metadata("design:returntype", void 0)
|
|
5805
|
-
], AttributeApplicationValidator.prototype, "
|
|
5972
|
+
], AttributeApplicationValidator.prototype, "_checkConstraint", null);
|
|
5806
5973
|
function assignableToAttributeParam(arg, param, attr) {
|
|
5807
5974
|
const argResolvedType = arg.$resolvedType;
|
|
5808
5975
|
if (!argResolvedType) {
|
|
@@ -5812,6 +5979,10 @@ function assignableToAttributeParam(arg, param, attr) {
|
|
|
5812
5979
|
let dstIsArray = param.type.array;
|
|
5813
5980
|
if (dstType === "ContextType") {
|
|
5814
5981
|
if (isDataField(attr.$container)) {
|
|
5982
|
+
const dstIsTypedJson = hasAttribute(attr.$container, "@json");
|
|
5983
|
+
if (dstIsTypedJson && attr.decl.ref?.name === "@default") {
|
|
5984
|
+
return argResolvedType.decl === "String";
|
|
5985
|
+
}
|
|
5815
5986
|
dstIsArray = attr.$container.type.array;
|
|
5816
5987
|
}
|
|
5817
5988
|
}
|
|
@@ -5903,6 +6074,9 @@ function isValidAttributeTarget(attrDecl, targetDecl) {
|
|
|
5903
6074
|
case "TypeDefField":
|
|
5904
6075
|
allowed = allowed || isTypeDef(targetDecl.type.reference?.ref);
|
|
5905
6076
|
break;
|
|
6077
|
+
case "ListField":
|
|
6078
|
+
allowed = allowed || !isDataModel(targetDecl.type.reference?.ref) && targetDecl.type.array;
|
|
6079
|
+
break;
|
|
5906
6080
|
default:
|
|
5907
6081
|
break;
|
|
5908
6082
|
}
|
|
@@ -5929,7 +6103,7 @@ var AttributeValidator = class {
|
|
|
5929
6103
|
};
|
|
5930
6104
|
|
|
5931
6105
|
// src/validators/datamodel-validator.ts
|
|
5932
|
-
import { invariant
|
|
6106
|
+
import { invariant } from "@zenstackhq/common-helpers";
|
|
5933
6107
|
import { AstUtils as AstUtils3 } from "langium";
|
|
5934
6108
|
|
|
5935
6109
|
// src/validators/common.ts
|
|
@@ -6300,7 +6474,7 @@ var DataModelValidator = class {
|
|
|
6300
6474
|
if (!model.baseModel) {
|
|
6301
6475
|
return;
|
|
6302
6476
|
}
|
|
6303
|
-
|
|
6477
|
+
invariant(model.baseModel.ref, "baseModel must be resolved");
|
|
6304
6478
|
if (!isDelegateModel(model.baseModel.ref)) {
|
|
6305
6479
|
accept("error", `Model ${model.baseModel.$refText} cannot be extended because it's not a delegate model`, {
|
|
6306
6480
|
node: model,
|
|
@@ -6322,7 +6496,7 @@ var DataModelValidator = class {
|
|
|
6322
6496
|
}
|
|
6323
6497
|
seen.push(current);
|
|
6324
6498
|
if (current.baseModel) {
|
|
6325
|
-
|
|
6499
|
+
invariant(current.baseModel.ref, "baseModel must be resolved");
|
|
6326
6500
|
todo.push(current.baseModel.ref);
|
|
6327
6501
|
}
|
|
6328
6502
|
}
|
|
@@ -6452,11 +6626,21 @@ var ExpressionValidator = class {
|
|
|
6452
6626
|
}
|
|
6453
6627
|
}
|
|
6454
6628
|
switch (expr.$type) {
|
|
6629
|
+
case "MemberAccessExpr":
|
|
6630
|
+
this.validateMemberAccessExpr(expr, accept);
|
|
6631
|
+
break;
|
|
6455
6632
|
case "BinaryExpr":
|
|
6456
6633
|
this.validateBinaryExpr(expr, accept);
|
|
6457
6634
|
break;
|
|
6458
6635
|
}
|
|
6459
6636
|
}
|
|
6637
|
+
validateMemberAccessExpr(expr, accept) {
|
|
6638
|
+
if (isBeforeInvocation(expr.operand) && isDataModel(expr.$resolvedType?.decl)) {
|
|
6639
|
+
accept("error", "relation fields cannot be accessed from `before()`", {
|
|
6640
|
+
node: expr
|
|
6641
|
+
});
|
|
6642
|
+
}
|
|
6643
|
+
}
|
|
6460
6644
|
validateBinaryExpr(expr, accept) {
|
|
6461
6645
|
switch (expr.operator) {
|
|
6462
6646
|
case "in": {
|
|
@@ -6509,23 +6693,25 @@ var ExpressionValidator = class {
|
|
|
6509
6693
|
"Any"
|
|
6510
6694
|
];
|
|
6511
6695
|
}
|
|
6512
|
-
|
|
6696
|
+
const leftResolvedDecl = expr.left.$resolvedType?.decl;
|
|
6697
|
+
const rightResolvedDecl = expr.right.$resolvedType?.decl;
|
|
6698
|
+
if (leftResolvedDecl && (typeof leftResolvedDecl !== "string" || !supportedShapes.includes(leftResolvedDecl))) {
|
|
6513
6699
|
accept("error", `invalid operand type for "${expr.operator}" operator`, {
|
|
6514
6700
|
node: expr.left
|
|
6515
6701
|
});
|
|
6516
6702
|
return;
|
|
6517
6703
|
}
|
|
6518
|
-
if (typeof
|
|
6704
|
+
if (rightResolvedDecl && (typeof rightResolvedDecl !== "string" || !supportedShapes.includes(rightResolvedDecl))) {
|
|
6519
6705
|
accept("error", `invalid operand type for "${expr.operator}" operator`, {
|
|
6520
6706
|
node: expr.right
|
|
6521
6707
|
});
|
|
6522
6708
|
return;
|
|
6523
6709
|
}
|
|
6524
|
-
if (
|
|
6710
|
+
if (leftResolvedDecl === "DateTime" && rightResolvedDecl && rightResolvedDecl !== "DateTime") {
|
|
6525
6711
|
accept("error", "incompatible operand types", {
|
|
6526
6712
|
node: expr
|
|
6527
6713
|
});
|
|
6528
|
-
} else if (
|
|
6714
|
+
} else if (rightResolvedDecl === "DateTime" && leftResolvedDecl && leftResolvedDecl !== "DateTime") {
|
|
6529
6715
|
accept("error", "incompatible operand types", {
|
|
6530
6716
|
node: expr
|
|
6531
6717
|
});
|
|
@@ -6565,11 +6751,11 @@ var ExpressionValidator = class {
|
|
|
6565
6751
|
});
|
|
6566
6752
|
}
|
|
6567
6753
|
if (isDataFieldReference(expr.left) && (isThisExpr(expr.right) || isDataFieldReference(expr.right))) {
|
|
6568
|
-
accept("error", "comparison between
|
|
6754
|
+
accept("error", "comparison between models is not supported", {
|
|
6569
6755
|
node: expr
|
|
6570
6756
|
});
|
|
6571
6757
|
} else if (isDataFieldReference(expr.right) && (isThisExpr(expr.left) || isDataFieldReference(expr.left))) {
|
|
6572
|
-
accept("error", "comparison between
|
|
6758
|
+
accept("error", "comparison between models is not supported", {
|
|
6573
6759
|
node: expr
|
|
6574
6760
|
});
|
|
6575
6761
|
}
|
|
@@ -6659,41 +6845,39 @@ var FunctionInvocationValidator = class {
|
|
|
6659
6845
|
if (!this.validateArgs(funcDecl, expr.args, accept)) {
|
|
6660
6846
|
return;
|
|
6661
6847
|
}
|
|
6662
|
-
|
|
6663
|
-
|
|
6664
|
-
|
|
6665
|
-
|
|
6666
|
-
|
|
6667
|
-
|
|
6668
|
-
break;
|
|
6669
|
-
}
|
|
6670
|
-
curr = curr.$container;
|
|
6848
|
+
let curr = expr.$container;
|
|
6849
|
+
let containerAttribute;
|
|
6850
|
+
while (curr) {
|
|
6851
|
+
if (isDataModelAttribute(curr) || isDataFieldAttribute(curr)) {
|
|
6852
|
+
containerAttribute = curr;
|
|
6853
|
+
break;
|
|
6671
6854
|
}
|
|
6672
|
-
|
|
6673
|
-
|
|
6674
|
-
|
|
6675
|
-
|
|
6676
|
-
|
|
6855
|
+
curr = curr.$container;
|
|
6856
|
+
}
|
|
6857
|
+
const exprContext = this.getExpressionContext(containerAttribute);
|
|
6858
|
+
const funcAllowedContext = getFunctionExpressionContext(funcDecl);
|
|
6859
|
+
if (exprContext && !funcAllowedContext.includes(exprContext)) {
|
|
6860
|
+
accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
|
|
6861
|
+
node: expr
|
|
6862
|
+
});
|
|
6863
|
+
return;
|
|
6864
|
+
}
|
|
6865
|
+
const allCasing = [
|
|
6866
|
+
"original",
|
|
6867
|
+
"upper",
|
|
6868
|
+
"lower",
|
|
6869
|
+
"capitalize",
|
|
6870
|
+
"uncapitalize"
|
|
6871
|
+
];
|
|
6872
|
+
if ([
|
|
6873
|
+
"currentModel",
|
|
6874
|
+
"currentOperation"
|
|
6875
|
+
].includes(funcDecl.name)) {
|
|
6876
|
+
const arg = getLiteral(expr.args[0]?.value);
|
|
6877
|
+
if (arg && !allCasing.includes(arg)) {
|
|
6878
|
+
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6879
|
+
node: expr.args[0]
|
|
6677
6880
|
});
|
|
6678
|
-
return;
|
|
6679
|
-
}
|
|
6680
|
-
const allCasing = [
|
|
6681
|
-
"original",
|
|
6682
|
-
"upper",
|
|
6683
|
-
"lower",
|
|
6684
|
-
"capitalize",
|
|
6685
|
-
"uncapitalize"
|
|
6686
|
-
];
|
|
6687
|
-
if ([
|
|
6688
|
-
"currentModel",
|
|
6689
|
-
"currentOperation"
|
|
6690
|
-
].includes(funcDecl.name)) {
|
|
6691
|
-
const arg = getLiteral(expr.args[0]?.value);
|
|
6692
|
-
if (arg && !allCasing.includes(arg)) {
|
|
6693
|
-
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6694
|
-
node: expr.args[0]
|
|
6695
|
-
});
|
|
6696
|
-
}
|
|
6697
6881
|
}
|
|
6698
6882
|
}
|
|
6699
6883
|
const checker = invocationCheckers.get(expr.function.$refText);
|
|
@@ -6701,6 +6885,18 @@ var FunctionInvocationValidator = class {
|
|
|
6701
6885
|
checker.value.call(this, expr, accept);
|
|
6702
6886
|
}
|
|
6703
6887
|
}
|
|
6888
|
+
getExpressionContext(containerAttribute) {
|
|
6889
|
+
if (!containerAttribute) {
|
|
6890
|
+
return void 0;
|
|
6891
|
+
}
|
|
6892
|
+
if (this.isValidationAttribute(containerAttribute)) {
|
|
6893
|
+
return ExpressionContext.ValidationRule;
|
|
6894
|
+
}
|
|
6895
|
+
return match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
|
|
6896
|
+
}
|
|
6897
|
+
isValidationAttribute(attr) {
|
|
6898
|
+
return !!attr.decl.ref?.attributes.some((attr2) => attr2.decl.$refText === "@@@validation");
|
|
6899
|
+
}
|
|
6704
6900
|
validateArgs(funcDecl, args, accept) {
|
|
6705
6901
|
let success = true;
|
|
6706
6902
|
for (let i = 0; i < funcDecl.params.length; i++) {
|
|
@@ -6761,6 +6957,44 @@ var FunctionInvocationValidator = class {
|
|
|
6761
6957
|
}
|
|
6762
6958
|
return true;
|
|
6763
6959
|
}
|
|
6960
|
+
_checkLength(expr, accept) {
|
|
6961
|
+
const msg = "argument must be a string or list field";
|
|
6962
|
+
const fieldArg = expr.args[0].value;
|
|
6963
|
+
if (!isDataFieldReference(fieldArg)) {
|
|
6964
|
+
accept("error", msg, {
|
|
6965
|
+
node: expr.args[0]
|
|
6966
|
+
});
|
|
6967
|
+
return;
|
|
6968
|
+
}
|
|
6969
|
+
if (isDataModel(fieldArg.$resolvedType?.decl)) {
|
|
6970
|
+
accept("error", msg, {
|
|
6971
|
+
node: expr.args[0]
|
|
6972
|
+
});
|
|
6973
|
+
return;
|
|
6974
|
+
}
|
|
6975
|
+
if (!fieldArg.$resolvedType?.array && fieldArg.$resolvedType?.decl !== "String") {
|
|
6976
|
+
accept("error", msg, {
|
|
6977
|
+
node: expr.args[0]
|
|
6978
|
+
});
|
|
6979
|
+
}
|
|
6980
|
+
}
|
|
6981
|
+
_checkRegex(expr, accept) {
|
|
6982
|
+
const regex = expr.args[1]?.value;
|
|
6983
|
+
if (!isStringLiteral(regex)) {
|
|
6984
|
+
accept("error", "second argument must be a string literal", {
|
|
6985
|
+
node: expr.args[1]
|
|
6986
|
+
});
|
|
6987
|
+
return;
|
|
6988
|
+
}
|
|
6989
|
+
try {
|
|
6990
|
+
new RegExp(regex.value);
|
|
6991
|
+
} catch (e) {
|
|
6992
|
+
accept("error", "invalid regular expression: " + e.message, {
|
|
6993
|
+
node: expr.args[1]
|
|
6994
|
+
});
|
|
6995
|
+
}
|
|
6996
|
+
}
|
|
6997
|
+
// TODO: move this to policy plugin
|
|
6764
6998
|
_checkCheck(expr, accept) {
|
|
6765
6999
|
let valid = true;
|
|
6766
7000
|
const fieldArg = expr.args[0].value;
|
|
@@ -6832,6 +7066,24 @@ var FunctionInvocationValidator = class {
|
|
|
6832
7066
|
}
|
|
6833
7067
|
}
|
|
6834
7068
|
};
|
|
7069
|
+
_ts_decorate2([
|
|
7070
|
+
func("length"),
|
|
7071
|
+
_ts_metadata2("design:type", Function),
|
|
7072
|
+
_ts_metadata2("design:paramtypes", [
|
|
7073
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7074
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7075
|
+
]),
|
|
7076
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7077
|
+
], FunctionInvocationValidator.prototype, "_checkLength", null);
|
|
7078
|
+
_ts_decorate2([
|
|
7079
|
+
func("regex"),
|
|
7080
|
+
_ts_metadata2("design:type", Function),
|
|
7081
|
+
_ts_metadata2("design:paramtypes", [
|
|
7082
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7083
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7084
|
+
]),
|
|
7085
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7086
|
+
], FunctionInvocationValidator.prototype, "_checkRegex", null);
|
|
6835
7087
|
_ts_decorate2([
|
|
6836
7088
|
func("check"),
|
|
6837
7089
|
_ts_metadata2("design:type", Function),
|
|
@@ -6941,50 +7193,1039 @@ var ZModelValidator = class {
|
|
|
6941
7193
|
constructor(services) {
|
|
6942
7194
|
this.services = services;
|
|
6943
7195
|
}
|
|
6944
|
-
shouldCheck(node) {
|
|
6945
|
-
let doc;
|
|
6946
|
-
let currNode = node;
|
|
6947
|
-
while (currNode) {
|
|
6948
|
-
if (currNode.$document) {
|
|
6949
|
-
doc = currNode.$document;
|
|
6950
|
-
break;
|
|
6951
|
-
}
|
|
6952
|
-
currNode = currNode.$container;
|
|
6953
|
-
}
|
|
6954
|
-
return doc?.parseResult.lexerErrors.length === 0 && doc?.parseResult.parserErrors.length === 0;
|
|
6955
|
-
}
|
|
6956
7196
|
checkModel(node, accept) {
|
|
6957
|
-
|
|
7197
|
+
new SchemaValidator(this.services.shared.workspace.LangiumDocuments).validate(node, accept);
|
|
6958
7198
|
}
|
|
6959
7199
|
checkDataSource(node, accept) {
|
|
6960
|
-
|
|
7200
|
+
new DataSourceValidator().validate(node, accept);
|
|
6961
7201
|
}
|
|
6962
7202
|
checkDataModel(node, accept) {
|
|
6963
|
-
|
|
7203
|
+
new DataModelValidator().validate(node, accept);
|
|
6964
7204
|
}
|
|
6965
7205
|
checkTypeDef(node, accept) {
|
|
6966
|
-
|
|
7206
|
+
new TypeDefValidator().validate(node, accept);
|
|
6967
7207
|
}
|
|
6968
7208
|
checkEnum(node, accept) {
|
|
6969
|
-
|
|
7209
|
+
new EnumValidator().validate(node, accept);
|
|
6970
7210
|
}
|
|
6971
7211
|
checkAttribute(node, accept) {
|
|
6972
|
-
|
|
7212
|
+
new AttributeValidator().validate(node, accept);
|
|
6973
7213
|
}
|
|
6974
7214
|
checkExpression(node, accept) {
|
|
6975
|
-
|
|
7215
|
+
new ExpressionValidator().validate(node, accept);
|
|
6976
7216
|
}
|
|
6977
7217
|
checkFunctionInvocation(node, accept) {
|
|
6978
|
-
|
|
7218
|
+
new FunctionInvocationValidator().validate(node, accept);
|
|
6979
7219
|
}
|
|
6980
7220
|
checkFunctionDecl(node, accept) {
|
|
6981
|
-
|
|
7221
|
+
new FunctionDeclValidator().validate(node, accept);
|
|
6982
7222
|
}
|
|
6983
7223
|
};
|
|
6984
7224
|
|
|
6985
|
-
// src/zmodel-
|
|
6986
|
-
import {
|
|
7225
|
+
// src/zmodel-comment-provider.ts
|
|
7226
|
+
import { DefaultCommentProvider } from "langium";
|
|
6987
7227
|
import { match as match2 } from "ts-pattern";
|
|
7228
|
+
var ZModelCommentProvider = class extends DefaultCommentProvider {
|
|
7229
|
+
static {
|
|
7230
|
+
__name(this, "ZModelCommentProvider");
|
|
7231
|
+
}
|
|
7232
|
+
getComment(node) {
|
|
7233
|
+
let comment = super.getComment(node);
|
|
7234
|
+
if (!comment) {
|
|
7235
|
+
comment = match2(node).when(isDataModel, (d) => `/**
|
|
7236
|
+
* Model *${d.name}*
|
|
7237
|
+
*/`).when(isTypeDef, (d) => `/**
|
|
7238
|
+
* Type *${d.name}*
|
|
7239
|
+
*/`).when(isEnum, (e) => `/**
|
|
7240
|
+
* Enum *${e.name}*
|
|
7241
|
+
*/`).when(isEnumField, (f) => `/**
|
|
7242
|
+
* Value of enum *${f.$container?.name}*
|
|
7243
|
+
*/`).when(isDataField, (f) => `/**
|
|
7244
|
+
* Field of *${f.$container?.name}*
|
|
7245
|
+
*/`).when(isFunctionDecl, (f) => `/**
|
|
7246
|
+
* Function *${f.name}*
|
|
7247
|
+
*/`).otherwise(() => "");
|
|
7248
|
+
}
|
|
7249
|
+
return comment;
|
|
7250
|
+
}
|
|
7251
|
+
};
|
|
7252
|
+
|
|
7253
|
+
// src/zmodel-completion-provider.ts
|
|
7254
|
+
import { DefaultCompletionProvider } from "langium/lsp";
|
|
7255
|
+
import fs2 from "fs";
|
|
7256
|
+
import { P as P2, match as match3 } from "ts-pattern";
|
|
7257
|
+
import { CompletionItemKind } from "vscode-languageserver";
|
|
7258
|
+
|
|
7259
|
+
// src/zmodel-code-generator.ts
|
|
7260
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
7261
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
7262
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
7263
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
7264
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7265
|
+
}
|
|
7266
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
7267
|
+
function _ts_metadata3(k, v) {
|
|
7268
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
7269
|
+
}
|
|
7270
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
7271
|
+
var generationHandlers = /* @__PURE__ */ new Map();
|
|
7272
|
+
function gen(name) {
|
|
7273
|
+
return function(_target, _propertyKey, descriptor) {
|
|
7274
|
+
if (!generationHandlers.get(name)) {
|
|
7275
|
+
generationHandlers.set(name, descriptor);
|
|
7276
|
+
}
|
|
7277
|
+
return descriptor;
|
|
7278
|
+
};
|
|
7279
|
+
}
|
|
7280
|
+
__name(gen, "gen");
|
|
7281
|
+
var ZModelCodeGenerator = class {
|
|
7282
|
+
static {
|
|
7283
|
+
__name(this, "ZModelCodeGenerator");
|
|
7284
|
+
}
|
|
7285
|
+
options;
|
|
7286
|
+
constructor(options) {
|
|
7287
|
+
this.options = {
|
|
7288
|
+
binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
|
|
7289
|
+
unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
|
|
7290
|
+
indent: options?.indent ?? 4,
|
|
7291
|
+
quote: options?.quote ?? "single"
|
|
7292
|
+
};
|
|
7293
|
+
}
|
|
7294
|
+
/**
|
|
7295
|
+
* Generates ZModel source code from AST.
|
|
7296
|
+
*/
|
|
7297
|
+
generate(ast) {
|
|
7298
|
+
const handler = generationHandlers.get(ast.$type);
|
|
7299
|
+
if (!handler) {
|
|
7300
|
+
throw new Error(`No generation handler found for ${ast.$type}`);
|
|
7301
|
+
}
|
|
7302
|
+
return handler.value.call(this, ast);
|
|
7303
|
+
}
|
|
7304
|
+
_generateModel(ast) {
|
|
7305
|
+
return ast.declarations.map((d) => this.generate(d)).join("\n\n");
|
|
7306
|
+
}
|
|
7307
|
+
_generateDataSource(ast) {
|
|
7308
|
+
return `datasource ${ast.name} {
|
|
7309
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7310
|
+
}`;
|
|
7311
|
+
}
|
|
7312
|
+
_generateEnum(ast) {
|
|
7313
|
+
return `enum ${ast.name} {
|
|
7314
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7315
|
+
}`;
|
|
7316
|
+
}
|
|
7317
|
+
_generateEnumField(ast) {
|
|
7318
|
+
return `${ast.name}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7319
|
+
}
|
|
7320
|
+
_generateGenerator(ast) {
|
|
7321
|
+
return `generator ${ast.name} {
|
|
7322
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7323
|
+
}`;
|
|
7324
|
+
}
|
|
7325
|
+
_generateConfigField(ast) {
|
|
7326
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7327
|
+
}
|
|
7328
|
+
_generateConfigArrayExpr(ast) {
|
|
7329
|
+
return `[${ast.items.map((x) => this.generate(x)).join(", ")}]`;
|
|
7330
|
+
}
|
|
7331
|
+
_generateConfigInvocationExpr(ast) {
|
|
7332
|
+
if (ast.args.length === 0) {
|
|
7333
|
+
return ast.name;
|
|
7334
|
+
} else {
|
|
7335
|
+
return `${ast.name}(${ast.args.map((x) => (x.name ? x.name + ": " : "") + this.generate(x.value)).join(", ")})`;
|
|
7336
|
+
}
|
|
7337
|
+
}
|
|
7338
|
+
_generatePlugin(ast) {
|
|
7339
|
+
return `plugin ${ast.name} {
|
|
7340
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7341
|
+
}`;
|
|
7342
|
+
}
|
|
7343
|
+
_generatePluginField(ast) {
|
|
7344
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7345
|
+
}
|
|
7346
|
+
_generateDataModel(ast) {
|
|
7347
|
+
return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.ref?.name).join(", ") : ""} {
|
|
7348
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
7349
|
+
}`;
|
|
7350
|
+
}
|
|
7351
|
+
_generateDataField(ast) {
|
|
7352
|
+
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7353
|
+
}
|
|
7354
|
+
fieldType(type) {
|
|
7355
|
+
const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
|
|
7356
|
+
return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
|
|
7357
|
+
}
|
|
7358
|
+
_generateDataModelAttribute(ast) {
|
|
7359
|
+
return this.attribute(ast);
|
|
7360
|
+
}
|
|
7361
|
+
_generateDataFieldAttribute(ast) {
|
|
7362
|
+
return this.attribute(ast);
|
|
7363
|
+
}
|
|
7364
|
+
attribute(ast) {
|
|
7365
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7366
|
+
return `${resolved(ast.decl).name}${args}`;
|
|
7367
|
+
}
|
|
7368
|
+
_generateAttributeArg(ast) {
|
|
7369
|
+
if (ast.name) {
|
|
7370
|
+
return `${ast.name}: ${this.generate(ast.value)}`;
|
|
7371
|
+
} else {
|
|
7372
|
+
return this.generate(ast.value);
|
|
7373
|
+
}
|
|
7374
|
+
}
|
|
7375
|
+
_generateObjectExpr(ast) {
|
|
7376
|
+
return `{ ${ast.fields.map((field) => this.objectField(field)).join(", ")} }`;
|
|
7377
|
+
}
|
|
7378
|
+
objectField(field) {
|
|
7379
|
+
return `${field.name}: ${this.generate(field.value)}`;
|
|
7380
|
+
}
|
|
7381
|
+
_generateArrayExpr(ast) {
|
|
7382
|
+
return `[${ast.items.map((item) => this.generate(item)).join(", ")}]`;
|
|
7383
|
+
}
|
|
7384
|
+
_generateLiteralExpr(ast) {
|
|
7385
|
+
return this.options.quote === "single" ? `'${ast.value}'` : `"${ast.value}"`;
|
|
7386
|
+
}
|
|
7387
|
+
_generateNumberLiteral(ast) {
|
|
7388
|
+
return ast.value.toString();
|
|
7389
|
+
}
|
|
7390
|
+
_generateBooleanLiteral(ast) {
|
|
7391
|
+
return ast.value.toString();
|
|
7392
|
+
}
|
|
7393
|
+
_generateUnaryExpr(ast) {
|
|
7394
|
+
return `${ast.operator}${this.unaryExprSpace}${this.generate(ast.operand)}`;
|
|
7395
|
+
}
|
|
7396
|
+
_generateBinaryExpr(ast) {
|
|
7397
|
+
const operator = ast.operator;
|
|
7398
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7399
|
+
const rightExpr = this.generate(ast.right);
|
|
7400
|
+
const { left: isLeftParenthesis, right: isRightParenthesis } = this.isParenthesesNeededForBinaryExpr(ast);
|
|
7401
|
+
return `${isLeftParenthesis ? "(" : ""}${this.generate(ast.left)}${isLeftParenthesis ? ")" : ""}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${operator}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${isRightParenthesis ? "(" : ""}${isCollectionPredicate2 ? `[${rightExpr}]` : rightExpr}${isRightParenthesis ? ")" : ""}`;
|
|
7402
|
+
}
|
|
7403
|
+
_generateReferenceExpr(ast) {
|
|
7404
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7405
|
+
return `${ast.target.ref?.name}${args}`;
|
|
7406
|
+
}
|
|
7407
|
+
_generateReferenceArg(ast) {
|
|
7408
|
+
return `${ast.name}:${this.generate(ast.value)}`;
|
|
7409
|
+
}
|
|
7410
|
+
_generateMemberExpr(ast) {
|
|
7411
|
+
return `${this.generate(ast.operand)}.${ast.member.ref?.name}`;
|
|
7412
|
+
}
|
|
7413
|
+
_generateInvocationExpr(ast) {
|
|
7414
|
+
return `${ast.function.ref?.name}(${ast.args.map((x) => this.argument(x)).join(", ")})`;
|
|
7415
|
+
}
|
|
7416
|
+
_generateNullExpr() {
|
|
7417
|
+
return "null";
|
|
7418
|
+
}
|
|
7419
|
+
_generateThisExpr() {
|
|
7420
|
+
return "this";
|
|
7421
|
+
}
|
|
7422
|
+
_generateAttribute(ast) {
|
|
7423
|
+
return `attribute ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")})`;
|
|
7424
|
+
}
|
|
7425
|
+
_generateAttributeParam(ast) {
|
|
7426
|
+
return `${ast.default ? "_ " : ""}${ast.name}: ${this.generate(ast.type)}`;
|
|
7427
|
+
}
|
|
7428
|
+
_generateAttributeParamType(ast) {
|
|
7429
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}${ast.optional ? "?" : ""}`;
|
|
7430
|
+
}
|
|
7431
|
+
_generateFunctionDecl(ast) {
|
|
7432
|
+
return `function ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")}) ${ast.returnType ? ": " + this.generate(ast.returnType) : ""} {}`;
|
|
7433
|
+
}
|
|
7434
|
+
_generateFunctionParam(ast) {
|
|
7435
|
+
return `${ast.name}: ${this.generate(ast.type)}`;
|
|
7436
|
+
}
|
|
7437
|
+
_generateFunctionParamType(ast) {
|
|
7438
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}`;
|
|
7439
|
+
}
|
|
7440
|
+
_generateTypeDef(ast) {
|
|
7441
|
+
return `type ${ast.name} {
|
|
7442
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
7443
|
+
}`;
|
|
7444
|
+
}
|
|
7445
|
+
argument(ast) {
|
|
7446
|
+
return this.generate(ast.value);
|
|
7447
|
+
}
|
|
7448
|
+
get binaryExprSpace() {
|
|
7449
|
+
return " ".repeat(this.options.binaryExprNumberOfSpaces);
|
|
7450
|
+
}
|
|
7451
|
+
get unaryExprSpace() {
|
|
7452
|
+
return " ".repeat(this.options.unaryExprNumberOfSpaces);
|
|
7453
|
+
}
|
|
7454
|
+
get indent() {
|
|
7455
|
+
return " ".repeat(this.options.indent);
|
|
7456
|
+
}
|
|
7457
|
+
isParenthesesNeededForBinaryExpr(ast) {
|
|
7458
|
+
const result = {
|
|
7459
|
+
left: false,
|
|
7460
|
+
right: false
|
|
7461
|
+
};
|
|
7462
|
+
const operator = ast.operator;
|
|
7463
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7464
|
+
const currentPriority = BinaryExprOperatorPriority[operator];
|
|
7465
|
+
if (ast.left.$type === BinaryExpr && BinaryExprOperatorPriority[ast.left["operator"]] < currentPriority) {
|
|
7466
|
+
result.left = true;
|
|
7467
|
+
}
|
|
7468
|
+
if (!isCollectionPredicate2 && ast.right.$type === BinaryExpr && BinaryExprOperatorPriority[ast.right["operator"]] <= currentPriority) {
|
|
7469
|
+
result.right = true;
|
|
7470
|
+
}
|
|
7471
|
+
return result;
|
|
7472
|
+
}
|
|
7473
|
+
isCollectionPredicateOperator(op) {
|
|
7474
|
+
return [
|
|
7475
|
+
"?",
|
|
7476
|
+
"!",
|
|
7477
|
+
"^"
|
|
7478
|
+
].includes(op);
|
|
7479
|
+
}
|
|
7480
|
+
};
|
|
7481
|
+
_ts_decorate3([
|
|
7482
|
+
gen(Model),
|
|
7483
|
+
_ts_metadata3("design:type", Function),
|
|
7484
|
+
_ts_metadata3("design:paramtypes", [
|
|
7485
|
+
typeof Model === "undefined" ? Object : Model
|
|
7486
|
+
]),
|
|
7487
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7488
|
+
], ZModelCodeGenerator.prototype, "_generateModel", null);
|
|
7489
|
+
_ts_decorate3([
|
|
7490
|
+
gen(DataSource),
|
|
7491
|
+
_ts_metadata3("design:type", Function),
|
|
7492
|
+
_ts_metadata3("design:paramtypes", [
|
|
7493
|
+
typeof DataSource === "undefined" ? Object : DataSource
|
|
7494
|
+
]),
|
|
7495
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7496
|
+
], ZModelCodeGenerator.prototype, "_generateDataSource", null);
|
|
7497
|
+
_ts_decorate3([
|
|
7498
|
+
gen(Enum),
|
|
7499
|
+
_ts_metadata3("design:type", Function),
|
|
7500
|
+
_ts_metadata3("design:paramtypes", [
|
|
7501
|
+
typeof Enum === "undefined" ? Object : Enum
|
|
7502
|
+
]),
|
|
7503
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7504
|
+
], ZModelCodeGenerator.prototype, "_generateEnum", null);
|
|
7505
|
+
_ts_decorate3([
|
|
7506
|
+
gen(EnumField),
|
|
7507
|
+
_ts_metadata3("design:type", Function),
|
|
7508
|
+
_ts_metadata3("design:paramtypes", [
|
|
7509
|
+
typeof EnumField === "undefined" ? Object : EnumField
|
|
7510
|
+
]),
|
|
7511
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7512
|
+
], ZModelCodeGenerator.prototype, "_generateEnumField", null);
|
|
7513
|
+
_ts_decorate3([
|
|
7514
|
+
gen(GeneratorDecl),
|
|
7515
|
+
_ts_metadata3("design:type", Function),
|
|
7516
|
+
_ts_metadata3("design:paramtypes", [
|
|
7517
|
+
typeof GeneratorDecl === "undefined" ? Object : GeneratorDecl
|
|
7518
|
+
]),
|
|
7519
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7520
|
+
], ZModelCodeGenerator.prototype, "_generateGenerator", null);
|
|
7521
|
+
_ts_decorate3([
|
|
7522
|
+
gen(ConfigField),
|
|
7523
|
+
_ts_metadata3("design:type", Function),
|
|
7524
|
+
_ts_metadata3("design:paramtypes", [
|
|
7525
|
+
typeof ConfigField === "undefined" ? Object : ConfigField
|
|
7526
|
+
]),
|
|
7527
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7528
|
+
], ZModelCodeGenerator.prototype, "_generateConfigField", null);
|
|
7529
|
+
_ts_decorate3([
|
|
7530
|
+
gen(ConfigArrayExpr),
|
|
7531
|
+
_ts_metadata3("design:type", Function),
|
|
7532
|
+
_ts_metadata3("design:paramtypes", [
|
|
7533
|
+
typeof ConfigArrayExpr === "undefined" ? Object : ConfigArrayExpr
|
|
7534
|
+
]),
|
|
7535
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7536
|
+
], ZModelCodeGenerator.prototype, "_generateConfigArrayExpr", null);
|
|
7537
|
+
_ts_decorate3([
|
|
7538
|
+
gen(ConfigInvocationExpr),
|
|
7539
|
+
_ts_metadata3("design:type", Function),
|
|
7540
|
+
_ts_metadata3("design:paramtypes", [
|
|
7541
|
+
typeof ConfigInvocationExpr === "undefined" ? Object : ConfigInvocationExpr
|
|
7542
|
+
]),
|
|
7543
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7544
|
+
], ZModelCodeGenerator.prototype, "_generateConfigInvocationExpr", null);
|
|
7545
|
+
_ts_decorate3([
|
|
7546
|
+
gen(Plugin),
|
|
7547
|
+
_ts_metadata3("design:type", Function),
|
|
7548
|
+
_ts_metadata3("design:paramtypes", [
|
|
7549
|
+
typeof Plugin === "undefined" ? Object : Plugin
|
|
7550
|
+
]),
|
|
7551
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7552
|
+
], ZModelCodeGenerator.prototype, "_generatePlugin", null);
|
|
7553
|
+
_ts_decorate3([
|
|
7554
|
+
gen(PluginField),
|
|
7555
|
+
_ts_metadata3("design:type", Function),
|
|
7556
|
+
_ts_metadata3("design:paramtypes", [
|
|
7557
|
+
typeof PluginField === "undefined" ? Object : PluginField
|
|
7558
|
+
]),
|
|
7559
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7560
|
+
], ZModelCodeGenerator.prototype, "_generatePluginField", null);
|
|
7561
|
+
_ts_decorate3([
|
|
7562
|
+
gen(DataModel),
|
|
7563
|
+
_ts_metadata3("design:type", Function),
|
|
7564
|
+
_ts_metadata3("design:paramtypes", [
|
|
7565
|
+
typeof DataModel === "undefined" ? Object : DataModel
|
|
7566
|
+
]),
|
|
7567
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7568
|
+
], ZModelCodeGenerator.prototype, "_generateDataModel", null);
|
|
7569
|
+
_ts_decorate3([
|
|
7570
|
+
gen(DataField),
|
|
7571
|
+
_ts_metadata3("design:type", Function),
|
|
7572
|
+
_ts_metadata3("design:paramtypes", [
|
|
7573
|
+
typeof DataField === "undefined" ? Object : DataField
|
|
7574
|
+
]),
|
|
7575
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7576
|
+
], ZModelCodeGenerator.prototype, "_generateDataField", null);
|
|
7577
|
+
_ts_decorate3([
|
|
7578
|
+
gen(DataModelAttribute),
|
|
7579
|
+
_ts_metadata3("design:type", Function),
|
|
7580
|
+
_ts_metadata3("design:paramtypes", [
|
|
7581
|
+
typeof DataModelAttribute === "undefined" ? Object : DataModelAttribute
|
|
7582
|
+
]),
|
|
7583
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7584
|
+
], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
|
|
7585
|
+
_ts_decorate3([
|
|
7586
|
+
gen(DataFieldAttribute),
|
|
7587
|
+
_ts_metadata3("design:type", Function),
|
|
7588
|
+
_ts_metadata3("design:paramtypes", [
|
|
7589
|
+
typeof DataFieldAttribute === "undefined" ? Object : DataFieldAttribute
|
|
7590
|
+
]),
|
|
7591
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7592
|
+
], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
|
|
7593
|
+
_ts_decorate3([
|
|
7594
|
+
gen(AttributeArg),
|
|
7595
|
+
_ts_metadata3("design:type", Function),
|
|
7596
|
+
_ts_metadata3("design:paramtypes", [
|
|
7597
|
+
typeof AttributeArg === "undefined" ? Object : AttributeArg
|
|
7598
|
+
]),
|
|
7599
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7600
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeArg", null);
|
|
7601
|
+
_ts_decorate3([
|
|
7602
|
+
gen(ObjectExpr),
|
|
7603
|
+
_ts_metadata3("design:type", Function),
|
|
7604
|
+
_ts_metadata3("design:paramtypes", [
|
|
7605
|
+
typeof ObjectExpr === "undefined" ? Object : ObjectExpr
|
|
7606
|
+
]),
|
|
7607
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7608
|
+
], ZModelCodeGenerator.prototype, "_generateObjectExpr", null);
|
|
7609
|
+
_ts_decorate3([
|
|
7610
|
+
gen(ArrayExpr),
|
|
7611
|
+
_ts_metadata3("design:type", Function),
|
|
7612
|
+
_ts_metadata3("design:paramtypes", [
|
|
7613
|
+
typeof ArrayExpr === "undefined" ? Object : ArrayExpr
|
|
7614
|
+
]),
|
|
7615
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7616
|
+
], ZModelCodeGenerator.prototype, "_generateArrayExpr", null);
|
|
7617
|
+
_ts_decorate3([
|
|
7618
|
+
gen(StringLiteral),
|
|
7619
|
+
_ts_metadata3("design:type", Function),
|
|
7620
|
+
_ts_metadata3("design:paramtypes", [
|
|
7621
|
+
typeof LiteralExpr === "undefined" ? Object : LiteralExpr
|
|
7622
|
+
]),
|
|
7623
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7624
|
+
], ZModelCodeGenerator.prototype, "_generateLiteralExpr", null);
|
|
7625
|
+
_ts_decorate3([
|
|
7626
|
+
gen(NumberLiteral),
|
|
7627
|
+
_ts_metadata3("design:type", Function),
|
|
7628
|
+
_ts_metadata3("design:paramtypes", [
|
|
7629
|
+
typeof NumberLiteral === "undefined" ? Object : NumberLiteral
|
|
7630
|
+
]),
|
|
7631
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7632
|
+
], ZModelCodeGenerator.prototype, "_generateNumberLiteral", null);
|
|
7633
|
+
_ts_decorate3([
|
|
7634
|
+
gen(BooleanLiteral),
|
|
7635
|
+
_ts_metadata3("design:type", Function),
|
|
7636
|
+
_ts_metadata3("design:paramtypes", [
|
|
7637
|
+
typeof BooleanLiteral === "undefined" ? Object : BooleanLiteral
|
|
7638
|
+
]),
|
|
7639
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7640
|
+
], ZModelCodeGenerator.prototype, "_generateBooleanLiteral", null);
|
|
7641
|
+
_ts_decorate3([
|
|
7642
|
+
gen(UnaryExpr),
|
|
7643
|
+
_ts_metadata3("design:type", Function),
|
|
7644
|
+
_ts_metadata3("design:paramtypes", [
|
|
7645
|
+
typeof UnaryExpr === "undefined" ? Object : UnaryExpr
|
|
7646
|
+
]),
|
|
7647
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7648
|
+
], ZModelCodeGenerator.prototype, "_generateUnaryExpr", null);
|
|
7649
|
+
_ts_decorate3([
|
|
7650
|
+
gen(BinaryExpr),
|
|
7651
|
+
_ts_metadata3("design:type", Function),
|
|
7652
|
+
_ts_metadata3("design:paramtypes", [
|
|
7653
|
+
typeof BinaryExpr === "undefined" ? Object : BinaryExpr
|
|
7654
|
+
]),
|
|
7655
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7656
|
+
], ZModelCodeGenerator.prototype, "_generateBinaryExpr", null);
|
|
7657
|
+
_ts_decorate3([
|
|
7658
|
+
gen(ReferenceExpr),
|
|
7659
|
+
_ts_metadata3("design:type", Function),
|
|
7660
|
+
_ts_metadata3("design:paramtypes", [
|
|
7661
|
+
typeof ReferenceExpr === "undefined" ? Object : ReferenceExpr
|
|
7662
|
+
]),
|
|
7663
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7664
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceExpr", null);
|
|
7665
|
+
_ts_decorate3([
|
|
7666
|
+
gen(ReferenceArg),
|
|
7667
|
+
_ts_metadata3("design:type", Function),
|
|
7668
|
+
_ts_metadata3("design:paramtypes", [
|
|
7669
|
+
typeof ReferenceArg === "undefined" ? Object : ReferenceArg
|
|
7670
|
+
]),
|
|
7671
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7672
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceArg", null);
|
|
7673
|
+
_ts_decorate3([
|
|
7674
|
+
gen(MemberAccessExpr),
|
|
7675
|
+
_ts_metadata3("design:type", Function),
|
|
7676
|
+
_ts_metadata3("design:paramtypes", [
|
|
7677
|
+
typeof MemberAccessExpr === "undefined" ? Object : MemberAccessExpr
|
|
7678
|
+
]),
|
|
7679
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7680
|
+
], ZModelCodeGenerator.prototype, "_generateMemberExpr", null);
|
|
7681
|
+
_ts_decorate3([
|
|
7682
|
+
gen(InvocationExpr),
|
|
7683
|
+
_ts_metadata3("design:type", Function),
|
|
7684
|
+
_ts_metadata3("design:paramtypes", [
|
|
7685
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr
|
|
7686
|
+
]),
|
|
7687
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7688
|
+
], ZModelCodeGenerator.prototype, "_generateInvocationExpr", null);
|
|
7689
|
+
_ts_decorate3([
|
|
7690
|
+
gen(NullExpr),
|
|
7691
|
+
_ts_metadata3("design:type", Function),
|
|
7692
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7693
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7694
|
+
], ZModelCodeGenerator.prototype, "_generateNullExpr", null);
|
|
7695
|
+
_ts_decorate3([
|
|
7696
|
+
gen(ThisExpr),
|
|
7697
|
+
_ts_metadata3("design:type", Function),
|
|
7698
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7699
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7700
|
+
], ZModelCodeGenerator.prototype, "_generateThisExpr", null);
|
|
7701
|
+
_ts_decorate3([
|
|
7702
|
+
gen(Attribute),
|
|
7703
|
+
_ts_metadata3("design:type", Function),
|
|
7704
|
+
_ts_metadata3("design:paramtypes", [
|
|
7705
|
+
typeof Attribute === "undefined" ? Object : Attribute
|
|
7706
|
+
]),
|
|
7707
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7708
|
+
], ZModelCodeGenerator.prototype, "_generateAttribute", null);
|
|
7709
|
+
_ts_decorate3([
|
|
7710
|
+
gen(AttributeParam),
|
|
7711
|
+
_ts_metadata3("design:type", Function),
|
|
7712
|
+
_ts_metadata3("design:paramtypes", [
|
|
7713
|
+
typeof AttributeParam === "undefined" ? Object : AttributeParam
|
|
7714
|
+
]),
|
|
7715
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7716
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParam", null);
|
|
7717
|
+
_ts_decorate3([
|
|
7718
|
+
gen(AttributeParamType),
|
|
7719
|
+
_ts_metadata3("design:type", Function),
|
|
7720
|
+
_ts_metadata3("design:paramtypes", [
|
|
7721
|
+
typeof AttributeParamType === "undefined" ? Object : AttributeParamType
|
|
7722
|
+
]),
|
|
7723
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7724
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParamType", null);
|
|
7725
|
+
_ts_decorate3([
|
|
7726
|
+
gen(FunctionDecl),
|
|
7727
|
+
_ts_metadata3("design:type", Function),
|
|
7728
|
+
_ts_metadata3("design:paramtypes", [
|
|
7729
|
+
typeof FunctionDecl === "undefined" ? Object : FunctionDecl
|
|
7730
|
+
]),
|
|
7731
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7732
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionDecl", null);
|
|
7733
|
+
_ts_decorate3([
|
|
7734
|
+
gen(FunctionParam),
|
|
7735
|
+
_ts_metadata3("design:type", Function),
|
|
7736
|
+
_ts_metadata3("design:paramtypes", [
|
|
7737
|
+
typeof FunctionParam === "undefined" ? Object : FunctionParam
|
|
7738
|
+
]),
|
|
7739
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7740
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParam", null);
|
|
7741
|
+
_ts_decorate3([
|
|
7742
|
+
gen(FunctionParamType),
|
|
7743
|
+
_ts_metadata3("design:type", Function),
|
|
7744
|
+
_ts_metadata3("design:paramtypes", [
|
|
7745
|
+
typeof FunctionParamType === "undefined" ? Object : FunctionParamType
|
|
7746
|
+
]),
|
|
7747
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7748
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParamType", null);
|
|
7749
|
+
_ts_decorate3([
|
|
7750
|
+
gen(TypeDef),
|
|
7751
|
+
_ts_metadata3("design:type", Function),
|
|
7752
|
+
_ts_metadata3("design:paramtypes", [
|
|
7753
|
+
typeof TypeDef === "undefined" ? Object : TypeDef
|
|
7754
|
+
]),
|
|
7755
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7756
|
+
], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
|
|
7757
|
+
|
|
7758
|
+
// src/zmodel-completion-provider.ts
|
|
7759
|
+
var ZModelCompletionProvider = class extends DefaultCompletionProvider {
|
|
7760
|
+
static {
|
|
7761
|
+
__name(this, "ZModelCompletionProvider");
|
|
7762
|
+
}
|
|
7763
|
+
services;
|
|
7764
|
+
constructor(services) {
|
|
7765
|
+
super(services), this.services = services;
|
|
7766
|
+
}
|
|
7767
|
+
completionOptions = {
|
|
7768
|
+
triggerCharacters: [
|
|
7769
|
+
"@",
|
|
7770
|
+
"(",
|
|
7771
|
+
",",
|
|
7772
|
+
"."
|
|
7773
|
+
]
|
|
7774
|
+
};
|
|
7775
|
+
async getCompletion(document, params) {
|
|
7776
|
+
try {
|
|
7777
|
+
return await super.getCompletion(document, params);
|
|
7778
|
+
} catch (e) {
|
|
7779
|
+
console.error("Completion error:", e.message);
|
|
7780
|
+
return void 0;
|
|
7781
|
+
}
|
|
7782
|
+
}
|
|
7783
|
+
completionFor(context, next, acceptor) {
|
|
7784
|
+
if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
7785
|
+
const completions = this.getCompletionFromHint(context.node);
|
|
7786
|
+
if (completions) {
|
|
7787
|
+
completions.forEach((c) => acceptor(context, c));
|
|
7788
|
+
return;
|
|
7789
|
+
}
|
|
7790
|
+
}
|
|
7791
|
+
return super.completionFor(context, next, acceptor);
|
|
7792
|
+
}
|
|
7793
|
+
getCompletionFromHint(contextNode) {
|
|
7794
|
+
const unfilledParams = this.getUnfilledAttributeParams(contextNode);
|
|
7795
|
+
const nextParam = unfilledParams[0];
|
|
7796
|
+
if (!nextParam) {
|
|
7797
|
+
return void 0;
|
|
7798
|
+
}
|
|
7799
|
+
const hintAttr = getAttribute(nextParam, "@@@completionHint");
|
|
7800
|
+
if (hintAttr) {
|
|
7801
|
+
const hint = hintAttr.args[0];
|
|
7802
|
+
if (hint?.value) {
|
|
7803
|
+
if (isArrayExpr(hint.value)) {
|
|
7804
|
+
return hint.value.items.map((item) => {
|
|
7805
|
+
return {
|
|
7806
|
+
label: `${item.value}`,
|
|
7807
|
+
kind: CompletionItemKind.Value,
|
|
7808
|
+
detail: "Parameter",
|
|
7809
|
+
sortText: "0"
|
|
7810
|
+
};
|
|
7811
|
+
});
|
|
7812
|
+
}
|
|
7813
|
+
}
|
|
7814
|
+
}
|
|
7815
|
+
return void 0;
|
|
7816
|
+
}
|
|
7817
|
+
// TODO: this doesn't work when the file contains parse errors
|
|
7818
|
+
getUnfilledAttributeParams(contextNode) {
|
|
7819
|
+
try {
|
|
7820
|
+
const params = contextNode.decl.ref?.params;
|
|
7821
|
+
if (params) {
|
|
7822
|
+
const args = contextNode.args;
|
|
7823
|
+
let unfilledParams = [
|
|
7824
|
+
...params
|
|
7825
|
+
];
|
|
7826
|
+
args.forEach((arg) => {
|
|
7827
|
+
if (arg.name) {
|
|
7828
|
+
unfilledParams = unfilledParams.filter((p) => p.name !== arg.name);
|
|
7829
|
+
} else {
|
|
7830
|
+
unfilledParams.shift();
|
|
7831
|
+
}
|
|
7832
|
+
});
|
|
7833
|
+
return unfilledParams;
|
|
7834
|
+
}
|
|
7835
|
+
} catch {
|
|
7836
|
+
}
|
|
7837
|
+
return [];
|
|
7838
|
+
}
|
|
7839
|
+
completionForCrossReference(context, crossRef, acceptor) {
|
|
7840
|
+
if (crossRef.property === "member" && !isMemberAccessExpr(context.node)) {
|
|
7841
|
+
return;
|
|
7842
|
+
}
|
|
7843
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
7844
|
+
if (item.insertText?.startsWith("@@@") || item.label?.startsWith("@@@")) {
|
|
7845
|
+
return;
|
|
7846
|
+
}
|
|
7847
|
+
if ("nodeDescription" in item) {
|
|
7848
|
+
const node = this.getAstNode(item.nodeDescription);
|
|
7849
|
+
if (!node) {
|
|
7850
|
+
return;
|
|
7851
|
+
}
|
|
7852
|
+
if ((isEnum(node) || isEnumField(node)) && isFromStdlib(node)) {
|
|
7853
|
+
return;
|
|
7854
|
+
}
|
|
7855
|
+
if ((isDataModelAttribute(context2.node) || isDataFieldAttribute(context2.node)) && !this.filterAttributeApplicationCompletion(context2.node, node)) {
|
|
7856
|
+
return;
|
|
7857
|
+
}
|
|
7858
|
+
}
|
|
7859
|
+
acceptor(context2, item);
|
|
7860
|
+
}, "customAcceptor");
|
|
7861
|
+
return super.completionForCrossReference(context, crossRef, customAcceptor);
|
|
7862
|
+
}
|
|
7863
|
+
completionForKeyword(context, keyword, acceptor) {
|
|
7864
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
7865
|
+
if (!this.filterKeywordForContext(context2, keyword.value)) {
|
|
7866
|
+
return;
|
|
7867
|
+
}
|
|
7868
|
+
acceptor(context2, item);
|
|
7869
|
+
}, "customAcceptor");
|
|
7870
|
+
return super.completionForKeyword(context, keyword, customAcceptor);
|
|
7871
|
+
}
|
|
7872
|
+
filterKeywordForContext(context, keyword) {
|
|
7873
|
+
if (isInvocationExpr(context.node)) {
|
|
7874
|
+
return [
|
|
7875
|
+
"true",
|
|
7876
|
+
"false",
|
|
7877
|
+
"null",
|
|
7878
|
+
"this"
|
|
7879
|
+
].includes(keyword);
|
|
7880
|
+
} else if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
7881
|
+
const exprContext = this.getAttributeContextType(context.node);
|
|
7882
|
+
if (exprContext === "DefaultValue") {
|
|
7883
|
+
return [
|
|
7884
|
+
"true",
|
|
7885
|
+
"false",
|
|
7886
|
+
"null"
|
|
7887
|
+
].includes(keyword);
|
|
7888
|
+
} else {
|
|
7889
|
+
return [
|
|
7890
|
+
"true",
|
|
7891
|
+
"false",
|
|
7892
|
+
"null",
|
|
7893
|
+
"this"
|
|
7894
|
+
].includes(keyword);
|
|
7895
|
+
}
|
|
7896
|
+
} else {
|
|
7897
|
+
return true;
|
|
7898
|
+
}
|
|
7899
|
+
}
|
|
7900
|
+
filterAttributeApplicationCompletion(contextNode, node) {
|
|
7901
|
+
const attrContextType = this.getAttributeContextType(contextNode);
|
|
7902
|
+
if (isFunctionDecl(node) && attrContextType) {
|
|
7903
|
+
const funcExprContextAttr = getAttribute(node, "@@@expressionContext");
|
|
7904
|
+
if (funcExprContextAttr && funcExprContextAttr.args[0]) {
|
|
7905
|
+
const arg = funcExprContextAttr.args[0];
|
|
7906
|
+
if (isArrayExpr(arg.value)) {
|
|
7907
|
+
return arg.value.items.some((item) => isEnumFieldReference(item) && item.target.$refText === attrContextType);
|
|
7908
|
+
}
|
|
7909
|
+
}
|
|
7910
|
+
return false;
|
|
7911
|
+
}
|
|
7912
|
+
if (isDataField(node)) {
|
|
7913
|
+
return attrContextType !== "DefaultValue";
|
|
7914
|
+
}
|
|
7915
|
+
return true;
|
|
7916
|
+
}
|
|
7917
|
+
getAttributeContextType(node) {
|
|
7918
|
+
return match3(node.decl.$refText).with("@default", () => "DefaultValue").with(P2.union("@@allow", "@allow", "@@deny", "@deny"), () => "AccessPolicy").with("@@validate", () => "ValidationRule").otherwise(() => void 0);
|
|
7919
|
+
}
|
|
7920
|
+
createReferenceCompletionItem(nodeDescription) {
|
|
7921
|
+
const node = this.getAstNode(nodeDescription);
|
|
7922
|
+
const documentation = this.getNodeDocumentation(node);
|
|
7923
|
+
return match3(node).when(isDataModel, () => ({
|
|
7924
|
+
nodeDescription,
|
|
7925
|
+
kind: CompletionItemKind.Class,
|
|
7926
|
+
detail: "Model",
|
|
7927
|
+
sortText: "1",
|
|
7928
|
+
documentation
|
|
7929
|
+
})).when(isTypeDef, () => ({
|
|
7930
|
+
nodeDescription,
|
|
7931
|
+
kind: CompletionItemKind.Class,
|
|
7932
|
+
detail: "Type",
|
|
7933
|
+
sortText: "1",
|
|
7934
|
+
documentation
|
|
7935
|
+
})).when(isDataField, () => ({
|
|
7936
|
+
nodeDescription,
|
|
7937
|
+
kind: CompletionItemKind.Field,
|
|
7938
|
+
detail: "Field",
|
|
7939
|
+
sortText: "0",
|
|
7940
|
+
documentation
|
|
7941
|
+
})).when(isEnum, () => ({
|
|
7942
|
+
nodeDescription,
|
|
7943
|
+
kind: CompletionItemKind.Class,
|
|
7944
|
+
detail: "Enum",
|
|
7945
|
+
sortText: "1",
|
|
7946
|
+
documentation
|
|
7947
|
+
})).when(isEnumField, (d) => {
|
|
7948
|
+
const container = d.$container;
|
|
7949
|
+
return {
|
|
7950
|
+
nodeDescription,
|
|
7951
|
+
kind: CompletionItemKind.Enum,
|
|
7952
|
+
detail: `Value of enum "${container.name}"`,
|
|
7953
|
+
sortText: "1",
|
|
7954
|
+
documentation
|
|
7955
|
+
};
|
|
7956
|
+
}).when(isFunctionDecl, () => ({
|
|
7957
|
+
nodeDescription,
|
|
7958
|
+
insertText: this.getFunctionInsertText(nodeDescription),
|
|
7959
|
+
kind: CompletionItemKind.Function,
|
|
7960
|
+
detail: "Function",
|
|
7961
|
+
sortText: "1",
|
|
7962
|
+
documentation
|
|
7963
|
+
})).when(isAttribute, () => ({
|
|
7964
|
+
nodeDescription,
|
|
7965
|
+
insertText: this.getAttributeInsertText(nodeDescription),
|
|
7966
|
+
kind: CompletionItemKind.Property,
|
|
7967
|
+
detail: "Attribute",
|
|
7968
|
+
sortText: "1",
|
|
7969
|
+
documentation
|
|
7970
|
+
})).otherwise(() => ({
|
|
7971
|
+
nodeDescription,
|
|
7972
|
+
kind: CompletionItemKind.Reference,
|
|
7973
|
+
detail: nodeDescription.type,
|
|
7974
|
+
sortText: "2",
|
|
7975
|
+
documentation
|
|
7976
|
+
}));
|
|
7977
|
+
}
|
|
7978
|
+
getFunctionInsertText(nodeDescription) {
|
|
7979
|
+
const node = this.getAstNode(nodeDescription);
|
|
7980
|
+
if (isFunctionDecl(node)) {
|
|
7981
|
+
if (node.params.some((p) => !p.optional)) {
|
|
7982
|
+
return nodeDescription.name;
|
|
7983
|
+
}
|
|
7984
|
+
}
|
|
7985
|
+
return `${nodeDescription.name}()`;
|
|
7986
|
+
}
|
|
7987
|
+
getAttributeInsertText(nodeDescription) {
|
|
7988
|
+
const node = this.getAstNode(nodeDescription);
|
|
7989
|
+
if (isAttribute(node)) {
|
|
7990
|
+
if (node.name === "@relation") {
|
|
7991
|
+
return `${nodeDescription.name}(fields: [], references: [])`;
|
|
7992
|
+
}
|
|
7993
|
+
}
|
|
7994
|
+
return nodeDescription.name;
|
|
7995
|
+
}
|
|
7996
|
+
getAstNode(nodeDescription) {
|
|
7997
|
+
let node = nodeDescription.node;
|
|
7998
|
+
if (!node) {
|
|
7999
|
+
const doc = this.getOrCreateDocumentSync(nodeDescription);
|
|
8000
|
+
if (!doc) {
|
|
8001
|
+
return void 0;
|
|
8002
|
+
}
|
|
8003
|
+
node = this.services.workspace.AstNodeLocator.getAstNode(doc.parseResult.value, nodeDescription.path);
|
|
8004
|
+
if (!node) {
|
|
8005
|
+
return void 0;
|
|
8006
|
+
}
|
|
8007
|
+
}
|
|
8008
|
+
return node;
|
|
8009
|
+
}
|
|
8010
|
+
getOrCreateDocumentSync(nodeDescription) {
|
|
8011
|
+
let doc = this.services.shared.workspace.LangiumDocuments.getDocument(nodeDescription.documentUri);
|
|
8012
|
+
if (!doc) {
|
|
8013
|
+
try {
|
|
8014
|
+
const content = fs2.readFileSync(nodeDescription.documentUri.fsPath, "utf-8");
|
|
8015
|
+
doc = this.services.shared.workspace.LangiumDocuments.createDocument(nodeDescription.documentUri, content);
|
|
8016
|
+
} catch {
|
|
8017
|
+
console.warn("Failed to read or create document:", nodeDescription.documentUri);
|
|
8018
|
+
return void 0;
|
|
8019
|
+
}
|
|
8020
|
+
}
|
|
8021
|
+
return doc;
|
|
8022
|
+
}
|
|
8023
|
+
getNodeDocumentation(node) {
|
|
8024
|
+
if (!node) {
|
|
8025
|
+
return void 0;
|
|
8026
|
+
}
|
|
8027
|
+
const md = this.commentsToMarkdown(node);
|
|
8028
|
+
return {
|
|
8029
|
+
kind: "markdown",
|
|
8030
|
+
value: md
|
|
8031
|
+
};
|
|
8032
|
+
}
|
|
8033
|
+
commentsToMarkdown(node) {
|
|
8034
|
+
const md = this.services.documentation.DocumentationProvider.getDocumentation(node) ?? "";
|
|
8035
|
+
const zModelGenerator = new ZModelCodeGenerator();
|
|
8036
|
+
const docs = [];
|
|
8037
|
+
try {
|
|
8038
|
+
match3(node).when(isAttribute, (attr) => {
|
|
8039
|
+
docs.push("```prisma", zModelGenerator.generate(attr), "```");
|
|
8040
|
+
}).when(isFunctionDecl, (func2) => {
|
|
8041
|
+
docs.push("```ts", zModelGenerator.generate(func2), "```");
|
|
8042
|
+
}).when(isDataModel, (model) => {
|
|
8043
|
+
docs.push("```prisma", `model ${model.name} { ... }`, "```");
|
|
8044
|
+
}).when(isEnum, (enumDecl) => {
|
|
8045
|
+
docs.push("```prisma", zModelGenerator.generate(enumDecl), "```");
|
|
8046
|
+
}).when(isDataField, (field) => {
|
|
8047
|
+
docs.push(`${field.name}: ${field.type.type ?? field.type.reference?.$refText}`);
|
|
8048
|
+
}).otherwise((ast) => {
|
|
8049
|
+
const name = ast.name;
|
|
8050
|
+
if (name) {
|
|
8051
|
+
docs.push(name);
|
|
8052
|
+
}
|
|
8053
|
+
});
|
|
8054
|
+
} catch {
|
|
8055
|
+
}
|
|
8056
|
+
if (md) {
|
|
8057
|
+
docs.push("___", md);
|
|
8058
|
+
}
|
|
8059
|
+
return docs.join("\n");
|
|
8060
|
+
}
|
|
8061
|
+
};
|
|
8062
|
+
|
|
8063
|
+
// src/zmodel-definition.ts
|
|
8064
|
+
import { DefaultDefinitionProvider } from "langium/lsp";
|
|
8065
|
+
import { LocationLink, Range } from "vscode-languageserver";
|
|
8066
|
+
var ZModelDefinitionProvider = class extends DefaultDefinitionProvider {
|
|
8067
|
+
static {
|
|
8068
|
+
__name(this, "ZModelDefinitionProvider");
|
|
8069
|
+
}
|
|
8070
|
+
documents;
|
|
8071
|
+
constructor(services) {
|
|
8072
|
+
super(services);
|
|
8073
|
+
this.documents = services.shared.workspace.LangiumDocuments;
|
|
8074
|
+
}
|
|
8075
|
+
collectLocationLinks(sourceCstNode, _params) {
|
|
8076
|
+
if (isModelImport(sourceCstNode.astNode)) {
|
|
8077
|
+
const importedModel = resolveImport(this.documents, sourceCstNode.astNode);
|
|
8078
|
+
if (importedModel?.$document) {
|
|
8079
|
+
const targetObject = importedModel;
|
|
8080
|
+
const selectionRange = this.nameProvider.getNameNode(targetObject)?.range ?? Range.create(0, 0, 0, 0);
|
|
8081
|
+
const previewRange = targetObject.$cstNode?.range ?? Range.create(0, 0, 0, 0);
|
|
8082
|
+
return [
|
|
8083
|
+
LocationLink.create(importedModel.$document.uri.toString(), previewRange, selectionRange, sourceCstNode.range)
|
|
8084
|
+
];
|
|
8085
|
+
}
|
|
8086
|
+
return void 0;
|
|
8087
|
+
}
|
|
8088
|
+
return super.collectLocationLinks(sourceCstNode, _params);
|
|
8089
|
+
}
|
|
8090
|
+
};
|
|
8091
|
+
|
|
8092
|
+
// src/zmodel-document-builder.ts
|
|
8093
|
+
import { DefaultDocumentBuilder } from "langium";
|
|
8094
|
+
var ZModelDocumentBuilder = class extends DefaultDocumentBuilder {
|
|
8095
|
+
static {
|
|
8096
|
+
__name(this, "ZModelDocumentBuilder");
|
|
8097
|
+
}
|
|
8098
|
+
constructor(services) {
|
|
8099
|
+
super(services);
|
|
8100
|
+
let validationOptions = this.updateBuildOptions.validation;
|
|
8101
|
+
const stopFlags = {
|
|
8102
|
+
stopAfterLinkingErrors: true,
|
|
8103
|
+
stopAfterLexingErrors: true,
|
|
8104
|
+
stopAfterParsingErrors: true
|
|
8105
|
+
};
|
|
8106
|
+
if (validationOptions === true) {
|
|
8107
|
+
validationOptions = stopFlags;
|
|
8108
|
+
} else if (typeof validationOptions === "object") {
|
|
8109
|
+
validationOptions = {
|
|
8110
|
+
...validationOptions,
|
|
8111
|
+
...stopFlags
|
|
8112
|
+
};
|
|
8113
|
+
}
|
|
8114
|
+
this.updateBuildOptions = {
|
|
8115
|
+
...this.updateBuildOptions,
|
|
8116
|
+
validation: validationOptions
|
|
8117
|
+
};
|
|
8118
|
+
}
|
|
8119
|
+
};
|
|
8120
|
+
|
|
8121
|
+
// src/zmodel-documentation-provider.ts
|
|
8122
|
+
import { JSDocDocumentationProvider } from "langium";
|
|
8123
|
+
var ZModelDocumentationProvider = class extends JSDocDocumentationProvider {
|
|
8124
|
+
static {
|
|
8125
|
+
__name(this, "ZModelDocumentationProvider");
|
|
8126
|
+
}
|
|
8127
|
+
getDocumentation(node) {
|
|
8128
|
+
if ("comments" in node && Array.isArray(node.comments) && node.comments.length > 0) {
|
|
8129
|
+
return node.comments.map((c) => c.replace(/^[/]*\s*/, "")).join("\n");
|
|
8130
|
+
}
|
|
8131
|
+
return super.getDocumentation(node);
|
|
8132
|
+
}
|
|
8133
|
+
};
|
|
8134
|
+
|
|
8135
|
+
// src/zmodel-formatter.ts
|
|
8136
|
+
import { AbstractFormatter, Formatting } from "langium/lsp";
|
|
8137
|
+
var ZModelFormatter = class extends AbstractFormatter {
|
|
8138
|
+
static {
|
|
8139
|
+
__name(this, "ZModelFormatter");
|
|
8140
|
+
}
|
|
8141
|
+
formatOptions;
|
|
8142
|
+
configurationProvider;
|
|
8143
|
+
constructor(services) {
|
|
8144
|
+
super();
|
|
8145
|
+
this.configurationProvider = services.shared.workspace.ConfigurationProvider;
|
|
8146
|
+
}
|
|
8147
|
+
format(node) {
|
|
8148
|
+
const formatter = this.getNodeFormatter(node);
|
|
8149
|
+
if (isDataField(node)) {
|
|
8150
|
+
if (isDataModel(node.$container) || isTypeDef(node.$container)) {
|
|
8151
|
+
const dataModel = node.$container;
|
|
8152
|
+
const compareFn = /* @__PURE__ */ __name((a, b) => b - a, "compareFn");
|
|
8153
|
+
const maxNameLength = dataModel.fields.map((x) => x.name.length).sort(compareFn)[0] ?? 0;
|
|
8154
|
+
const maxTypeLength = dataModel.fields.map(this.getFieldTypeLength).sort(compareFn)[0] ?? 0;
|
|
8155
|
+
formatter.property("type").prepend(Formatting.spaces(maxNameLength - node.name.length + 1));
|
|
8156
|
+
if (node.attributes.length > 0) {
|
|
8157
|
+
formatter.node(node.attributes[0]).prepend(Formatting.spaces(maxTypeLength - this.getFieldTypeLength(node) + 1));
|
|
8158
|
+
formatter.nodes(...node.attributes.slice(1)).prepend(Formatting.oneSpace());
|
|
8159
|
+
}
|
|
8160
|
+
} else {
|
|
8161
|
+
formatter.property("type").prepend(Formatting.oneSpace());
|
|
8162
|
+
if (node.attributes.length > 0) {
|
|
8163
|
+
formatter.properties("attributes").prepend(Formatting.oneSpace());
|
|
8164
|
+
}
|
|
8165
|
+
}
|
|
8166
|
+
} else if (isDataFieldAttribute(node)) {
|
|
8167
|
+
formatter.keyword("(").surround(Formatting.noSpace());
|
|
8168
|
+
formatter.keyword(")").prepend(Formatting.noSpace());
|
|
8169
|
+
formatter.keyword(",").append(Formatting.oneSpace());
|
|
8170
|
+
if (node.args.length > 1) {
|
|
8171
|
+
formatter.nodes(...node.args.slice(1)).prepend(Formatting.oneSpace());
|
|
8172
|
+
}
|
|
8173
|
+
} else if (isAttributeArg(node)) {
|
|
8174
|
+
formatter.keyword(":").prepend(Formatting.noSpace());
|
|
8175
|
+
formatter.keyword(":").append(Formatting.oneSpace());
|
|
8176
|
+
} else if (isAbstractDeclaration(node)) {
|
|
8177
|
+
const bracesOpen = formatter.keyword("{");
|
|
8178
|
+
const bracesClose = formatter.keyword("}");
|
|
8179
|
+
formatter.interior(bracesOpen, bracesClose).prepend(Formatting.indent({
|
|
8180
|
+
allowMore: true
|
|
8181
|
+
}));
|
|
8182
|
+
bracesOpen.prepend(Formatting.oneSpace());
|
|
8183
|
+
bracesClose.prepend(Formatting.newLine());
|
|
8184
|
+
} else if (isModel(node)) {
|
|
8185
|
+
const model = node;
|
|
8186
|
+
const nodes = formatter.nodes(...model.declarations);
|
|
8187
|
+
nodes.prepend(Formatting.noIndent());
|
|
8188
|
+
}
|
|
8189
|
+
}
|
|
8190
|
+
formatDocument(document, params) {
|
|
8191
|
+
this.formatOptions = params.options;
|
|
8192
|
+
this.configurationProvider.getConfiguration(ZModelLanguageMetaData.languageId, "format").then((config) => {
|
|
8193
|
+
if (config) {
|
|
8194
|
+
}
|
|
8195
|
+
});
|
|
8196
|
+
return super.formatDocument(document, params);
|
|
8197
|
+
}
|
|
8198
|
+
getFormatOptions() {
|
|
8199
|
+
return this.formatOptions;
|
|
8200
|
+
}
|
|
8201
|
+
getIndent() {
|
|
8202
|
+
return 1;
|
|
8203
|
+
}
|
|
8204
|
+
getFieldTypeLength(field) {
|
|
8205
|
+
let length;
|
|
8206
|
+
if (field.type.type) {
|
|
8207
|
+
length = field.type.type.length;
|
|
8208
|
+
} else if (field.type.reference) {
|
|
8209
|
+
length = field.type.reference.$refText.length;
|
|
8210
|
+
} else if (isDataField(field) && field.type.unsupported) {
|
|
8211
|
+
const name = `Unsupported("${field.type.unsupported.value.value}")`;
|
|
8212
|
+
length = name.length;
|
|
8213
|
+
} else {
|
|
8214
|
+
length = 1;
|
|
8215
|
+
}
|
|
8216
|
+
if (field.type.optional) {
|
|
8217
|
+
length += 1;
|
|
8218
|
+
}
|
|
8219
|
+
if (field.type.array) {
|
|
8220
|
+
length += 2;
|
|
8221
|
+
}
|
|
8222
|
+
return length;
|
|
8223
|
+
}
|
|
8224
|
+
};
|
|
8225
|
+
|
|
8226
|
+
// src/zmodel-linker.ts
|
|
8227
|
+
import { AstUtils as AstUtils6, Cancellation, DefaultLinker, DocumentState, interruptAndCheck } from "langium";
|
|
8228
|
+
import { match as match4 } from "ts-pattern";
|
|
6988
8229
|
var ZModelLinker = class extends DefaultLinker {
|
|
6989
8230
|
static {
|
|
6990
8231
|
__name(this, "ZModelLinker");
|
|
@@ -7005,21 +8246,19 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7005
8246
|
}
|
|
7006
8247
|
document.state = DocumentState.Linked;
|
|
7007
8248
|
}
|
|
7008
|
-
linkReference(
|
|
7009
|
-
|
|
8249
|
+
linkReference(refInfo, document, extraScopes) {
|
|
8250
|
+
const defaultRef = refInfo.reference;
|
|
8251
|
+
if (defaultRef._ref) {
|
|
8252
|
+
return;
|
|
8253
|
+
}
|
|
8254
|
+
if (this.resolveFromScopeProviders(refInfo.reference, document, extraScopes)) {
|
|
7010
8255
|
return;
|
|
7011
8256
|
}
|
|
7012
|
-
|
|
7013
|
-
this.doLink({
|
|
7014
|
-
reference,
|
|
7015
|
-
container,
|
|
7016
|
-
property
|
|
7017
|
-
}, document);
|
|
8257
|
+
this.doLink(refInfo, document);
|
|
7018
8258
|
}
|
|
7019
8259
|
//#endregion
|
|
7020
8260
|
//#region Expression type resolving
|
|
7021
|
-
resolveFromScopeProviders(
|
|
7022
|
-
const reference = node[property];
|
|
8261
|
+
resolveFromScopeProviders(reference, document, providers) {
|
|
7023
8262
|
for (const provider of providers) {
|
|
7024
8263
|
const target = provider(reference.$refText);
|
|
7025
8264
|
if (target) {
|
|
@@ -7148,7 +8387,11 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7148
8387
|
}
|
|
7149
8388
|
}
|
|
7150
8389
|
resolveInvocation(node, document, extraScopes) {
|
|
7151
|
-
this.linkReference(
|
|
8390
|
+
this.linkReference({
|
|
8391
|
+
reference: node.function,
|
|
8392
|
+
container: node,
|
|
8393
|
+
property: "function"
|
|
8394
|
+
}, document, extraScopes);
|
|
7152
8395
|
node.args.forEach((arg) => this.resolve(arg, document, extraScopes));
|
|
7153
8396
|
if (node.function.ref) {
|
|
7154
8397
|
const funcDecl = node.function.ref;
|
|
@@ -7161,7 +8404,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7161
8404
|
nullable: true
|
|
7162
8405
|
};
|
|
7163
8406
|
}
|
|
7164
|
-
} else if (
|
|
8407
|
+
} else if (isBeforeInvocation(node)) {
|
|
7165
8408
|
node.$resolvedType = {
|
|
7166
8409
|
decl: getContainingDataModel(node)
|
|
7167
8410
|
};
|
|
@@ -7171,7 +8414,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7171
8414
|
}
|
|
7172
8415
|
}
|
|
7173
8416
|
resolveLiteral(node) {
|
|
7174
|
-
const type =
|
|
8417
|
+
const type = match4(node).when(isStringLiteral, () => "String").when(isBooleanLiteral, () => "Boolean").when(isNumberLiteral, () => "Int").exhaustive();
|
|
7175
8418
|
if (type) {
|
|
7176
8419
|
this.resolveToBuiltinTypeOrDecl(node, type);
|
|
7177
8420
|
}
|
|
@@ -7225,7 +8468,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7225
8468
|
if (isArrayExpr(node.value)) {
|
|
7226
8469
|
node.value.items.forEach((item) => {
|
|
7227
8470
|
if (isReferenceExpr(item)) {
|
|
7228
|
-
const resolved2 = this.resolveFromScopeProviders(item
|
|
8471
|
+
const resolved2 = this.resolveFromScopeProviders(item.target, document, [
|
|
7229
8472
|
scopeProvider
|
|
7230
8473
|
]);
|
|
7231
8474
|
if (resolved2) {
|
|
@@ -7239,7 +8482,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7239
8482
|
this.resolveToBuiltinTypeOrDecl(node.value, node.value.items[0].$resolvedType.decl, true);
|
|
7240
8483
|
}
|
|
7241
8484
|
} else if (isReferenceExpr(node.value)) {
|
|
7242
|
-
const resolved2 = this.resolveFromScopeProviders(node.value
|
|
8485
|
+
const resolved2 = this.resolveFromScopeProviders(node.value.target, document, [
|
|
7243
8486
|
scopeProvider
|
|
7244
8487
|
]);
|
|
7245
8488
|
if (resolved2) {
|
|
@@ -7291,13 +8534,9 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7291
8534
|
this.resolveDefault(node, document, scopes);
|
|
7292
8535
|
}
|
|
7293
8536
|
resolveDefault(node, document, extraScopes) {
|
|
7294
|
-
|
|
7295
|
-
|
|
7296
|
-
|
|
7297
|
-
this.linkReference(node, property, document, extraScopes);
|
|
7298
|
-
}
|
|
7299
|
-
}
|
|
7300
|
-
}
|
|
8537
|
+
AstUtils6.streamReferences(node).forEach((ref) => {
|
|
8538
|
+
this.linkReference(ref, document, extraScopes);
|
|
8539
|
+
});
|
|
7301
8540
|
for (const child of AstUtils6.streamContents(node)) {
|
|
7302
8541
|
this.resolve(child, document, extraScopes);
|
|
7303
8542
|
}
|
|
@@ -7343,7 +8582,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7343
8582
|
|
|
7344
8583
|
// src/zmodel-scope.ts
|
|
7345
8584
|
import { AstUtils as AstUtils7, DefaultScopeComputation, DefaultScopeProvider, EMPTY_SCOPE, StreamScope, UriUtils, interruptAndCheck as interruptAndCheck2 } from "langium";
|
|
7346
|
-
import { match as
|
|
8585
|
+
import { match as match5 } from "ts-pattern";
|
|
7347
8586
|
var ZModelScopeComputation = class extends DefaultScopeComputation {
|
|
7348
8587
|
static {
|
|
7349
8588
|
__name(this, "ZModelScopeComputation");
|
|
@@ -7367,7 +8606,7 @@ var ZModelScopeComputation = class extends DefaultScopeComputation {
|
|
|
7367
8606
|
}
|
|
7368
8607
|
processNode(node, document, scopes) {
|
|
7369
8608
|
super.processNode(node, document, scopes);
|
|
7370
|
-
if (isDataModel(node)) {
|
|
8609
|
+
if (isDataModel(node) || isTypeDef(node)) {
|
|
7371
8610
|
const bases = getRecursiveBases(node);
|
|
7372
8611
|
for (const base of bases) {
|
|
7373
8612
|
for (const field of base.fields) {
|
|
@@ -7420,7 +8659,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7420
8659
|
// isAuthOrAuthMemberAccess(node.operand) ||
|
|
7421
8660
|
!!AstUtils7.getContainerOfType(node, isTypeDef)
|
|
7422
8661
|
);
|
|
7423
|
-
return
|
|
8662
|
+
return match5(node.operand).when(isReferenceExpr, (operand) => {
|
|
7424
8663
|
const ref = operand.target.ref;
|
|
7425
8664
|
if (isDataField(ref)) {
|
|
7426
8665
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7438,7 +8677,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7438
8677
|
if (isAuthInvocation(operand)) {
|
|
7439
8678
|
return this.createScopeForAuth(node, globalScope);
|
|
7440
8679
|
}
|
|
7441
|
-
if (
|
|
8680
|
+
if (isBeforeInvocation(operand)) {
|
|
7442
8681
|
return this.createScopeForContainingModel(node, globalScope);
|
|
7443
8682
|
}
|
|
7444
8683
|
return EMPTY_SCOPE;
|
|
@@ -7449,7 +8688,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7449
8688
|
const globalScope = this.getGlobalScope(referenceType, context);
|
|
7450
8689
|
const collection = collectionPredicate.left;
|
|
7451
8690
|
const allowTypeDefScope = false;
|
|
7452
|
-
return
|
|
8691
|
+
return match5(collection).when(isReferenceExpr, (expr) => {
|
|
7453
8692
|
const ref = expr.target.ref;
|
|
7454
8693
|
if (isDataField(ref)) {
|
|
7455
8694
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7511,11 +8750,95 @@ function getCollectionPredicateContext(node) {
|
|
|
7511
8750
|
}
|
|
7512
8751
|
__name(getCollectionPredicateContext, "getCollectionPredicateContext");
|
|
7513
8752
|
|
|
8753
|
+
// src/zmodel-semantic.ts
|
|
8754
|
+
import { AbstractSemanticTokenProvider } from "langium/lsp";
|
|
8755
|
+
import { SemanticTokenTypes } from "vscode-languageserver";
|
|
8756
|
+
var ZModelSemanticTokenProvider = class extends AbstractSemanticTokenProvider {
|
|
8757
|
+
static {
|
|
8758
|
+
__name(this, "ZModelSemanticTokenProvider");
|
|
8759
|
+
}
|
|
8760
|
+
highlightElement(node, acceptor) {
|
|
8761
|
+
if (isDataModel(node)) {
|
|
8762
|
+
acceptor({
|
|
8763
|
+
node,
|
|
8764
|
+
property: "name",
|
|
8765
|
+
type: SemanticTokenTypes.type
|
|
8766
|
+
});
|
|
8767
|
+
acceptor({
|
|
8768
|
+
node,
|
|
8769
|
+
property: "mixins",
|
|
8770
|
+
type: SemanticTokenTypes.type
|
|
8771
|
+
});
|
|
8772
|
+
acceptor({
|
|
8773
|
+
node,
|
|
8774
|
+
property: "baseModel",
|
|
8775
|
+
type: SemanticTokenTypes.type
|
|
8776
|
+
});
|
|
8777
|
+
} else if (isDataSource(node) || isGeneratorDecl(node) || isPlugin(node) || isEnum(node) || isTypeDef(node)) {
|
|
8778
|
+
acceptor({
|
|
8779
|
+
node,
|
|
8780
|
+
property: "name",
|
|
8781
|
+
type: SemanticTokenTypes.type
|
|
8782
|
+
});
|
|
8783
|
+
} else if (isDataField(node) || isConfigField(node) || isAttributeArg(node) || isPluginField(node) || isEnumField(node)) {
|
|
8784
|
+
acceptor({
|
|
8785
|
+
node,
|
|
8786
|
+
property: "name",
|
|
8787
|
+
type: SemanticTokenTypes.variable
|
|
8788
|
+
});
|
|
8789
|
+
} else if (isDataFieldType(node)) {
|
|
8790
|
+
if (node.type) {
|
|
8791
|
+
acceptor({
|
|
8792
|
+
node,
|
|
8793
|
+
property: "type",
|
|
8794
|
+
type: SemanticTokenTypes.type
|
|
8795
|
+
});
|
|
8796
|
+
} else {
|
|
8797
|
+
acceptor({
|
|
8798
|
+
node,
|
|
8799
|
+
property: "reference",
|
|
8800
|
+
type: SemanticTokenTypes.macro
|
|
8801
|
+
});
|
|
8802
|
+
}
|
|
8803
|
+
} else if (isDataModelAttribute(node) || isDataFieldAttribute(node) || isInternalAttribute(node)) {
|
|
8804
|
+
acceptor({
|
|
8805
|
+
node,
|
|
8806
|
+
property: "decl",
|
|
8807
|
+
type: SemanticTokenTypes.function
|
|
8808
|
+
});
|
|
8809
|
+
} else if (isInvocationExpr(node)) {
|
|
8810
|
+
acceptor({
|
|
8811
|
+
node,
|
|
8812
|
+
property: "function",
|
|
8813
|
+
type: SemanticTokenTypes.function
|
|
8814
|
+
});
|
|
8815
|
+
} else if (isFunctionDecl(node) || isAttribute(node)) {
|
|
8816
|
+
acceptor({
|
|
8817
|
+
node,
|
|
8818
|
+
property: "name",
|
|
8819
|
+
type: SemanticTokenTypes.function
|
|
8820
|
+
});
|
|
8821
|
+
} else if (isReferenceExpr(node)) {
|
|
8822
|
+
acceptor({
|
|
8823
|
+
node,
|
|
8824
|
+
property: "target",
|
|
8825
|
+
type: SemanticTokenTypes.variable
|
|
8826
|
+
});
|
|
8827
|
+
} else if (isMemberAccessExpr(node)) {
|
|
8828
|
+
acceptor({
|
|
8829
|
+
node,
|
|
8830
|
+
property: "member",
|
|
8831
|
+
type: SemanticTokenTypes.property
|
|
8832
|
+
});
|
|
8833
|
+
}
|
|
8834
|
+
}
|
|
8835
|
+
};
|
|
8836
|
+
|
|
7514
8837
|
// src/zmodel-workspace-manager.ts
|
|
7515
|
-
import { DefaultWorkspaceManager, URI as URI2
|
|
7516
|
-
import
|
|
8838
|
+
import { DefaultWorkspaceManager, URI as URI2 } from "langium";
|
|
8839
|
+
import fs3 from "fs";
|
|
7517
8840
|
import path2 from "path";
|
|
7518
|
-
import { fileURLToPath } from "url";
|
|
8841
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
7519
8842
|
var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
7520
8843
|
static {
|
|
7521
8844
|
__name(this, "ZModelWorkspaceManager");
|
|
@@ -7539,7 +8862,7 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7539
8862
|
});
|
|
7540
8863
|
const languagePackageDir = path2.dirname(languagePackagePath);
|
|
7541
8864
|
const candidateStdlibPath = path2.join(languagePackageDir, "res", STD_LIB_MODULE_NAME);
|
|
7542
|
-
if (
|
|
8865
|
+
if (fs3.existsSync(candidateStdlibPath)) {
|
|
7543
8866
|
installedStdlibPath = candidateStdlibPath;
|
|
7544
8867
|
console.log(`Found installed zenstack package stdlib at: ${installedStdlibPath}`);
|
|
7545
8868
|
break;
|
|
@@ -7551,73 +8874,12 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7551
8874
|
if (installedStdlibPath) {
|
|
7552
8875
|
stdLibPath = installedStdlibPath;
|
|
7553
8876
|
} else {
|
|
7554
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(
|
|
8877
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(fileURLToPath2(import.meta.url));
|
|
7555
8878
|
stdLibPath = path2.join(_dirname, "../res", STD_LIB_MODULE_NAME);
|
|
7556
8879
|
console.log(`Using bundled stdlib in extension:`, stdLibPath);
|
|
7557
8880
|
}
|
|
7558
8881
|
const stdlib = await this.documentFactory.fromUri(URI2.file(stdLibPath));
|
|
7559
8882
|
collector(stdlib);
|
|
7560
|
-
const documents = this.langiumDocuments.all;
|
|
7561
|
-
const pluginModels = /* @__PURE__ */ new Set();
|
|
7562
|
-
documents.forEach((doc) => {
|
|
7563
|
-
const parsed = doc.parseResult.value;
|
|
7564
|
-
parsed.declarations.forEach((decl) => {
|
|
7565
|
-
if (isPlugin(decl)) {
|
|
7566
|
-
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
7567
|
-
if (providerField) {
|
|
7568
|
-
const provider = getLiteral(providerField.value);
|
|
7569
|
-
if (provider) {
|
|
7570
|
-
pluginModels.add(provider);
|
|
7571
|
-
}
|
|
7572
|
-
}
|
|
7573
|
-
}
|
|
7574
|
-
});
|
|
7575
|
-
});
|
|
7576
|
-
if (pluginModels.size > 0) {
|
|
7577
|
-
console.log(`Used plugin modules: ${Array.from(pluginModels)}`);
|
|
7578
|
-
const pendingPluginModules = new Set(pluginModels);
|
|
7579
|
-
await Promise.all(folders.map((wf) => [
|
|
7580
|
-
wf,
|
|
7581
|
-
this.getRootFolder(wf)
|
|
7582
|
-
]).map(async (entry) => this.loadPluginModels(...entry, pendingPluginModules, collector)));
|
|
7583
|
-
}
|
|
7584
|
-
}
|
|
7585
|
-
async loadPluginModels(workspaceFolder, folderPath, pendingPluginModels, collector) {
|
|
7586
|
-
const content = (await this.fileSystemProvider.readDirectory(folderPath)).sort((a, b) => {
|
|
7587
|
-
if (a.isDirectory && b.isDirectory) {
|
|
7588
|
-
const aName = UriUtils2.basename(a.uri);
|
|
7589
|
-
if (aName === "node_modules") {
|
|
7590
|
-
return -1;
|
|
7591
|
-
} else {
|
|
7592
|
-
return 1;
|
|
7593
|
-
}
|
|
7594
|
-
} else {
|
|
7595
|
-
return 0;
|
|
7596
|
-
}
|
|
7597
|
-
});
|
|
7598
|
-
for (const entry of content) {
|
|
7599
|
-
if (entry.isDirectory) {
|
|
7600
|
-
const name = UriUtils2.basename(entry.uri);
|
|
7601
|
-
if (name === "node_modules") {
|
|
7602
|
-
for (const plugin of Array.from(pendingPluginModels)) {
|
|
7603
|
-
const path4 = UriUtils2.joinPath(entry.uri, plugin, PLUGIN_MODULE_NAME);
|
|
7604
|
-
try {
|
|
7605
|
-
await this.fileSystemProvider.readFile(path4);
|
|
7606
|
-
const document = await this.langiumDocuments.getOrCreateDocument(path4);
|
|
7607
|
-
collector(document);
|
|
7608
|
-
console.log(`Adding plugin document from ${path4.path}`);
|
|
7609
|
-
pendingPluginModels.delete(plugin);
|
|
7610
|
-
if (pendingPluginModels.size === 0) {
|
|
7611
|
-
return;
|
|
7612
|
-
}
|
|
7613
|
-
} catch {
|
|
7614
|
-
}
|
|
7615
|
-
}
|
|
7616
|
-
} else {
|
|
7617
|
-
await this.loadPluginModels(workspaceFolder, entry.uri, pendingPluginModels, collector);
|
|
7618
|
-
}
|
|
7619
|
-
}
|
|
7620
|
-
}
|
|
7621
8883
|
}
|
|
7622
8884
|
};
|
|
7623
8885
|
|
|
@@ -7630,14 +8892,25 @@ var ZModelLanguageModule = {
|
|
|
7630
8892
|
},
|
|
7631
8893
|
validation: {
|
|
7632
8894
|
ZModelValidator: /* @__PURE__ */ __name((services) => new ZModelValidator(services), "ZModelValidator")
|
|
8895
|
+
},
|
|
8896
|
+
lsp: {
|
|
8897
|
+
Formatter: /* @__PURE__ */ __name((services) => new ZModelFormatter(services), "Formatter"),
|
|
8898
|
+
DefinitionProvider: /* @__PURE__ */ __name((services) => new ZModelDefinitionProvider(services), "DefinitionProvider"),
|
|
8899
|
+
CompletionProvider: /* @__PURE__ */ __name((services) => new ZModelCompletionProvider(services), "CompletionProvider"),
|
|
8900
|
+
SemanticTokenProvider: /* @__PURE__ */ __name((services) => new ZModelSemanticTokenProvider(services), "SemanticTokenProvider")
|
|
8901
|
+
},
|
|
8902
|
+
documentation: {
|
|
8903
|
+
CommentProvider: /* @__PURE__ */ __name((services) => new ZModelCommentProvider(services), "CommentProvider"),
|
|
8904
|
+
DocumentationProvider: /* @__PURE__ */ __name((services) => new ZModelDocumentationProvider(services), "DocumentationProvider")
|
|
7633
8905
|
}
|
|
7634
8906
|
};
|
|
7635
8907
|
var ZModelSharedModule = {
|
|
7636
8908
|
workspace: {
|
|
8909
|
+
DocumentBuilder: /* @__PURE__ */ __name((services) => new ZModelDocumentBuilder(services), "DocumentBuilder"),
|
|
7637
8910
|
WorkspaceManager: /* @__PURE__ */ __name((services) => new ZModelWorkspaceManager(services), "WorkspaceManager")
|
|
7638
8911
|
}
|
|
7639
8912
|
};
|
|
7640
|
-
function createZModelLanguageServices(context) {
|
|
8913
|
+
function createZModelLanguageServices(context, logToConsole = false) {
|
|
7641
8914
|
const shared = inject(createDefaultSharedModule(context), ZModelGeneratedSharedModule, ZModelSharedModule);
|
|
7642
8915
|
const ZModelLanguage = inject(createDefaultModule({
|
|
7643
8916
|
shared
|
|
@@ -7647,30 +8920,47 @@ function createZModelLanguageServices(context) {
|
|
|
7647
8920
|
if (!context.connection) {
|
|
7648
8921
|
shared.workspace.ConfigurationProvider.initialized({});
|
|
7649
8922
|
}
|
|
8923
|
+
shared.workspace.DocumentBuilder.onBuildPhase(DocumentState2.Parsed, async (documents) => {
|
|
8924
|
+
for (const doc of documents) {
|
|
8925
|
+
if (doc.parseResult.lexerErrors.length > 0 || doc.parseResult.parserErrors.length > 0) {
|
|
8926
|
+
continue;
|
|
8927
|
+
}
|
|
8928
|
+
if (doc.uri.scheme !== "file") {
|
|
8929
|
+
continue;
|
|
8930
|
+
}
|
|
8931
|
+
const schemaPath = fileURLToPath3(doc.uri.toString());
|
|
8932
|
+
const pluginSchemas = getPluginDocuments(doc.parseResult.value, schemaPath);
|
|
8933
|
+
for (const plugin of pluginSchemas) {
|
|
8934
|
+
const pluginDocUri = URI3.file(path3.resolve(plugin));
|
|
8935
|
+
let pluginDoc = shared.workspace.LangiumDocuments.getDocument(pluginDocUri);
|
|
8936
|
+
if (!pluginDoc) {
|
|
8937
|
+
pluginDoc = await shared.workspace.LangiumDocuments.getOrCreateDocument(pluginDocUri);
|
|
8938
|
+
if (pluginDoc) {
|
|
8939
|
+
shared.workspace.IndexManager.updateContent(pluginDoc);
|
|
8940
|
+
if (logToConsole) {
|
|
8941
|
+
console.log(`Loaded plugin model: ${plugin}`);
|
|
8942
|
+
}
|
|
8943
|
+
}
|
|
8944
|
+
}
|
|
8945
|
+
}
|
|
8946
|
+
}
|
|
8947
|
+
});
|
|
7650
8948
|
return {
|
|
7651
8949
|
shared,
|
|
7652
8950
|
ZModelLanguage
|
|
7653
8951
|
};
|
|
7654
8952
|
}
|
|
7655
8953
|
__name(createZModelLanguageServices, "createZModelLanguageServices");
|
|
7656
|
-
|
|
7657
|
-
|
|
7658
|
-
function createZModelServices() {
|
|
7659
|
-
return createZModelLanguageServices(NodeFileSystem);
|
|
8954
|
+
function createZModelServices(logToConsole = false) {
|
|
8955
|
+
return createZModelLanguageServices(NodeFileSystem, logToConsole);
|
|
7660
8956
|
}
|
|
7661
8957
|
__name(createZModelServices, "createZModelServices");
|
|
7662
|
-
|
|
7663
|
-
|
|
7664
|
-
|
|
7665
|
-
}
|
|
7666
|
-
constructor(message) {
|
|
7667
|
-
super(message);
|
|
7668
|
-
}
|
|
7669
|
-
};
|
|
7670
|
-
async function loadDocument(fileName, pluginModelFiles = []) {
|
|
7671
|
-
const { ZModelLanguage: services } = createZModelServices();
|
|
8958
|
+
|
|
8959
|
+
// src/document.ts
|
|
8960
|
+
async function loadDocument(fileName, additionalModelFiles = []) {
|
|
8961
|
+
const { ZModelLanguage: services } = createZModelServices(false);
|
|
7672
8962
|
const extensions = services.LanguageMetaData.fileExtensions;
|
|
7673
|
-
if (!extensions.includes(
|
|
8963
|
+
if (!extensions.includes(path4.extname(fileName))) {
|
|
7674
8964
|
return {
|
|
7675
8965
|
success: false,
|
|
7676
8966
|
errors: [
|
|
@@ -7679,7 +8969,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7679
8969
|
warnings: []
|
|
7680
8970
|
};
|
|
7681
8971
|
}
|
|
7682
|
-
if (!
|
|
8972
|
+
if (!fs4.existsSync(fileName)) {
|
|
7683
8973
|
return {
|
|
7684
8974
|
success: false,
|
|
7685
8975
|
errors: [
|
|
@@ -7688,23 +8978,27 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7688
8978
|
warnings: []
|
|
7689
8979
|
};
|
|
7690
8980
|
}
|
|
7691
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname :
|
|
7692
|
-
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(
|
|
7693
|
-
const pluginDocs = await Promise.all(pluginModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
|
|
8981
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path4.dirname(fileURLToPath4(import.meta.url));
|
|
8982
|
+
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(path4.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
|
|
7694
8983
|
const langiumDocuments = services.shared.workspace.LangiumDocuments;
|
|
7695
|
-
const document = await langiumDocuments.getOrCreateDocument(
|
|
8984
|
+
const document = await langiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(fileName)));
|
|
7696
8985
|
const importedURIs = await loadImports(document, langiumDocuments);
|
|
7697
8986
|
const importedDocuments = [];
|
|
7698
8987
|
for (const uri of importedURIs) {
|
|
7699
8988
|
importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
|
|
7700
8989
|
}
|
|
8990
|
+
const additionalDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(file)))));
|
|
7701
8991
|
await services.shared.workspace.DocumentBuilder.build([
|
|
7702
8992
|
stdLib,
|
|
7703
|
-
...
|
|
8993
|
+
...additionalDocs,
|
|
7704
8994
|
document,
|
|
7705
8995
|
...importedDocuments
|
|
7706
8996
|
], {
|
|
7707
|
-
validation:
|
|
8997
|
+
validation: {
|
|
8998
|
+
stopAfterLexingErrors: true,
|
|
8999
|
+
stopAfterParsingErrors: true,
|
|
9000
|
+
stopAfterLinkingErrors: true
|
|
9001
|
+
}
|
|
7708
9002
|
});
|
|
7709
9003
|
const diagnostics = langiumDocuments.all.flatMap((doc) => (doc.diagnostics ?? []).map((diag) => ({
|
|
7710
9004
|
doc,
|
|
@@ -7714,7 +9008,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
|
|
|
7714
9008
|
const warnings = [];
|
|
7715
9009
|
if (diagnostics.length > 0) {
|
|
7716
9010
|
for (const { doc, diag } of diagnostics) {
|
|
7717
|
-
const message = `${
|
|
9011
|
+
const message = `${path4.relative(process.cwd(), doc.uri.fsPath)}:${diag.range.start.line + 1}:${diag.range.start.character + 1} - ${diag.message}`;
|
|
7718
9012
|
if (diag.severity === 1) {
|
|
7719
9013
|
errors.push(message);
|
|
7720
9014
|
} else {
|
|
@@ -7763,7 +9057,7 @@ async function loadImports(document, documents, uris = /* @__PURE__ */ new Set()
|
|
|
7763
9057
|
}
|
|
7764
9058
|
}
|
|
7765
9059
|
}
|
|
7766
|
-
return Array.from(uris).filter((x) => uriString != x).map((e) =>
|
|
9060
|
+
return Array.from(uris).filter((x) => uriString != x).map((e) => URI4.parse(e));
|
|
7767
9061
|
}
|
|
7768
9062
|
__name(loadImports, "loadImports");
|
|
7769
9063
|
function mergeImportsDeclarations(documents, model) {
|
|
@@ -7813,7 +9107,7 @@ function validationAfterImportMerge(model) {
|
|
|
7813
9107
|
}
|
|
7814
9108
|
__name(validationAfterImportMerge, "validationAfterImportMerge");
|
|
7815
9109
|
export {
|
|
7816
|
-
|
|
9110
|
+
ZModelCodeGenerator,
|
|
7817
9111
|
ZModelLanguageMetaData,
|
|
7818
9112
|
ZModelLanguageModule,
|
|
7819
9113
|
ZModelSharedModule,
|