@zenstackhq/language 3.0.0-beta.9 → 3.1.0
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 +1597 -234
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +89 -19
- package/dist/index.d.ts +89 -19
- package/dist/index.js +1543 -184
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +99 -8
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +7 -5
- package/dist/utils.d.ts +7 -5
- package/dist/utils.js +93 -5
- package/dist/utils.js.map +1 -1
- package/package.json +9 -11
- package/res/stdlib.zmodel +34 -101
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, TextDocument, 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,14 +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
5162
|
import { AstUtils, URI } from "langium";
|
|
5113
5163
|
import fs from "fs";
|
|
5164
|
+
import { createRequire } from "module";
|
|
5114
5165
|
import path from "path";
|
|
5166
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
5115
5167
|
function hasAttribute(decl, name) {
|
|
5116
5168
|
return !!getAttribute(decl, name);
|
|
5117
5169
|
}
|
|
@@ -5215,7 +5267,7 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
|
|
|
5215
5267
|
] : []
|
|
5216
5268
|
];
|
|
5217
5269
|
bases.forEach((base) => {
|
|
5218
|
-
const baseDecl = decl.$container.declarations.find((d) => isTypeDef(d) || isDataModel(d) && d.name === base.$refText);
|
|
5270
|
+
const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
|
|
5219
5271
|
if (baseDecl) {
|
|
5220
5272
|
if (!includeDelegate && isDelegateModel(baseDecl)) {
|
|
5221
5273
|
return;
|
|
@@ -5338,6 +5390,10 @@ function getArray(expr) {
|
|
|
5338
5390
|
return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
|
|
5339
5391
|
}
|
|
5340
5392
|
__name(getArray, "getArray");
|
|
5393
|
+
function getAttributeArg(attr, name) {
|
|
5394
|
+
return attr.args.find((arg) => arg.$resolvedParam?.name === name)?.value;
|
|
5395
|
+
}
|
|
5396
|
+
__name(getAttributeArg, "getAttributeArg");
|
|
5341
5397
|
function getFunctionExpressionContext(funcDecl) {
|
|
5342
5398
|
const funcAllowedContext = [];
|
|
5343
5399
|
const funcAttr = funcDecl.attributes.find((attr) => attr.decl.$refText === "@@@expressionContext");
|
|
@@ -5425,15 +5481,15 @@ function getAllDeclarationsIncludingImports(documents, model) {
|
|
|
5425
5481
|
}
|
|
5426
5482
|
__name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
|
|
5427
5483
|
function getAuthDecl(decls) {
|
|
5428
|
-
let authModel = decls.find((
|
|
5484
|
+
let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
|
|
5429
5485
|
if (!authModel) {
|
|
5430
|
-
authModel = decls.find((
|
|
5486
|
+
authModel = decls.find((d) => d.name === "User");
|
|
5431
5487
|
}
|
|
5432
5488
|
return authModel;
|
|
5433
5489
|
}
|
|
5434
5490
|
__name(getAuthDecl, "getAuthDecl");
|
|
5435
5491
|
function isBeforeInvocation(node) {
|
|
5436
|
-
return isInvocationExpr(node) && node.function.ref?.name === "before"
|
|
5492
|
+
return isInvocationExpr(node) && node.function.ref?.name === "before";
|
|
5437
5493
|
}
|
|
5438
5494
|
__name(isBeforeInvocation, "isBeforeInvocation");
|
|
5439
5495
|
function isCollectionPredicate(node) {
|
|
@@ -5516,13 +5572,23 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
|
|
|
5516
5572
|
}
|
|
5517
5573
|
if (isDataModel(decl) && decl.baseModel) {
|
|
5518
5574
|
if (decl.baseModel.ref) {
|
|
5519
|
-
|
|
5575
|
+
const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
|
|
5576
|
+
attributes.push(...attrs);
|
|
5520
5577
|
}
|
|
5521
5578
|
}
|
|
5522
5579
|
attributes.push(...decl.attributes);
|
|
5523
5580
|
return attributes;
|
|
5524
5581
|
}
|
|
5525
5582
|
__name(getAllAttributes, "getAllAttributes");
|
|
5583
|
+
function isNonInheritableAttribute(attr) {
|
|
5584
|
+
const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
|
|
5585
|
+
return [
|
|
5586
|
+
"@@map",
|
|
5587
|
+
"@@unique",
|
|
5588
|
+
"@@index"
|
|
5589
|
+
].includes(attrName);
|
|
5590
|
+
}
|
|
5591
|
+
__name(isNonInheritableAttribute, "isNonInheritableAttribute");
|
|
5526
5592
|
function getDocument(node) {
|
|
5527
5593
|
const rootNode = findRootNode(node);
|
|
5528
5594
|
const result = rootNode.$document;
|
|
@@ -5532,6 +5598,71 @@ function getDocument(node) {
|
|
|
5532
5598
|
return result;
|
|
5533
5599
|
}
|
|
5534
5600
|
__name(getDocument, "getDocument");
|
|
5601
|
+
function getPluginDocuments(model, schemaPath) {
|
|
5602
|
+
const result = [];
|
|
5603
|
+
for (const decl of model.declarations.filter(isPlugin)) {
|
|
5604
|
+
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
5605
|
+
if (!providerField) {
|
|
5606
|
+
continue;
|
|
5607
|
+
}
|
|
5608
|
+
const provider = getLiteral(providerField.value);
|
|
5609
|
+
if (!provider) {
|
|
5610
|
+
continue;
|
|
5611
|
+
}
|
|
5612
|
+
let pluginModelFile;
|
|
5613
|
+
let providerPath = path.resolve(path.dirname(schemaPath), provider);
|
|
5614
|
+
if (fs.existsSync(providerPath)) {
|
|
5615
|
+
if (fs.statSync(providerPath).isDirectory()) {
|
|
5616
|
+
providerPath = path.join(providerPath, "index.js");
|
|
5617
|
+
}
|
|
5618
|
+
pluginModelFile = path.resolve(path.dirname(providerPath), PLUGIN_MODULE_NAME);
|
|
5619
|
+
if (!fs.existsSync(pluginModelFile)) {
|
|
5620
|
+
pluginModelFile = findUp([
|
|
5621
|
+
PLUGIN_MODULE_NAME
|
|
5622
|
+
], path.dirname(providerPath));
|
|
5623
|
+
}
|
|
5624
|
+
}
|
|
5625
|
+
if (!pluginModelFile) {
|
|
5626
|
+
if (typeof import.meta.resolve === "function") {
|
|
5627
|
+
try {
|
|
5628
|
+
const resolvedUrl = import.meta.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5629
|
+
pluginModelFile = fileURLToPath(resolvedUrl);
|
|
5630
|
+
} catch {
|
|
5631
|
+
}
|
|
5632
|
+
}
|
|
5633
|
+
}
|
|
5634
|
+
if (!pluginModelFile) {
|
|
5635
|
+
try {
|
|
5636
|
+
const require2 = createRequire(pathToFileURL(schemaPath));
|
|
5637
|
+
pluginModelFile = require2.resolve(`${provider}/${PLUGIN_MODULE_NAME}`);
|
|
5638
|
+
} catch {
|
|
5639
|
+
}
|
|
5640
|
+
}
|
|
5641
|
+
if (pluginModelFile && fs.existsSync(pluginModelFile)) {
|
|
5642
|
+
result.push(pluginModelFile);
|
|
5643
|
+
}
|
|
5644
|
+
}
|
|
5645
|
+
return result;
|
|
5646
|
+
}
|
|
5647
|
+
__name(getPluginDocuments, "getPluginDocuments");
|
|
5648
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
5649
|
+
if (!names.some((name) => !!name)) {
|
|
5650
|
+
return void 0;
|
|
5651
|
+
}
|
|
5652
|
+
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
|
|
5653
|
+
if (multiple === false && target) {
|
|
5654
|
+
return path.join(cwd, target);
|
|
5655
|
+
}
|
|
5656
|
+
if (target) {
|
|
5657
|
+
result.push(path.join(cwd, target));
|
|
5658
|
+
}
|
|
5659
|
+
const up = path.resolve(cwd, "..");
|
|
5660
|
+
if (up === cwd) {
|
|
5661
|
+
return multiple && result.length > 0 ? result : void 0;
|
|
5662
|
+
}
|
|
5663
|
+
return findUp(names, up, multiple, result);
|
|
5664
|
+
}
|
|
5665
|
+
__name(findUp, "findUp");
|
|
5535
5666
|
function findRootNode(node) {
|
|
5536
5667
|
while (node.$container) {
|
|
5537
5668
|
node = node.$container;
|
|
@@ -5541,6 +5672,9 @@ function findRootNode(node) {
|
|
|
5541
5672
|
__name(findRootNode, "findRootNode");
|
|
5542
5673
|
|
|
5543
5674
|
// src/validators/attribute-application-validator.ts
|
|
5675
|
+
import { invariant } from "@zenstackhq/common-helpers";
|
|
5676
|
+
import { AstUtils as AstUtils2 } from "langium";
|
|
5677
|
+
import pluralize from "pluralize";
|
|
5544
5678
|
function _ts_decorate(decorators, target, key, desc) {
|
|
5545
5679
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5546
5680
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -5758,8 +5892,23 @@ var AttributeApplicationValidator = class {
|
|
|
5758
5892
|
});
|
|
5759
5893
|
}
|
|
5760
5894
|
}
|
|
5761
|
-
|
|
5762
|
-
const
|
|
5895
|
+
_checkId(attr, accept) {
|
|
5896
|
+
const dm = getContainingDataModel(attr);
|
|
5897
|
+
if (dm?.isView) {
|
|
5898
|
+
accept("error", `\`@id\` is not allowed for views`, {
|
|
5899
|
+
node: attr
|
|
5900
|
+
});
|
|
5901
|
+
}
|
|
5902
|
+
}
|
|
5903
|
+
_checkConstraint(attr, accept) {
|
|
5904
|
+
const dm = getContainingDataModel(attr);
|
|
5905
|
+
if (dm?.isView && (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@index")) {
|
|
5906
|
+
accept("error", `\`${attr.decl.$refText}\` is not allowed for views`, {
|
|
5907
|
+
node: attr
|
|
5908
|
+
});
|
|
5909
|
+
}
|
|
5910
|
+
const fields = getAttributeArg(attr, "fields");
|
|
5911
|
+
const attrName = attr.decl.ref?.name;
|
|
5763
5912
|
if (!fields) {
|
|
5764
5913
|
accept("error", `expects an array of field references`, {
|
|
5765
5914
|
node: attr.args[0]
|
|
@@ -5768,7 +5917,7 @@ var AttributeApplicationValidator = class {
|
|
|
5768
5917
|
}
|
|
5769
5918
|
if (isArrayExpr(fields)) {
|
|
5770
5919
|
if (fields.items.length === 0) {
|
|
5771
|
-
accept("error",
|
|
5920
|
+
accept("error", `\`${attrName}\` expects at least one field reference`, {
|
|
5772
5921
|
node: fields
|
|
5773
5922
|
});
|
|
5774
5923
|
return;
|
|
@@ -5787,7 +5936,7 @@ var AttributeApplicationValidator = class {
|
|
|
5787
5936
|
return;
|
|
5788
5937
|
}
|
|
5789
5938
|
if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
|
|
5790
|
-
accept("error", `Cannot use fields inherited from a polymorphic base model in
|
|
5939
|
+
accept("error", `Cannot use fields inherited from a polymorphic base model in \`${attrName}\``, {
|
|
5791
5940
|
node: item
|
|
5792
5941
|
});
|
|
5793
5942
|
}
|
|
@@ -5798,6 +5947,24 @@ var AttributeApplicationValidator = class {
|
|
|
5798
5947
|
});
|
|
5799
5948
|
}
|
|
5800
5949
|
}
|
|
5950
|
+
_checkSchema(attr, accept) {
|
|
5951
|
+
const schemaName = getStringLiteral(attr.args[0]?.value);
|
|
5952
|
+
invariant(schemaName, `@@schema expects a string literal`);
|
|
5953
|
+
const zmodel = AstUtils2.getContainerOfType(attr, isModel);
|
|
5954
|
+
const datasource = zmodel.declarations.find(isDataSource);
|
|
5955
|
+
if (datasource) {
|
|
5956
|
+
let found = false;
|
|
5957
|
+
const schemas = datasource.fields.find((f) => f.name === "schemas");
|
|
5958
|
+
if (schemas && isConfigArrayExpr(schemas.value)) {
|
|
5959
|
+
found = schemas.value.items.some((item) => isLiteralExpr(item) && item.value === schemaName);
|
|
5960
|
+
}
|
|
5961
|
+
if (!found) {
|
|
5962
|
+
accept("error", `Schema "${schemaName}" is not defined in the datasource`, {
|
|
5963
|
+
node: attr
|
|
5964
|
+
});
|
|
5965
|
+
}
|
|
5966
|
+
}
|
|
5967
|
+
}
|
|
5801
5968
|
validatePolicyKinds(kind, candidates, attr, accept) {
|
|
5802
5969
|
const items = kind.split(",").map((x) => x.trim());
|
|
5803
5970
|
items.forEach((item) => {
|
|
@@ -5840,15 +6007,34 @@ _ts_decorate([
|
|
|
5840
6007
|
_ts_metadata("design:returntype", void 0)
|
|
5841
6008
|
], AttributeApplicationValidator.prototype, "_checkValidate", null);
|
|
5842
6009
|
_ts_decorate([
|
|
5843
|
-
check("
|
|
6010
|
+
check("@id"),
|
|
6011
|
+
_ts_metadata("design:type", Function),
|
|
6012
|
+
_ts_metadata("design:paramtypes", [
|
|
6013
|
+
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
6014
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
6015
|
+
]),
|
|
6016
|
+
_ts_metadata("design:returntype", void 0)
|
|
6017
|
+
], AttributeApplicationValidator.prototype, "_checkId", null);
|
|
6018
|
+
_ts_decorate([
|
|
5844
6019
|
check("@@id"),
|
|
6020
|
+
check("@@index"),
|
|
6021
|
+
check("@@unique"),
|
|
5845
6022
|
_ts_metadata("design:type", Function),
|
|
5846
6023
|
_ts_metadata("design:paramtypes", [
|
|
5847
6024
|
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
5848
6025
|
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
5849
6026
|
]),
|
|
5850
6027
|
_ts_metadata("design:returntype", void 0)
|
|
5851
|
-
], AttributeApplicationValidator.prototype, "
|
|
6028
|
+
], AttributeApplicationValidator.prototype, "_checkConstraint", null);
|
|
6029
|
+
_ts_decorate([
|
|
6030
|
+
check("@@schema"),
|
|
6031
|
+
_ts_metadata("design:type", Function),
|
|
6032
|
+
_ts_metadata("design:paramtypes", [
|
|
6033
|
+
typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
|
|
6034
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
6035
|
+
]),
|
|
6036
|
+
_ts_metadata("design:returntype", void 0)
|
|
6037
|
+
], AttributeApplicationValidator.prototype, "_checkSchema", null);
|
|
5852
6038
|
function assignableToAttributeParam(arg, param, attr) {
|
|
5853
6039
|
const argResolvedType = arg.$resolvedType;
|
|
5854
6040
|
if (!argResolvedType) {
|
|
@@ -5982,7 +6168,7 @@ var AttributeValidator = class {
|
|
|
5982
6168
|
};
|
|
5983
6169
|
|
|
5984
6170
|
// src/validators/datamodel-validator.ts
|
|
5985
|
-
import { invariant } from "@zenstackhq/common-helpers";
|
|
6171
|
+
import { invariant as invariant2 } from "@zenstackhq/common-helpers";
|
|
5986
6172
|
import { AstUtils as AstUtils3 } from "langium";
|
|
5987
6173
|
|
|
5988
6174
|
// src/validators/common.ts
|
|
@@ -6029,7 +6215,7 @@ var DataModelValidator = class {
|
|
|
6029
6215
|
const uniqueFields = allFields.filter((f) => f.attributes.find((attr) => attr.decl.ref?.name === "@unique"));
|
|
6030
6216
|
const modelLevelIds = getModelIdFields(dm);
|
|
6031
6217
|
const modelUniqueFields = getModelUniqueFields(dm);
|
|
6032
|
-
if (idFields.length === 0 && modelLevelIds.length === 0 && uniqueFields.length === 0 && modelUniqueFields.length === 0) {
|
|
6218
|
+
if (!dm.isView && idFields.length === 0 && modelLevelIds.length === 0 && uniqueFields.length === 0 && modelUniqueFields.length === 0) {
|
|
6033
6219
|
accept("error", "Model must have at least one unique criteria. Either mark a single field with `@id`, `@unique` or add a multi field criterion with `@@id([])` or `@@unique([])` to the model.", {
|
|
6034
6220
|
node: dm
|
|
6035
6221
|
});
|
|
@@ -6223,7 +6409,7 @@ var DataModelValidator = class {
|
|
|
6223
6409
|
if (!thisRelation.valid) {
|
|
6224
6410
|
return;
|
|
6225
6411
|
}
|
|
6226
|
-
if (this.isFieldInheritedFromDelegateModel(field)) {
|
|
6412
|
+
if (this.isFieldInheritedFromDelegateModel(field, contextModel)) {
|
|
6227
6413
|
return;
|
|
6228
6414
|
}
|
|
6229
6415
|
if (this.isSelfRelation(field)) {
|
|
@@ -6346,14 +6532,14 @@ var DataModelValidator = class {
|
|
|
6346
6532
|
}
|
|
6347
6533
|
}
|
|
6348
6534
|
// checks if the given field is inherited directly or indirectly from a delegate model
|
|
6349
|
-
isFieldInheritedFromDelegateModel(field) {
|
|
6350
|
-
return isDelegateModel(field.$container);
|
|
6535
|
+
isFieldInheritedFromDelegateModel(field, contextModel) {
|
|
6536
|
+
return field.$container !== contextModel && isDelegateModel(field.$container);
|
|
6351
6537
|
}
|
|
6352
6538
|
validateInherits(model, accept) {
|
|
6353
6539
|
if (!model.baseModel) {
|
|
6354
6540
|
return;
|
|
6355
6541
|
}
|
|
6356
|
-
|
|
6542
|
+
invariant2(model.baseModel.ref, "baseModel must be resolved");
|
|
6357
6543
|
if (!isDelegateModel(model.baseModel.ref)) {
|
|
6358
6544
|
accept("error", `Model ${model.baseModel.$refText} cannot be extended because it's not a delegate model`, {
|
|
6359
6545
|
node: model,
|
|
@@ -6375,7 +6561,7 @@ var DataModelValidator = class {
|
|
|
6375
6561
|
}
|
|
6376
6562
|
seen.push(current);
|
|
6377
6563
|
if (current.baseModel) {
|
|
6378
|
-
|
|
6564
|
+
invariant2(current.baseModel.ref, "baseModel must be resolved");
|
|
6379
6565
|
todo.push(current.baseModel.ref);
|
|
6380
6566
|
}
|
|
6381
6567
|
}
|
|
@@ -6406,7 +6592,6 @@ var DataSourceValidator = class {
|
|
|
6406
6592
|
validateDuplicatedDeclarations(ds, ds.fields, accept);
|
|
6407
6593
|
this.validateProvider(ds, accept);
|
|
6408
6594
|
this.validateUrl(ds, accept);
|
|
6409
|
-
this.validateRelationMode(ds, accept);
|
|
6410
6595
|
}
|
|
6411
6596
|
validateProvider(ds, accept) {
|
|
6412
6597
|
const provider = ds.fields.find((f) => f.name === "provider");
|
|
@@ -6416,16 +6601,61 @@ var DataSourceValidator = class {
|
|
|
6416
6601
|
});
|
|
6417
6602
|
return;
|
|
6418
6603
|
}
|
|
6419
|
-
const
|
|
6420
|
-
if (!
|
|
6604
|
+
const providerValue = getStringLiteral(provider.value);
|
|
6605
|
+
if (!providerValue) {
|
|
6421
6606
|
accept("error", '"provider" must be set to a string literal', {
|
|
6422
6607
|
node: provider.value
|
|
6423
6608
|
});
|
|
6424
|
-
} else if (!SUPPORTED_PROVIDERS.includes(
|
|
6425
|
-
accept("error", `Provider "${
|
|
6609
|
+
} else if (!SUPPORTED_PROVIDERS.includes(providerValue)) {
|
|
6610
|
+
accept("error", `Provider "${providerValue}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
|
|
6426
6611
|
node: provider.value
|
|
6427
6612
|
});
|
|
6428
6613
|
}
|
|
6614
|
+
const defaultSchemaField = ds.fields.find((f) => f.name === "defaultSchema");
|
|
6615
|
+
let defaultSchemaValue;
|
|
6616
|
+
if (defaultSchemaField) {
|
|
6617
|
+
if (providerValue !== "postgresql") {
|
|
6618
|
+
accept("error", '"defaultSchema" is only supported for "postgresql" provider', {
|
|
6619
|
+
node: defaultSchemaField
|
|
6620
|
+
});
|
|
6621
|
+
}
|
|
6622
|
+
defaultSchemaValue = getStringLiteral(defaultSchemaField.value);
|
|
6623
|
+
if (!defaultSchemaValue) {
|
|
6624
|
+
accept("error", '"defaultSchema" must be a string literal', {
|
|
6625
|
+
node: defaultSchemaField.value
|
|
6626
|
+
});
|
|
6627
|
+
}
|
|
6628
|
+
}
|
|
6629
|
+
const schemasField = ds.fields.find((f) => f.name === "schemas");
|
|
6630
|
+
if (schemasField) {
|
|
6631
|
+
if (providerValue !== "postgresql") {
|
|
6632
|
+
accept("error", '"schemas" is only supported for "postgresql" provider', {
|
|
6633
|
+
node: schemasField
|
|
6634
|
+
});
|
|
6635
|
+
}
|
|
6636
|
+
const schemasValue = schemasField.value;
|
|
6637
|
+
if (!isConfigArrayExpr(schemasValue) || !schemasValue.items.every((e) => isLiteralExpr(e) && typeof getStringLiteral(e) === "string")) {
|
|
6638
|
+
accept("error", '"schemas" must be an array of string literals', {
|
|
6639
|
+
node: schemasField
|
|
6640
|
+
});
|
|
6641
|
+
} else {
|
|
6642
|
+
const schemasArray = schemasValue.items.map((e) => getStringLiteral(e));
|
|
6643
|
+
if (defaultSchemaValue) {
|
|
6644
|
+
if (!schemasArray.includes(defaultSchemaValue)) {
|
|
6645
|
+
accept("error", `"${defaultSchemaValue}" must be included in the "schemas" array`, {
|
|
6646
|
+
node: schemasField
|
|
6647
|
+
});
|
|
6648
|
+
}
|
|
6649
|
+
} else {
|
|
6650
|
+
const hasImplicitPublicSchema = ds.$container.declarations.some((d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === "@@schema"));
|
|
6651
|
+
if (hasImplicitPublicSchema && !schemasArray.includes("public")) {
|
|
6652
|
+
accept("error", `"public" must be included in the "schemas" array`, {
|
|
6653
|
+
node: schemasField
|
|
6654
|
+
});
|
|
6655
|
+
}
|
|
6656
|
+
}
|
|
6657
|
+
}
|
|
6658
|
+
}
|
|
6429
6659
|
}
|
|
6430
6660
|
validateUrl(ds, accept) {
|
|
6431
6661
|
const urlField = ds.fields.find((f) => f.name === "url");
|
|
@@ -6439,20 +6669,6 @@ var DataSourceValidator = class {
|
|
|
6439
6669
|
});
|
|
6440
6670
|
}
|
|
6441
6671
|
}
|
|
6442
|
-
validateRelationMode(ds, accept) {
|
|
6443
|
-
const field = ds.fields.find((f) => f.name === "relationMode");
|
|
6444
|
-
if (field) {
|
|
6445
|
-
const val = getStringLiteral(field.value);
|
|
6446
|
-
if (!val || ![
|
|
6447
|
-
"foreignKeys",
|
|
6448
|
-
"prisma"
|
|
6449
|
-
].includes(val)) {
|
|
6450
|
-
accept("error", '"relationMode" must be set to "foreignKeys" or "prisma"', {
|
|
6451
|
-
node: field.value
|
|
6452
|
-
});
|
|
6453
|
-
}
|
|
6454
|
-
}
|
|
6455
|
-
}
|
|
6456
6672
|
};
|
|
6457
6673
|
|
|
6458
6674
|
// src/validators/enum-validator.ts
|
|
@@ -6511,6 +6727,9 @@ var ExpressionValidator = class {
|
|
|
6511
6727
|
case "BinaryExpr":
|
|
6512
6728
|
this.validateBinaryExpr(expr, accept);
|
|
6513
6729
|
break;
|
|
6730
|
+
case "UnaryExpr":
|
|
6731
|
+
this.validateUnaryExpr(expr, accept);
|
|
6732
|
+
break;
|
|
6514
6733
|
}
|
|
6515
6734
|
}
|
|
6516
6735
|
validateMemberAccessExpr(expr, accept) {
|
|
@@ -6652,6 +6871,13 @@ var ExpressionValidator = class {
|
|
|
6652
6871
|
break;
|
|
6653
6872
|
}
|
|
6654
6873
|
}
|
|
6874
|
+
validateUnaryExpr(expr, accept) {
|
|
6875
|
+
if (expr.operand.$resolvedType && expr.operand.$resolvedType.decl !== "Boolean") {
|
|
6876
|
+
accept("error", `operand of "${expr.operator}" must be of Boolean type`, {
|
|
6877
|
+
node: expr.operand
|
|
6878
|
+
});
|
|
6879
|
+
}
|
|
6880
|
+
}
|
|
6655
6881
|
validateCollectionPredicate(expr, accept) {
|
|
6656
6882
|
if (!expr.$resolvedType) {
|
|
6657
6883
|
accept("error", "collection predicate can only be used on an array of model type", {
|
|
@@ -6721,44 +6947,56 @@ var FunctionInvocationValidator = class {
|
|
|
6721
6947
|
});
|
|
6722
6948
|
return;
|
|
6723
6949
|
}
|
|
6724
|
-
if (!this.validateArgs(funcDecl, expr
|
|
6950
|
+
if (!this.validateArgs(funcDecl, expr, accept)) {
|
|
6725
6951
|
return;
|
|
6726
6952
|
}
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
|
|
6733
|
-
break;
|
|
6734
|
-
}
|
|
6735
|
-
curr = curr.$container;
|
|
6953
|
+
let curr = expr.$container;
|
|
6954
|
+
let containerAttribute;
|
|
6955
|
+
while (curr) {
|
|
6956
|
+
if (isDataModelAttribute(curr) || isDataFieldAttribute(curr)) {
|
|
6957
|
+
containerAttribute = curr;
|
|
6958
|
+
break;
|
|
6736
6959
|
}
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
|
|
6741
|
-
|
|
6960
|
+
curr = curr.$container;
|
|
6961
|
+
}
|
|
6962
|
+
const exprContext = this.getExpressionContext(containerAttribute);
|
|
6963
|
+
const funcAllowedContext = getFunctionExpressionContext(funcDecl);
|
|
6964
|
+
if (exprContext && !funcAllowedContext.includes(exprContext)) {
|
|
6965
|
+
accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
|
|
6966
|
+
node: expr
|
|
6967
|
+
});
|
|
6968
|
+
return;
|
|
6969
|
+
}
|
|
6970
|
+
const allCasing = [
|
|
6971
|
+
"original",
|
|
6972
|
+
"upper",
|
|
6973
|
+
"lower",
|
|
6974
|
+
"capitalize",
|
|
6975
|
+
"uncapitalize"
|
|
6976
|
+
];
|
|
6977
|
+
if ([
|
|
6978
|
+
"currentModel",
|
|
6979
|
+
"currentOperation"
|
|
6980
|
+
].includes(funcDecl.name)) {
|
|
6981
|
+
const arg = getLiteral(expr.args[0]?.value);
|
|
6982
|
+
if (arg && !allCasing.includes(arg)) {
|
|
6983
|
+
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6984
|
+
node: expr.args[0]
|
|
6742
6985
|
});
|
|
6743
|
-
return;
|
|
6744
6986
|
}
|
|
6745
|
-
|
|
6746
|
-
|
|
6747
|
-
|
|
6748
|
-
|
|
6749
|
-
|
|
6750
|
-
|
|
6751
|
-
|
|
6752
|
-
|
|
6753
|
-
|
|
6754
|
-
|
|
6755
|
-
|
|
6756
|
-
|
|
6757
|
-
|
|
6758
|
-
accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
|
|
6759
|
-
node: expr.args[0]
|
|
6760
|
-
});
|
|
6761
|
-
}
|
|
6987
|
+
}
|
|
6988
|
+
if ([
|
|
6989
|
+
"uuid",
|
|
6990
|
+
"ulid",
|
|
6991
|
+
"cuid",
|
|
6992
|
+
"nanoid"
|
|
6993
|
+
].includes(funcDecl.name)) {
|
|
6994
|
+
const formatParamIdx = funcDecl.params.findIndex((param) => param.name === "format");
|
|
6995
|
+
const formatArg = getLiteral(expr.args[formatParamIdx]?.value);
|
|
6996
|
+
if (formatArg !== void 0 && !/(?<!\\)%s/g.test(formatArg)) {
|
|
6997
|
+
accept("error", 'argument must include "%s"', {
|
|
6998
|
+
node: expr.args[formatParamIdx]
|
|
6999
|
+
});
|
|
6762
7000
|
}
|
|
6763
7001
|
}
|
|
6764
7002
|
const checker = invocationCheckers.get(expr.function.$refText);
|
|
@@ -6778,18 +7016,18 @@ var FunctionInvocationValidator = class {
|
|
|
6778
7016
|
isValidationAttribute(attr) {
|
|
6779
7017
|
return !!attr.decl.ref?.attributes.some((attr2) => attr2.decl.$refText === "@@@validation");
|
|
6780
7018
|
}
|
|
6781
|
-
validateArgs(funcDecl,
|
|
7019
|
+
validateArgs(funcDecl, expr, accept) {
|
|
6782
7020
|
let success = true;
|
|
6783
7021
|
for (let i = 0; i < funcDecl.params.length; i++) {
|
|
6784
7022
|
const param = funcDecl.params[i];
|
|
6785
7023
|
if (!param) {
|
|
6786
7024
|
continue;
|
|
6787
7025
|
}
|
|
6788
|
-
const arg = args[i];
|
|
7026
|
+
const arg = expr.args[i];
|
|
6789
7027
|
if (!arg) {
|
|
6790
7028
|
if (!param.optional) {
|
|
6791
7029
|
accept("error", `missing argument for parameter "${param.name}"`, {
|
|
6792
|
-
node:
|
|
7030
|
+
node: expr
|
|
6793
7031
|
});
|
|
6794
7032
|
success = false;
|
|
6795
7033
|
}
|
|
@@ -6838,6 +7076,46 @@ var FunctionInvocationValidator = class {
|
|
|
6838
7076
|
}
|
|
6839
7077
|
return true;
|
|
6840
7078
|
}
|
|
7079
|
+
_checkUuid(expr, accept) {
|
|
7080
|
+
const versionArg = expr.args[0]?.value;
|
|
7081
|
+
if (versionArg) {
|
|
7082
|
+
const version = getLiteral(versionArg);
|
|
7083
|
+
if (version !== void 0 && version !== 4 && version !== 7) {
|
|
7084
|
+
accept("error", "first argument must be 4 or 7", {
|
|
7085
|
+
node: expr.args[0]
|
|
7086
|
+
});
|
|
7087
|
+
}
|
|
7088
|
+
}
|
|
7089
|
+
}
|
|
7090
|
+
_checkCuid(expr, accept) {
|
|
7091
|
+
const versionArg = expr.args[0]?.value;
|
|
7092
|
+
if (versionArg) {
|
|
7093
|
+
const version = getLiteral(versionArg);
|
|
7094
|
+
if (version !== void 0 && version !== 1 && version !== 2) {
|
|
7095
|
+
accept("error", "first argument must be 1 or 2", {
|
|
7096
|
+
node: expr.args[0]
|
|
7097
|
+
});
|
|
7098
|
+
}
|
|
7099
|
+
}
|
|
7100
|
+
}
|
|
7101
|
+
_checkNanoid(expr, accept) {
|
|
7102
|
+
const lengthArg = expr.args[0]?.value;
|
|
7103
|
+
if (lengthArg) {
|
|
7104
|
+
const length = getLiteral(lengthArg);
|
|
7105
|
+
if (length !== void 0 && length <= 0) {
|
|
7106
|
+
accept("error", "first argument must be a positive number", {
|
|
7107
|
+
node: expr.args[0]
|
|
7108
|
+
});
|
|
7109
|
+
}
|
|
7110
|
+
}
|
|
7111
|
+
}
|
|
7112
|
+
_checkAuth(expr, accept) {
|
|
7113
|
+
if (!expr.$resolvedType) {
|
|
7114
|
+
accept("error", 'cannot resolve `auth()` - make sure you have a model or type with `@auth` attribute or named "User"', {
|
|
7115
|
+
node: expr
|
|
7116
|
+
});
|
|
7117
|
+
}
|
|
7118
|
+
}
|
|
6841
7119
|
_checkLength(expr, accept) {
|
|
6842
7120
|
const msg = "argument must be a string or list field";
|
|
6843
7121
|
const fieldArg = expr.args[0].value;
|
|
@@ -6947,6 +7225,42 @@ var FunctionInvocationValidator = class {
|
|
|
6947
7225
|
}
|
|
6948
7226
|
}
|
|
6949
7227
|
};
|
|
7228
|
+
_ts_decorate2([
|
|
7229
|
+
func("uuid"),
|
|
7230
|
+
_ts_metadata2("design:type", Function),
|
|
7231
|
+
_ts_metadata2("design:paramtypes", [
|
|
7232
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7233
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7234
|
+
]),
|
|
7235
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7236
|
+
], FunctionInvocationValidator.prototype, "_checkUuid", null);
|
|
7237
|
+
_ts_decorate2([
|
|
7238
|
+
func("cuid"),
|
|
7239
|
+
_ts_metadata2("design:type", Function),
|
|
7240
|
+
_ts_metadata2("design:paramtypes", [
|
|
7241
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7242
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7243
|
+
]),
|
|
7244
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7245
|
+
], FunctionInvocationValidator.prototype, "_checkCuid", null);
|
|
7246
|
+
_ts_decorate2([
|
|
7247
|
+
func("nanoid"),
|
|
7248
|
+
_ts_metadata2("design:type", Function),
|
|
7249
|
+
_ts_metadata2("design:paramtypes", [
|
|
7250
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7251
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7252
|
+
]),
|
|
7253
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7254
|
+
], FunctionInvocationValidator.prototype, "_checkNanoid", null);
|
|
7255
|
+
_ts_decorate2([
|
|
7256
|
+
func("auth"),
|
|
7257
|
+
_ts_metadata2("design:type", Function),
|
|
7258
|
+
_ts_metadata2("design:paramtypes", [
|
|
7259
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
|
|
7260
|
+
typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
|
|
7261
|
+
]),
|
|
7262
|
+
_ts_metadata2("design:returntype", void 0)
|
|
7263
|
+
], FunctionInvocationValidator.prototype, "_checkAuth", null);
|
|
6950
7264
|
_ts_decorate2([
|
|
6951
7265
|
func("length"),
|
|
6952
7266
|
_ts_metadata2("design:type", Function),
|
|
@@ -7055,6 +7369,7 @@ function registerValidationChecks(services) {
|
|
|
7055
7369
|
const checks = {
|
|
7056
7370
|
Model: validator.checkModel,
|
|
7057
7371
|
DataSource: validator.checkDataSource,
|
|
7372
|
+
GeneratorDecl: validator.checkGenerator,
|
|
7058
7373
|
DataModel: validator.checkDataModel,
|
|
7059
7374
|
TypeDef: validator.checkTypeDef,
|
|
7060
7375
|
Enum: validator.checkEnum,
|
|
@@ -7080,6 +7395,11 @@ var ZModelValidator = class {
|
|
|
7080
7395
|
checkDataSource(node, accept) {
|
|
7081
7396
|
new DataSourceValidator().validate(node, accept);
|
|
7082
7397
|
}
|
|
7398
|
+
checkGenerator(node, accept) {
|
|
7399
|
+
accept("warning", '"generator" is not used by ZenStack and should be removed.', {
|
|
7400
|
+
node
|
|
7401
|
+
});
|
|
7402
|
+
}
|
|
7083
7403
|
checkDataModel(node, accept) {
|
|
7084
7404
|
new DataModelValidator().validate(node, accept);
|
|
7085
7405
|
}
|
|
@@ -7103,6 +7423,873 @@ var ZModelValidator = class {
|
|
|
7103
7423
|
}
|
|
7104
7424
|
};
|
|
7105
7425
|
|
|
7426
|
+
// src/zmodel-comment-provider.ts
|
|
7427
|
+
import { DefaultCommentProvider } from "langium";
|
|
7428
|
+
import { match as match2 } from "ts-pattern";
|
|
7429
|
+
var ZModelCommentProvider = class extends DefaultCommentProvider {
|
|
7430
|
+
static {
|
|
7431
|
+
__name(this, "ZModelCommentProvider");
|
|
7432
|
+
}
|
|
7433
|
+
getComment(node) {
|
|
7434
|
+
let comment = super.getComment(node);
|
|
7435
|
+
if (!comment) {
|
|
7436
|
+
comment = match2(node).when(isDataModel, (d) => `/**
|
|
7437
|
+
* Model *${d.name}*
|
|
7438
|
+
*/`).when(isTypeDef, (d) => `/**
|
|
7439
|
+
* Type *${d.name}*
|
|
7440
|
+
*/`).when(isEnum, (e) => `/**
|
|
7441
|
+
* Enum *${e.name}*
|
|
7442
|
+
*/`).when(isEnumField, (f) => `/**
|
|
7443
|
+
* Value of enum *${f.$container?.name}*
|
|
7444
|
+
*/`).when(isDataField, (f) => `/**
|
|
7445
|
+
* Field of *${f.$container?.name}*
|
|
7446
|
+
*/`).when(isFunctionDecl, (f) => `/**
|
|
7447
|
+
* Function *${f.name}*
|
|
7448
|
+
*/`).otherwise(() => "");
|
|
7449
|
+
}
|
|
7450
|
+
return comment;
|
|
7451
|
+
}
|
|
7452
|
+
};
|
|
7453
|
+
|
|
7454
|
+
// src/zmodel-completion-provider.ts
|
|
7455
|
+
import { DefaultCompletionProvider } from "langium/lsp";
|
|
7456
|
+
import fs2 from "fs";
|
|
7457
|
+
import { P as P2, match as match3 } from "ts-pattern";
|
|
7458
|
+
import { CompletionItemKind } from "vscode-languageserver";
|
|
7459
|
+
|
|
7460
|
+
// src/zmodel-code-generator.ts
|
|
7461
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
7462
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
7463
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
7464
|
+
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;
|
|
7465
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7466
|
+
}
|
|
7467
|
+
__name(_ts_decorate3, "_ts_decorate");
|
|
7468
|
+
function _ts_metadata3(k, v) {
|
|
7469
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
7470
|
+
}
|
|
7471
|
+
__name(_ts_metadata3, "_ts_metadata");
|
|
7472
|
+
var generationHandlers = /* @__PURE__ */ new Map();
|
|
7473
|
+
function gen(name) {
|
|
7474
|
+
return function(_target, _propertyKey, descriptor) {
|
|
7475
|
+
if (!generationHandlers.get(name)) {
|
|
7476
|
+
generationHandlers.set(name, descriptor);
|
|
7477
|
+
}
|
|
7478
|
+
return descriptor;
|
|
7479
|
+
};
|
|
7480
|
+
}
|
|
7481
|
+
__name(gen, "gen");
|
|
7482
|
+
var ZModelCodeGenerator = class {
|
|
7483
|
+
static {
|
|
7484
|
+
__name(this, "ZModelCodeGenerator");
|
|
7485
|
+
}
|
|
7486
|
+
options;
|
|
7487
|
+
constructor(options) {
|
|
7488
|
+
this.options = {
|
|
7489
|
+
binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
|
|
7490
|
+
unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
|
|
7491
|
+
indent: options?.indent ?? 4,
|
|
7492
|
+
quote: options?.quote ?? "single"
|
|
7493
|
+
};
|
|
7494
|
+
}
|
|
7495
|
+
/**
|
|
7496
|
+
* Generates ZModel source code from AST.
|
|
7497
|
+
*/
|
|
7498
|
+
generate(ast) {
|
|
7499
|
+
const handler = generationHandlers.get(ast.$type);
|
|
7500
|
+
if (!handler) {
|
|
7501
|
+
throw new Error(`No generation handler found for ${ast.$type}`);
|
|
7502
|
+
}
|
|
7503
|
+
return handler.value.call(this, ast);
|
|
7504
|
+
}
|
|
7505
|
+
_generateModel(ast) {
|
|
7506
|
+
return ast.declarations.map((d) => this.generate(d)).join("\n\n");
|
|
7507
|
+
}
|
|
7508
|
+
_generateDataSource(ast) {
|
|
7509
|
+
return `datasource ${ast.name} {
|
|
7510
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7511
|
+
}`;
|
|
7512
|
+
}
|
|
7513
|
+
_generateEnum(ast) {
|
|
7514
|
+
return `enum ${ast.name} {
|
|
7515
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7516
|
+
}`;
|
|
7517
|
+
}
|
|
7518
|
+
_generateEnumField(ast) {
|
|
7519
|
+
return `${ast.name}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7520
|
+
}
|
|
7521
|
+
_generateGenerator(ast) {
|
|
7522
|
+
return `generator ${ast.name} {
|
|
7523
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7524
|
+
}`;
|
|
7525
|
+
}
|
|
7526
|
+
_generateConfigField(ast) {
|
|
7527
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7528
|
+
}
|
|
7529
|
+
_generateConfigArrayExpr(ast) {
|
|
7530
|
+
return `[${ast.items.map((x) => this.generate(x)).join(", ")}]`;
|
|
7531
|
+
}
|
|
7532
|
+
_generateConfigInvocationExpr(ast) {
|
|
7533
|
+
if (ast.args.length === 0) {
|
|
7534
|
+
return ast.name;
|
|
7535
|
+
} else {
|
|
7536
|
+
return `${ast.name}(${ast.args.map((x) => (x.name ? x.name + ": " : "") + this.generate(x.value)).join(", ")})`;
|
|
7537
|
+
}
|
|
7538
|
+
}
|
|
7539
|
+
_generatePlugin(ast) {
|
|
7540
|
+
return `plugin ${ast.name} {
|
|
7541
|
+
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
7542
|
+
}`;
|
|
7543
|
+
}
|
|
7544
|
+
_generatePluginField(ast) {
|
|
7545
|
+
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
7546
|
+
}
|
|
7547
|
+
_generateDataModel(ast) {
|
|
7548
|
+
return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.$refText).join(", ") : ""} {
|
|
7549
|
+
${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") : ""}
|
|
7550
|
+
}`;
|
|
7551
|
+
}
|
|
7552
|
+
_generateDataField(ast) {
|
|
7553
|
+
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
7554
|
+
}
|
|
7555
|
+
fieldType(type) {
|
|
7556
|
+
const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
|
|
7557
|
+
return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
|
|
7558
|
+
}
|
|
7559
|
+
_generateDataModelAttribute(ast) {
|
|
7560
|
+
return this.attribute(ast);
|
|
7561
|
+
}
|
|
7562
|
+
_generateDataFieldAttribute(ast) {
|
|
7563
|
+
return this.attribute(ast);
|
|
7564
|
+
}
|
|
7565
|
+
attribute(ast) {
|
|
7566
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7567
|
+
return `${ast.decl.$refText}${args}`;
|
|
7568
|
+
}
|
|
7569
|
+
_generateAttributeArg(ast) {
|
|
7570
|
+
if (ast.name) {
|
|
7571
|
+
return `${ast.name}: ${this.generate(ast.value)}`;
|
|
7572
|
+
} else {
|
|
7573
|
+
return this.generate(ast.value);
|
|
7574
|
+
}
|
|
7575
|
+
}
|
|
7576
|
+
_generateObjectExpr(ast) {
|
|
7577
|
+
return `{ ${ast.fields.map((field) => this.objectField(field)).join(", ")} }`;
|
|
7578
|
+
}
|
|
7579
|
+
objectField(field) {
|
|
7580
|
+
return `${field.name}: ${this.generate(field.value)}`;
|
|
7581
|
+
}
|
|
7582
|
+
_generateArrayExpr(ast) {
|
|
7583
|
+
return `[${ast.items.map((item) => this.generate(item)).join(", ")}]`;
|
|
7584
|
+
}
|
|
7585
|
+
_generateLiteralExpr(ast) {
|
|
7586
|
+
return this.options.quote === "single" ? `'${ast.value}'` : `"${ast.value}"`;
|
|
7587
|
+
}
|
|
7588
|
+
_generateNumberLiteral(ast) {
|
|
7589
|
+
return ast.value.toString();
|
|
7590
|
+
}
|
|
7591
|
+
_generateBooleanLiteral(ast) {
|
|
7592
|
+
return ast.value.toString();
|
|
7593
|
+
}
|
|
7594
|
+
_generateUnaryExpr(ast) {
|
|
7595
|
+
return `${ast.operator}${this.unaryExprSpace}${this.generate(ast.operand)}`;
|
|
7596
|
+
}
|
|
7597
|
+
_generateBinaryExpr(ast) {
|
|
7598
|
+
const operator = ast.operator;
|
|
7599
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7600
|
+
const rightExpr = this.generate(ast.right);
|
|
7601
|
+
const { left: isLeftParenthesis, right: isRightParenthesis } = this.isParenthesesNeededForBinaryExpr(ast);
|
|
7602
|
+
return `${isLeftParenthesis ? "(" : ""}${this.generate(ast.left)}${isLeftParenthesis ? ")" : ""}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${operator}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${isRightParenthesis ? "(" : ""}${isCollectionPredicate2 ? `[${rightExpr}]` : rightExpr}${isRightParenthesis ? ")" : ""}`;
|
|
7603
|
+
}
|
|
7604
|
+
_generateReferenceExpr(ast) {
|
|
7605
|
+
const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
|
|
7606
|
+
return `${ast.target.$refText}${args}`;
|
|
7607
|
+
}
|
|
7608
|
+
_generateReferenceArg(ast) {
|
|
7609
|
+
return `${ast.name}:${this.generate(ast.value)}`;
|
|
7610
|
+
}
|
|
7611
|
+
_generateMemberExpr(ast) {
|
|
7612
|
+
return `${this.generate(ast.operand)}.${ast.member.$refText}`;
|
|
7613
|
+
}
|
|
7614
|
+
_generateInvocationExpr(ast) {
|
|
7615
|
+
return `${ast.function.$refText}(${ast.args.map((x) => this.argument(x)).join(", ")})`;
|
|
7616
|
+
}
|
|
7617
|
+
_generateNullExpr() {
|
|
7618
|
+
return "null";
|
|
7619
|
+
}
|
|
7620
|
+
_generateThisExpr() {
|
|
7621
|
+
return "this";
|
|
7622
|
+
}
|
|
7623
|
+
_generateAttribute(ast) {
|
|
7624
|
+
return `attribute ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")})`;
|
|
7625
|
+
}
|
|
7626
|
+
_generateAttributeParam(ast) {
|
|
7627
|
+
return `${ast.default ? "_ " : ""}${ast.name}: ${this.generate(ast.type)}`;
|
|
7628
|
+
}
|
|
7629
|
+
_generateAttributeParamType(ast) {
|
|
7630
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}${ast.optional ? "?" : ""}`;
|
|
7631
|
+
}
|
|
7632
|
+
_generateFunctionDecl(ast) {
|
|
7633
|
+
return `function ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")}) ${ast.returnType ? ": " + this.generate(ast.returnType) : ""} {}`;
|
|
7634
|
+
}
|
|
7635
|
+
_generateFunctionParam(ast) {
|
|
7636
|
+
return `${ast.name}: ${this.generate(ast.type)}`;
|
|
7637
|
+
}
|
|
7638
|
+
_generateFunctionParamType(ast) {
|
|
7639
|
+
return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}`;
|
|
7640
|
+
}
|
|
7641
|
+
_generateTypeDef(ast) {
|
|
7642
|
+
return `type ${ast.name} {
|
|
7643
|
+
${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") : ""}
|
|
7644
|
+
}`;
|
|
7645
|
+
}
|
|
7646
|
+
argument(ast) {
|
|
7647
|
+
return this.generate(ast.value);
|
|
7648
|
+
}
|
|
7649
|
+
get binaryExprSpace() {
|
|
7650
|
+
return " ".repeat(this.options.binaryExprNumberOfSpaces);
|
|
7651
|
+
}
|
|
7652
|
+
get unaryExprSpace() {
|
|
7653
|
+
return " ".repeat(this.options.unaryExprNumberOfSpaces);
|
|
7654
|
+
}
|
|
7655
|
+
get indent() {
|
|
7656
|
+
return " ".repeat(this.options.indent);
|
|
7657
|
+
}
|
|
7658
|
+
isParenthesesNeededForBinaryExpr(ast) {
|
|
7659
|
+
const result = {
|
|
7660
|
+
left: false,
|
|
7661
|
+
right: false
|
|
7662
|
+
};
|
|
7663
|
+
const operator = ast.operator;
|
|
7664
|
+
const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
|
|
7665
|
+
const currentPriority = BinaryExprOperatorPriority[operator];
|
|
7666
|
+
if (ast.left.$type === BinaryExpr && BinaryExprOperatorPriority[ast.left["operator"]] < currentPriority) {
|
|
7667
|
+
result.left = true;
|
|
7668
|
+
}
|
|
7669
|
+
if (!isCollectionPredicate2 && ast.right.$type === BinaryExpr && BinaryExprOperatorPriority[ast.right["operator"]] <= currentPriority) {
|
|
7670
|
+
result.right = true;
|
|
7671
|
+
}
|
|
7672
|
+
return result;
|
|
7673
|
+
}
|
|
7674
|
+
isCollectionPredicateOperator(op) {
|
|
7675
|
+
return [
|
|
7676
|
+
"?",
|
|
7677
|
+
"!",
|
|
7678
|
+
"^"
|
|
7679
|
+
].includes(op);
|
|
7680
|
+
}
|
|
7681
|
+
};
|
|
7682
|
+
_ts_decorate3([
|
|
7683
|
+
gen(Model),
|
|
7684
|
+
_ts_metadata3("design:type", Function),
|
|
7685
|
+
_ts_metadata3("design:paramtypes", [
|
|
7686
|
+
typeof Model === "undefined" ? Object : Model
|
|
7687
|
+
]),
|
|
7688
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7689
|
+
], ZModelCodeGenerator.prototype, "_generateModel", null);
|
|
7690
|
+
_ts_decorate3([
|
|
7691
|
+
gen(DataSource),
|
|
7692
|
+
_ts_metadata3("design:type", Function),
|
|
7693
|
+
_ts_metadata3("design:paramtypes", [
|
|
7694
|
+
typeof DataSource === "undefined" ? Object : DataSource
|
|
7695
|
+
]),
|
|
7696
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7697
|
+
], ZModelCodeGenerator.prototype, "_generateDataSource", null);
|
|
7698
|
+
_ts_decorate3([
|
|
7699
|
+
gen(Enum),
|
|
7700
|
+
_ts_metadata3("design:type", Function),
|
|
7701
|
+
_ts_metadata3("design:paramtypes", [
|
|
7702
|
+
typeof Enum === "undefined" ? Object : Enum
|
|
7703
|
+
]),
|
|
7704
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7705
|
+
], ZModelCodeGenerator.prototype, "_generateEnum", null);
|
|
7706
|
+
_ts_decorate3([
|
|
7707
|
+
gen(EnumField),
|
|
7708
|
+
_ts_metadata3("design:type", Function),
|
|
7709
|
+
_ts_metadata3("design:paramtypes", [
|
|
7710
|
+
typeof EnumField === "undefined" ? Object : EnumField
|
|
7711
|
+
]),
|
|
7712
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7713
|
+
], ZModelCodeGenerator.prototype, "_generateEnumField", null);
|
|
7714
|
+
_ts_decorate3([
|
|
7715
|
+
gen(GeneratorDecl),
|
|
7716
|
+
_ts_metadata3("design:type", Function),
|
|
7717
|
+
_ts_metadata3("design:paramtypes", [
|
|
7718
|
+
typeof GeneratorDecl === "undefined" ? Object : GeneratorDecl
|
|
7719
|
+
]),
|
|
7720
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7721
|
+
], ZModelCodeGenerator.prototype, "_generateGenerator", null);
|
|
7722
|
+
_ts_decorate3([
|
|
7723
|
+
gen(ConfigField),
|
|
7724
|
+
_ts_metadata3("design:type", Function),
|
|
7725
|
+
_ts_metadata3("design:paramtypes", [
|
|
7726
|
+
typeof ConfigField === "undefined" ? Object : ConfigField
|
|
7727
|
+
]),
|
|
7728
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7729
|
+
], ZModelCodeGenerator.prototype, "_generateConfigField", null);
|
|
7730
|
+
_ts_decorate3([
|
|
7731
|
+
gen(ConfigArrayExpr),
|
|
7732
|
+
_ts_metadata3("design:type", Function),
|
|
7733
|
+
_ts_metadata3("design:paramtypes", [
|
|
7734
|
+
typeof ConfigArrayExpr === "undefined" ? Object : ConfigArrayExpr
|
|
7735
|
+
]),
|
|
7736
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7737
|
+
], ZModelCodeGenerator.prototype, "_generateConfigArrayExpr", null);
|
|
7738
|
+
_ts_decorate3([
|
|
7739
|
+
gen(ConfigInvocationExpr),
|
|
7740
|
+
_ts_metadata3("design:type", Function),
|
|
7741
|
+
_ts_metadata3("design:paramtypes", [
|
|
7742
|
+
typeof ConfigInvocationExpr === "undefined" ? Object : ConfigInvocationExpr
|
|
7743
|
+
]),
|
|
7744
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7745
|
+
], ZModelCodeGenerator.prototype, "_generateConfigInvocationExpr", null);
|
|
7746
|
+
_ts_decorate3([
|
|
7747
|
+
gen(Plugin),
|
|
7748
|
+
_ts_metadata3("design:type", Function),
|
|
7749
|
+
_ts_metadata3("design:paramtypes", [
|
|
7750
|
+
typeof Plugin === "undefined" ? Object : Plugin
|
|
7751
|
+
]),
|
|
7752
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7753
|
+
], ZModelCodeGenerator.prototype, "_generatePlugin", null);
|
|
7754
|
+
_ts_decorate3([
|
|
7755
|
+
gen(PluginField),
|
|
7756
|
+
_ts_metadata3("design:type", Function),
|
|
7757
|
+
_ts_metadata3("design:paramtypes", [
|
|
7758
|
+
typeof PluginField === "undefined" ? Object : PluginField
|
|
7759
|
+
]),
|
|
7760
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7761
|
+
], ZModelCodeGenerator.prototype, "_generatePluginField", null);
|
|
7762
|
+
_ts_decorate3([
|
|
7763
|
+
gen(DataModel),
|
|
7764
|
+
_ts_metadata3("design:type", Function),
|
|
7765
|
+
_ts_metadata3("design:paramtypes", [
|
|
7766
|
+
typeof DataModel === "undefined" ? Object : DataModel
|
|
7767
|
+
]),
|
|
7768
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7769
|
+
], ZModelCodeGenerator.prototype, "_generateDataModel", null);
|
|
7770
|
+
_ts_decorate3([
|
|
7771
|
+
gen(DataField),
|
|
7772
|
+
_ts_metadata3("design:type", Function),
|
|
7773
|
+
_ts_metadata3("design:paramtypes", [
|
|
7774
|
+
typeof DataField === "undefined" ? Object : DataField
|
|
7775
|
+
]),
|
|
7776
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7777
|
+
], ZModelCodeGenerator.prototype, "_generateDataField", null);
|
|
7778
|
+
_ts_decorate3([
|
|
7779
|
+
gen(DataModelAttribute),
|
|
7780
|
+
_ts_metadata3("design:type", Function),
|
|
7781
|
+
_ts_metadata3("design:paramtypes", [
|
|
7782
|
+
typeof DataModelAttribute === "undefined" ? Object : DataModelAttribute
|
|
7783
|
+
]),
|
|
7784
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7785
|
+
], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
|
|
7786
|
+
_ts_decorate3([
|
|
7787
|
+
gen(DataFieldAttribute),
|
|
7788
|
+
_ts_metadata3("design:type", Function),
|
|
7789
|
+
_ts_metadata3("design:paramtypes", [
|
|
7790
|
+
typeof DataFieldAttribute === "undefined" ? Object : DataFieldAttribute
|
|
7791
|
+
]),
|
|
7792
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7793
|
+
], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
|
|
7794
|
+
_ts_decorate3([
|
|
7795
|
+
gen(AttributeArg),
|
|
7796
|
+
_ts_metadata3("design:type", Function),
|
|
7797
|
+
_ts_metadata3("design:paramtypes", [
|
|
7798
|
+
typeof AttributeArg === "undefined" ? Object : AttributeArg
|
|
7799
|
+
]),
|
|
7800
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7801
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeArg", null);
|
|
7802
|
+
_ts_decorate3([
|
|
7803
|
+
gen(ObjectExpr),
|
|
7804
|
+
_ts_metadata3("design:type", Function),
|
|
7805
|
+
_ts_metadata3("design:paramtypes", [
|
|
7806
|
+
typeof ObjectExpr === "undefined" ? Object : ObjectExpr
|
|
7807
|
+
]),
|
|
7808
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7809
|
+
], ZModelCodeGenerator.prototype, "_generateObjectExpr", null);
|
|
7810
|
+
_ts_decorate3([
|
|
7811
|
+
gen(ArrayExpr),
|
|
7812
|
+
_ts_metadata3("design:type", Function),
|
|
7813
|
+
_ts_metadata3("design:paramtypes", [
|
|
7814
|
+
typeof ArrayExpr === "undefined" ? Object : ArrayExpr
|
|
7815
|
+
]),
|
|
7816
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7817
|
+
], ZModelCodeGenerator.prototype, "_generateArrayExpr", null);
|
|
7818
|
+
_ts_decorate3([
|
|
7819
|
+
gen(StringLiteral),
|
|
7820
|
+
_ts_metadata3("design:type", Function),
|
|
7821
|
+
_ts_metadata3("design:paramtypes", [
|
|
7822
|
+
typeof LiteralExpr === "undefined" ? Object : LiteralExpr
|
|
7823
|
+
]),
|
|
7824
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7825
|
+
], ZModelCodeGenerator.prototype, "_generateLiteralExpr", null);
|
|
7826
|
+
_ts_decorate3([
|
|
7827
|
+
gen(NumberLiteral),
|
|
7828
|
+
_ts_metadata3("design:type", Function),
|
|
7829
|
+
_ts_metadata3("design:paramtypes", [
|
|
7830
|
+
typeof NumberLiteral === "undefined" ? Object : NumberLiteral
|
|
7831
|
+
]),
|
|
7832
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7833
|
+
], ZModelCodeGenerator.prototype, "_generateNumberLiteral", null);
|
|
7834
|
+
_ts_decorate3([
|
|
7835
|
+
gen(BooleanLiteral),
|
|
7836
|
+
_ts_metadata3("design:type", Function),
|
|
7837
|
+
_ts_metadata3("design:paramtypes", [
|
|
7838
|
+
typeof BooleanLiteral === "undefined" ? Object : BooleanLiteral
|
|
7839
|
+
]),
|
|
7840
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7841
|
+
], ZModelCodeGenerator.prototype, "_generateBooleanLiteral", null);
|
|
7842
|
+
_ts_decorate3([
|
|
7843
|
+
gen(UnaryExpr),
|
|
7844
|
+
_ts_metadata3("design:type", Function),
|
|
7845
|
+
_ts_metadata3("design:paramtypes", [
|
|
7846
|
+
typeof UnaryExpr === "undefined" ? Object : UnaryExpr
|
|
7847
|
+
]),
|
|
7848
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7849
|
+
], ZModelCodeGenerator.prototype, "_generateUnaryExpr", null);
|
|
7850
|
+
_ts_decorate3([
|
|
7851
|
+
gen(BinaryExpr),
|
|
7852
|
+
_ts_metadata3("design:type", Function),
|
|
7853
|
+
_ts_metadata3("design:paramtypes", [
|
|
7854
|
+
typeof BinaryExpr === "undefined" ? Object : BinaryExpr
|
|
7855
|
+
]),
|
|
7856
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7857
|
+
], ZModelCodeGenerator.prototype, "_generateBinaryExpr", null);
|
|
7858
|
+
_ts_decorate3([
|
|
7859
|
+
gen(ReferenceExpr),
|
|
7860
|
+
_ts_metadata3("design:type", Function),
|
|
7861
|
+
_ts_metadata3("design:paramtypes", [
|
|
7862
|
+
typeof ReferenceExpr === "undefined" ? Object : ReferenceExpr
|
|
7863
|
+
]),
|
|
7864
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7865
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceExpr", null);
|
|
7866
|
+
_ts_decorate3([
|
|
7867
|
+
gen(ReferenceArg),
|
|
7868
|
+
_ts_metadata3("design:type", Function),
|
|
7869
|
+
_ts_metadata3("design:paramtypes", [
|
|
7870
|
+
typeof ReferenceArg === "undefined" ? Object : ReferenceArg
|
|
7871
|
+
]),
|
|
7872
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7873
|
+
], ZModelCodeGenerator.prototype, "_generateReferenceArg", null);
|
|
7874
|
+
_ts_decorate3([
|
|
7875
|
+
gen(MemberAccessExpr),
|
|
7876
|
+
_ts_metadata3("design:type", Function),
|
|
7877
|
+
_ts_metadata3("design:paramtypes", [
|
|
7878
|
+
typeof MemberAccessExpr === "undefined" ? Object : MemberAccessExpr
|
|
7879
|
+
]),
|
|
7880
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7881
|
+
], ZModelCodeGenerator.prototype, "_generateMemberExpr", null);
|
|
7882
|
+
_ts_decorate3([
|
|
7883
|
+
gen(InvocationExpr),
|
|
7884
|
+
_ts_metadata3("design:type", Function),
|
|
7885
|
+
_ts_metadata3("design:paramtypes", [
|
|
7886
|
+
typeof InvocationExpr === "undefined" ? Object : InvocationExpr
|
|
7887
|
+
]),
|
|
7888
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7889
|
+
], ZModelCodeGenerator.prototype, "_generateInvocationExpr", null);
|
|
7890
|
+
_ts_decorate3([
|
|
7891
|
+
gen(NullExpr),
|
|
7892
|
+
_ts_metadata3("design:type", Function),
|
|
7893
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7894
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7895
|
+
], ZModelCodeGenerator.prototype, "_generateNullExpr", null);
|
|
7896
|
+
_ts_decorate3([
|
|
7897
|
+
gen(ThisExpr),
|
|
7898
|
+
_ts_metadata3("design:type", Function),
|
|
7899
|
+
_ts_metadata3("design:paramtypes", []),
|
|
7900
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7901
|
+
], ZModelCodeGenerator.prototype, "_generateThisExpr", null);
|
|
7902
|
+
_ts_decorate3([
|
|
7903
|
+
gen(Attribute),
|
|
7904
|
+
_ts_metadata3("design:type", Function),
|
|
7905
|
+
_ts_metadata3("design:paramtypes", [
|
|
7906
|
+
typeof Attribute === "undefined" ? Object : Attribute
|
|
7907
|
+
]),
|
|
7908
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7909
|
+
], ZModelCodeGenerator.prototype, "_generateAttribute", null);
|
|
7910
|
+
_ts_decorate3([
|
|
7911
|
+
gen(AttributeParam),
|
|
7912
|
+
_ts_metadata3("design:type", Function),
|
|
7913
|
+
_ts_metadata3("design:paramtypes", [
|
|
7914
|
+
typeof AttributeParam === "undefined" ? Object : AttributeParam
|
|
7915
|
+
]),
|
|
7916
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7917
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParam", null);
|
|
7918
|
+
_ts_decorate3([
|
|
7919
|
+
gen(AttributeParamType),
|
|
7920
|
+
_ts_metadata3("design:type", Function),
|
|
7921
|
+
_ts_metadata3("design:paramtypes", [
|
|
7922
|
+
typeof AttributeParamType === "undefined" ? Object : AttributeParamType
|
|
7923
|
+
]),
|
|
7924
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7925
|
+
], ZModelCodeGenerator.prototype, "_generateAttributeParamType", null);
|
|
7926
|
+
_ts_decorate3([
|
|
7927
|
+
gen(FunctionDecl),
|
|
7928
|
+
_ts_metadata3("design:type", Function),
|
|
7929
|
+
_ts_metadata3("design:paramtypes", [
|
|
7930
|
+
typeof FunctionDecl === "undefined" ? Object : FunctionDecl
|
|
7931
|
+
]),
|
|
7932
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7933
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionDecl", null);
|
|
7934
|
+
_ts_decorate3([
|
|
7935
|
+
gen(FunctionParam),
|
|
7936
|
+
_ts_metadata3("design:type", Function),
|
|
7937
|
+
_ts_metadata3("design:paramtypes", [
|
|
7938
|
+
typeof FunctionParam === "undefined" ? Object : FunctionParam
|
|
7939
|
+
]),
|
|
7940
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7941
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParam", null);
|
|
7942
|
+
_ts_decorate3([
|
|
7943
|
+
gen(FunctionParamType),
|
|
7944
|
+
_ts_metadata3("design:type", Function),
|
|
7945
|
+
_ts_metadata3("design:paramtypes", [
|
|
7946
|
+
typeof FunctionParamType === "undefined" ? Object : FunctionParamType
|
|
7947
|
+
]),
|
|
7948
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7949
|
+
], ZModelCodeGenerator.prototype, "_generateFunctionParamType", null);
|
|
7950
|
+
_ts_decorate3([
|
|
7951
|
+
gen(TypeDef),
|
|
7952
|
+
_ts_metadata3("design:type", Function),
|
|
7953
|
+
_ts_metadata3("design:paramtypes", [
|
|
7954
|
+
typeof TypeDef === "undefined" ? Object : TypeDef
|
|
7955
|
+
]),
|
|
7956
|
+
_ts_metadata3("design:returntype", void 0)
|
|
7957
|
+
], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
|
|
7958
|
+
|
|
7959
|
+
// src/zmodel-completion-provider.ts
|
|
7960
|
+
var ZModelCompletionProvider = class extends DefaultCompletionProvider {
|
|
7961
|
+
static {
|
|
7962
|
+
__name(this, "ZModelCompletionProvider");
|
|
7963
|
+
}
|
|
7964
|
+
services;
|
|
7965
|
+
constructor(services) {
|
|
7966
|
+
super(services), this.services = services;
|
|
7967
|
+
}
|
|
7968
|
+
completionOptions = {
|
|
7969
|
+
triggerCharacters: [
|
|
7970
|
+
"@",
|
|
7971
|
+
"(",
|
|
7972
|
+
",",
|
|
7973
|
+
"."
|
|
7974
|
+
]
|
|
7975
|
+
};
|
|
7976
|
+
async getCompletion(document, params) {
|
|
7977
|
+
try {
|
|
7978
|
+
return await super.getCompletion(document, params);
|
|
7979
|
+
} catch (e) {
|
|
7980
|
+
console.error("Completion error:", e.message);
|
|
7981
|
+
return void 0;
|
|
7982
|
+
}
|
|
7983
|
+
}
|
|
7984
|
+
completionFor(context, next, acceptor) {
|
|
7985
|
+
if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
7986
|
+
const completions = this.getCompletionFromHint(context.node);
|
|
7987
|
+
if (completions) {
|
|
7988
|
+
completions.forEach((c) => acceptor(context, c));
|
|
7989
|
+
return;
|
|
7990
|
+
}
|
|
7991
|
+
}
|
|
7992
|
+
return super.completionFor(context, next, acceptor);
|
|
7993
|
+
}
|
|
7994
|
+
getCompletionFromHint(contextNode) {
|
|
7995
|
+
const unfilledParams = this.getUnfilledAttributeParams(contextNode);
|
|
7996
|
+
const nextParam = unfilledParams[0];
|
|
7997
|
+
if (!nextParam) {
|
|
7998
|
+
return void 0;
|
|
7999
|
+
}
|
|
8000
|
+
const hintAttr = getAttribute(nextParam, "@@@completionHint");
|
|
8001
|
+
if (hintAttr) {
|
|
8002
|
+
const hint = hintAttr.args[0];
|
|
8003
|
+
if (hint?.value) {
|
|
8004
|
+
if (isArrayExpr(hint.value)) {
|
|
8005
|
+
return hint.value.items.map((item) => {
|
|
8006
|
+
return {
|
|
8007
|
+
label: `${item.value}`,
|
|
8008
|
+
kind: CompletionItemKind.Value,
|
|
8009
|
+
detail: "Parameter",
|
|
8010
|
+
sortText: "0"
|
|
8011
|
+
};
|
|
8012
|
+
});
|
|
8013
|
+
}
|
|
8014
|
+
}
|
|
8015
|
+
}
|
|
8016
|
+
return void 0;
|
|
8017
|
+
}
|
|
8018
|
+
// TODO: this doesn't work when the file contains parse errors
|
|
8019
|
+
getUnfilledAttributeParams(contextNode) {
|
|
8020
|
+
try {
|
|
8021
|
+
const params = contextNode.decl.ref?.params;
|
|
8022
|
+
if (params) {
|
|
8023
|
+
const args = contextNode.args;
|
|
8024
|
+
let unfilledParams = [
|
|
8025
|
+
...params
|
|
8026
|
+
];
|
|
8027
|
+
args.forEach((arg) => {
|
|
8028
|
+
if (arg.name) {
|
|
8029
|
+
unfilledParams = unfilledParams.filter((p) => p.name !== arg.name);
|
|
8030
|
+
} else {
|
|
8031
|
+
unfilledParams.shift();
|
|
8032
|
+
}
|
|
8033
|
+
});
|
|
8034
|
+
return unfilledParams;
|
|
8035
|
+
}
|
|
8036
|
+
} catch {
|
|
8037
|
+
}
|
|
8038
|
+
return [];
|
|
8039
|
+
}
|
|
8040
|
+
completionForCrossReference(context, crossRef, acceptor) {
|
|
8041
|
+
if (crossRef.property === "member" && !isMemberAccessExpr(context.node)) {
|
|
8042
|
+
return;
|
|
8043
|
+
}
|
|
8044
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
8045
|
+
if (item.insertText?.startsWith("@@@") || item.label?.startsWith("@@@")) {
|
|
8046
|
+
return;
|
|
8047
|
+
}
|
|
8048
|
+
if ("nodeDescription" in item) {
|
|
8049
|
+
const node = this.getAstNode(item.nodeDescription);
|
|
8050
|
+
if (!node) {
|
|
8051
|
+
return;
|
|
8052
|
+
}
|
|
8053
|
+
if ((isEnum(node) || isEnumField(node)) && isFromStdlib(node)) {
|
|
8054
|
+
return;
|
|
8055
|
+
}
|
|
8056
|
+
if ((isDataModelAttribute(context2.node) || isDataFieldAttribute(context2.node)) && !this.filterAttributeApplicationCompletion(context2.node, node)) {
|
|
8057
|
+
return;
|
|
8058
|
+
}
|
|
8059
|
+
}
|
|
8060
|
+
acceptor(context2, item);
|
|
8061
|
+
}, "customAcceptor");
|
|
8062
|
+
return super.completionForCrossReference(context, crossRef, customAcceptor);
|
|
8063
|
+
}
|
|
8064
|
+
completionForKeyword(context, keyword, acceptor) {
|
|
8065
|
+
const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
|
|
8066
|
+
if (!this.filterKeywordForContext(context2, keyword.value)) {
|
|
8067
|
+
return;
|
|
8068
|
+
}
|
|
8069
|
+
acceptor(context2, item);
|
|
8070
|
+
}, "customAcceptor");
|
|
8071
|
+
return super.completionForKeyword(context, keyword, customAcceptor);
|
|
8072
|
+
}
|
|
8073
|
+
filterKeywordForContext(context, keyword) {
|
|
8074
|
+
if (isInvocationExpr(context.node)) {
|
|
8075
|
+
return [
|
|
8076
|
+
"true",
|
|
8077
|
+
"false",
|
|
8078
|
+
"null",
|
|
8079
|
+
"this"
|
|
8080
|
+
].includes(keyword);
|
|
8081
|
+
} else if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
|
|
8082
|
+
const exprContext = this.getAttributeContextType(context.node);
|
|
8083
|
+
if (exprContext === "DefaultValue") {
|
|
8084
|
+
return [
|
|
8085
|
+
"true",
|
|
8086
|
+
"false",
|
|
8087
|
+
"null"
|
|
8088
|
+
].includes(keyword);
|
|
8089
|
+
} else {
|
|
8090
|
+
return [
|
|
8091
|
+
"true",
|
|
8092
|
+
"false",
|
|
8093
|
+
"null",
|
|
8094
|
+
"this"
|
|
8095
|
+
].includes(keyword);
|
|
8096
|
+
}
|
|
8097
|
+
} else {
|
|
8098
|
+
return true;
|
|
8099
|
+
}
|
|
8100
|
+
}
|
|
8101
|
+
filterAttributeApplicationCompletion(contextNode, node) {
|
|
8102
|
+
const attrContextType = this.getAttributeContextType(contextNode);
|
|
8103
|
+
if (isFunctionDecl(node) && attrContextType) {
|
|
8104
|
+
const funcExprContextAttr = getAttribute(node, "@@@expressionContext");
|
|
8105
|
+
if (funcExprContextAttr && funcExprContextAttr.args[0]) {
|
|
8106
|
+
const arg = funcExprContextAttr.args[0];
|
|
8107
|
+
if (isArrayExpr(arg.value)) {
|
|
8108
|
+
return arg.value.items.some((item) => isEnumFieldReference(item) && item.target.$refText === attrContextType);
|
|
8109
|
+
}
|
|
8110
|
+
}
|
|
8111
|
+
return false;
|
|
8112
|
+
}
|
|
8113
|
+
if (isDataField(node)) {
|
|
8114
|
+
return attrContextType !== "DefaultValue";
|
|
8115
|
+
}
|
|
8116
|
+
return true;
|
|
8117
|
+
}
|
|
8118
|
+
getAttributeContextType(node) {
|
|
8119
|
+
return match3(node.decl.$refText).with("@default", () => "DefaultValue").with(P2.union("@@allow", "@allow", "@@deny", "@deny"), () => "AccessPolicy").with("@@validate", () => "ValidationRule").otherwise(() => void 0);
|
|
8120
|
+
}
|
|
8121
|
+
createReferenceCompletionItem(nodeDescription) {
|
|
8122
|
+
const node = this.getAstNode(nodeDescription);
|
|
8123
|
+
const documentation = this.getNodeDocumentation(node);
|
|
8124
|
+
return match3(node).when(isDataModel, () => ({
|
|
8125
|
+
nodeDescription,
|
|
8126
|
+
kind: CompletionItemKind.Class,
|
|
8127
|
+
detail: "Model",
|
|
8128
|
+
sortText: "1",
|
|
8129
|
+
documentation
|
|
8130
|
+
})).when(isTypeDef, () => ({
|
|
8131
|
+
nodeDescription,
|
|
8132
|
+
kind: CompletionItemKind.Class,
|
|
8133
|
+
detail: "Type",
|
|
8134
|
+
sortText: "1",
|
|
8135
|
+
documentation
|
|
8136
|
+
})).when(isDataField, () => ({
|
|
8137
|
+
nodeDescription,
|
|
8138
|
+
kind: CompletionItemKind.Field,
|
|
8139
|
+
detail: "Field",
|
|
8140
|
+
sortText: "0",
|
|
8141
|
+
documentation
|
|
8142
|
+
})).when(isEnum, () => ({
|
|
8143
|
+
nodeDescription,
|
|
8144
|
+
kind: CompletionItemKind.Class,
|
|
8145
|
+
detail: "Enum",
|
|
8146
|
+
sortText: "1",
|
|
8147
|
+
documentation
|
|
8148
|
+
})).when(isEnumField, (d) => {
|
|
8149
|
+
const container = d.$container;
|
|
8150
|
+
return {
|
|
8151
|
+
nodeDescription,
|
|
8152
|
+
kind: CompletionItemKind.Enum,
|
|
8153
|
+
detail: `Value of enum "${container.name}"`,
|
|
8154
|
+
sortText: "1",
|
|
8155
|
+
documentation
|
|
8156
|
+
};
|
|
8157
|
+
}).when(isFunctionDecl, () => ({
|
|
8158
|
+
nodeDescription,
|
|
8159
|
+
insertText: this.getFunctionInsertText(nodeDescription),
|
|
8160
|
+
kind: CompletionItemKind.Function,
|
|
8161
|
+
detail: "Function",
|
|
8162
|
+
sortText: "1",
|
|
8163
|
+
documentation
|
|
8164
|
+
})).when(isAttribute, () => ({
|
|
8165
|
+
nodeDescription,
|
|
8166
|
+
insertText: this.getAttributeInsertText(nodeDescription),
|
|
8167
|
+
kind: CompletionItemKind.Property,
|
|
8168
|
+
detail: "Attribute",
|
|
8169
|
+
sortText: "1",
|
|
8170
|
+
documentation
|
|
8171
|
+
})).otherwise(() => ({
|
|
8172
|
+
nodeDescription,
|
|
8173
|
+
kind: CompletionItemKind.Reference,
|
|
8174
|
+
detail: nodeDescription.type,
|
|
8175
|
+
sortText: "2",
|
|
8176
|
+
documentation
|
|
8177
|
+
}));
|
|
8178
|
+
}
|
|
8179
|
+
getFunctionInsertText(nodeDescription) {
|
|
8180
|
+
const node = this.getAstNode(nodeDescription);
|
|
8181
|
+
if (isFunctionDecl(node)) {
|
|
8182
|
+
if (node.params.some((p) => !p.optional)) {
|
|
8183
|
+
return nodeDescription.name;
|
|
8184
|
+
}
|
|
8185
|
+
}
|
|
8186
|
+
return `${nodeDescription.name}()`;
|
|
8187
|
+
}
|
|
8188
|
+
getAttributeInsertText(nodeDescription) {
|
|
8189
|
+
const node = this.getAstNode(nodeDescription);
|
|
8190
|
+
if (isAttribute(node)) {
|
|
8191
|
+
if (node.name === "@relation") {
|
|
8192
|
+
return `${nodeDescription.name}(fields: [], references: [])`;
|
|
8193
|
+
}
|
|
8194
|
+
}
|
|
8195
|
+
return nodeDescription.name;
|
|
8196
|
+
}
|
|
8197
|
+
getAstNode(nodeDescription) {
|
|
8198
|
+
let node = nodeDescription.node;
|
|
8199
|
+
if (!node) {
|
|
8200
|
+
const doc = this.getOrCreateDocumentSync(nodeDescription);
|
|
8201
|
+
if (!doc) {
|
|
8202
|
+
return void 0;
|
|
8203
|
+
}
|
|
8204
|
+
node = this.services.workspace.AstNodeLocator.getAstNode(doc.parseResult.value, nodeDescription.path);
|
|
8205
|
+
if (!node) {
|
|
8206
|
+
return void 0;
|
|
8207
|
+
}
|
|
8208
|
+
}
|
|
8209
|
+
return node;
|
|
8210
|
+
}
|
|
8211
|
+
getOrCreateDocumentSync(nodeDescription) {
|
|
8212
|
+
let doc = this.services.shared.workspace.LangiumDocuments.getDocument(nodeDescription.documentUri);
|
|
8213
|
+
if (!doc) {
|
|
8214
|
+
try {
|
|
8215
|
+
const content = fs2.readFileSync(nodeDescription.documentUri.fsPath, "utf-8");
|
|
8216
|
+
doc = this.services.shared.workspace.LangiumDocuments.createDocument(nodeDescription.documentUri, content);
|
|
8217
|
+
} catch {
|
|
8218
|
+
console.warn("Failed to read or create document:", nodeDescription.documentUri);
|
|
8219
|
+
return void 0;
|
|
8220
|
+
}
|
|
8221
|
+
}
|
|
8222
|
+
return doc;
|
|
8223
|
+
}
|
|
8224
|
+
getNodeDocumentation(node) {
|
|
8225
|
+
if (!node) {
|
|
8226
|
+
return void 0;
|
|
8227
|
+
}
|
|
8228
|
+
const md = this.commentsToMarkdown(node);
|
|
8229
|
+
return {
|
|
8230
|
+
kind: "markdown",
|
|
8231
|
+
value: md
|
|
8232
|
+
};
|
|
8233
|
+
}
|
|
8234
|
+
commentsToMarkdown(node) {
|
|
8235
|
+
const md = this.services.documentation.DocumentationProvider.getDocumentation(node) ?? "";
|
|
8236
|
+
const zModelGenerator = new ZModelCodeGenerator();
|
|
8237
|
+
const docs = [];
|
|
8238
|
+
try {
|
|
8239
|
+
match3(node).when(isAttribute, (attr) => {
|
|
8240
|
+
docs.push("```prisma", zModelGenerator.generate(attr), "```");
|
|
8241
|
+
}).when(isFunctionDecl, (func2) => {
|
|
8242
|
+
docs.push("```ts", zModelGenerator.generate(func2), "```");
|
|
8243
|
+
}).when(isDataModel, (model) => {
|
|
8244
|
+
docs.push("```prisma", `model ${model.name} { ... }`, "```");
|
|
8245
|
+
}).when(isEnum, (enumDecl) => {
|
|
8246
|
+
docs.push("```prisma", zModelGenerator.generate(enumDecl), "```");
|
|
8247
|
+
}).when(isDataField, (field) => {
|
|
8248
|
+
docs.push(`${field.name}: ${field.type.type ?? field.type.reference?.$refText}`);
|
|
8249
|
+
}).otherwise((ast) => {
|
|
8250
|
+
const name = ast.name;
|
|
8251
|
+
if (name) {
|
|
8252
|
+
docs.push(name);
|
|
8253
|
+
}
|
|
8254
|
+
});
|
|
8255
|
+
} catch {
|
|
8256
|
+
}
|
|
8257
|
+
if (md) {
|
|
8258
|
+
docs.push("___", md);
|
|
8259
|
+
}
|
|
8260
|
+
return docs.join("\n");
|
|
8261
|
+
}
|
|
8262
|
+
};
|
|
8263
|
+
|
|
8264
|
+
// src/zmodel-definition.ts
|
|
8265
|
+
import { DefaultDefinitionProvider } from "langium/lsp";
|
|
8266
|
+
import { LocationLink, Range } from "vscode-languageserver";
|
|
8267
|
+
var ZModelDefinitionProvider = class extends DefaultDefinitionProvider {
|
|
8268
|
+
static {
|
|
8269
|
+
__name(this, "ZModelDefinitionProvider");
|
|
8270
|
+
}
|
|
8271
|
+
documents;
|
|
8272
|
+
constructor(services) {
|
|
8273
|
+
super(services);
|
|
8274
|
+
this.documents = services.shared.workspace.LangiumDocuments;
|
|
8275
|
+
}
|
|
8276
|
+
collectLocationLinks(sourceCstNode, _params) {
|
|
8277
|
+
if (isModelImport(sourceCstNode.astNode)) {
|
|
8278
|
+
const importedModel = resolveImport(this.documents, sourceCstNode.astNode);
|
|
8279
|
+
if (importedModel?.$document) {
|
|
8280
|
+
const targetObject = importedModel;
|
|
8281
|
+
const selectionRange = this.nameProvider.getNameNode(targetObject)?.range ?? Range.create(0, 0, 0, 0);
|
|
8282
|
+
const previewRange = targetObject.$cstNode?.range ?? Range.create(0, 0, 0, 0);
|
|
8283
|
+
return [
|
|
8284
|
+
LocationLink.create(importedModel.$document.uri.toString(), previewRange, selectionRange, sourceCstNode.range)
|
|
8285
|
+
];
|
|
8286
|
+
}
|
|
8287
|
+
return void 0;
|
|
8288
|
+
}
|
|
8289
|
+
return super.collectLocationLinks(sourceCstNode, _params);
|
|
8290
|
+
}
|
|
8291
|
+
};
|
|
8292
|
+
|
|
7106
8293
|
// src/zmodel-document-builder.ts
|
|
7107
8294
|
import { DefaultDocumentBuilder } from "langium";
|
|
7108
8295
|
var ZModelDocumentBuilder = class extends DefaultDocumentBuilder {
|
|
@@ -7132,9 +8319,114 @@ var ZModelDocumentBuilder = class extends DefaultDocumentBuilder {
|
|
|
7132
8319
|
}
|
|
7133
8320
|
};
|
|
7134
8321
|
|
|
8322
|
+
// src/zmodel-documentation-provider.ts
|
|
8323
|
+
import { JSDocDocumentationProvider } from "langium";
|
|
8324
|
+
var ZModelDocumentationProvider = class extends JSDocDocumentationProvider {
|
|
8325
|
+
static {
|
|
8326
|
+
__name(this, "ZModelDocumentationProvider");
|
|
8327
|
+
}
|
|
8328
|
+
getDocumentation(node) {
|
|
8329
|
+
if ("comments" in node && Array.isArray(node.comments) && node.comments.length > 0) {
|
|
8330
|
+
return node.comments.map((c) => c.replace(/^[/]*\s*/, "")).join("\n");
|
|
8331
|
+
}
|
|
8332
|
+
return super.getDocumentation(node);
|
|
8333
|
+
}
|
|
8334
|
+
};
|
|
8335
|
+
|
|
8336
|
+
// src/zmodel-formatter.ts
|
|
8337
|
+
import { AbstractFormatter, Formatting } from "langium/lsp";
|
|
8338
|
+
var ZModelFormatter = class extends AbstractFormatter {
|
|
8339
|
+
static {
|
|
8340
|
+
__name(this, "ZModelFormatter");
|
|
8341
|
+
}
|
|
8342
|
+
formatOptions;
|
|
8343
|
+
configurationProvider;
|
|
8344
|
+
constructor(services) {
|
|
8345
|
+
super();
|
|
8346
|
+
this.configurationProvider = services.shared.workspace.ConfigurationProvider;
|
|
8347
|
+
}
|
|
8348
|
+
format(node) {
|
|
8349
|
+
const formatter = this.getNodeFormatter(node);
|
|
8350
|
+
if (isDataField(node)) {
|
|
8351
|
+
if (isDataModel(node.$container) || isTypeDef(node.$container)) {
|
|
8352
|
+
const dataModel = node.$container;
|
|
8353
|
+
const compareFn = /* @__PURE__ */ __name((a, b) => b - a, "compareFn");
|
|
8354
|
+
const maxNameLength = dataModel.fields.map((x) => x.name.length).sort(compareFn)[0] ?? 0;
|
|
8355
|
+
const maxTypeLength = dataModel.fields.map(this.getFieldTypeLength).sort(compareFn)[0] ?? 0;
|
|
8356
|
+
formatter.property("type").prepend(Formatting.spaces(maxNameLength - node.name.length + 1));
|
|
8357
|
+
if (node.attributes.length > 0) {
|
|
8358
|
+
formatter.node(node.attributes[0]).prepend(Formatting.spaces(maxTypeLength - this.getFieldTypeLength(node) + 1));
|
|
8359
|
+
formatter.nodes(...node.attributes.slice(1)).prepend(Formatting.oneSpace());
|
|
8360
|
+
}
|
|
8361
|
+
} else {
|
|
8362
|
+
formatter.property("type").prepend(Formatting.oneSpace());
|
|
8363
|
+
if (node.attributes.length > 0) {
|
|
8364
|
+
formatter.properties("attributes").prepend(Formatting.oneSpace());
|
|
8365
|
+
}
|
|
8366
|
+
}
|
|
8367
|
+
} else if (isDataFieldAttribute(node)) {
|
|
8368
|
+
formatter.keyword("(").surround(Formatting.noSpace());
|
|
8369
|
+
formatter.keyword(")").prepend(Formatting.noSpace());
|
|
8370
|
+
formatter.keyword(",").append(Formatting.oneSpace());
|
|
8371
|
+
if (node.args.length > 1) {
|
|
8372
|
+
formatter.nodes(...node.args.slice(1)).prepend(Formatting.oneSpace());
|
|
8373
|
+
}
|
|
8374
|
+
} else if (isAttributeArg(node)) {
|
|
8375
|
+
formatter.keyword(":").prepend(Formatting.noSpace());
|
|
8376
|
+
formatter.keyword(":").append(Formatting.oneSpace());
|
|
8377
|
+
} else if (isAbstractDeclaration(node)) {
|
|
8378
|
+
const bracesOpen = formatter.keyword("{");
|
|
8379
|
+
const bracesClose = formatter.keyword("}");
|
|
8380
|
+
formatter.interior(bracesOpen, bracesClose).prepend(Formatting.indent({
|
|
8381
|
+
allowMore: true
|
|
8382
|
+
}));
|
|
8383
|
+
bracesOpen.prepend(Formatting.oneSpace());
|
|
8384
|
+
bracesClose.prepend(Formatting.newLine());
|
|
8385
|
+
} else if (isModel(node)) {
|
|
8386
|
+
const model = node;
|
|
8387
|
+
const nodes = formatter.nodes(...model.declarations);
|
|
8388
|
+
nodes.prepend(Formatting.noIndent());
|
|
8389
|
+
}
|
|
8390
|
+
}
|
|
8391
|
+
formatDocument(document, params) {
|
|
8392
|
+
this.formatOptions = params.options;
|
|
8393
|
+
this.configurationProvider.getConfiguration(ZModelLanguageMetaData.languageId, "format").then((config) => {
|
|
8394
|
+
if (config) {
|
|
8395
|
+
}
|
|
8396
|
+
});
|
|
8397
|
+
return super.formatDocument(document, params);
|
|
8398
|
+
}
|
|
8399
|
+
getFormatOptions() {
|
|
8400
|
+
return this.formatOptions;
|
|
8401
|
+
}
|
|
8402
|
+
getIndent() {
|
|
8403
|
+
return 1;
|
|
8404
|
+
}
|
|
8405
|
+
getFieldTypeLength(field) {
|
|
8406
|
+
let length;
|
|
8407
|
+
if (field.type.type) {
|
|
8408
|
+
length = field.type.type.length;
|
|
8409
|
+
} else if (field.type.reference) {
|
|
8410
|
+
length = field.type.reference.$refText.length;
|
|
8411
|
+
} else if (isDataField(field) && field.type.unsupported) {
|
|
8412
|
+
const name = `Unsupported("${field.type.unsupported.value.value}")`;
|
|
8413
|
+
length = name.length;
|
|
8414
|
+
} else {
|
|
8415
|
+
length = 1;
|
|
8416
|
+
}
|
|
8417
|
+
if (field.type.optional) {
|
|
8418
|
+
length += 1;
|
|
8419
|
+
}
|
|
8420
|
+
if (field.type.array) {
|
|
8421
|
+
length += 2;
|
|
8422
|
+
}
|
|
8423
|
+
return length;
|
|
8424
|
+
}
|
|
8425
|
+
};
|
|
8426
|
+
|
|
7135
8427
|
// src/zmodel-linker.ts
|
|
7136
8428
|
import { AstUtils as AstUtils6, Cancellation, DefaultLinker, DocumentState, interruptAndCheck } from "langium";
|
|
7137
|
-
import { match as
|
|
8429
|
+
import { match as match4 } from "ts-pattern";
|
|
7138
8430
|
var ZModelLinker = class extends DefaultLinker {
|
|
7139
8431
|
static {
|
|
7140
8432
|
__name(this, "ZModelLinker");
|
|
@@ -7323,7 +8615,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7323
8615
|
}
|
|
7324
8616
|
}
|
|
7325
8617
|
resolveLiteral(node) {
|
|
7326
|
-
const type =
|
|
8618
|
+
const type = match4(node).when(isStringLiteral, () => "String").when(isBooleanLiteral, () => "Boolean").when(isNumberLiteral, () => "Int").exhaustive();
|
|
7327
8619
|
if (type) {
|
|
7328
8620
|
this.resolveToBuiltinTypeOrDecl(node, type);
|
|
7329
8621
|
}
|
|
@@ -7491,7 +8783,7 @@ var ZModelLinker = class extends DefaultLinker {
|
|
|
7491
8783
|
|
|
7492
8784
|
// src/zmodel-scope.ts
|
|
7493
8785
|
import { AstUtils as AstUtils7, DefaultScopeComputation, DefaultScopeProvider, EMPTY_SCOPE, StreamScope, UriUtils, interruptAndCheck as interruptAndCheck2 } from "langium";
|
|
7494
|
-
import { match as
|
|
8786
|
+
import { match as match5 } from "ts-pattern";
|
|
7495
8787
|
var ZModelScopeComputation = class extends DefaultScopeComputation {
|
|
7496
8788
|
static {
|
|
7497
8789
|
__name(this, "ZModelScopeComputation");
|
|
@@ -7564,11 +8856,8 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7564
8856
|
const referenceType = this.reflection.getReferenceType(context);
|
|
7565
8857
|
const globalScope = this.getGlobalScope(referenceType, context);
|
|
7566
8858
|
const node = context.container;
|
|
7567
|
-
const allowTypeDefScope = (
|
|
7568
|
-
|
|
7569
|
-
!!AstUtils7.getContainerOfType(node, isTypeDef)
|
|
7570
|
-
);
|
|
7571
|
-
return match3(node.operand).when(isReferenceExpr, (operand) => {
|
|
8859
|
+
const allowTypeDefScope = isAuthOrAuthMemberAccess(node.operand) || !!AstUtils7.getContainerOfType(node, isTypeDef);
|
|
8860
|
+
return match5(node.operand).when(isReferenceExpr, (operand) => {
|
|
7572
8861
|
const ref = operand.target.ref;
|
|
7573
8862
|
if (isDataField(ref)) {
|
|
7574
8863
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7596,8 +8885,8 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
|
|
|
7596
8885
|
const referenceType = this.reflection.getReferenceType(context);
|
|
7597
8886
|
const globalScope = this.getGlobalScope(referenceType, context);
|
|
7598
8887
|
const collection = collectionPredicate.left;
|
|
7599
|
-
const allowTypeDefScope =
|
|
7600
|
-
return
|
|
8888
|
+
const allowTypeDefScope = isAuthOrAuthMemberAccess(collection);
|
|
8889
|
+
return match5(collection).when(isReferenceExpr, (expr) => {
|
|
7601
8890
|
const ref = expr.target.ref;
|
|
7602
8891
|
if (isDataField(ref)) {
|
|
7603
8892
|
return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
|
|
@@ -7659,11 +8948,95 @@ function getCollectionPredicateContext(node) {
|
|
|
7659
8948
|
}
|
|
7660
8949
|
__name(getCollectionPredicateContext, "getCollectionPredicateContext");
|
|
7661
8950
|
|
|
8951
|
+
// src/zmodel-semantic.ts
|
|
8952
|
+
import { AbstractSemanticTokenProvider } from "langium/lsp";
|
|
8953
|
+
import { SemanticTokenTypes } from "vscode-languageserver";
|
|
8954
|
+
var ZModelSemanticTokenProvider = class extends AbstractSemanticTokenProvider {
|
|
8955
|
+
static {
|
|
8956
|
+
__name(this, "ZModelSemanticTokenProvider");
|
|
8957
|
+
}
|
|
8958
|
+
highlightElement(node, acceptor) {
|
|
8959
|
+
if (isDataModel(node)) {
|
|
8960
|
+
acceptor({
|
|
8961
|
+
node,
|
|
8962
|
+
property: "name",
|
|
8963
|
+
type: SemanticTokenTypes.type
|
|
8964
|
+
});
|
|
8965
|
+
acceptor({
|
|
8966
|
+
node,
|
|
8967
|
+
property: "mixins",
|
|
8968
|
+
type: SemanticTokenTypes.type
|
|
8969
|
+
});
|
|
8970
|
+
acceptor({
|
|
8971
|
+
node,
|
|
8972
|
+
property: "baseModel",
|
|
8973
|
+
type: SemanticTokenTypes.type
|
|
8974
|
+
});
|
|
8975
|
+
} else if (isDataSource(node) || isGeneratorDecl(node) || isPlugin(node) || isEnum(node) || isTypeDef(node)) {
|
|
8976
|
+
acceptor({
|
|
8977
|
+
node,
|
|
8978
|
+
property: "name",
|
|
8979
|
+
type: SemanticTokenTypes.type
|
|
8980
|
+
});
|
|
8981
|
+
} else if (isDataField(node) || isConfigField(node) || isAttributeArg(node) || isPluginField(node) || isEnumField(node)) {
|
|
8982
|
+
acceptor({
|
|
8983
|
+
node,
|
|
8984
|
+
property: "name",
|
|
8985
|
+
type: SemanticTokenTypes.variable
|
|
8986
|
+
});
|
|
8987
|
+
} else if (isDataFieldType(node)) {
|
|
8988
|
+
if (node.type) {
|
|
8989
|
+
acceptor({
|
|
8990
|
+
node,
|
|
8991
|
+
property: "type",
|
|
8992
|
+
type: SemanticTokenTypes.type
|
|
8993
|
+
});
|
|
8994
|
+
} else {
|
|
8995
|
+
acceptor({
|
|
8996
|
+
node,
|
|
8997
|
+
property: "reference",
|
|
8998
|
+
type: SemanticTokenTypes.macro
|
|
8999
|
+
});
|
|
9000
|
+
}
|
|
9001
|
+
} else if (isDataModelAttribute(node) || isDataFieldAttribute(node) || isInternalAttribute(node)) {
|
|
9002
|
+
acceptor({
|
|
9003
|
+
node,
|
|
9004
|
+
property: "decl",
|
|
9005
|
+
type: SemanticTokenTypes.function
|
|
9006
|
+
});
|
|
9007
|
+
} else if (isInvocationExpr(node)) {
|
|
9008
|
+
acceptor({
|
|
9009
|
+
node,
|
|
9010
|
+
property: "function",
|
|
9011
|
+
type: SemanticTokenTypes.function
|
|
9012
|
+
});
|
|
9013
|
+
} else if (isFunctionDecl(node) || isAttribute(node)) {
|
|
9014
|
+
acceptor({
|
|
9015
|
+
node,
|
|
9016
|
+
property: "name",
|
|
9017
|
+
type: SemanticTokenTypes.function
|
|
9018
|
+
});
|
|
9019
|
+
} else if (isReferenceExpr(node)) {
|
|
9020
|
+
acceptor({
|
|
9021
|
+
node,
|
|
9022
|
+
property: "target",
|
|
9023
|
+
type: SemanticTokenTypes.variable
|
|
9024
|
+
});
|
|
9025
|
+
} else if (isMemberAccessExpr(node)) {
|
|
9026
|
+
acceptor({
|
|
9027
|
+
node,
|
|
9028
|
+
property: "member",
|
|
9029
|
+
type: SemanticTokenTypes.property
|
|
9030
|
+
});
|
|
9031
|
+
}
|
|
9032
|
+
}
|
|
9033
|
+
};
|
|
9034
|
+
|
|
7662
9035
|
// src/zmodel-workspace-manager.ts
|
|
7663
|
-
import { DefaultWorkspaceManager, URI as URI2
|
|
7664
|
-
import
|
|
9036
|
+
import { DefaultWorkspaceManager, URI as URI2 } from "langium";
|
|
9037
|
+
import fs3 from "fs";
|
|
7665
9038
|
import path2 from "path";
|
|
7666
|
-
import { fileURLToPath } from "url";
|
|
9039
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
7667
9040
|
var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
7668
9041
|
static {
|
|
7669
9042
|
__name(this, "ZModelWorkspaceManager");
|
|
@@ -7687,7 +9060,7 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7687
9060
|
});
|
|
7688
9061
|
const languagePackageDir = path2.dirname(languagePackagePath);
|
|
7689
9062
|
const candidateStdlibPath = path2.join(languagePackageDir, "res", STD_LIB_MODULE_NAME);
|
|
7690
|
-
if (
|
|
9063
|
+
if (fs3.existsSync(candidateStdlibPath)) {
|
|
7691
9064
|
installedStdlibPath = candidateStdlibPath;
|
|
7692
9065
|
console.log(`Found installed zenstack package stdlib at: ${installedStdlibPath}`);
|
|
7693
9066
|
break;
|
|
@@ -7699,73 +9072,12 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
|
|
|
7699
9072
|
if (installedStdlibPath) {
|
|
7700
9073
|
stdLibPath = installedStdlibPath;
|
|
7701
9074
|
} else {
|
|
7702
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(
|
|
9075
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(fileURLToPath2(import.meta.url));
|
|
7703
9076
|
stdLibPath = path2.join(_dirname, "../res", STD_LIB_MODULE_NAME);
|
|
7704
9077
|
console.log(`Using bundled stdlib in extension:`, stdLibPath);
|
|
7705
9078
|
}
|
|
7706
9079
|
const stdlib = await this.documentFactory.fromUri(URI2.file(stdLibPath));
|
|
7707
9080
|
collector(stdlib);
|
|
7708
|
-
const documents = this.langiumDocuments.all;
|
|
7709
|
-
const pluginModels = /* @__PURE__ */ new Set();
|
|
7710
|
-
documents.forEach((doc) => {
|
|
7711
|
-
const parsed = doc.parseResult.value;
|
|
7712
|
-
parsed.declarations.forEach((decl) => {
|
|
7713
|
-
if (isPlugin(decl)) {
|
|
7714
|
-
const providerField = decl.fields.find((f) => f.name === "provider");
|
|
7715
|
-
if (providerField) {
|
|
7716
|
-
const provider = getLiteral(providerField.value);
|
|
7717
|
-
if (provider) {
|
|
7718
|
-
pluginModels.add(provider);
|
|
7719
|
-
}
|
|
7720
|
-
}
|
|
7721
|
-
}
|
|
7722
|
-
});
|
|
7723
|
-
});
|
|
7724
|
-
if (pluginModels.size > 0) {
|
|
7725
|
-
console.log(`Used plugin modules: ${Array.from(pluginModels)}`);
|
|
7726
|
-
const pendingPluginModules = new Set(pluginModels);
|
|
7727
|
-
await Promise.all(folders.map((wf) => [
|
|
7728
|
-
wf,
|
|
7729
|
-
this.getRootFolder(wf)
|
|
7730
|
-
]).map(async (entry) => this.loadPluginModels(...entry, pendingPluginModules, collector)));
|
|
7731
|
-
}
|
|
7732
|
-
}
|
|
7733
|
-
async loadPluginModels(workspaceFolder, folderPath, pendingPluginModels, collector) {
|
|
7734
|
-
const content = (await this.fileSystemProvider.readDirectory(folderPath)).sort((a, b) => {
|
|
7735
|
-
if (a.isDirectory && b.isDirectory) {
|
|
7736
|
-
const aName = UriUtils2.basename(a.uri);
|
|
7737
|
-
if (aName === "node_modules") {
|
|
7738
|
-
return -1;
|
|
7739
|
-
} else {
|
|
7740
|
-
return 1;
|
|
7741
|
-
}
|
|
7742
|
-
} else {
|
|
7743
|
-
return 0;
|
|
7744
|
-
}
|
|
7745
|
-
});
|
|
7746
|
-
for (const entry of content) {
|
|
7747
|
-
if (entry.isDirectory) {
|
|
7748
|
-
const name = UriUtils2.basename(entry.uri);
|
|
7749
|
-
if (name === "node_modules") {
|
|
7750
|
-
for (const plugin of Array.from(pendingPluginModels)) {
|
|
7751
|
-
const path4 = UriUtils2.joinPath(entry.uri, plugin, PLUGIN_MODULE_NAME);
|
|
7752
|
-
try {
|
|
7753
|
-
await this.fileSystemProvider.readFile(path4);
|
|
7754
|
-
const document = await this.langiumDocuments.getOrCreateDocument(path4);
|
|
7755
|
-
collector(document);
|
|
7756
|
-
console.log(`Adding plugin document from ${path4.path}`);
|
|
7757
|
-
pendingPluginModels.delete(plugin);
|
|
7758
|
-
if (pendingPluginModels.size === 0) {
|
|
7759
|
-
return;
|
|
7760
|
-
}
|
|
7761
|
-
} catch {
|
|
7762
|
-
}
|
|
7763
|
-
}
|
|
7764
|
-
} else {
|
|
7765
|
-
await this.loadPluginModels(workspaceFolder, entry.uri, pendingPluginModels, collector);
|
|
7766
|
-
}
|
|
7767
|
-
}
|
|
7768
|
-
}
|
|
7769
9081
|
}
|
|
7770
9082
|
};
|
|
7771
9083
|
|
|
@@ -7778,6 +9090,16 @@ var ZModelLanguageModule = {
|
|
|
7778
9090
|
},
|
|
7779
9091
|
validation: {
|
|
7780
9092
|
ZModelValidator: /* @__PURE__ */ __name((services) => new ZModelValidator(services), "ZModelValidator")
|
|
9093
|
+
},
|
|
9094
|
+
lsp: {
|
|
9095
|
+
Formatter: /* @__PURE__ */ __name((services) => new ZModelFormatter(services), "Formatter"),
|
|
9096
|
+
DefinitionProvider: /* @__PURE__ */ __name((services) => new ZModelDefinitionProvider(services), "DefinitionProvider"),
|
|
9097
|
+
CompletionProvider: /* @__PURE__ */ __name((services) => new ZModelCompletionProvider(services), "CompletionProvider"),
|
|
9098
|
+
SemanticTokenProvider: /* @__PURE__ */ __name((services) => new ZModelSemanticTokenProvider(services), "SemanticTokenProvider")
|
|
9099
|
+
},
|
|
9100
|
+
documentation: {
|
|
9101
|
+
CommentProvider: /* @__PURE__ */ __name((services) => new ZModelCommentProvider(services), "CommentProvider"),
|
|
9102
|
+
DocumentationProvider: /* @__PURE__ */ __name((services) => new ZModelDocumentationProvider(services), "DocumentationProvider")
|
|
7781
9103
|
}
|
|
7782
9104
|
};
|
|
7783
9105
|
var ZModelSharedModule = {
|
|
@@ -7786,7 +9108,7 @@ var ZModelSharedModule = {
|
|
|
7786
9108
|
WorkspaceManager: /* @__PURE__ */ __name((services) => new ZModelWorkspaceManager(services), "WorkspaceManager")
|
|
7787
9109
|
}
|
|
7788
9110
|
};
|
|
7789
|
-
function createZModelLanguageServices(context) {
|
|
9111
|
+
function createZModelLanguageServices(context, logToConsole = false) {
|
|
7790
9112
|
const shared = inject(createDefaultSharedModule(context), ZModelGeneratedSharedModule, ZModelSharedModule);
|
|
7791
9113
|
const ZModelLanguage = inject(createDefaultModule({
|
|
7792
9114
|
shared
|
|
@@ -7796,30 +9118,47 @@ function createZModelLanguageServices(context) {
|
|
|
7796
9118
|
if (!context.connection) {
|
|
7797
9119
|
shared.workspace.ConfigurationProvider.initialized({});
|
|
7798
9120
|
}
|
|
9121
|
+
shared.workspace.DocumentBuilder.onBuildPhase(DocumentState2.Parsed, async (documents) => {
|
|
9122
|
+
for (const doc of documents) {
|
|
9123
|
+
if (doc.parseResult.lexerErrors.length > 0 || doc.parseResult.parserErrors.length > 0) {
|
|
9124
|
+
continue;
|
|
9125
|
+
}
|
|
9126
|
+
if (doc.uri.scheme !== "file") {
|
|
9127
|
+
continue;
|
|
9128
|
+
}
|
|
9129
|
+
const schemaPath = fileURLToPath3(doc.uri.toString());
|
|
9130
|
+
const pluginSchemas = getPluginDocuments(doc.parseResult.value, schemaPath);
|
|
9131
|
+
for (const plugin of pluginSchemas) {
|
|
9132
|
+
const pluginDocUri = URI3.file(path3.resolve(plugin));
|
|
9133
|
+
let pluginDoc = shared.workspace.LangiumDocuments.getDocument(pluginDocUri);
|
|
9134
|
+
if (!pluginDoc) {
|
|
9135
|
+
pluginDoc = await shared.workspace.LangiumDocuments.getOrCreateDocument(pluginDocUri);
|
|
9136
|
+
if (pluginDoc) {
|
|
9137
|
+
shared.workspace.IndexManager.updateContent(pluginDoc);
|
|
9138
|
+
if (logToConsole) {
|
|
9139
|
+
console.log(`Loaded plugin model: ${plugin}`);
|
|
9140
|
+
}
|
|
9141
|
+
}
|
|
9142
|
+
}
|
|
9143
|
+
}
|
|
9144
|
+
}
|
|
9145
|
+
});
|
|
7799
9146
|
return {
|
|
7800
9147
|
shared,
|
|
7801
9148
|
ZModelLanguage
|
|
7802
9149
|
};
|
|
7803
9150
|
}
|
|
7804
9151
|
__name(createZModelLanguageServices, "createZModelLanguageServices");
|
|
7805
|
-
|
|
7806
|
-
|
|
7807
|
-
function createZModelServices() {
|
|
7808
|
-
return createZModelLanguageServices(NodeFileSystem);
|
|
9152
|
+
function createZModelServices(logToConsole = false) {
|
|
9153
|
+
return createZModelLanguageServices(NodeFileSystem, logToConsole);
|
|
7809
9154
|
}
|
|
7810
9155
|
__name(createZModelServices, "createZModelServices");
|
|
7811
|
-
|
|
7812
|
-
|
|
7813
|
-
__name(this, "DocumentLoadError");
|
|
7814
|
-
}
|
|
7815
|
-
constructor(message) {
|
|
7816
|
-
super(message);
|
|
7817
|
-
}
|
|
7818
|
-
};
|
|
9156
|
+
|
|
9157
|
+
// src/document.ts
|
|
7819
9158
|
async function loadDocument(fileName, additionalModelFiles = []) {
|
|
7820
|
-
const { ZModelLanguage: services } = createZModelServices();
|
|
9159
|
+
const { ZModelLanguage: services } = createZModelServices(false);
|
|
7821
9160
|
const extensions = services.LanguageMetaData.fileExtensions;
|
|
7822
|
-
if (!extensions.includes(
|
|
9161
|
+
if (!extensions.includes(path4.extname(fileName))) {
|
|
7823
9162
|
return {
|
|
7824
9163
|
success: false,
|
|
7825
9164
|
errors: [
|
|
@@ -7828,7 +9167,7 @@ async function loadDocument(fileName, additionalModelFiles = []) {
|
|
|
7828
9167
|
warnings: []
|
|
7829
9168
|
};
|
|
7830
9169
|
}
|
|
7831
|
-
if (!
|
|
9170
|
+
if (!fs4.existsSync(fileName)) {
|
|
7832
9171
|
return {
|
|
7833
9172
|
success: false,
|
|
7834
9173
|
errors: [
|
|
@@ -7837,19 +9176,19 @@ async function loadDocument(fileName, additionalModelFiles = []) {
|
|
|
7837
9176
|
warnings: []
|
|
7838
9177
|
};
|
|
7839
9178
|
}
|
|
7840
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname :
|
|
7841
|
-
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(
|
|
7842
|
-
const pluginDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
|
|
9179
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path4.dirname(fileURLToPath4(import.meta.url));
|
|
9180
|
+
const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(path4.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
|
|
7843
9181
|
const langiumDocuments = services.shared.workspace.LangiumDocuments;
|
|
7844
|
-
const document = await langiumDocuments.getOrCreateDocument(
|
|
9182
|
+
const document = await langiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(fileName)));
|
|
7845
9183
|
const importedURIs = await loadImports(document, langiumDocuments);
|
|
7846
9184
|
const importedDocuments = [];
|
|
7847
9185
|
for (const uri of importedURIs) {
|
|
7848
9186
|
importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
|
|
7849
9187
|
}
|
|
9188
|
+
const additionalDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(file)))));
|
|
7850
9189
|
await services.shared.workspace.DocumentBuilder.build([
|
|
7851
9190
|
stdLib,
|
|
7852
|
-
...
|
|
9191
|
+
...additionalDocs,
|
|
7853
9192
|
document,
|
|
7854
9193
|
...importedDocuments
|
|
7855
9194
|
], {
|
|
@@ -7867,7 +9206,7 @@ async function loadDocument(fileName, additionalModelFiles = []) {
|
|
|
7867
9206
|
const warnings = [];
|
|
7868
9207
|
if (diagnostics.length > 0) {
|
|
7869
9208
|
for (const { doc, diag } of diagnostics) {
|
|
7870
|
-
const message = `${
|
|
9209
|
+
const message = `${path4.relative(process.cwd(), doc.uri.fsPath)}:${diag.range.start.line + 1}:${diag.range.start.character + 1} - ${diag.message}`;
|
|
7871
9210
|
if (diag.severity === 1) {
|
|
7872
9211
|
errors.push(message);
|
|
7873
9212
|
} else {
|
|
@@ -7916,7 +9255,7 @@ async function loadImports(document, documents, uris = /* @__PURE__ */ new Set()
|
|
|
7916
9255
|
}
|
|
7917
9256
|
}
|
|
7918
9257
|
}
|
|
7919
|
-
return Array.from(uris).filter((x) => uriString != x).map((e) =>
|
|
9258
|
+
return Array.from(uris).filter((x) => uriString != x).map((e) => URI4.parse(e));
|
|
7920
9259
|
}
|
|
7921
9260
|
__name(loadImports, "loadImports");
|
|
7922
9261
|
function mergeImportsDeclarations(documents, model) {
|
|
@@ -7965,13 +9304,33 @@ function validationAfterImportMerge(model) {
|
|
|
7965
9304
|
return errors;
|
|
7966
9305
|
}
|
|
7967
9306
|
__name(validationAfterImportMerge, "validationAfterImportMerge");
|
|
9307
|
+
async function formatDocument(content) {
|
|
9308
|
+
const services = createZModelServices().ZModelLanguage;
|
|
9309
|
+
const langiumDocuments = services.shared.workspace.LangiumDocuments;
|
|
9310
|
+
const document = langiumDocuments.createDocument(URI4.parse("memory://schema.zmodel"), content);
|
|
9311
|
+
const formatter = services.lsp.Formatter;
|
|
9312
|
+
const identifier = {
|
|
9313
|
+
uri: document.uri.toString()
|
|
9314
|
+
};
|
|
9315
|
+
const options = formatter.getFormatOptions() ?? {
|
|
9316
|
+
insertSpaces: true,
|
|
9317
|
+
tabSize: 4
|
|
9318
|
+
};
|
|
9319
|
+
const edits = await formatter.formatDocument(document, {
|
|
9320
|
+
options,
|
|
9321
|
+
textDocument: identifier
|
|
9322
|
+
});
|
|
9323
|
+
return TextDocument.applyEdits(document.textDocument, edits);
|
|
9324
|
+
}
|
|
9325
|
+
__name(formatDocument, "formatDocument");
|
|
7968
9326
|
export {
|
|
7969
|
-
|
|
9327
|
+
ZModelCodeGenerator,
|
|
7970
9328
|
ZModelLanguageMetaData,
|
|
7971
9329
|
ZModelLanguageModule,
|
|
7972
9330
|
ZModelSharedModule,
|
|
7973
9331
|
createZModelLanguageServices,
|
|
7974
9332
|
createZModelServices,
|
|
9333
|
+
formatDocument,
|
|
7975
9334
|
loadDocument
|
|
7976
9335
|
};
|
|
7977
9336
|
//# sourceMappingURL=index.js.map
|