@zenstackhq/language 3.0.0-beta.3 → 3.0.0-beta.31

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.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/index.ts
11
- import { isAstNode, URI as URI3 } from "langium";
12
- import { NodeFileSystem } from "langium/node";
13
- import fs3 from "fs";
14
- import path3 from "path";
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,15 +5158,12 @@ var ZModelGeneratedModule = {
5104
5158
  parser: {}
5105
5159
  };
5106
5160
 
5107
- // src/validators/attribute-application-validator.ts
5108
- import { AstUtils as AstUtils2 } from "langium";
5109
- import pluralize from "pluralize";
5110
-
5111
5161
  // src/utils.ts
5112
- import { invariant } from "@zenstackhq/common-helpers";
5113
5162
  import { AstUtils, URI } from "langium";
5114
5163
  import fs from "fs";
5164
+ import { createRequire } from "module";
5115
5165
  import path from "path";
5166
+ import { fileURLToPath, pathToFileURL } from "url";
5116
5167
  function hasAttribute(decl, name) {
5117
5168
  return !!getAttribute(decl, name);
5118
5169
  }
@@ -5192,10 +5243,6 @@ function isRelationshipField(field) {
5192
5243
  return isDataModel(field.type.reference?.ref);
5193
5244
  }
5194
5245
  __name(isRelationshipField, "isRelationshipField");
5195
- function isFutureExpr(node) {
5196
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
5197
- }
5198
- __name(isFutureExpr, "isFutureExpr");
5199
5246
  function isDelegateModel(node) {
5200
5247
  return isDataModel(node) && hasAttribute(node, "@@delegate");
5201
5248
  }
@@ -5213,8 +5260,14 @@ function getRecursiveBases(decl, includeDelegate = true, seen = /* @__PURE__ */
5213
5260
  return result;
5214
5261
  }
5215
5262
  seen.add(decl);
5216
- decl.mixins.forEach((mixin) => {
5217
- const baseDecl = mixin.ref;
5263
+ const bases = [
5264
+ ...decl.mixins,
5265
+ ...isDataModel(decl) && decl.baseModel ? [
5266
+ decl.baseModel
5267
+ ] : []
5268
+ ];
5269
+ bases.forEach((base) => {
5270
+ const baseDecl = decl.$container.declarations.find((d) => (isTypeDef(d) || isDataModel(d)) && d.name === base.$refText);
5218
5271
  if (baseDecl) {
5219
5272
  if (!includeDelegate && isDelegateModel(baseDecl)) {
5220
5273
  return;
@@ -5337,6 +5390,10 @@ function getArray(expr) {
5337
5390
  return isArrayExpr(expr) || isConfigArrayExpr(expr) ? expr.items : void 0;
5338
5391
  }
5339
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");
5340
5397
  function getFunctionExpressionContext(funcDecl) {
5341
5398
  const funcAllowedContext = [];
5342
5399
  const funcAttr = funcDecl.attributes.find((attr) => attr.decl.$refText === "@@@expressionContext");
@@ -5354,7 +5411,7 @@ function getFunctionExpressionContext(funcDecl) {
5354
5411
  }
5355
5412
  __name(getFunctionExpressionContext, "getFunctionExpressionContext");
5356
5413
  function isCheckInvocation(node) {
5357
- return isInvocationExpr(node) && node.function.ref?.name === "check" && isFromStdlib(node.function.ref);
5414
+ return isInvocationExpr(node) && node.function.ref?.name === "check";
5358
5415
  }
5359
5416
  __name(isCheckInvocation, "isCheckInvocation");
5360
5417
  function resolveTransitiveImports(documents, model) {
@@ -5424,17 +5481,17 @@ function getAllDeclarationsIncludingImports(documents, model) {
5424
5481
  }
5425
5482
  __name(getAllDeclarationsIncludingImports, "getAllDeclarationsIncludingImports");
5426
5483
  function getAuthDecl(decls) {
5427
- let authModel = decls.find((m) => hasAttribute(m, "@@auth"));
5484
+ let authModel = decls.find((d) => hasAttribute(d, "@@auth"));
5428
5485
  if (!authModel) {
5429
- authModel = decls.find((m) => m.name === "User");
5486
+ authModel = decls.find((d) => d.name === "User");
5430
5487
  }
5431
5488
  return authModel;
5432
5489
  }
5433
5490
  __name(getAuthDecl, "getAuthDecl");
5434
- function isFutureInvocation(node) {
5435
- return isInvocationExpr(node) && node.function.ref?.name === "future" && isFromStdlib(node.function.ref);
5491
+ function isBeforeInvocation(node) {
5492
+ return isInvocationExpr(node) && node.function.ref?.name === "before";
5436
5493
  }
5437
- __name(isFutureInvocation, "isFutureInvocation");
5494
+ __name(isBeforeInvocation, "isBeforeInvocation");
5438
5495
  function isCollectionPredicate(node) {
5439
5496
  return isBinaryExpr(node) && [
5440
5497
  "?",
@@ -5489,12 +5546,14 @@ function getAllFields(decl, includeIgnored = false, seen = /* @__PURE__ */ new S
5489
5546
  seen.add(decl);
5490
5547
  const fields = [];
5491
5548
  for (const mixin of decl.mixins) {
5492
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
5493
- fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
5549
+ if (mixin.ref) {
5550
+ fields.push(...getAllFields(mixin.ref, includeIgnored, seen));
5551
+ }
5494
5552
  }
5495
5553
  if (isDataModel(decl) && decl.baseModel) {
5496
- invariant(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
5497
- fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
5554
+ if (decl.baseModel.ref) {
5555
+ fields.push(...getAllFields(decl.baseModel.ref, includeIgnored, seen));
5556
+ }
5498
5557
  }
5499
5558
  fields.push(...decl.fields.filter((f) => includeIgnored || !hasAttribute(f, "@ignore")));
5500
5559
  return fields;
@@ -5507,17 +5566,29 @@ function getAllAttributes(decl, seen = /* @__PURE__ */ new Set()) {
5507
5566
  seen.add(decl);
5508
5567
  const attributes = [];
5509
5568
  for (const mixin of decl.mixins) {
5510
- invariant(mixin.ref, `Mixin ${mixin.$refText} is not resolved`);
5511
- attributes.push(...getAllAttributes(mixin.ref, seen));
5569
+ if (mixin.ref) {
5570
+ attributes.push(...getAllAttributes(mixin.ref, seen));
5571
+ }
5512
5572
  }
5513
5573
  if (isDataModel(decl) && decl.baseModel) {
5514
- invariant(decl.baseModel.ref, `Base model ${decl.baseModel.$refText} is not resolved`);
5515
- attributes.push(...getAllAttributes(decl.baseModel.ref, seen));
5574
+ if (decl.baseModel.ref) {
5575
+ const attrs = getAllAttributes(decl.baseModel.ref, seen).filter((attr) => !isNonInheritableAttribute(attr));
5576
+ attributes.push(...attrs);
5577
+ }
5516
5578
  }
5517
5579
  attributes.push(...decl.attributes);
5518
5580
  return attributes;
5519
5581
  }
5520
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");
5521
5592
  function getDocument(node) {
5522
5593
  const rootNode = findRootNode(node);
5523
5594
  const result = rootNode.$document;
@@ -5527,6 +5598,71 @@ function getDocument(node) {
5527
5598
  return result;
5528
5599
  }
5529
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");
5530
5666
  function findRootNode(node) {
5531
5667
  while (node.$container) {
5532
5668
  node = node.$container;
@@ -5536,6 +5672,9 @@ function findRootNode(node) {
5536
5672
  __name(findRootNode, "findRootNode");
5537
5673
 
5538
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";
5539
5678
  function _ts_decorate(decorators, target, key, desc) {
5540
5679
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5541
5680
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -5649,6 +5788,7 @@ var AttributeApplicationValidator = class {
5649
5788
  });
5650
5789
  }
5651
5790
  }
5791
+ // TODO: design a way to let plugin register validation
5652
5792
  _checkModelLevelPolicy(attr, accept) {
5653
5793
  const kind = getStringLiteral(attr.args[0]?.value);
5654
5794
  if (!kind) {
@@ -5661,11 +5801,61 @@ var AttributeApplicationValidator = class {
5661
5801
  "create",
5662
5802
  "read",
5663
5803
  "update",
5804
+ "post-update",
5664
5805
  "delete",
5665
5806
  "all"
5666
5807
  ], attr, accept);
5667
- this.rejectEncryptedFields(attr, accept);
5808
+ if ((kind === "create" || kind === "all") && attr.args[1]?.value) {
5809
+ this.rejectNonOwnedRelationInExpression(attr.args[1].value, accept);
5810
+ }
5811
+ if (kind !== "post-update" && attr.args[1]?.value) {
5812
+ const beforeCall = AstUtils2.streamAst(attr.args[1]?.value).find(isBeforeInvocation);
5813
+ if (beforeCall) {
5814
+ accept("error", `"before()" is only allowed in "post-update" policy rules`, {
5815
+ node: beforeCall
5816
+ });
5817
+ }
5818
+ }
5819
+ }
5820
+ rejectNonOwnedRelationInExpression(expr, accept) {
5821
+ const contextModel = AstUtils2.getContainerOfType(expr, isDataModel);
5822
+ if (!contextModel) {
5823
+ return;
5824
+ }
5825
+ if (AstUtils2.streamAst(expr).some((node) => {
5826
+ if (!isDataFieldReference(node)) {
5827
+ return false;
5828
+ }
5829
+ if (node.target.ref?.$container !== contextModel) {
5830
+ return false;
5831
+ }
5832
+ const field = node.target.ref;
5833
+ if (!isRelationshipField(field)) {
5834
+ return false;
5835
+ }
5836
+ if (isAuthOrAuthMemberAccess(node)) {
5837
+ return false;
5838
+ }
5839
+ const startNode = isCollectionPredicate(node.$container) && node.$container.left === node ? node.$container : node;
5840
+ const collectionPredicate = AstUtils2.getContainerOfType(startNode.$container, isCollectionPredicate);
5841
+ if (collectionPredicate && isAuthOrAuthMemberAccess(collectionPredicate.left)) {
5842
+ return false;
5843
+ }
5844
+ const relationAttr = field.attributes.find((attr) => attr.decl.ref?.name === "@relation");
5845
+ if (!relationAttr) {
5846
+ return true;
5847
+ }
5848
+ if (!relationAttr.args.some((arg) => arg.name === "fields")) {
5849
+ return true;
5850
+ }
5851
+ return false;
5852
+ })) {
5853
+ accept("error", `non-owned relation fields are not allowed in "create" rules`, {
5854
+ node: expr
5855
+ });
5856
+ }
5668
5857
  }
5858
+ // TODO: design a way to let plugin register validation
5669
5859
  _checkFieldLevelPolicy(attr, accept) {
5670
5860
  const kind = getStringLiteral(attr.args[0]?.value);
5671
5861
  if (!kind) {
@@ -5680,8 +5870,8 @@ var AttributeApplicationValidator = class {
5680
5870
  "all"
5681
5871
  ], attr, accept);
5682
5872
  const expr = attr.args[1]?.value;
5683
- if (expr && AstUtils2.streamAst(expr).some((node) => isFutureExpr(node))) {
5684
- accept("error", `"future()" is not allowed in field-level policy rules`, {
5873
+ if (expr && AstUtils2.streamAst(expr).some((node) => isBeforeInvocation(node))) {
5874
+ accept("error", `"before()" is not allowed in field-level policy rules`, {
5685
5875
  node: expr
5686
5876
  });
5687
5877
  }
@@ -5693,7 +5883,6 @@ var AttributeApplicationValidator = class {
5693
5883
  });
5694
5884
  }
5695
5885
  }
5696
- this.rejectEncryptedFields(attr, accept);
5697
5886
  }
5698
5887
  _checkValidate(attr, accept) {
5699
5888
  const condition = attr.args[0]?.value;
@@ -5703,8 +5892,9 @@ var AttributeApplicationValidator = class {
5703
5892
  });
5704
5893
  }
5705
5894
  }
5706
- _checkUnique(attr, accept) {
5707
- const fields = attr.args[0]?.value;
5895
+ _checkConstraint(attr, accept) {
5896
+ const fields = getAttributeArg(attr, "fields");
5897
+ const attrName = attr.decl.ref?.name;
5708
5898
  if (!fields) {
5709
5899
  accept("error", `expects an array of field references`, {
5710
5900
  node: attr.args[0]
@@ -5713,7 +5903,7 @@ var AttributeApplicationValidator = class {
5713
5903
  }
5714
5904
  if (isArrayExpr(fields)) {
5715
5905
  if (fields.items.length === 0) {
5716
- accept("error", `\`@@unique\` expects at least one field reference`, {
5906
+ accept("error", `\`${attrName}\` expects at least one field reference`, {
5717
5907
  node: fields
5718
5908
  });
5719
5909
  return;
@@ -5732,7 +5922,7 @@ var AttributeApplicationValidator = class {
5732
5922
  return;
5733
5923
  }
5734
5924
  if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
5735
- accept("error", `Cannot use fields inherited from a polymorphic base model in \`@@unique\``, {
5925
+ accept("error", `Cannot use fields inherited from a polymorphic base model in \`${attrName}\``, {
5736
5926
  node: item
5737
5927
  });
5738
5928
  }
@@ -5743,14 +5933,23 @@ var AttributeApplicationValidator = class {
5743
5933
  });
5744
5934
  }
5745
5935
  }
5746
- rejectEncryptedFields(attr, accept) {
5747
- AstUtils2.streamAllContents(attr).forEach((node) => {
5748
- if (isDataFieldReference(node) && hasAttribute(node.target.ref, "@encrypted")) {
5749
- accept("error", `Encrypted fields cannot be used in policy rules`, {
5750
- node
5936
+ _checkSchema(attr, accept) {
5937
+ const schemaName = getStringLiteral(attr.args[0]?.value);
5938
+ invariant(schemaName, `@@schema expects a string literal`);
5939
+ const zmodel = AstUtils2.getContainerOfType(attr, isModel);
5940
+ const datasource = zmodel.declarations.find(isDataSource);
5941
+ if (datasource) {
5942
+ let found = false;
5943
+ const schemas = datasource.fields.find((f) => f.name === "schemas");
5944
+ if (schemas && isConfigArrayExpr(schemas.value)) {
5945
+ found = schemas.value.items.some((item) => isLiteralExpr(item) && item.value === schemaName);
5946
+ }
5947
+ if (!found) {
5948
+ accept("error", `Schema "${schemaName}" is not defined in the datasource`, {
5949
+ node: attr
5751
5950
  });
5752
5951
  }
5753
- });
5952
+ }
5754
5953
  }
5755
5954
  validatePolicyKinds(kind, candidates, attr, accept) {
5756
5955
  const items = kind.split(",").map((x) => x.trim());
@@ -5794,15 +5993,25 @@ _ts_decorate([
5794
5993
  _ts_metadata("design:returntype", void 0)
5795
5994
  ], AttributeApplicationValidator.prototype, "_checkValidate", null);
5796
5995
  _ts_decorate([
5797
- check("@@unique"),
5798
5996
  check("@@id"),
5997
+ check("@@index"),
5998
+ check("@@unique"),
5799
5999
  _ts_metadata("design:type", Function),
5800
6000
  _ts_metadata("design:paramtypes", [
5801
6001
  typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
5802
6002
  typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
5803
6003
  ]),
5804
6004
  _ts_metadata("design:returntype", void 0)
5805
- ], AttributeApplicationValidator.prototype, "_checkUnique", null);
6005
+ ], AttributeApplicationValidator.prototype, "_checkConstraint", null);
6006
+ _ts_decorate([
6007
+ check("@@schema"),
6008
+ _ts_metadata("design:type", Function),
6009
+ _ts_metadata("design:paramtypes", [
6010
+ typeof AttributeApplication === "undefined" ? Object : AttributeApplication,
6011
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
6012
+ ]),
6013
+ _ts_metadata("design:returntype", void 0)
6014
+ ], AttributeApplicationValidator.prototype, "_checkSchema", null);
5806
6015
  function assignableToAttributeParam(arg, param, attr) {
5807
6016
  const argResolvedType = arg.$resolvedType;
5808
6017
  if (!argResolvedType) {
@@ -5812,6 +6021,10 @@ function assignableToAttributeParam(arg, param, attr) {
5812
6021
  let dstIsArray = param.type.array;
5813
6022
  if (dstType === "ContextType") {
5814
6023
  if (isDataField(attr.$container)) {
6024
+ const dstIsTypedJson = hasAttribute(attr.$container, "@json");
6025
+ if (dstIsTypedJson && attr.decl.ref?.name === "@default") {
6026
+ return argResolvedType.decl === "String";
6027
+ }
5815
6028
  dstIsArray = attr.$container.type.array;
5816
6029
  }
5817
6030
  }
@@ -5903,6 +6116,9 @@ function isValidAttributeTarget(attrDecl, targetDecl) {
5903
6116
  case "TypeDefField":
5904
6117
  allowed = allowed || isTypeDef(targetDecl.type.reference?.ref);
5905
6118
  break;
6119
+ case "ListField":
6120
+ allowed = allowed || !isDataModel(targetDecl.type.reference?.ref) && targetDecl.type.array;
6121
+ break;
5906
6122
  default:
5907
6123
  break;
5908
6124
  }
@@ -6170,7 +6386,7 @@ var DataModelValidator = class {
6170
6386
  if (!thisRelation.valid) {
6171
6387
  return;
6172
6388
  }
6173
- if (this.isFieldInheritedFromDelegateModel(field)) {
6389
+ if (this.isFieldInheritedFromDelegateModel(field, contextModel)) {
6174
6390
  return;
6175
6391
  }
6176
6392
  if (this.isSelfRelation(field)) {
@@ -6293,8 +6509,8 @@ var DataModelValidator = class {
6293
6509
  }
6294
6510
  }
6295
6511
  // checks if the given field is inherited directly or indirectly from a delegate model
6296
- isFieldInheritedFromDelegateModel(field) {
6297
- return isDelegateModel(field.$container);
6512
+ isFieldInheritedFromDelegateModel(field, contextModel) {
6513
+ return field.$container !== contextModel && isDelegateModel(field.$container);
6298
6514
  }
6299
6515
  validateInherits(model, accept) {
6300
6516
  if (!model.baseModel) {
@@ -6353,7 +6569,6 @@ var DataSourceValidator = class {
6353
6569
  validateDuplicatedDeclarations(ds, ds.fields, accept);
6354
6570
  this.validateProvider(ds, accept);
6355
6571
  this.validateUrl(ds, accept);
6356
- this.validateRelationMode(ds, accept);
6357
6572
  }
6358
6573
  validateProvider(ds, accept) {
6359
6574
  const provider = ds.fields.find((f) => f.name === "provider");
@@ -6363,16 +6578,61 @@ var DataSourceValidator = class {
6363
6578
  });
6364
6579
  return;
6365
6580
  }
6366
- const value = getStringLiteral(provider.value);
6367
- if (!value) {
6581
+ const providerValue = getStringLiteral(provider.value);
6582
+ if (!providerValue) {
6368
6583
  accept("error", '"provider" must be set to a string literal', {
6369
6584
  node: provider.value
6370
6585
  });
6371
- } else if (!SUPPORTED_PROVIDERS.includes(value)) {
6372
- accept("error", `Provider "${value}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
6586
+ } else if (!SUPPORTED_PROVIDERS.includes(providerValue)) {
6587
+ accept("error", `Provider "${providerValue}" is not supported. Choose from ${SUPPORTED_PROVIDERS.map((p) => '"' + p + '"').join(" | ")}.`, {
6373
6588
  node: provider.value
6374
6589
  });
6375
6590
  }
6591
+ const defaultSchemaField = ds.fields.find((f) => f.name === "defaultSchema");
6592
+ let defaultSchemaValue;
6593
+ if (defaultSchemaField) {
6594
+ if (providerValue !== "postgresql") {
6595
+ accept("error", '"defaultSchema" is only supported for "postgresql" provider', {
6596
+ node: defaultSchemaField
6597
+ });
6598
+ }
6599
+ defaultSchemaValue = getStringLiteral(defaultSchemaField.value);
6600
+ if (!defaultSchemaValue) {
6601
+ accept("error", '"defaultSchema" must be a string literal', {
6602
+ node: defaultSchemaField.value
6603
+ });
6604
+ }
6605
+ }
6606
+ const schemasField = ds.fields.find((f) => f.name === "schemas");
6607
+ if (schemasField) {
6608
+ if (providerValue !== "postgresql") {
6609
+ accept("error", '"schemas" is only supported for "postgresql" provider', {
6610
+ node: schemasField
6611
+ });
6612
+ }
6613
+ const schemasValue = schemasField.value;
6614
+ if (!isConfigArrayExpr(schemasValue) || !schemasValue.items.every((e) => isLiteralExpr(e) && typeof getStringLiteral(e) === "string")) {
6615
+ accept("error", '"schemas" must be an array of string literals', {
6616
+ node: schemasField
6617
+ });
6618
+ } else {
6619
+ const schemasArray = schemasValue.items.map((e) => getStringLiteral(e));
6620
+ if (defaultSchemaValue) {
6621
+ if (!schemasArray.includes(defaultSchemaValue)) {
6622
+ accept("error", `"${defaultSchemaValue}" must be included in the "schemas" array`, {
6623
+ node: schemasField
6624
+ });
6625
+ }
6626
+ } else {
6627
+ const hasImplicitPublicSchema = ds.$container.declarations.some((d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === "@@schema"));
6628
+ if (hasImplicitPublicSchema && !schemasArray.includes("public")) {
6629
+ accept("error", `"public" must be included in the "schemas" array`, {
6630
+ node: schemasField
6631
+ });
6632
+ }
6633
+ }
6634
+ }
6635
+ }
6376
6636
  }
6377
6637
  validateUrl(ds, accept) {
6378
6638
  const urlField = ds.fields.find((f) => f.name === "url");
@@ -6386,20 +6646,6 @@ var DataSourceValidator = class {
6386
6646
  });
6387
6647
  }
6388
6648
  }
6389
- validateRelationMode(ds, accept) {
6390
- const field = ds.fields.find((f) => f.name === "relationMode");
6391
- if (field) {
6392
- const val = getStringLiteral(field.value);
6393
- if (!val || ![
6394
- "foreignKeys",
6395
- "prisma"
6396
- ].includes(val)) {
6397
- accept("error", '"relationMode" must be set to "foreignKeys" or "prisma"', {
6398
- node: field.value
6399
- });
6400
- }
6401
- }
6402
- }
6403
6649
  };
6404
6650
 
6405
6651
  // src/validators/enum-validator.ts
@@ -6452,11 +6698,21 @@ var ExpressionValidator = class {
6452
6698
  }
6453
6699
  }
6454
6700
  switch (expr.$type) {
6701
+ case "MemberAccessExpr":
6702
+ this.validateMemberAccessExpr(expr, accept);
6703
+ break;
6455
6704
  case "BinaryExpr":
6456
6705
  this.validateBinaryExpr(expr, accept);
6457
6706
  break;
6458
6707
  }
6459
6708
  }
6709
+ validateMemberAccessExpr(expr, accept) {
6710
+ if (isBeforeInvocation(expr.operand) && isDataModel(expr.$resolvedType?.decl)) {
6711
+ accept("error", "relation fields cannot be accessed from `before()`", {
6712
+ node: expr
6713
+ });
6714
+ }
6715
+ }
6460
6716
  validateBinaryExpr(expr, accept) {
6461
6717
  switch (expr.operator) {
6462
6718
  case "in": {
@@ -6509,23 +6765,25 @@ var ExpressionValidator = class {
6509
6765
  "Any"
6510
6766
  ];
6511
6767
  }
6512
- if (typeof expr.left.$resolvedType?.decl !== "string" || !supportedShapes.includes(expr.left.$resolvedType.decl)) {
6768
+ const leftResolvedDecl = expr.left.$resolvedType?.decl;
6769
+ const rightResolvedDecl = expr.right.$resolvedType?.decl;
6770
+ if (leftResolvedDecl && (typeof leftResolvedDecl !== "string" || !supportedShapes.includes(leftResolvedDecl))) {
6513
6771
  accept("error", `invalid operand type for "${expr.operator}" operator`, {
6514
6772
  node: expr.left
6515
6773
  });
6516
6774
  return;
6517
6775
  }
6518
- if (typeof expr.right.$resolvedType?.decl !== "string" || !supportedShapes.includes(expr.right.$resolvedType.decl)) {
6776
+ if (rightResolvedDecl && (typeof rightResolvedDecl !== "string" || !supportedShapes.includes(rightResolvedDecl))) {
6519
6777
  accept("error", `invalid operand type for "${expr.operator}" operator`, {
6520
6778
  node: expr.right
6521
6779
  });
6522
6780
  return;
6523
6781
  }
6524
- if (expr.left.$resolvedType.decl === "DateTime" && expr.right.$resolvedType.decl !== "DateTime") {
6782
+ if (leftResolvedDecl === "DateTime" && rightResolvedDecl && rightResolvedDecl !== "DateTime") {
6525
6783
  accept("error", "incompatible operand types", {
6526
6784
  node: expr
6527
6785
  });
6528
- } else if (expr.right.$resolvedType.decl === "DateTime" && expr.left.$resolvedType.decl !== "DateTime") {
6786
+ } else if (rightResolvedDecl === "DateTime" && leftResolvedDecl && leftResolvedDecl !== "DateTime") {
6529
6787
  accept("error", "incompatible operand types", {
6530
6788
  node: expr
6531
6789
  });
@@ -6565,11 +6823,11 @@ var ExpressionValidator = class {
6565
6823
  });
6566
6824
  }
6567
6825
  if (isDataFieldReference(expr.left) && (isThisExpr(expr.right) || isDataFieldReference(expr.right))) {
6568
- accept("error", "comparison between model-typed fields are not supported", {
6826
+ accept("error", "comparison between models is not supported", {
6569
6827
  node: expr
6570
6828
  });
6571
6829
  } else if (isDataFieldReference(expr.right) && (isThisExpr(expr.left) || isDataFieldReference(expr.left))) {
6572
- accept("error", "comparison between model-typed fields are not supported", {
6830
+ accept("error", "comparison between models is not supported", {
6573
6831
  node: expr
6574
6832
  });
6575
6833
  }
@@ -6659,41 +6917,39 @@ var FunctionInvocationValidator = class {
6659
6917
  if (!this.validateArgs(funcDecl, expr.args, accept)) {
6660
6918
  return;
6661
6919
  }
6662
- if (isFromStdlib(funcDecl)) {
6663
- let curr = expr.$container;
6664
- let containerAttribute;
6665
- while (curr) {
6666
- if (isDataModelAttribute(curr) || isDataFieldAttribute(curr)) {
6667
- containerAttribute = curr;
6668
- break;
6669
- }
6670
- curr = curr.$container;
6920
+ let curr = expr.$container;
6921
+ let containerAttribute;
6922
+ while (curr) {
6923
+ if (isDataModelAttribute(curr) || isDataFieldAttribute(curr)) {
6924
+ containerAttribute = curr;
6925
+ break;
6671
6926
  }
6672
- const exprContext = match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@validate", () => ExpressionContext.ValidationRule).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
6673
- const funcAllowedContext = getFunctionExpressionContext(funcDecl);
6674
- if (exprContext && !funcAllowedContext.includes(exprContext)) {
6675
- accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
6676
- node: expr
6927
+ curr = curr.$container;
6928
+ }
6929
+ const exprContext = this.getExpressionContext(containerAttribute);
6930
+ const funcAllowedContext = getFunctionExpressionContext(funcDecl);
6931
+ if (exprContext && !funcAllowedContext.includes(exprContext)) {
6932
+ accept("error", `function "${funcDecl.name}" is not allowed in the current context: ${exprContext}`, {
6933
+ node: expr
6934
+ });
6935
+ return;
6936
+ }
6937
+ const allCasing = [
6938
+ "original",
6939
+ "upper",
6940
+ "lower",
6941
+ "capitalize",
6942
+ "uncapitalize"
6943
+ ];
6944
+ if ([
6945
+ "currentModel",
6946
+ "currentOperation"
6947
+ ].includes(funcDecl.name)) {
6948
+ const arg = getLiteral(expr.args[0]?.value);
6949
+ if (arg && !allCasing.includes(arg)) {
6950
+ accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
6951
+ node: expr.args[0]
6677
6952
  });
6678
- return;
6679
- }
6680
- const allCasing = [
6681
- "original",
6682
- "upper",
6683
- "lower",
6684
- "capitalize",
6685
- "uncapitalize"
6686
- ];
6687
- if ([
6688
- "currentModel",
6689
- "currentOperation"
6690
- ].includes(funcDecl.name)) {
6691
- const arg = getLiteral(expr.args[0]?.value);
6692
- if (arg && !allCasing.includes(arg)) {
6693
- accept("error", `argument must be one of: ${allCasing.map((c) => '"' + c + '"').join(", ")}`, {
6694
- node: expr.args[0]
6695
- });
6696
- }
6697
6953
  }
6698
6954
  }
6699
6955
  const checker = invocationCheckers.get(expr.function.$refText);
@@ -6701,6 +6957,18 @@ var FunctionInvocationValidator = class {
6701
6957
  checker.value.call(this, expr, accept);
6702
6958
  }
6703
6959
  }
6960
+ getExpressionContext(containerAttribute) {
6961
+ if (!containerAttribute) {
6962
+ return void 0;
6963
+ }
6964
+ if (this.isValidationAttribute(containerAttribute)) {
6965
+ return ExpressionContext.ValidationRule;
6966
+ }
6967
+ return match(containerAttribute?.decl.$refText).with("@default", () => ExpressionContext.DefaultValue).with(P.union("@@allow", "@@deny", "@allow", "@deny"), () => ExpressionContext.AccessPolicy).with("@@index", () => ExpressionContext.Index).otherwise(() => void 0);
6968
+ }
6969
+ isValidationAttribute(attr) {
6970
+ return !!attr.decl.ref?.attributes.some((attr2) => attr2.decl.$refText === "@@@validation");
6971
+ }
6704
6972
  validateArgs(funcDecl, args, accept) {
6705
6973
  let success = true;
6706
6974
  for (let i = 0; i < funcDecl.params.length; i++) {
@@ -6761,6 +7029,51 @@ var FunctionInvocationValidator = class {
6761
7029
  }
6762
7030
  return true;
6763
7031
  }
7032
+ _checkAuth(expr, accept) {
7033
+ if (!expr.$resolvedType) {
7034
+ accept("error", 'cannot resolve `auth()` - make sure you have a model or type with `@auth` attribute or named "User"', {
7035
+ node: expr
7036
+ });
7037
+ }
7038
+ }
7039
+ _checkLength(expr, accept) {
7040
+ const msg = "argument must be a string or list field";
7041
+ const fieldArg = expr.args[0].value;
7042
+ if (!isDataFieldReference(fieldArg)) {
7043
+ accept("error", msg, {
7044
+ node: expr.args[0]
7045
+ });
7046
+ return;
7047
+ }
7048
+ if (isDataModel(fieldArg.$resolvedType?.decl)) {
7049
+ accept("error", msg, {
7050
+ node: expr.args[0]
7051
+ });
7052
+ return;
7053
+ }
7054
+ if (!fieldArg.$resolvedType?.array && fieldArg.$resolvedType?.decl !== "String") {
7055
+ accept("error", msg, {
7056
+ node: expr.args[0]
7057
+ });
7058
+ }
7059
+ }
7060
+ _checkRegex(expr, accept) {
7061
+ const regex = expr.args[1]?.value;
7062
+ if (!isStringLiteral(regex)) {
7063
+ accept("error", "second argument must be a string literal", {
7064
+ node: expr.args[1]
7065
+ });
7066
+ return;
7067
+ }
7068
+ try {
7069
+ new RegExp(regex.value);
7070
+ } catch (e) {
7071
+ accept("error", "invalid regular expression: " + e.message, {
7072
+ node: expr.args[1]
7073
+ });
7074
+ }
7075
+ }
7076
+ // TODO: move this to policy plugin
6764
7077
  _checkCheck(expr, accept) {
6765
7078
  let valid = true;
6766
7079
  const fieldArg = expr.args[0].value;
@@ -6832,6 +7145,33 @@ var FunctionInvocationValidator = class {
6832
7145
  }
6833
7146
  }
6834
7147
  };
7148
+ _ts_decorate2([
7149
+ func("auth"),
7150
+ _ts_metadata2("design:type", Function),
7151
+ _ts_metadata2("design:paramtypes", [
7152
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
7153
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
7154
+ ]),
7155
+ _ts_metadata2("design:returntype", void 0)
7156
+ ], FunctionInvocationValidator.prototype, "_checkAuth", null);
7157
+ _ts_decorate2([
7158
+ func("length"),
7159
+ _ts_metadata2("design:type", Function),
7160
+ _ts_metadata2("design:paramtypes", [
7161
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
7162
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
7163
+ ]),
7164
+ _ts_metadata2("design:returntype", void 0)
7165
+ ], FunctionInvocationValidator.prototype, "_checkLength", null);
7166
+ _ts_decorate2([
7167
+ func("regex"),
7168
+ _ts_metadata2("design:type", Function),
7169
+ _ts_metadata2("design:paramtypes", [
7170
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr,
7171
+ typeof ValidationAcceptor === "undefined" ? Object : ValidationAcceptor
7172
+ ]),
7173
+ _ts_metadata2("design:returntype", void 0)
7174
+ ], FunctionInvocationValidator.prototype, "_checkRegex", null);
6835
7175
  _ts_decorate2([
6836
7176
  func("check"),
6837
7177
  _ts_metadata2("design:type", Function),
@@ -6922,6 +7262,7 @@ function registerValidationChecks(services) {
6922
7262
  const checks = {
6923
7263
  Model: validator.checkModel,
6924
7264
  DataSource: validator.checkDataSource,
7265
+ GeneratorDecl: validator.checkGenerator,
6925
7266
  DataModel: validator.checkDataModel,
6926
7267
  TypeDef: validator.checkTypeDef,
6927
7268
  Enum: validator.checkEnum,
@@ -6941,50 +7282,1044 @@ var ZModelValidator = class {
6941
7282
  constructor(services) {
6942
7283
  this.services = services;
6943
7284
  }
6944
- shouldCheck(node) {
6945
- let doc;
6946
- let currNode = node;
6947
- while (currNode) {
6948
- if (currNode.$document) {
6949
- doc = currNode.$document;
6950
- break;
6951
- }
6952
- currNode = currNode.$container;
6953
- }
6954
- return doc?.parseResult.lexerErrors.length === 0 && doc?.parseResult.parserErrors.length === 0;
6955
- }
6956
7285
  checkModel(node, accept) {
6957
- this.shouldCheck(node) && new SchemaValidator(this.services.shared.workspace.LangiumDocuments).validate(node, accept);
7286
+ new SchemaValidator(this.services.shared.workspace.LangiumDocuments).validate(node, accept);
6958
7287
  }
6959
7288
  checkDataSource(node, accept) {
6960
- this.shouldCheck(node) && new DataSourceValidator().validate(node, accept);
7289
+ new DataSourceValidator().validate(node, accept);
7290
+ }
7291
+ checkGenerator(node, accept) {
7292
+ accept("warning", '"generator" is not used by ZenStack and should be removed.', {
7293
+ node
7294
+ });
6961
7295
  }
6962
7296
  checkDataModel(node, accept) {
6963
- this.shouldCheck(node) && new DataModelValidator().validate(node, accept);
7297
+ new DataModelValidator().validate(node, accept);
6964
7298
  }
6965
7299
  checkTypeDef(node, accept) {
6966
- this.shouldCheck(node) && new TypeDefValidator().validate(node, accept);
7300
+ new TypeDefValidator().validate(node, accept);
6967
7301
  }
6968
7302
  checkEnum(node, accept) {
6969
- this.shouldCheck(node) && new EnumValidator().validate(node, accept);
7303
+ new EnumValidator().validate(node, accept);
6970
7304
  }
6971
7305
  checkAttribute(node, accept) {
6972
- this.shouldCheck(node) && new AttributeValidator().validate(node, accept);
7306
+ new AttributeValidator().validate(node, accept);
6973
7307
  }
6974
7308
  checkExpression(node, accept) {
6975
- this.shouldCheck(node) && new ExpressionValidator().validate(node, accept);
7309
+ new ExpressionValidator().validate(node, accept);
6976
7310
  }
6977
7311
  checkFunctionInvocation(node, accept) {
6978
- this.shouldCheck(node) && new FunctionInvocationValidator().validate(node, accept);
7312
+ new FunctionInvocationValidator().validate(node, accept);
6979
7313
  }
6980
7314
  checkFunctionDecl(node, accept) {
6981
- this.shouldCheck(node) && new FunctionDeclValidator().validate(node, accept);
7315
+ new FunctionDeclValidator().validate(node, accept);
6982
7316
  }
6983
7317
  };
6984
7318
 
6985
- // src/zmodel-linker.ts
6986
- import { AstUtils as AstUtils6, Cancellation, DefaultLinker, DocumentState, interruptAndCheck, isReference } from "langium";
7319
+ // src/zmodel-comment-provider.ts
7320
+ import { DefaultCommentProvider } from "langium";
6987
7321
  import { match as match2 } from "ts-pattern";
7322
+ var ZModelCommentProvider = class extends DefaultCommentProvider {
7323
+ static {
7324
+ __name(this, "ZModelCommentProvider");
7325
+ }
7326
+ getComment(node) {
7327
+ let comment = super.getComment(node);
7328
+ if (!comment) {
7329
+ comment = match2(node).when(isDataModel, (d) => `/**
7330
+ * Model *${d.name}*
7331
+ */`).when(isTypeDef, (d) => `/**
7332
+ * Type *${d.name}*
7333
+ */`).when(isEnum, (e) => `/**
7334
+ * Enum *${e.name}*
7335
+ */`).when(isEnumField, (f) => `/**
7336
+ * Value of enum *${f.$container?.name}*
7337
+ */`).when(isDataField, (f) => `/**
7338
+ * Field of *${f.$container?.name}*
7339
+ */`).when(isFunctionDecl, (f) => `/**
7340
+ * Function *${f.name}*
7341
+ */`).otherwise(() => "");
7342
+ }
7343
+ return comment;
7344
+ }
7345
+ };
7346
+
7347
+ // src/zmodel-completion-provider.ts
7348
+ import { DefaultCompletionProvider } from "langium/lsp";
7349
+ import fs2 from "fs";
7350
+ import { P as P2, match as match3 } from "ts-pattern";
7351
+ import { CompletionItemKind } from "vscode-languageserver";
7352
+
7353
+ // src/zmodel-code-generator.ts
7354
+ function _ts_decorate3(decorators, target, key, desc) {
7355
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
7356
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
7357
+ 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;
7358
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7359
+ }
7360
+ __name(_ts_decorate3, "_ts_decorate");
7361
+ function _ts_metadata3(k, v) {
7362
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
7363
+ }
7364
+ __name(_ts_metadata3, "_ts_metadata");
7365
+ var generationHandlers = /* @__PURE__ */ new Map();
7366
+ function gen(name) {
7367
+ return function(_target, _propertyKey, descriptor) {
7368
+ if (!generationHandlers.get(name)) {
7369
+ generationHandlers.set(name, descriptor);
7370
+ }
7371
+ return descriptor;
7372
+ };
7373
+ }
7374
+ __name(gen, "gen");
7375
+ var ZModelCodeGenerator = class {
7376
+ static {
7377
+ __name(this, "ZModelCodeGenerator");
7378
+ }
7379
+ options;
7380
+ constructor(options) {
7381
+ this.options = {
7382
+ binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
7383
+ unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
7384
+ indent: options?.indent ?? 4,
7385
+ quote: options?.quote ?? "single"
7386
+ };
7387
+ }
7388
+ /**
7389
+ * Generates ZModel source code from AST.
7390
+ */
7391
+ generate(ast) {
7392
+ const handler = generationHandlers.get(ast.$type);
7393
+ if (!handler) {
7394
+ throw new Error(`No generation handler found for ${ast.$type}`);
7395
+ }
7396
+ return handler.value.call(this, ast);
7397
+ }
7398
+ _generateModel(ast) {
7399
+ return ast.declarations.map((d) => this.generate(d)).join("\n\n");
7400
+ }
7401
+ _generateDataSource(ast) {
7402
+ return `datasource ${ast.name} {
7403
+ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
7404
+ }`;
7405
+ }
7406
+ _generateEnum(ast) {
7407
+ return `enum ${ast.name} {
7408
+ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
7409
+ }`;
7410
+ }
7411
+ _generateEnumField(ast) {
7412
+ return `${ast.name}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
7413
+ }
7414
+ _generateGenerator(ast) {
7415
+ return `generator ${ast.name} {
7416
+ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
7417
+ }`;
7418
+ }
7419
+ _generateConfigField(ast) {
7420
+ return `${ast.name} = ${this.generate(ast.value)}`;
7421
+ }
7422
+ _generateConfigArrayExpr(ast) {
7423
+ return `[${ast.items.map((x) => this.generate(x)).join(", ")}]`;
7424
+ }
7425
+ _generateConfigInvocationExpr(ast) {
7426
+ if (ast.args.length === 0) {
7427
+ return ast.name;
7428
+ } else {
7429
+ return `${ast.name}(${ast.args.map((x) => (x.name ? x.name + ": " : "") + this.generate(x.value)).join(", ")})`;
7430
+ }
7431
+ }
7432
+ _generatePlugin(ast) {
7433
+ return `plugin ${ast.name} {
7434
+ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
7435
+ }`;
7436
+ }
7437
+ _generatePluginField(ast) {
7438
+ return `${ast.name} = ${this.generate(ast.value)}`;
7439
+ }
7440
+ _generateDataModel(ast) {
7441
+ return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.$refText).join(", ") : ""} {
7442
+ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
7443
+ }`;
7444
+ }
7445
+ _generateDataField(ast) {
7446
+ return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
7447
+ }
7448
+ fieldType(type) {
7449
+ const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
7450
+ return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
7451
+ }
7452
+ _generateDataModelAttribute(ast) {
7453
+ return this.attribute(ast);
7454
+ }
7455
+ _generateDataFieldAttribute(ast) {
7456
+ return this.attribute(ast);
7457
+ }
7458
+ attribute(ast) {
7459
+ const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
7460
+ return `${ast.decl.$refText}${args}`;
7461
+ }
7462
+ _generateAttributeArg(ast) {
7463
+ if (ast.name) {
7464
+ return `${ast.name}: ${this.generate(ast.value)}`;
7465
+ } else {
7466
+ return this.generate(ast.value);
7467
+ }
7468
+ }
7469
+ _generateObjectExpr(ast) {
7470
+ return `{ ${ast.fields.map((field) => this.objectField(field)).join(", ")} }`;
7471
+ }
7472
+ objectField(field) {
7473
+ return `${field.name}: ${this.generate(field.value)}`;
7474
+ }
7475
+ _generateArrayExpr(ast) {
7476
+ return `[${ast.items.map((item) => this.generate(item)).join(", ")}]`;
7477
+ }
7478
+ _generateLiteralExpr(ast) {
7479
+ return this.options.quote === "single" ? `'${ast.value}'` : `"${ast.value}"`;
7480
+ }
7481
+ _generateNumberLiteral(ast) {
7482
+ return ast.value.toString();
7483
+ }
7484
+ _generateBooleanLiteral(ast) {
7485
+ return ast.value.toString();
7486
+ }
7487
+ _generateUnaryExpr(ast) {
7488
+ return `${ast.operator}${this.unaryExprSpace}${this.generate(ast.operand)}`;
7489
+ }
7490
+ _generateBinaryExpr(ast) {
7491
+ const operator = ast.operator;
7492
+ const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
7493
+ const rightExpr = this.generate(ast.right);
7494
+ const { left: isLeftParenthesis, right: isRightParenthesis } = this.isParenthesesNeededForBinaryExpr(ast);
7495
+ return `${isLeftParenthesis ? "(" : ""}${this.generate(ast.left)}${isLeftParenthesis ? ")" : ""}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${operator}${isCollectionPredicate2 ? "" : this.binaryExprSpace}${isRightParenthesis ? "(" : ""}${isCollectionPredicate2 ? `[${rightExpr}]` : rightExpr}${isRightParenthesis ? ")" : ""}`;
7496
+ }
7497
+ _generateReferenceExpr(ast) {
7498
+ const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
7499
+ return `${ast.target.$refText}${args}`;
7500
+ }
7501
+ _generateReferenceArg(ast) {
7502
+ return `${ast.name}:${this.generate(ast.value)}`;
7503
+ }
7504
+ _generateMemberExpr(ast) {
7505
+ return `${this.generate(ast.operand)}.${ast.member.$refText}`;
7506
+ }
7507
+ _generateInvocationExpr(ast) {
7508
+ return `${ast.function.$refText}(${ast.args.map((x) => this.argument(x)).join(", ")})`;
7509
+ }
7510
+ _generateNullExpr() {
7511
+ return "null";
7512
+ }
7513
+ _generateThisExpr() {
7514
+ return "this";
7515
+ }
7516
+ _generateAttribute(ast) {
7517
+ return `attribute ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")})`;
7518
+ }
7519
+ _generateAttributeParam(ast) {
7520
+ return `${ast.default ? "_ " : ""}${ast.name}: ${this.generate(ast.type)}`;
7521
+ }
7522
+ _generateAttributeParamType(ast) {
7523
+ return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}${ast.optional ? "?" : ""}`;
7524
+ }
7525
+ _generateFunctionDecl(ast) {
7526
+ return `function ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")}) ${ast.returnType ? ": " + this.generate(ast.returnType) : ""} {}`;
7527
+ }
7528
+ _generateFunctionParam(ast) {
7529
+ return `${ast.name}: ${this.generate(ast.type)}`;
7530
+ }
7531
+ _generateFunctionParamType(ast) {
7532
+ return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}`;
7533
+ }
7534
+ _generateTypeDef(ast) {
7535
+ return `type ${ast.name} {
7536
+ ${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") : ""}
7537
+ }`;
7538
+ }
7539
+ argument(ast) {
7540
+ return this.generate(ast.value);
7541
+ }
7542
+ get binaryExprSpace() {
7543
+ return " ".repeat(this.options.binaryExprNumberOfSpaces);
7544
+ }
7545
+ get unaryExprSpace() {
7546
+ return " ".repeat(this.options.unaryExprNumberOfSpaces);
7547
+ }
7548
+ get indent() {
7549
+ return " ".repeat(this.options.indent);
7550
+ }
7551
+ isParenthesesNeededForBinaryExpr(ast) {
7552
+ const result = {
7553
+ left: false,
7554
+ right: false
7555
+ };
7556
+ const operator = ast.operator;
7557
+ const isCollectionPredicate2 = this.isCollectionPredicateOperator(operator);
7558
+ const currentPriority = BinaryExprOperatorPriority[operator];
7559
+ if (ast.left.$type === BinaryExpr && BinaryExprOperatorPriority[ast.left["operator"]] < currentPriority) {
7560
+ result.left = true;
7561
+ }
7562
+ if (!isCollectionPredicate2 && ast.right.$type === BinaryExpr && BinaryExprOperatorPriority[ast.right["operator"]] <= currentPriority) {
7563
+ result.right = true;
7564
+ }
7565
+ return result;
7566
+ }
7567
+ isCollectionPredicateOperator(op) {
7568
+ return [
7569
+ "?",
7570
+ "!",
7571
+ "^"
7572
+ ].includes(op);
7573
+ }
7574
+ };
7575
+ _ts_decorate3([
7576
+ gen(Model),
7577
+ _ts_metadata3("design:type", Function),
7578
+ _ts_metadata3("design:paramtypes", [
7579
+ typeof Model === "undefined" ? Object : Model
7580
+ ]),
7581
+ _ts_metadata3("design:returntype", void 0)
7582
+ ], ZModelCodeGenerator.prototype, "_generateModel", null);
7583
+ _ts_decorate3([
7584
+ gen(DataSource),
7585
+ _ts_metadata3("design:type", Function),
7586
+ _ts_metadata3("design:paramtypes", [
7587
+ typeof DataSource === "undefined" ? Object : DataSource
7588
+ ]),
7589
+ _ts_metadata3("design:returntype", void 0)
7590
+ ], ZModelCodeGenerator.prototype, "_generateDataSource", null);
7591
+ _ts_decorate3([
7592
+ gen(Enum),
7593
+ _ts_metadata3("design:type", Function),
7594
+ _ts_metadata3("design:paramtypes", [
7595
+ typeof Enum === "undefined" ? Object : Enum
7596
+ ]),
7597
+ _ts_metadata3("design:returntype", void 0)
7598
+ ], ZModelCodeGenerator.prototype, "_generateEnum", null);
7599
+ _ts_decorate3([
7600
+ gen(EnumField),
7601
+ _ts_metadata3("design:type", Function),
7602
+ _ts_metadata3("design:paramtypes", [
7603
+ typeof EnumField === "undefined" ? Object : EnumField
7604
+ ]),
7605
+ _ts_metadata3("design:returntype", void 0)
7606
+ ], ZModelCodeGenerator.prototype, "_generateEnumField", null);
7607
+ _ts_decorate3([
7608
+ gen(GeneratorDecl),
7609
+ _ts_metadata3("design:type", Function),
7610
+ _ts_metadata3("design:paramtypes", [
7611
+ typeof GeneratorDecl === "undefined" ? Object : GeneratorDecl
7612
+ ]),
7613
+ _ts_metadata3("design:returntype", void 0)
7614
+ ], ZModelCodeGenerator.prototype, "_generateGenerator", null);
7615
+ _ts_decorate3([
7616
+ gen(ConfigField),
7617
+ _ts_metadata3("design:type", Function),
7618
+ _ts_metadata3("design:paramtypes", [
7619
+ typeof ConfigField === "undefined" ? Object : ConfigField
7620
+ ]),
7621
+ _ts_metadata3("design:returntype", void 0)
7622
+ ], ZModelCodeGenerator.prototype, "_generateConfigField", null);
7623
+ _ts_decorate3([
7624
+ gen(ConfigArrayExpr),
7625
+ _ts_metadata3("design:type", Function),
7626
+ _ts_metadata3("design:paramtypes", [
7627
+ typeof ConfigArrayExpr === "undefined" ? Object : ConfigArrayExpr
7628
+ ]),
7629
+ _ts_metadata3("design:returntype", void 0)
7630
+ ], ZModelCodeGenerator.prototype, "_generateConfigArrayExpr", null);
7631
+ _ts_decorate3([
7632
+ gen(ConfigInvocationExpr),
7633
+ _ts_metadata3("design:type", Function),
7634
+ _ts_metadata3("design:paramtypes", [
7635
+ typeof ConfigInvocationExpr === "undefined" ? Object : ConfigInvocationExpr
7636
+ ]),
7637
+ _ts_metadata3("design:returntype", void 0)
7638
+ ], ZModelCodeGenerator.prototype, "_generateConfigInvocationExpr", null);
7639
+ _ts_decorate3([
7640
+ gen(Plugin),
7641
+ _ts_metadata3("design:type", Function),
7642
+ _ts_metadata3("design:paramtypes", [
7643
+ typeof Plugin === "undefined" ? Object : Plugin
7644
+ ]),
7645
+ _ts_metadata3("design:returntype", void 0)
7646
+ ], ZModelCodeGenerator.prototype, "_generatePlugin", null);
7647
+ _ts_decorate3([
7648
+ gen(PluginField),
7649
+ _ts_metadata3("design:type", Function),
7650
+ _ts_metadata3("design:paramtypes", [
7651
+ typeof PluginField === "undefined" ? Object : PluginField
7652
+ ]),
7653
+ _ts_metadata3("design:returntype", void 0)
7654
+ ], ZModelCodeGenerator.prototype, "_generatePluginField", null);
7655
+ _ts_decorate3([
7656
+ gen(DataModel),
7657
+ _ts_metadata3("design:type", Function),
7658
+ _ts_metadata3("design:paramtypes", [
7659
+ typeof DataModel === "undefined" ? Object : DataModel
7660
+ ]),
7661
+ _ts_metadata3("design:returntype", void 0)
7662
+ ], ZModelCodeGenerator.prototype, "_generateDataModel", null);
7663
+ _ts_decorate3([
7664
+ gen(DataField),
7665
+ _ts_metadata3("design:type", Function),
7666
+ _ts_metadata3("design:paramtypes", [
7667
+ typeof DataField === "undefined" ? Object : DataField
7668
+ ]),
7669
+ _ts_metadata3("design:returntype", void 0)
7670
+ ], ZModelCodeGenerator.prototype, "_generateDataField", null);
7671
+ _ts_decorate3([
7672
+ gen(DataModelAttribute),
7673
+ _ts_metadata3("design:type", Function),
7674
+ _ts_metadata3("design:paramtypes", [
7675
+ typeof DataModelAttribute === "undefined" ? Object : DataModelAttribute
7676
+ ]),
7677
+ _ts_metadata3("design:returntype", void 0)
7678
+ ], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
7679
+ _ts_decorate3([
7680
+ gen(DataFieldAttribute),
7681
+ _ts_metadata3("design:type", Function),
7682
+ _ts_metadata3("design:paramtypes", [
7683
+ typeof DataFieldAttribute === "undefined" ? Object : DataFieldAttribute
7684
+ ]),
7685
+ _ts_metadata3("design:returntype", void 0)
7686
+ ], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
7687
+ _ts_decorate3([
7688
+ gen(AttributeArg),
7689
+ _ts_metadata3("design:type", Function),
7690
+ _ts_metadata3("design:paramtypes", [
7691
+ typeof AttributeArg === "undefined" ? Object : AttributeArg
7692
+ ]),
7693
+ _ts_metadata3("design:returntype", void 0)
7694
+ ], ZModelCodeGenerator.prototype, "_generateAttributeArg", null);
7695
+ _ts_decorate3([
7696
+ gen(ObjectExpr),
7697
+ _ts_metadata3("design:type", Function),
7698
+ _ts_metadata3("design:paramtypes", [
7699
+ typeof ObjectExpr === "undefined" ? Object : ObjectExpr
7700
+ ]),
7701
+ _ts_metadata3("design:returntype", void 0)
7702
+ ], ZModelCodeGenerator.prototype, "_generateObjectExpr", null);
7703
+ _ts_decorate3([
7704
+ gen(ArrayExpr),
7705
+ _ts_metadata3("design:type", Function),
7706
+ _ts_metadata3("design:paramtypes", [
7707
+ typeof ArrayExpr === "undefined" ? Object : ArrayExpr
7708
+ ]),
7709
+ _ts_metadata3("design:returntype", void 0)
7710
+ ], ZModelCodeGenerator.prototype, "_generateArrayExpr", null);
7711
+ _ts_decorate3([
7712
+ gen(StringLiteral),
7713
+ _ts_metadata3("design:type", Function),
7714
+ _ts_metadata3("design:paramtypes", [
7715
+ typeof LiteralExpr === "undefined" ? Object : LiteralExpr
7716
+ ]),
7717
+ _ts_metadata3("design:returntype", void 0)
7718
+ ], ZModelCodeGenerator.prototype, "_generateLiteralExpr", null);
7719
+ _ts_decorate3([
7720
+ gen(NumberLiteral),
7721
+ _ts_metadata3("design:type", Function),
7722
+ _ts_metadata3("design:paramtypes", [
7723
+ typeof NumberLiteral === "undefined" ? Object : NumberLiteral
7724
+ ]),
7725
+ _ts_metadata3("design:returntype", void 0)
7726
+ ], ZModelCodeGenerator.prototype, "_generateNumberLiteral", null);
7727
+ _ts_decorate3([
7728
+ gen(BooleanLiteral),
7729
+ _ts_metadata3("design:type", Function),
7730
+ _ts_metadata3("design:paramtypes", [
7731
+ typeof BooleanLiteral === "undefined" ? Object : BooleanLiteral
7732
+ ]),
7733
+ _ts_metadata3("design:returntype", void 0)
7734
+ ], ZModelCodeGenerator.prototype, "_generateBooleanLiteral", null);
7735
+ _ts_decorate3([
7736
+ gen(UnaryExpr),
7737
+ _ts_metadata3("design:type", Function),
7738
+ _ts_metadata3("design:paramtypes", [
7739
+ typeof UnaryExpr === "undefined" ? Object : UnaryExpr
7740
+ ]),
7741
+ _ts_metadata3("design:returntype", void 0)
7742
+ ], ZModelCodeGenerator.prototype, "_generateUnaryExpr", null);
7743
+ _ts_decorate3([
7744
+ gen(BinaryExpr),
7745
+ _ts_metadata3("design:type", Function),
7746
+ _ts_metadata3("design:paramtypes", [
7747
+ typeof BinaryExpr === "undefined" ? Object : BinaryExpr
7748
+ ]),
7749
+ _ts_metadata3("design:returntype", void 0)
7750
+ ], ZModelCodeGenerator.prototype, "_generateBinaryExpr", null);
7751
+ _ts_decorate3([
7752
+ gen(ReferenceExpr),
7753
+ _ts_metadata3("design:type", Function),
7754
+ _ts_metadata3("design:paramtypes", [
7755
+ typeof ReferenceExpr === "undefined" ? Object : ReferenceExpr
7756
+ ]),
7757
+ _ts_metadata3("design:returntype", void 0)
7758
+ ], ZModelCodeGenerator.prototype, "_generateReferenceExpr", null);
7759
+ _ts_decorate3([
7760
+ gen(ReferenceArg),
7761
+ _ts_metadata3("design:type", Function),
7762
+ _ts_metadata3("design:paramtypes", [
7763
+ typeof ReferenceArg === "undefined" ? Object : ReferenceArg
7764
+ ]),
7765
+ _ts_metadata3("design:returntype", void 0)
7766
+ ], ZModelCodeGenerator.prototype, "_generateReferenceArg", null);
7767
+ _ts_decorate3([
7768
+ gen(MemberAccessExpr),
7769
+ _ts_metadata3("design:type", Function),
7770
+ _ts_metadata3("design:paramtypes", [
7771
+ typeof MemberAccessExpr === "undefined" ? Object : MemberAccessExpr
7772
+ ]),
7773
+ _ts_metadata3("design:returntype", void 0)
7774
+ ], ZModelCodeGenerator.prototype, "_generateMemberExpr", null);
7775
+ _ts_decorate3([
7776
+ gen(InvocationExpr),
7777
+ _ts_metadata3("design:type", Function),
7778
+ _ts_metadata3("design:paramtypes", [
7779
+ typeof InvocationExpr === "undefined" ? Object : InvocationExpr
7780
+ ]),
7781
+ _ts_metadata3("design:returntype", void 0)
7782
+ ], ZModelCodeGenerator.prototype, "_generateInvocationExpr", null);
7783
+ _ts_decorate3([
7784
+ gen(NullExpr),
7785
+ _ts_metadata3("design:type", Function),
7786
+ _ts_metadata3("design:paramtypes", []),
7787
+ _ts_metadata3("design:returntype", void 0)
7788
+ ], ZModelCodeGenerator.prototype, "_generateNullExpr", null);
7789
+ _ts_decorate3([
7790
+ gen(ThisExpr),
7791
+ _ts_metadata3("design:type", Function),
7792
+ _ts_metadata3("design:paramtypes", []),
7793
+ _ts_metadata3("design:returntype", void 0)
7794
+ ], ZModelCodeGenerator.prototype, "_generateThisExpr", null);
7795
+ _ts_decorate3([
7796
+ gen(Attribute),
7797
+ _ts_metadata3("design:type", Function),
7798
+ _ts_metadata3("design:paramtypes", [
7799
+ typeof Attribute === "undefined" ? Object : Attribute
7800
+ ]),
7801
+ _ts_metadata3("design:returntype", void 0)
7802
+ ], ZModelCodeGenerator.prototype, "_generateAttribute", null);
7803
+ _ts_decorate3([
7804
+ gen(AttributeParam),
7805
+ _ts_metadata3("design:type", Function),
7806
+ _ts_metadata3("design:paramtypes", [
7807
+ typeof AttributeParam === "undefined" ? Object : AttributeParam
7808
+ ]),
7809
+ _ts_metadata3("design:returntype", void 0)
7810
+ ], ZModelCodeGenerator.prototype, "_generateAttributeParam", null);
7811
+ _ts_decorate3([
7812
+ gen(AttributeParamType),
7813
+ _ts_metadata3("design:type", Function),
7814
+ _ts_metadata3("design:paramtypes", [
7815
+ typeof AttributeParamType === "undefined" ? Object : AttributeParamType
7816
+ ]),
7817
+ _ts_metadata3("design:returntype", void 0)
7818
+ ], ZModelCodeGenerator.prototype, "_generateAttributeParamType", null);
7819
+ _ts_decorate3([
7820
+ gen(FunctionDecl),
7821
+ _ts_metadata3("design:type", Function),
7822
+ _ts_metadata3("design:paramtypes", [
7823
+ typeof FunctionDecl === "undefined" ? Object : FunctionDecl
7824
+ ]),
7825
+ _ts_metadata3("design:returntype", void 0)
7826
+ ], ZModelCodeGenerator.prototype, "_generateFunctionDecl", null);
7827
+ _ts_decorate3([
7828
+ gen(FunctionParam),
7829
+ _ts_metadata3("design:type", Function),
7830
+ _ts_metadata3("design:paramtypes", [
7831
+ typeof FunctionParam === "undefined" ? Object : FunctionParam
7832
+ ]),
7833
+ _ts_metadata3("design:returntype", void 0)
7834
+ ], ZModelCodeGenerator.prototype, "_generateFunctionParam", null);
7835
+ _ts_decorate3([
7836
+ gen(FunctionParamType),
7837
+ _ts_metadata3("design:type", Function),
7838
+ _ts_metadata3("design:paramtypes", [
7839
+ typeof FunctionParamType === "undefined" ? Object : FunctionParamType
7840
+ ]),
7841
+ _ts_metadata3("design:returntype", void 0)
7842
+ ], ZModelCodeGenerator.prototype, "_generateFunctionParamType", null);
7843
+ _ts_decorate3([
7844
+ gen(TypeDef),
7845
+ _ts_metadata3("design:type", Function),
7846
+ _ts_metadata3("design:paramtypes", [
7847
+ typeof TypeDef === "undefined" ? Object : TypeDef
7848
+ ]),
7849
+ _ts_metadata3("design:returntype", void 0)
7850
+ ], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
7851
+
7852
+ // src/zmodel-completion-provider.ts
7853
+ var ZModelCompletionProvider = class extends DefaultCompletionProvider {
7854
+ static {
7855
+ __name(this, "ZModelCompletionProvider");
7856
+ }
7857
+ services;
7858
+ constructor(services) {
7859
+ super(services), this.services = services;
7860
+ }
7861
+ completionOptions = {
7862
+ triggerCharacters: [
7863
+ "@",
7864
+ "(",
7865
+ ",",
7866
+ "."
7867
+ ]
7868
+ };
7869
+ async getCompletion(document, params) {
7870
+ try {
7871
+ return await super.getCompletion(document, params);
7872
+ } catch (e) {
7873
+ console.error("Completion error:", e.message);
7874
+ return void 0;
7875
+ }
7876
+ }
7877
+ completionFor(context, next, acceptor) {
7878
+ if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
7879
+ const completions = this.getCompletionFromHint(context.node);
7880
+ if (completions) {
7881
+ completions.forEach((c) => acceptor(context, c));
7882
+ return;
7883
+ }
7884
+ }
7885
+ return super.completionFor(context, next, acceptor);
7886
+ }
7887
+ getCompletionFromHint(contextNode) {
7888
+ const unfilledParams = this.getUnfilledAttributeParams(contextNode);
7889
+ const nextParam = unfilledParams[0];
7890
+ if (!nextParam) {
7891
+ return void 0;
7892
+ }
7893
+ const hintAttr = getAttribute(nextParam, "@@@completionHint");
7894
+ if (hintAttr) {
7895
+ const hint = hintAttr.args[0];
7896
+ if (hint?.value) {
7897
+ if (isArrayExpr(hint.value)) {
7898
+ return hint.value.items.map((item) => {
7899
+ return {
7900
+ label: `${item.value}`,
7901
+ kind: CompletionItemKind.Value,
7902
+ detail: "Parameter",
7903
+ sortText: "0"
7904
+ };
7905
+ });
7906
+ }
7907
+ }
7908
+ }
7909
+ return void 0;
7910
+ }
7911
+ // TODO: this doesn't work when the file contains parse errors
7912
+ getUnfilledAttributeParams(contextNode) {
7913
+ try {
7914
+ const params = contextNode.decl.ref?.params;
7915
+ if (params) {
7916
+ const args = contextNode.args;
7917
+ let unfilledParams = [
7918
+ ...params
7919
+ ];
7920
+ args.forEach((arg) => {
7921
+ if (arg.name) {
7922
+ unfilledParams = unfilledParams.filter((p) => p.name !== arg.name);
7923
+ } else {
7924
+ unfilledParams.shift();
7925
+ }
7926
+ });
7927
+ return unfilledParams;
7928
+ }
7929
+ } catch {
7930
+ }
7931
+ return [];
7932
+ }
7933
+ completionForCrossReference(context, crossRef, acceptor) {
7934
+ if (crossRef.property === "member" && !isMemberAccessExpr(context.node)) {
7935
+ return;
7936
+ }
7937
+ const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
7938
+ if (item.insertText?.startsWith("@@@") || item.label?.startsWith("@@@")) {
7939
+ return;
7940
+ }
7941
+ if ("nodeDescription" in item) {
7942
+ const node = this.getAstNode(item.nodeDescription);
7943
+ if (!node) {
7944
+ return;
7945
+ }
7946
+ if ((isEnum(node) || isEnumField(node)) && isFromStdlib(node)) {
7947
+ return;
7948
+ }
7949
+ if ((isDataModelAttribute(context2.node) || isDataFieldAttribute(context2.node)) && !this.filterAttributeApplicationCompletion(context2.node, node)) {
7950
+ return;
7951
+ }
7952
+ }
7953
+ acceptor(context2, item);
7954
+ }, "customAcceptor");
7955
+ return super.completionForCrossReference(context, crossRef, customAcceptor);
7956
+ }
7957
+ completionForKeyword(context, keyword, acceptor) {
7958
+ const customAcceptor = /* @__PURE__ */ __name((context2, item) => {
7959
+ if (!this.filterKeywordForContext(context2, keyword.value)) {
7960
+ return;
7961
+ }
7962
+ acceptor(context2, item);
7963
+ }, "customAcceptor");
7964
+ return super.completionForKeyword(context, keyword, customAcceptor);
7965
+ }
7966
+ filterKeywordForContext(context, keyword) {
7967
+ if (isInvocationExpr(context.node)) {
7968
+ return [
7969
+ "true",
7970
+ "false",
7971
+ "null",
7972
+ "this"
7973
+ ].includes(keyword);
7974
+ } else if (isDataModelAttribute(context.node) || isDataFieldAttribute(context.node)) {
7975
+ const exprContext = this.getAttributeContextType(context.node);
7976
+ if (exprContext === "DefaultValue") {
7977
+ return [
7978
+ "true",
7979
+ "false",
7980
+ "null"
7981
+ ].includes(keyword);
7982
+ } else {
7983
+ return [
7984
+ "true",
7985
+ "false",
7986
+ "null",
7987
+ "this"
7988
+ ].includes(keyword);
7989
+ }
7990
+ } else {
7991
+ return true;
7992
+ }
7993
+ }
7994
+ filterAttributeApplicationCompletion(contextNode, node) {
7995
+ const attrContextType = this.getAttributeContextType(contextNode);
7996
+ if (isFunctionDecl(node) && attrContextType) {
7997
+ const funcExprContextAttr = getAttribute(node, "@@@expressionContext");
7998
+ if (funcExprContextAttr && funcExprContextAttr.args[0]) {
7999
+ const arg = funcExprContextAttr.args[0];
8000
+ if (isArrayExpr(arg.value)) {
8001
+ return arg.value.items.some((item) => isEnumFieldReference(item) && item.target.$refText === attrContextType);
8002
+ }
8003
+ }
8004
+ return false;
8005
+ }
8006
+ if (isDataField(node)) {
8007
+ return attrContextType !== "DefaultValue";
8008
+ }
8009
+ return true;
8010
+ }
8011
+ getAttributeContextType(node) {
8012
+ return match3(node.decl.$refText).with("@default", () => "DefaultValue").with(P2.union("@@allow", "@allow", "@@deny", "@deny"), () => "AccessPolicy").with("@@validate", () => "ValidationRule").otherwise(() => void 0);
8013
+ }
8014
+ createReferenceCompletionItem(nodeDescription) {
8015
+ const node = this.getAstNode(nodeDescription);
8016
+ const documentation = this.getNodeDocumentation(node);
8017
+ return match3(node).when(isDataModel, () => ({
8018
+ nodeDescription,
8019
+ kind: CompletionItemKind.Class,
8020
+ detail: "Model",
8021
+ sortText: "1",
8022
+ documentation
8023
+ })).when(isTypeDef, () => ({
8024
+ nodeDescription,
8025
+ kind: CompletionItemKind.Class,
8026
+ detail: "Type",
8027
+ sortText: "1",
8028
+ documentation
8029
+ })).when(isDataField, () => ({
8030
+ nodeDescription,
8031
+ kind: CompletionItemKind.Field,
8032
+ detail: "Field",
8033
+ sortText: "0",
8034
+ documentation
8035
+ })).when(isEnum, () => ({
8036
+ nodeDescription,
8037
+ kind: CompletionItemKind.Class,
8038
+ detail: "Enum",
8039
+ sortText: "1",
8040
+ documentation
8041
+ })).when(isEnumField, (d) => {
8042
+ const container = d.$container;
8043
+ return {
8044
+ nodeDescription,
8045
+ kind: CompletionItemKind.Enum,
8046
+ detail: `Value of enum "${container.name}"`,
8047
+ sortText: "1",
8048
+ documentation
8049
+ };
8050
+ }).when(isFunctionDecl, () => ({
8051
+ nodeDescription,
8052
+ insertText: this.getFunctionInsertText(nodeDescription),
8053
+ kind: CompletionItemKind.Function,
8054
+ detail: "Function",
8055
+ sortText: "1",
8056
+ documentation
8057
+ })).when(isAttribute, () => ({
8058
+ nodeDescription,
8059
+ insertText: this.getAttributeInsertText(nodeDescription),
8060
+ kind: CompletionItemKind.Property,
8061
+ detail: "Attribute",
8062
+ sortText: "1",
8063
+ documentation
8064
+ })).otherwise(() => ({
8065
+ nodeDescription,
8066
+ kind: CompletionItemKind.Reference,
8067
+ detail: nodeDescription.type,
8068
+ sortText: "2",
8069
+ documentation
8070
+ }));
8071
+ }
8072
+ getFunctionInsertText(nodeDescription) {
8073
+ const node = this.getAstNode(nodeDescription);
8074
+ if (isFunctionDecl(node)) {
8075
+ if (node.params.some((p) => !p.optional)) {
8076
+ return nodeDescription.name;
8077
+ }
8078
+ }
8079
+ return `${nodeDescription.name}()`;
8080
+ }
8081
+ getAttributeInsertText(nodeDescription) {
8082
+ const node = this.getAstNode(nodeDescription);
8083
+ if (isAttribute(node)) {
8084
+ if (node.name === "@relation") {
8085
+ return `${nodeDescription.name}(fields: [], references: [])`;
8086
+ }
8087
+ }
8088
+ return nodeDescription.name;
8089
+ }
8090
+ getAstNode(nodeDescription) {
8091
+ let node = nodeDescription.node;
8092
+ if (!node) {
8093
+ const doc = this.getOrCreateDocumentSync(nodeDescription);
8094
+ if (!doc) {
8095
+ return void 0;
8096
+ }
8097
+ node = this.services.workspace.AstNodeLocator.getAstNode(doc.parseResult.value, nodeDescription.path);
8098
+ if (!node) {
8099
+ return void 0;
8100
+ }
8101
+ }
8102
+ return node;
8103
+ }
8104
+ getOrCreateDocumentSync(nodeDescription) {
8105
+ let doc = this.services.shared.workspace.LangiumDocuments.getDocument(nodeDescription.documentUri);
8106
+ if (!doc) {
8107
+ try {
8108
+ const content = fs2.readFileSync(nodeDescription.documentUri.fsPath, "utf-8");
8109
+ doc = this.services.shared.workspace.LangiumDocuments.createDocument(nodeDescription.documentUri, content);
8110
+ } catch {
8111
+ console.warn("Failed to read or create document:", nodeDescription.documentUri);
8112
+ return void 0;
8113
+ }
8114
+ }
8115
+ return doc;
8116
+ }
8117
+ getNodeDocumentation(node) {
8118
+ if (!node) {
8119
+ return void 0;
8120
+ }
8121
+ const md = this.commentsToMarkdown(node);
8122
+ return {
8123
+ kind: "markdown",
8124
+ value: md
8125
+ };
8126
+ }
8127
+ commentsToMarkdown(node) {
8128
+ const md = this.services.documentation.DocumentationProvider.getDocumentation(node) ?? "";
8129
+ const zModelGenerator = new ZModelCodeGenerator();
8130
+ const docs = [];
8131
+ try {
8132
+ match3(node).when(isAttribute, (attr) => {
8133
+ docs.push("```prisma", zModelGenerator.generate(attr), "```");
8134
+ }).when(isFunctionDecl, (func2) => {
8135
+ docs.push("```ts", zModelGenerator.generate(func2), "```");
8136
+ }).when(isDataModel, (model) => {
8137
+ docs.push("```prisma", `model ${model.name} { ... }`, "```");
8138
+ }).when(isEnum, (enumDecl) => {
8139
+ docs.push("```prisma", zModelGenerator.generate(enumDecl), "```");
8140
+ }).when(isDataField, (field) => {
8141
+ docs.push(`${field.name}: ${field.type.type ?? field.type.reference?.$refText}`);
8142
+ }).otherwise((ast) => {
8143
+ const name = ast.name;
8144
+ if (name) {
8145
+ docs.push(name);
8146
+ }
8147
+ });
8148
+ } catch {
8149
+ }
8150
+ if (md) {
8151
+ docs.push("___", md);
8152
+ }
8153
+ return docs.join("\n");
8154
+ }
8155
+ };
8156
+
8157
+ // src/zmodel-definition.ts
8158
+ import { DefaultDefinitionProvider } from "langium/lsp";
8159
+ import { LocationLink, Range } from "vscode-languageserver";
8160
+ var ZModelDefinitionProvider = class extends DefaultDefinitionProvider {
8161
+ static {
8162
+ __name(this, "ZModelDefinitionProvider");
8163
+ }
8164
+ documents;
8165
+ constructor(services) {
8166
+ super(services);
8167
+ this.documents = services.shared.workspace.LangiumDocuments;
8168
+ }
8169
+ collectLocationLinks(sourceCstNode, _params) {
8170
+ if (isModelImport(sourceCstNode.astNode)) {
8171
+ const importedModel = resolveImport(this.documents, sourceCstNode.astNode);
8172
+ if (importedModel?.$document) {
8173
+ const targetObject = importedModel;
8174
+ const selectionRange = this.nameProvider.getNameNode(targetObject)?.range ?? Range.create(0, 0, 0, 0);
8175
+ const previewRange = targetObject.$cstNode?.range ?? Range.create(0, 0, 0, 0);
8176
+ return [
8177
+ LocationLink.create(importedModel.$document.uri.toString(), previewRange, selectionRange, sourceCstNode.range)
8178
+ ];
8179
+ }
8180
+ return void 0;
8181
+ }
8182
+ return super.collectLocationLinks(sourceCstNode, _params);
8183
+ }
8184
+ };
8185
+
8186
+ // src/zmodel-document-builder.ts
8187
+ import { DefaultDocumentBuilder } from "langium";
8188
+ var ZModelDocumentBuilder = class extends DefaultDocumentBuilder {
8189
+ static {
8190
+ __name(this, "ZModelDocumentBuilder");
8191
+ }
8192
+ constructor(services) {
8193
+ super(services);
8194
+ let validationOptions = this.updateBuildOptions.validation;
8195
+ const stopFlags = {
8196
+ stopAfterLinkingErrors: true,
8197
+ stopAfterLexingErrors: true,
8198
+ stopAfterParsingErrors: true
8199
+ };
8200
+ if (validationOptions === true) {
8201
+ validationOptions = stopFlags;
8202
+ } else if (typeof validationOptions === "object") {
8203
+ validationOptions = {
8204
+ ...validationOptions,
8205
+ ...stopFlags
8206
+ };
8207
+ }
8208
+ this.updateBuildOptions = {
8209
+ ...this.updateBuildOptions,
8210
+ validation: validationOptions
8211
+ };
8212
+ }
8213
+ };
8214
+
8215
+ // src/zmodel-documentation-provider.ts
8216
+ import { JSDocDocumentationProvider } from "langium";
8217
+ var ZModelDocumentationProvider = class extends JSDocDocumentationProvider {
8218
+ static {
8219
+ __name(this, "ZModelDocumentationProvider");
8220
+ }
8221
+ getDocumentation(node) {
8222
+ if ("comments" in node && Array.isArray(node.comments) && node.comments.length > 0) {
8223
+ return node.comments.map((c) => c.replace(/^[/]*\s*/, "")).join("\n");
8224
+ }
8225
+ return super.getDocumentation(node);
8226
+ }
8227
+ };
8228
+
8229
+ // src/zmodel-formatter.ts
8230
+ import { AbstractFormatter, Formatting } from "langium/lsp";
8231
+ var ZModelFormatter = class extends AbstractFormatter {
8232
+ static {
8233
+ __name(this, "ZModelFormatter");
8234
+ }
8235
+ formatOptions;
8236
+ configurationProvider;
8237
+ constructor(services) {
8238
+ super();
8239
+ this.configurationProvider = services.shared.workspace.ConfigurationProvider;
8240
+ }
8241
+ format(node) {
8242
+ const formatter = this.getNodeFormatter(node);
8243
+ if (isDataField(node)) {
8244
+ if (isDataModel(node.$container) || isTypeDef(node.$container)) {
8245
+ const dataModel = node.$container;
8246
+ const compareFn = /* @__PURE__ */ __name((a, b) => b - a, "compareFn");
8247
+ const maxNameLength = dataModel.fields.map((x) => x.name.length).sort(compareFn)[0] ?? 0;
8248
+ const maxTypeLength = dataModel.fields.map(this.getFieldTypeLength).sort(compareFn)[0] ?? 0;
8249
+ formatter.property("type").prepend(Formatting.spaces(maxNameLength - node.name.length + 1));
8250
+ if (node.attributes.length > 0) {
8251
+ formatter.node(node.attributes[0]).prepend(Formatting.spaces(maxTypeLength - this.getFieldTypeLength(node) + 1));
8252
+ formatter.nodes(...node.attributes.slice(1)).prepend(Formatting.oneSpace());
8253
+ }
8254
+ } else {
8255
+ formatter.property("type").prepend(Formatting.oneSpace());
8256
+ if (node.attributes.length > 0) {
8257
+ formatter.properties("attributes").prepend(Formatting.oneSpace());
8258
+ }
8259
+ }
8260
+ } else if (isDataFieldAttribute(node)) {
8261
+ formatter.keyword("(").surround(Formatting.noSpace());
8262
+ formatter.keyword(")").prepend(Formatting.noSpace());
8263
+ formatter.keyword(",").append(Formatting.oneSpace());
8264
+ if (node.args.length > 1) {
8265
+ formatter.nodes(...node.args.slice(1)).prepend(Formatting.oneSpace());
8266
+ }
8267
+ } else if (isAttributeArg(node)) {
8268
+ formatter.keyword(":").prepend(Formatting.noSpace());
8269
+ formatter.keyword(":").append(Formatting.oneSpace());
8270
+ } else if (isAbstractDeclaration(node)) {
8271
+ const bracesOpen = formatter.keyword("{");
8272
+ const bracesClose = formatter.keyword("}");
8273
+ formatter.interior(bracesOpen, bracesClose).prepend(Formatting.indent({
8274
+ allowMore: true
8275
+ }));
8276
+ bracesOpen.prepend(Formatting.oneSpace());
8277
+ bracesClose.prepend(Formatting.newLine());
8278
+ } else if (isModel(node)) {
8279
+ const model = node;
8280
+ const nodes = formatter.nodes(...model.declarations);
8281
+ nodes.prepend(Formatting.noIndent());
8282
+ }
8283
+ }
8284
+ formatDocument(document, params) {
8285
+ this.formatOptions = params.options;
8286
+ this.configurationProvider.getConfiguration(ZModelLanguageMetaData.languageId, "format").then((config) => {
8287
+ if (config) {
8288
+ }
8289
+ });
8290
+ return super.formatDocument(document, params);
8291
+ }
8292
+ getFormatOptions() {
8293
+ return this.formatOptions;
8294
+ }
8295
+ getIndent() {
8296
+ return 1;
8297
+ }
8298
+ getFieldTypeLength(field) {
8299
+ let length;
8300
+ if (field.type.type) {
8301
+ length = field.type.type.length;
8302
+ } else if (field.type.reference) {
8303
+ length = field.type.reference.$refText.length;
8304
+ } else if (isDataField(field) && field.type.unsupported) {
8305
+ const name = `Unsupported("${field.type.unsupported.value.value}")`;
8306
+ length = name.length;
8307
+ } else {
8308
+ length = 1;
8309
+ }
8310
+ if (field.type.optional) {
8311
+ length += 1;
8312
+ }
8313
+ if (field.type.array) {
8314
+ length += 2;
8315
+ }
8316
+ return length;
8317
+ }
8318
+ };
8319
+
8320
+ // src/zmodel-linker.ts
8321
+ import { AstUtils as AstUtils6, Cancellation, DefaultLinker, DocumentState, interruptAndCheck } from "langium";
8322
+ import { match as match4 } from "ts-pattern";
6988
8323
  var ZModelLinker = class extends DefaultLinker {
6989
8324
  static {
6990
8325
  __name(this, "ZModelLinker");
@@ -7005,21 +8340,19 @@ var ZModelLinker = class extends DefaultLinker {
7005
8340
  }
7006
8341
  document.state = DocumentState.Linked;
7007
8342
  }
7008
- linkReference(container, property, document, extraScopes) {
7009
- if (this.resolveFromScopeProviders(container, property, document, extraScopes)) {
8343
+ linkReference(refInfo, document, extraScopes) {
8344
+ const defaultRef = refInfo.reference;
8345
+ if (defaultRef._ref) {
8346
+ return;
8347
+ }
8348
+ if (this.resolveFromScopeProviders(refInfo.reference, document, extraScopes)) {
7010
8349
  return;
7011
8350
  }
7012
- const reference = container[property];
7013
- this.doLink({
7014
- reference,
7015
- container,
7016
- property
7017
- }, document);
8351
+ this.doLink(refInfo, document);
7018
8352
  }
7019
8353
  //#endregion
7020
8354
  //#region Expression type resolving
7021
- resolveFromScopeProviders(node, property, document, providers) {
7022
- const reference = node[property];
8355
+ resolveFromScopeProviders(reference, document, providers) {
7023
8356
  for (const provider of providers) {
7024
8357
  const target = provider(reference.$refText);
7025
8358
  if (target) {
@@ -7148,7 +8481,11 @@ var ZModelLinker = class extends DefaultLinker {
7148
8481
  }
7149
8482
  }
7150
8483
  resolveInvocation(node, document, extraScopes) {
7151
- this.linkReference(node, "function", document, extraScopes);
8484
+ this.linkReference({
8485
+ reference: node.function,
8486
+ container: node,
8487
+ property: "function"
8488
+ }, document, extraScopes);
7152
8489
  node.args.forEach((arg) => this.resolve(arg, document, extraScopes));
7153
8490
  if (node.function.ref) {
7154
8491
  const funcDecl = node.function.ref;
@@ -7161,7 +8498,7 @@ var ZModelLinker = class extends DefaultLinker {
7161
8498
  nullable: true
7162
8499
  };
7163
8500
  }
7164
- } else if (isFutureExpr(node)) {
8501
+ } else if (isBeforeInvocation(node)) {
7165
8502
  node.$resolvedType = {
7166
8503
  decl: getContainingDataModel(node)
7167
8504
  };
@@ -7171,7 +8508,7 @@ var ZModelLinker = class extends DefaultLinker {
7171
8508
  }
7172
8509
  }
7173
8510
  resolveLiteral(node) {
7174
- const type = match2(node).when(isStringLiteral, () => "String").when(isBooleanLiteral, () => "Boolean").when(isNumberLiteral, () => "Int").exhaustive();
8511
+ const type = match4(node).when(isStringLiteral, () => "String").when(isBooleanLiteral, () => "Boolean").when(isNumberLiteral, () => "Int").exhaustive();
7175
8512
  if (type) {
7176
8513
  this.resolveToBuiltinTypeOrDecl(node, type);
7177
8514
  }
@@ -7225,7 +8562,7 @@ var ZModelLinker = class extends DefaultLinker {
7225
8562
  if (isArrayExpr(node.value)) {
7226
8563
  node.value.items.forEach((item) => {
7227
8564
  if (isReferenceExpr(item)) {
7228
- const resolved2 = this.resolveFromScopeProviders(item, "target", document, [
8565
+ const resolved2 = this.resolveFromScopeProviders(item.target, document, [
7229
8566
  scopeProvider
7230
8567
  ]);
7231
8568
  if (resolved2) {
@@ -7239,7 +8576,7 @@ var ZModelLinker = class extends DefaultLinker {
7239
8576
  this.resolveToBuiltinTypeOrDecl(node.value, node.value.items[0].$resolvedType.decl, true);
7240
8577
  }
7241
8578
  } else if (isReferenceExpr(node.value)) {
7242
- const resolved2 = this.resolveFromScopeProviders(node.value, "target", document, [
8579
+ const resolved2 = this.resolveFromScopeProviders(node.value.target, document, [
7243
8580
  scopeProvider
7244
8581
  ]);
7245
8582
  if (resolved2) {
@@ -7291,13 +8628,9 @@ var ZModelLinker = class extends DefaultLinker {
7291
8628
  this.resolveDefault(node, document, scopes);
7292
8629
  }
7293
8630
  resolveDefault(node, document, extraScopes) {
7294
- for (const [property, value] of Object.entries(node)) {
7295
- if (!property.startsWith("$")) {
7296
- if (isReference(value)) {
7297
- this.linkReference(node, property, document, extraScopes);
7298
- }
7299
- }
7300
- }
8631
+ AstUtils6.streamReferences(node).forEach((ref) => {
8632
+ this.linkReference(ref, document, extraScopes);
8633
+ });
7301
8634
  for (const child of AstUtils6.streamContents(node)) {
7302
8635
  this.resolve(child, document, extraScopes);
7303
8636
  }
@@ -7343,7 +8676,7 @@ var ZModelLinker = class extends DefaultLinker {
7343
8676
 
7344
8677
  // src/zmodel-scope.ts
7345
8678
  import { AstUtils as AstUtils7, DefaultScopeComputation, DefaultScopeProvider, EMPTY_SCOPE, StreamScope, UriUtils, interruptAndCheck as interruptAndCheck2 } from "langium";
7346
- import { match as match3 } from "ts-pattern";
8679
+ import { match as match5 } from "ts-pattern";
7347
8680
  var ZModelScopeComputation = class extends DefaultScopeComputation {
7348
8681
  static {
7349
8682
  __name(this, "ZModelScopeComputation");
@@ -7367,7 +8700,7 @@ var ZModelScopeComputation = class extends DefaultScopeComputation {
7367
8700
  }
7368
8701
  processNode(node, document, scopes) {
7369
8702
  super.processNode(node, document, scopes);
7370
- if (isDataModel(node)) {
8703
+ if (isDataModel(node) || isTypeDef(node)) {
7371
8704
  const bases = getRecursiveBases(node);
7372
8705
  for (const base of bases) {
7373
8706
  for (const field of base.fields) {
@@ -7416,11 +8749,8 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
7416
8749
  const referenceType = this.reflection.getReferenceType(context);
7417
8750
  const globalScope = this.getGlobalScope(referenceType, context);
7418
8751
  const node = context.container;
7419
- const allowTypeDefScope = (
7420
- // isAuthOrAuthMemberAccess(node.operand) ||
7421
- !!AstUtils7.getContainerOfType(node, isTypeDef)
7422
- );
7423
- return match3(node.operand).when(isReferenceExpr, (operand) => {
8752
+ const allowTypeDefScope = isAuthOrAuthMemberAccess(node.operand) || !!AstUtils7.getContainerOfType(node, isTypeDef);
8753
+ return match5(node.operand).when(isReferenceExpr, (operand) => {
7424
8754
  const ref = operand.target.ref;
7425
8755
  if (isDataField(ref)) {
7426
8756
  return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
@@ -7438,7 +8768,7 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
7438
8768
  if (isAuthInvocation(operand)) {
7439
8769
  return this.createScopeForAuth(node, globalScope);
7440
8770
  }
7441
- if (isFutureInvocation(operand)) {
8771
+ if (isBeforeInvocation(operand)) {
7442
8772
  return this.createScopeForContainingModel(node, globalScope);
7443
8773
  }
7444
8774
  return EMPTY_SCOPE;
@@ -7448,8 +8778,8 @@ var ZModelScopeProvider = class extends DefaultScopeProvider {
7448
8778
  const referenceType = this.reflection.getReferenceType(context);
7449
8779
  const globalScope = this.getGlobalScope(referenceType, context);
7450
8780
  const collection = collectionPredicate.left;
7451
- const allowTypeDefScope = false;
7452
- return match3(collection).when(isReferenceExpr, (expr) => {
8781
+ const allowTypeDefScope = isAuthOrAuthMemberAccess(collection);
8782
+ return match5(collection).when(isReferenceExpr, (expr) => {
7453
8783
  const ref = expr.target.ref;
7454
8784
  if (isDataField(ref)) {
7455
8785
  return this.createScopeForContainer(ref.type.reference?.ref, globalScope, allowTypeDefScope);
@@ -7511,11 +8841,95 @@ function getCollectionPredicateContext(node) {
7511
8841
  }
7512
8842
  __name(getCollectionPredicateContext, "getCollectionPredicateContext");
7513
8843
 
8844
+ // src/zmodel-semantic.ts
8845
+ import { AbstractSemanticTokenProvider } from "langium/lsp";
8846
+ import { SemanticTokenTypes } from "vscode-languageserver";
8847
+ var ZModelSemanticTokenProvider = class extends AbstractSemanticTokenProvider {
8848
+ static {
8849
+ __name(this, "ZModelSemanticTokenProvider");
8850
+ }
8851
+ highlightElement(node, acceptor) {
8852
+ if (isDataModel(node)) {
8853
+ acceptor({
8854
+ node,
8855
+ property: "name",
8856
+ type: SemanticTokenTypes.type
8857
+ });
8858
+ acceptor({
8859
+ node,
8860
+ property: "mixins",
8861
+ type: SemanticTokenTypes.type
8862
+ });
8863
+ acceptor({
8864
+ node,
8865
+ property: "baseModel",
8866
+ type: SemanticTokenTypes.type
8867
+ });
8868
+ } else if (isDataSource(node) || isGeneratorDecl(node) || isPlugin(node) || isEnum(node) || isTypeDef(node)) {
8869
+ acceptor({
8870
+ node,
8871
+ property: "name",
8872
+ type: SemanticTokenTypes.type
8873
+ });
8874
+ } else if (isDataField(node) || isConfigField(node) || isAttributeArg(node) || isPluginField(node) || isEnumField(node)) {
8875
+ acceptor({
8876
+ node,
8877
+ property: "name",
8878
+ type: SemanticTokenTypes.variable
8879
+ });
8880
+ } else if (isDataFieldType(node)) {
8881
+ if (node.type) {
8882
+ acceptor({
8883
+ node,
8884
+ property: "type",
8885
+ type: SemanticTokenTypes.type
8886
+ });
8887
+ } else {
8888
+ acceptor({
8889
+ node,
8890
+ property: "reference",
8891
+ type: SemanticTokenTypes.macro
8892
+ });
8893
+ }
8894
+ } else if (isDataModelAttribute(node) || isDataFieldAttribute(node) || isInternalAttribute(node)) {
8895
+ acceptor({
8896
+ node,
8897
+ property: "decl",
8898
+ type: SemanticTokenTypes.function
8899
+ });
8900
+ } else if (isInvocationExpr(node)) {
8901
+ acceptor({
8902
+ node,
8903
+ property: "function",
8904
+ type: SemanticTokenTypes.function
8905
+ });
8906
+ } else if (isFunctionDecl(node) || isAttribute(node)) {
8907
+ acceptor({
8908
+ node,
8909
+ property: "name",
8910
+ type: SemanticTokenTypes.function
8911
+ });
8912
+ } else if (isReferenceExpr(node)) {
8913
+ acceptor({
8914
+ node,
8915
+ property: "target",
8916
+ type: SemanticTokenTypes.variable
8917
+ });
8918
+ } else if (isMemberAccessExpr(node)) {
8919
+ acceptor({
8920
+ node,
8921
+ property: "member",
8922
+ type: SemanticTokenTypes.property
8923
+ });
8924
+ }
8925
+ }
8926
+ };
8927
+
7514
8928
  // src/zmodel-workspace-manager.ts
7515
- import { DefaultWorkspaceManager, URI as URI2, UriUtils as UriUtils2 } from "langium";
7516
- import fs2 from "fs";
8929
+ import { DefaultWorkspaceManager, URI as URI2 } from "langium";
8930
+ import fs3 from "fs";
7517
8931
  import path2 from "path";
7518
- import { fileURLToPath } from "url";
8932
+ import { fileURLToPath as fileURLToPath2 } from "url";
7519
8933
  var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
7520
8934
  static {
7521
8935
  __name(this, "ZModelWorkspaceManager");
@@ -7539,7 +8953,7 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
7539
8953
  });
7540
8954
  const languagePackageDir = path2.dirname(languagePackagePath);
7541
8955
  const candidateStdlibPath = path2.join(languagePackageDir, "res", STD_LIB_MODULE_NAME);
7542
- if (fs2.existsSync(candidateStdlibPath)) {
8956
+ if (fs3.existsSync(candidateStdlibPath)) {
7543
8957
  installedStdlibPath = candidateStdlibPath;
7544
8958
  console.log(`Found installed zenstack package stdlib at: ${installedStdlibPath}`);
7545
8959
  break;
@@ -7551,73 +8965,12 @@ var ZModelWorkspaceManager = class extends DefaultWorkspaceManager {
7551
8965
  if (installedStdlibPath) {
7552
8966
  stdLibPath = installedStdlibPath;
7553
8967
  } else {
7554
- const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(fileURLToPath(import.meta.url));
8968
+ const _dirname = typeof __dirname !== "undefined" ? __dirname : path2.dirname(fileURLToPath2(import.meta.url));
7555
8969
  stdLibPath = path2.join(_dirname, "../res", STD_LIB_MODULE_NAME);
7556
8970
  console.log(`Using bundled stdlib in extension:`, stdLibPath);
7557
8971
  }
7558
8972
  const stdlib = await this.documentFactory.fromUri(URI2.file(stdLibPath));
7559
8973
  collector(stdlib);
7560
- const documents = this.langiumDocuments.all;
7561
- const pluginModels = /* @__PURE__ */ new Set();
7562
- documents.forEach((doc) => {
7563
- const parsed = doc.parseResult.value;
7564
- parsed.declarations.forEach((decl) => {
7565
- if (isPlugin(decl)) {
7566
- const providerField = decl.fields.find((f) => f.name === "provider");
7567
- if (providerField) {
7568
- const provider = getLiteral(providerField.value);
7569
- if (provider) {
7570
- pluginModels.add(provider);
7571
- }
7572
- }
7573
- }
7574
- });
7575
- });
7576
- if (pluginModels.size > 0) {
7577
- console.log(`Used plugin modules: ${Array.from(pluginModels)}`);
7578
- const pendingPluginModules = new Set(pluginModels);
7579
- await Promise.all(folders.map((wf) => [
7580
- wf,
7581
- this.getRootFolder(wf)
7582
- ]).map(async (entry) => this.loadPluginModels(...entry, pendingPluginModules, collector)));
7583
- }
7584
- }
7585
- async loadPluginModels(workspaceFolder, folderPath, pendingPluginModels, collector) {
7586
- const content = (await this.fileSystemProvider.readDirectory(folderPath)).sort((a, b) => {
7587
- if (a.isDirectory && b.isDirectory) {
7588
- const aName = UriUtils2.basename(a.uri);
7589
- if (aName === "node_modules") {
7590
- return -1;
7591
- } else {
7592
- return 1;
7593
- }
7594
- } else {
7595
- return 0;
7596
- }
7597
- });
7598
- for (const entry of content) {
7599
- if (entry.isDirectory) {
7600
- const name = UriUtils2.basename(entry.uri);
7601
- if (name === "node_modules") {
7602
- for (const plugin of Array.from(pendingPluginModels)) {
7603
- const path4 = UriUtils2.joinPath(entry.uri, plugin, PLUGIN_MODULE_NAME);
7604
- try {
7605
- await this.fileSystemProvider.readFile(path4);
7606
- const document = await this.langiumDocuments.getOrCreateDocument(path4);
7607
- collector(document);
7608
- console.log(`Adding plugin document from ${path4.path}`);
7609
- pendingPluginModels.delete(plugin);
7610
- if (pendingPluginModels.size === 0) {
7611
- return;
7612
- }
7613
- } catch {
7614
- }
7615
- }
7616
- } else {
7617
- await this.loadPluginModels(workspaceFolder, entry.uri, pendingPluginModels, collector);
7618
- }
7619
- }
7620
- }
7621
8974
  }
7622
8975
  };
7623
8976
 
@@ -7630,14 +8983,25 @@ var ZModelLanguageModule = {
7630
8983
  },
7631
8984
  validation: {
7632
8985
  ZModelValidator: /* @__PURE__ */ __name((services) => new ZModelValidator(services), "ZModelValidator")
8986
+ },
8987
+ lsp: {
8988
+ Formatter: /* @__PURE__ */ __name((services) => new ZModelFormatter(services), "Formatter"),
8989
+ DefinitionProvider: /* @__PURE__ */ __name((services) => new ZModelDefinitionProvider(services), "DefinitionProvider"),
8990
+ CompletionProvider: /* @__PURE__ */ __name((services) => new ZModelCompletionProvider(services), "CompletionProvider"),
8991
+ SemanticTokenProvider: /* @__PURE__ */ __name((services) => new ZModelSemanticTokenProvider(services), "SemanticTokenProvider")
8992
+ },
8993
+ documentation: {
8994
+ CommentProvider: /* @__PURE__ */ __name((services) => new ZModelCommentProvider(services), "CommentProvider"),
8995
+ DocumentationProvider: /* @__PURE__ */ __name((services) => new ZModelDocumentationProvider(services), "DocumentationProvider")
7633
8996
  }
7634
8997
  };
7635
8998
  var ZModelSharedModule = {
7636
8999
  workspace: {
9000
+ DocumentBuilder: /* @__PURE__ */ __name((services) => new ZModelDocumentBuilder(services), "DocumentBuilder"),
7637
9001
  WorkspaceManager: /* @__PURE__ */ __name((services) => new ZModelWorkspaceManager(services), "WorkspaceManager")
7638
9002
  }
7639
9003
  };
7640
- function createZModelLanguageServices(context) {
9004
+ function createZModelLanguageServices(context, logToConsole = false) {
7641
9005
  const shared = inject(createDefaultSharedModule(context), ZModelGeneratedSharedModule, ZModelSharedModule);
7642
9006
  const ZModelLanguage = inject(createDefaultModule({
7643
9007
  shared
@@ -7647,30 +9011,47 @@ function createZModelLanguageServices(context) {
7647
9011
  if (!context.connection) {
7648
9012
  shared.workspace.ConfigurationProvider.initialized({});
7649
9013
  }
9014
+ shared.workspace.DocumentBuilder.onBuildPhase(DocumentState2.Parsed, async (documents) => {
9015
+ for (const doc of documents) {
9016
+ if (doc.parseResult.lexerErrors.length > 0 || doc.parseResult.parserErrors.length > 0) {
9017
+ continue;
9018
+ }
9019
+ if (doc.uri.scheme !== "file") {
9020
+ continue;
9021
+ }
9022
+ const schemaPath = fileURLToPath3(doc.uri.toString());
9023
+ const pluginSchemas = getPluginDocuments(doc.parseResult.value, schemaPath);
9024
+ for (const plugin of pluginSchemas) {
9025
+ const pluginDocUri = URI3.file(path3.resolve(plugin));
9026
+ let pluginDoc = shared.workspace.LangiumDocuments.getDocument(pluginDocUri);
9027
+ if (!pluginDoc) {
9028
+ pluginDoc = await shared.workspace.LangiumDocuments.getOrCreateDocument(pluginDocUri);
9029
+ if (pluginDoc) {
9030
+ shared.workspace.IndexManager.updateContent(pluginDoc);
9031
+ if (logToConsole) {
9032
+ console.log(`Loaded plugin model: ${plugin}`);
9033
+ }
9034
+ }
9035
+ }
9036
+ }
9037
+ }
9038
+ });
7650
9039
  return {
7651
9040
  shared,
7652
9041
  ZModelLanguage
7653
9042
  };
7654
9043
  }
7655
9044
  __name(createZModelLanguageServices, "createZModelLanguageServices");
7656
-
7657
- // src/index.ts
7658
- function createZModelServices() {
7659
- return createZModelLanguageServices(NodeFileSystem);
9045
+ function createZModelServices(logToConsole = false) {
9046
+ return createZModelLanguageServices(NodeFileSystem, logToConsole);
7660
9047
  }
7661
9048
  __name(createZModelServices, "createZModelServices");
7662
- var DocumentLoadError = class extends Error {
7663
- static {
7664
- __name(this, "DocumentLoadError");
7665
- }
7666
- constructor(message) {
7667
- super(message);
7668
- }
7669
- };
7670
- async function loadDocument(fileName, pluginModelFiles = []) {
7671
- const { ZModelLanguage: services } = createZModelServices();
9049
+
9050
+ // src/document.ts
9051
+ async function loadDocument(fileName, additionalModelFiles = []) {
9052
+ const { ZModelLanguage: services } = createZModelServices(false);
7672
9053
  const extensions = services.LanguageMetaData.fileExtensions;
7673
- if (!extensions.includes(path3.extname(fileName))) {
9054
+ if (!extensions.includes(path4.extname(fileName))) {
7674
9055
  return {
7675
9056
  success: false,
7676
9057
  errors: [
@@ -7679,7 +9060,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7679
9060
  warnings: []
7680
9061
  };
7681
9062
  }
7682
- if (!fs3.existsSync(fileName)) {
9063
+ if (!fs4.existsSync(fileName)) {
7683
9064
  return {
7684
9065
  success: false,
7685
9066
  errors: [
@@ -7688,23 +9069,27 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7688
9069
  warnings: []
7689
9070
  };
7690
9071
  }
7691
- const _dirname = typeof __dirname !== "undefined" ? __dirname : path3.dirname(fileURLToPath2(import.meta.url));
7692
- const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(path3.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
7693
- const pluginDocs = await Promise.all(pluginModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(file)))));
9072
+ const _dirname = typeof __dirname !== "undefined" ? __dirname : path4.dirname(fileURLToPath4(import.meta.url));
9073
+ const stdLib = await services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(path4.join(_dirname, "../res", STD_LIB_MODULE_NAME))));
7694
9074
  const langiumDocuments = services.shared.workspace.LangiumDocuments;
7695
- const document = await langiumDocuments.getOrCreateDocument(URI3.file(path3.resolve(fileName)));
9075
+ const document = await langiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(fileName)));
7696
9076
  const importedURIs = await loadImports(document, langiumDocuments);
7697
9077
  const importedDocuments = [];
7698
9078
  for (const uri of importedURIs) {
7699
9079
  importedDocuments.push(await langiumDocuments.getOrCreateDocument(uri));
7700
9080
  }
9081
+ const additionalDocs = await Promise.all(additionalModelFiles.map((file) => services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI4.file(path4.resolve(file)))));
7701
9082
  await services.shared.workspace.DocumentBuilder.build([
7702
9083
  stdLib,
7703
- ...pluginDocs,
9084
+ ...additionalDocs,
7704
9085
  document,
7705
9086
  ...importedDocuments
7706
9087
  ], {
7707
- validation: true
9088
+ validation: {
9089
+ stopAfterLexingErrors: true,
9090
+ stopAfterParsingErrors: true,
9091
+ stopAfterLinkingErrors: true
9092
+ }
7708
9093
  });
7709
9094
  const diagnostics = langiumDocuments.all.flatMap((doc) => (doc.diagnostics ?? []).map((diag) => ({
7710
9095
  doc,
@@ -7714,7 +9099,7 @@ async function loadDocument(fileName, pluginModelFiles = []) {
7714
9099
  const warnings = [];
7715
9100
  if (diagnostics.length > 0) {
7716
9101
  for (const { doc, diag } of diagnostics) {
7717
- const message = `${path3.relative(process.cwd(), doc.uri.fsPath)}:${diag.range.start.line + 1}:${diag.range.start.character + 1} - ${diag.message}`;
9102
+ const message = `${path4.relative(process.cwd(), doc.uri.fsPath)}:${diag.range.start.line + 1}:${diag.range.start.character + 1} - ${diag.message}`;
7718
9103
  if (diag.severity === 1) {
7719
9104
  errors.push(message);
7720
9105
  } else {
@@ -7763,7 +9148,7 @@ async function loadImports(document, documents, uris = /* @__PURE__ */ new Set()
7763
9148
  }
7764
9149
  }
7765
9150
  }
7766
- return Array.from(uris).filter((x) => uriString != x).map((e) => URI3.parse(e));
9151
+ return Array.from(uris).filter((x) => uriString != x).map((e) => URI4.parse(e));
7767
9152
  }
7768
9153
  __name(loadImports, "loadImports");
7769
9154
  function mergeImportsDeclarations(documents, model) {
@@ -7812,13 +9197,33 @@ function validationAfterImportMerge(model) {
7812
9197
  return errors;
7813
9198
  }
7814
9199
  __name(validationAfterImportMerge, "validationAfterImportMerge");
9200
+ async function formatDocument(content) {
9201
+ const services = createZModelServices().ZModelLanguage;
9202
+ const langiumDocuments = services.shared.workspace.LangiumDocuments;
9203
+ const document = langiumDocuments.createDocument(URI4.parse("memory://schema.zmodel"), content);
9204
+ const formatter = services.lsp.Formatter;
9205
+ const identifier = {
9206
+ uri: document.uri.toString()
9207
+ };
9208
+ const options = formatter.getFormatOptions() ?? {
9209
+ insertSpaces: true,
9210
+ tabSize: 4
9211
+ };
9212
+ const edits = await formatter.formatDocument(document, {
9213
+ options,
9214
+ textDocument: identifier
9215
+ });
9216
+ return TextDocument.applyEdits(document.textDocument, edits);
9217
+ }
9218
+ __name(formatDocument, "formatDocument");
7815
9219
  export {
7816
- DocumentLoadError,
9220
+ ZModelCodeGenerator,
7817
9221
  ZModelLanguageMetaData,
7818
9222
  ZModelLanguageModule,
7819
9223
  ZModelSharedModule,
7820
9224
  createZModelLanguageServices,
7821
9225
  createZModelServices,
9226
+ formatDocument,
7822
9227
  loadDocument
7823
9228
  };
7824
9229
  //# sourceMappingURL=index.js.map