@atscript/core 0.1.99 → 0.1.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2215,10 +2215,33 @@ function getRelFilterScope(owner) {
2215
2215
 
2216
2216
  //#endregion
2217
2217
  //#region packages/core/src/lsp/field-refs.ts
2218
+ function getArgSpecAt(annotationToken, index, doc) {
2219
+ return doc.resolveAnnotation(annotationToken.text.slice(1))?.arguments[index];
2220
+ }
2218
2221
  function getArgSpec(argToken, doc) {
2219
2222
  const annotationRef = argToken.annotationRef;
2220
2223
  if (!annotationRef || typeof argToken.index !== "number") return;
2221
- return doc.resolveAnnotation(annotationRef.text.slice(1))?.arguments[argToken.index];
2224
+ return getArgSpecAt(annotationRef, argToken.index, doc);
2225
+ }
2226
+ /**
2227
+ * The annotation an argument token belongs to, found among the annotations of the annotated
2228
+ * node (`argToken.parentNode`, a prop or an annotate entry). For `fieldScope` hooks that scope
2229
+ * an argument by its siblings (`getSiblingAnnotation(arg)?.args[0]?.text`).
2230
+ */ function getSiblingAnnotation(argToken) {
2231
+ return argToken.parentNode?.annotations?.find((a) => a.args.includes(argToken));
2232
+ }
2233
+ /**
2234
+ * The values an annotation argument may take, from the `valueScope` hook of its spec
2235
+ * (`undefined` when the argument declares none or the set cannot be determined).
2236
+ */ function getValueCandidates(annotationToken, argIndex, doc) {
2237
+ return getArgSpecAt(annotationToken, argIndex, doc)?.valueScope?.(annotationToken, doc);
2238
+ }
2239
+ /**
2240
+ * The declared value a value-argument token names (`valueScope`), or `undefined` when the token
2241
+ * is not a value argument or names no candidate.
2242
+ */ function getDeclaredValue(token, doc) {
2243
+ if (!token.annotationRef || typeof token.index !== "number" || token.isReference) return;
2244
+ return getValueCandidates(token.annotationRef, token.index, doc)?.find((c) => c.value === token.text);
2222
2245
  }
2223
2246
  /**
2224
2247
  * Field scope of an annotation argument: the answer of the argument spec's `fieldScope`
@@ -3846,7 +3869,7 @@ function _define_property$3(obj, key, value) {
3846
3869
  else obj[key] = value;
3847
3870
  return obj;
3848
3871
  }
3849
- var AtscriptDoc = class {
3872
+ var AtscriptDoc = class AtscriptDoc {
3850
3873
  get name() {
3851
3874
  return this.id.split("/").pop();
3852
3875
  }
@@ -3977,7 +4000,16 @@ var AtscriptDoc = class {
3977
4000
  });
3978
4001
  }
3979
4002
  }
4003
+ /**
4004
+ * The document in which the given annotation was written (and whose imports
4005
+ * its type-reference arguments resolve against). Annotations inherited into
4006
+ * another document through a chain ref, `extends` or `annotate` keep the
4007
+ * declaring document. `undefined` for annotations never registered on a doc.
4008
+ */ annotationOrigin(annotation) {
4009
+ return AtscriptDoc.annotationOrigins.get(annotation);
4010
+ }
3980
4011
  registerAnnotation(annotationTokens) {
4012
+ AtscriptDoc.annotationOrigins.set(annotationTokens, this);
3981
4013
  this.annotations.push(annotationTokens);
3982
4014
  const mainToken = annotationTokens.token;
3983
4015
  const args = annotationTokens.args;
@@ -3999,6 +4031,7 @@ var AtscriptDoc = class {
3999
4031
  const dotIdx = refToken.text.indexOf(".");
4000
4032
  if (dotIdx > 0) {
4001
4033
  const typeToken = refToken.clone({ text: refToken.text.slice(0, dotIdx) });
4034
+ typeToken.annotationRef = refToken.annotationRef;
4002
4035
  typeToken.isReference = true;
4003
4036
  this.referred.push(typeToken);
4004
4037
  } else {
@@ -4150,6 +4183,40 @@ var AtscriptDoc = class {
4150
4183
  return block?.blockType === "annotate" && isAnnotate(block.parentNode) ? block.parentNode : void 0;
4151
4184
  }
4152
4185
  /**
4186
+ * The definition an annotated node stands for, with the document that declares it:
4187
+ * a prop / type declaration's own definition, or — for an `annotate` block entry —
4188
+ * the type of the target property the entry names (`annotate Host { name @x.y }`
4189
+ * → the type of `Host.name`). Plugin hooks use it to introspect the field an
4190
+ * annotation sits on, whether it is written inline or in an annotate block.
4191
+ * `undefined` when the node has no definition or the entry does not resolve.
4192
+ */ annotatedDefinition(node) {
4193
+ if (!node) return;
4194
+ if (!isRef(node)) {
4195
+ const def = node.getDefinition();
4196
+ return def ? {
4197
+ def,
4198
+ doc: this
4199
+ } : void 0;
4200
+ }
4201
+ const idToken = node.token("identifier");
4202
+ const block = idToken ? this.annotateBlockAt(idToken.range.start.line, idToken.range.start.character) : void 0;
4203
+ if (!block) return {
4204
+ def: node,
4205
+ doc: this
4206
+ };
4207
+ const chain = [node.id, ...node.chain.map((c) => c.text)];
4208
+ const unwound = this.unwindType(block.targetName, chain);
4209
+ if (unwound?.def && (!isGroup(unwound.def) || unwound.node?.id === chain.at(-1))) return {
4210
+ def: unwound.def,
4211
+ doc: unwound.doc
4212
+ };
4213
+ const merged = this.resolveChainWithMerge(block.targetName, chain);
4214
+ return merged ? {
4215
+ def: merged,
4216
+ doc: this
4217
+ } : void 0;
4218
+ }
4219
+ /**
4153
4220
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
4154
4221
  * Returns a map of property path (dot-joined) → annotations array.
4155
4222
  * For entries without a chain (root-level), the key is the entry identifier.
@@ -4417,22 +4484,51 @@ var AtscriptDoc = class {
4417
4484
  } : void 0;
4418
4485
  }
4419
4486
  }
4487
+ /** Pushes an "Unknown identifier" error when `t` is not defined; returns whether it was. */ reportUnknownIdentifier(t) {
4488
+ if (this.registry.isDefined(t)) return false;
4489
+ this._allMessages.push({
4490
+ severity: 1,
4491
+ message: `Unknown identifier "${t.text}"`,
4492
+ range: t.range
4493
+ });
4494
+ return true;
4495
+ }
4496
+ /** Definition link from `token` to `target`, or to what the token resolves to when omitted. */ definitionLink(token, target) {
4497
+ const resolved = target ?? this.toRangeTarget(this.getDefinitionFor(token));
4498
+ if (!resolved) return;
4499
+ const range = resolved.range ?? zeroRange;
4500
+ return [{
4501
+ targetUri: resolved.uri,
4502
+ targetRange: range,
4503
+ targetSelectionRange: range,
4504
+ originSelectionRange: token.range
4505
+ }];
4506
+ }
4507
+ toRangeTarget(def) {
4508
+ return def && {
4509
+ uri: def.uri,
4510
+ range: def.token?.range
4511
+ };
4512
+ }
4420
4513
  getToDefinitionAt(line, character) {
4421
4514
  const token = this.tokensIndex.at(line, character);
4422
4515
  if (token) {
4516
+ if (token.annotationRef && token.isReference) return this.definitionLink(token);
4517
+ const declared = getDeclaredValue(token, this)?.definition;
4518
+ if (declared) return this.definitionLink(token, {
4519
+ uri: declared.doc.id,
4520
+ range: declared.token.range
4521
+ });
4423
4522
  const annotateBlock = this.annotateBlockAt(line, character);
4424
4523
  if (annotateBlock && isRef(token.parentNode)) {
4425
4524
  const targetName = annotateBlock.targetName;
4426
4525
  const entryRef = token.parentNode;
4427
4526
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
4428
4527
  const unwound = this.unwindType(targetName, chain);
4429
- if (unwound?.node) return [{
4430
- targetUri: unwound.doc.id,
4431
- targetRange: unwound.node.token("identifier")?.range ?? zeroRange,
4432
- targetSelectionRange: unwound.node.token("identifier")?.range ?? zeroRange,
4433
- originSelectionRange: token.range
4434
- }];
4435
- return;
4528
+ return unwound?.node ? this.definitionLink(token, {
4529
+ uri: unwound.doc.id,
4530
+ range: unwound.node.token("identifier")?.range
4531
+ }) : void 0;
4436
4532
  }
4437
4533
  const fieldRef = resolveFieldRefAt(token, this, character);
4438
4534
  if (fieldRef) {
@@ -4446,41 +4542,21 @@ var AtscriptDoc = class {
4446
4542
  }
4447
4543
  if (isQueryFieldRef(token.parentNode)) {
4448
4544
  const { typeRef, queryArgToken } = token.parentNode;
4449
- if (token === typeRef && queryArgToken && getQueryScope(queryArgToken, this)) {
4450
- const result = this.getDefinitionFor(token);
4451
- return result ? [{
4452
- targetUri: result.uri,
4453
- targetRange: result.token?.range ?? zeroRange,
4454
- targetSelectionRange: result.token?.range ?? zeroRange,
4455
- originSelectionRange: token.range
4456
- }] : void 0;
4457
- }
4545
+ if (token === typeRef && queryArgToken && getQueryScope(queryArgToken, this)) return this.definitionLink(token);
4458
4546
  return;
4459
4547
  }
4460
- if (isProp(token.parentNode)) return [{
4461
- targetUri: this.id,
4462
- targetRange: token.range,
4463
- targetSelectionRange: token.range,
4464
- originSelectionRange: token.range
4465
- }];
4548
+ if (isProp(token.parentNode)) return this.definitionLink(token, {
4549
+ uri: this.id,
4550
+ range: token.range
4551
+ });
4466
4552
  if (token.isChain && isRef(token.parentNode) && typeof token.index === "number") {
4467
4553
  const id = token.parentNode.id;
4468
4554
  const unwound = this.unwindType(id, token.parentNode.chain.slice(0, token.index));
4469
- if (unwound?.node) return [{
4470
- targetUri: unwound.doc.id,
4471
- targetRange: unwound.node.token("identifier")?.range ?? zeroRange,
4472
- targetSelectionRange: unwound.node.token("identifier")?.range ?? zeroRange,
4473
- originSelectionRange: token.range
4474
- }];
4475
- } else {
4476
- const result = this.getDefinitionFor(token);
4477
- return result ? [{
4478
- targetUri: result.uri,
4479
- targetRange: result.token?.range ?? zeroRange,
4480
- targetSelectionRange: result.token?.range ?? zeroRange,
4481
- originSelectionRange: token.range
4482
- }] : void 0;
4483
- }
4555
+ if (unwound?.node) return this.definitionLink(token, {
4556
+ uri: unwound.doc.id,
4557
+ range: unwound.node.token("identifier")?.range
4558
+ });
4559
+ } else return this.definitionLink(token);
4484
4560
  }
4485
4561
  }
4486
4562
  registerImport({ from, imports, block }) {
@@ -4675,6 +4751,10 @@ var AtscriptDoc = class {
4675
4751
  }
4676
4752
  }
4677
4753
  for (const t of this.referred) {
4754
+ if (t.annotationRef || isQueryFieldRef(t.parentNode)) {
4755
+ this.reportUnknownIdentifier(t);
4756
+ continue;
4757
+ }
4678
4758
  const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4679
4759
  if (annotateBlock) {
4680
4760
  const targetName = annotateBlock.targetName;
@@ -4708,14 +4788,7 @@ var AtscriptDoc = class {
4708
4788
  }
4709
4789
  continue;
4710
4790
  }
4711
- if (!this.registry.isDefined(t)) {
4712
- this._allMessages.push({
4713
- severity: 1,
4714
- message: `Unknown identifier "${t.text}"`,
4715
- range: t.range
4716
- });
4717
- continue;
4718
- }
4791
+ if (this.reportUnknownIdentifier(t)) continue;
4719
4792
  if (isRef(t.parentNode)) {
4720
4793
  const def = this.unwindType(t.parentNode.id, t.parentNode.chain)?.def;
4721
4794
  if (isPrimitive(def) && !def.config.type) {
@@ -4763,6 +4836,10 @@ var AtscriptDoc = class {
4763
4836
  let def = this.unwindType(targetName)?.def;
4764
4837
  if (!def) return;
4765
4838
  for (const prop of chain) {
4839
+ if (isRef(def)) {
4840
+ def = this.unwindType(def.id, def.chain)?.def;
4841
+ if (!def) return;
4842
+ }
4766
4843
  def = this.mergeIntersection(def);
4767
4844
  if (isProp(def)) {
4768
4845
  const inner = def.getDefinition();
@@ -4965,6 +5042,10 @@ var AtscriptDoc = class {
4965
5042
  this.registry = new IdRegistry(Array.from(config.primitives?.keys() || []));
4966
5043
  }
4967
5044
  };
5045
+ /**
5046
+ * Annotation tokens survive merges across refs, chain refs and `extends`
5047
+ * unchanged, so they are keyed by identity to the document that declared them.
5048
+ */ _define_property$3(AtscriptDoc, "annotationOrigins", /* @__PURE__ */ new WeakMap());
4968
5049
  const zeroRange = {
4969
5050
  start: {
4970
5051
  line: 0,
@@ -6386,11 +6467,14 @@ exports.createAtscriptPlugin = createAtscriptPlugin;
6386
6467
  exports.defineConfig = defineConfig;
6387
6468
  exports.fileUriToPath = fileUriToPath;
6388
6469
  exports.flattenInterfaceNode = flattenInterfaceNode;
6470
+ exports.getDeclaredValue = getDeclaredValue;
6389
6471
  exports.getFieldPathCompletionScope = getFieldPathCompletionScope;
6390
6472
  exports.getFieldsForType = getFieldsForType;
6391
6473
  exports.getQueryCompletionScope = getQueryCompletionScope;
6392
6474
  exports.getQueryScope = getQueryScope;
6393
6475
  exports.getRelPath = getRelPath;
6476
+ exports.getSiblingAnnotation = getSiblingAnnotation;
6477
+ exports.getValueCandidates = getValueCandidates;
6394
6478
  exports.hasNavPropAnnotation = hasNavPropAnnotation;
6395
6479
  exports.isAnnotate = isAnnotate;
6396
6480
  exports.isAnnotationSpec = isAnnotationSpec;
package/dist/index.d.ts CHANGED
@@ -551,6 +551,18 @@ declare class AtscriptDoc {
551
551
  private registerNodes;
552
552
  resolvedAnnotations: Token[];
553
553
  annotations: TAnnotationTokens[];
554
+ /**
555
+ * Annotation tokens survive merges across refs, chain refs and `extends`
556
+ * unchanged, so they are keyed by identity to the document that declared them.
557
+ */
558
+ private static readonly annotationOrigins;
559
+ /**
560
+ * The document in which the given annotation was written (and whose imports
561
+ * its type-reference arguments resolve against). Annotations inherited into
562
+ * another document through a chain ref, `extends` or `annotate` keep the
563
+ * declaring document. `undefined` for annotations never registered on a doc.
564
+ */
565
+ annotationOrigin(annotation: TAnnotationTokens): AtscriptDoc | undefined;
554
566
  registerAnnotation(annotationTokens: TAnnotationTokens): void;
555
567
  /**
556
568
  * Recursively resolves a type reference (and any property chain) to find the final underlying definition.
@@ -597,6 +609,18 @@ declare class AtscriptDoc {
597
609
  * resolve through the target interface rather than as top-level definitions.
598
610
  */
599
611
  annotateBlockAt(line: number, character: number): SemanticAnnotateNode | undefined;
612
+ /**
613
+ * The definition an annotated node stands for, with the document that declares it:
614
+ * a prop / type declaration's own definition, or — for an `annotate` block entry —
615
+ * the type of the target property the entry names (`annotate Host { name @x.y }`
616
+ * → the type of `Host.name`). Plugin hooks use it to introspect the field an
617
+ * annotation sits on, whether it is written inline or in an annotate block.
618
+ * `undefined` when the node has no definition or the entry does not resolve.
619
+ */
620
+ annotatedDefinition(node: SemanticNode | undefined): {
621
+ def: SemanticNode;
622
+ doc: AtscriptDoc;
623
+ } | undefined;
600
624
  /**
601
625
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
602
626
  * Returns a map of property path (dot-joined) → annotations array.
@@ -663,6 +687,11 @@ declare class AtscriptDoc {
663
687
  doc?: AtscriptDoc;
664
688
  token?: Token;
665
689
  } | undefined;
690
+ /** Pushes an "Unknown identifier" error when `t` is not defined; returns whether it was. */
691
+ private reportUnknownIdentifier;
692
+ /** Definition link from `token` to `target`, or to what the token resolves to when omitted. */
693
+ private definitionLink;
694
+ private toRangeTarget;
666
695
  getToDefinitionAt(line: number, character: number): {
667
696
  targetUri: string;
668
697
  targetRange: {
@@ -1013,6 +1042,21 @@ interface TQueryScope {
1013
1042
  type TBacktickArgKind = 'query' | 'expr' | 'order';
1014
1043
  /** Whether an annotation argument type is written in backticks (`query`, `expr`, `order`). */
1015
1044
  declare function isBacktickArgType(type: string | undefined): type is TBacktickArgKind;
1045
+ /**
1046
+ * One value a `string` / `number` annotation argument may take (returned by the `valueScope`
1047
+ * hook), for editor completion and go-to-definition.
1048
+ */
1049
+ interface TValueCandidate {
1050
+ /** The argument value as written without quotes (`open` for `'open'`). */
1051
+ value: string;
1052
+ /** Where the value is declared; go-to-definition on the argument jumps there. */
1053
+ definition?: {
1054
+ doc: AtscriptDoc;
1055
+ token: Token;
1056
+ };
1057
+ /** Shown beside the value in the completion list. */
1058
+ documentation?: string;
1059
+ }
1016
1060
  interface TAnnotationArgument {
1017
1061
  optional?: boolean;
1018
1062
  name: string;
@@ -1035,6 +1079,15 @@ interface TAnnotationArgument {
1035
1079
  * `'address.city'`) — same editor features on the string; `allowedTypes` is unused.
1036
1080
  */
1037
1081
  fieldScope?: (argToken: Token, doc: AtscriptDoc) => TQueryScope | undefined;
1082
+ /**
1083
+ * Editor value scope of a `string` or `number` argument that names one of a closed set of
1084
+ * values declared elsewhere in the document (the literals of the annotated field's union,
1085
+ * say). Called with the annotation's main token (its `parentNode` is the annotated node)
1086
+ * and the document that holds it; return `undefined` when the set cannot be determined.
1087
+ * Completion offers the candidates, go-to-definition on the argument follows
1088
+ * `TValueCandidate.definition`. Candidates are advisory — it does not validate.
1089
+ */
1090
+ valueScope?: (annotationToken: Token, doc: AtscriptDoc) => TValueCandidate[] | undefined;
1038
1091
  /**
1039
1092
  * For a `ref` argument: filters the type names offered by completion.
1040
1093
  * Called with each candidate declaration and the document that declares it;
@@ -1214,6 +1267,22 @@ interface TFieldRefTarget {
1214
1267
  /** Document that declares `prop`. */
1215
1268
  doc: AtscriptDoc;
1216
1269
  }
1270
+ /**
1271
+ * The annotation an argument token belongs to, found among the annotations of the annotated
1272
+ * node (`argToken.parentNode`, a prop or an annotate entry). For `fieldScope` hooks that scope
1273
+ * an argument by its siblings (`getSiblingAnnotation(arg)?.args[0]?.text`).
1274
+ */
1275
+ declare function getSiblingAnnotation(argToken: Token): TAnnotationTokens | undefined;
1276
+ /**
1277
+ * The values an annotation argument may take, from the `valueScope` hook of its spec
1278
+ * (`undefined` when the argument declares none or the set cannot be determined).
1279
+ */
1280
+ declare function getValueCandidates(annotationToken: Token, argIndex: number, doc: AtscriptDoc): TValueCandidate[] | undefined;
1281
+ /**
1282
+ * The declared value a value-argument token names (`valueScope`), or `undefined` when the token
1283
+ * is not a value argument or names no candidate.
1284
+ */
1285
+ declare function getDeclaredValue(token: Token, doc: AtscriptDoc): TValueCandidate | undefined;
1217
1286
  /**
1218
1287
  * Field scope of an annotation argument: the answer of the argument spec's `fieldScope`
1219
1288
  * hook when it declares one, otherwise the deprecated built-in `@db.*` query rules.
@@ -1302,5 +1371,5 @@ declare const BACKTICK_ARG_VOCABULARY: {
1302
1371
  readonly orderDirections: readonly ["asc", "desc"];
1303
1372
  };
1304
1373
 
1305
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BACKTICK_ARG_VOCABULARY, BuildRepo, DB_ENTITY_ANNOTATIONS, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticExprBinaryNode, SemanticExprCallNode, SemanticExprNode, SemanticExprNumberNode, SemanticExprUnaryNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticOrderNode, SemanticPrimitiveNode, SemanticPropNode, SemanticQueryComparisonNode, SemanticQueryFieldRefNode, SemanticQueryLogicalNode, SemanticQueryNode, SemanticQueryValueListNode, SemanticQueryValueNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, analyzeQueryCursorContext, build, clearResolveBareCache, createAtscriptPlugin, defineConfig, fileUriToPath, flattenInterfaceNode, getFieldPathCompletionScope, getFieldsForType, getQueryCompletionScope, getQueryScope, getRelPath, hasNavPropAnnotation, isAnnotate, isAnnotationSpec, isArray, isBacktickArgType, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isQueryComparison, isQueryFieldRef, isQueryLogical, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, parseBareSpecifier, resolveAnnotation, resolveAtscriptFromPath, resolveBareSpecifier, resolveConfigFile, resolveFieldRefAt, resolveQueryFieldRef };
1306
- export type { SemanticExprItemNode, SemanticQueryExprNode, TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TBacktickArgKind, TDbConfig, TDbConfigDeclarative, TExprBinaryOperator, TFlatFieldDescriptor, TFlattenOptions, TMessages, TNodeEntity, TOrderItem, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TQueryCursorContext, TQueryLogicalOperator, TQueryOperator, TQueryScope, TSemanticToken };
1374
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BACKTICK_ARG_VOCABULARY, BuildRepo, DB_ENTITY_ANNOTATIONS, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticExprBinaryNode, SemanticExprCallNode, SemanticExprNode, SemanticExprNumberNode, SemanticExprUnaryNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticOrderNode, SemanticPrimitiveNode, SemanticPropNode, SemanticQueryComparisonNode, SemanticQueryFieldRefNode, SemanticQueryLogicalNode, SemanticQueryNode, SemanticQueryValueListNode, SemanticQueryValueNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, analyzeQueryCursorContext, build, clearResolveBareCache, createAtscriptPlugin, defineConfig, fileUriToPath, flattenInterfaceNode, getDeclaredValue, getFieldPathCompletionScope, getFieldsForType, getQueryCompletionScope, getQueryScope, getRelPath, getSiblingAnnotation, getValueCandidates, hasNavPropAnnotation, isAnnotate, isAnnotationSpec, isArray, isBacktickArgType, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isQueryComparison, isQueryFieldRef, isQueryLogical, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, parseBareSpecifier, resolveAnnotation, resolveAtscriptFromPath, resolveBareSpecifier, resolveConfigFile, resolveFieldRefAt, resolveQueryFieldRef };
1375
+ export type { SemanticExprItemNode, SemanticQueryExprNode, TAnnotationArgument, TAnnotationSpecConfig, TAnnotationTokens, TAnnotationsTree, TAtscriptConfig, TAtscriptConfigInput, TAtscriptConfigOutput, TAtscriptDocConfig, TAtscriptPlugin, TAtscriptRenderFormat, TBacktickArgKind, TDbConfig, TDbConfigDeclarative, TExprBinaryOperator, TFlatFieldDescriptor, TFlattenOptions, TMessages, TNodeEntity, TOrderItem, TOutput, TOutputWithSource, TPluginManagers, TPluginOutput, TPrimitiveAnnotationArg, TPrimitiveAnnotationArgs, TPrimitiveAnnotationValue, TPrimitiveBaseConfig, TPrimitiveConfig, TPrimitiveTypeArray, TPrimitiveTypeComplex, TPrimitiveTypeDef, TPrimitiveTypeFinal, TPrimitiveTypeFinalOptional, TPrimitiveTypeObject, TQueryCursorContext, TQueryLogicalOperator, TQueryOperator, TQueryScope, TSemanticToken, TValueCandidate };
package/dist/index.mjs CHANGED
@@ -2184,10 +2184,33 @@ function getRelFilterScope(owner) {
2184
2184
 
2185
2185
  //#endregion
2186
2186
  //#region packages/core/src/lsp/field-refs.ts
2187
+ function getArgSpecAt(annotationToken, index, doc) {
2188
+ return doc.resolveAnnotation(annotationToken.text.slice(1))?.arguments[index];
2189
+ }
2187
2190
  function getArgSpec(argToken, doc) {
2188
2191
  const annotationRef = argToken.annotationRef;
2189
2192
  if (!annotationRef || typeof argToken.index !== "number") return;
2190
- return doc.resolveAnnotation(annotationRef.text.slice(1))?.arguments[argToken.index];
2193
+ return getArgSpecAt(annotationRef, argToken.index, doc);
2194
+ }
2195
+ /**
2196
+ * The annotation an argument token belongs to, found among the annotations of the annotated
2197
+ * node (`argToken.parentNode`, a prop or an annotate entry). For `fieldScope` hooks that scope
2198
+ * an argument by its siblings (`getSiblingAnnotation(arg)?.args[0]?.text`).
2199
+ */ function getSiblingAnnotation(argToken) {
2200
+ return argToken.parentNode?.annotations?.find((a) => a.args.includes(argToken));
2201
+ }
2202
+ /**
2203
+ * The values an annotation argument may take, from the `valueScope` hook of its spec
2204
+ * (`undefined` when the argument declares none or the set cannot be determined).
2205
+ */ function getValueCandidates(annotationToken, argIndex, doc) {
2206
+ return getArgSpecAt(annotationToken, argIndex, doc)?.valueScope?.(annotationToken, doc);
2207
+ }
2208
+ /**
2209
+ * The declared value a value-argument token names (`valueScope`), or `undefined` when the token
2210
+ * is not a value argument or names no candidate.
2211
+ */ function getDeclaredValue(token, doc) {
2212
+ if (!token.annotationRef || typeof token.index !== "number" || token.isReference) return;
2213
+ return getValueCandidates(token.annotationRef, token.index, doc)?.find((c) => c.value === token.text);
2191
2214
  }
2192
2215
  /**
2193
2216
  * Field scope of an annotation argument: the answer of the argument spec's `fieldScope`
@@ -3815,7 +3838,7 @@ function _define_property$3(obj, key, value) {
3815
3838
  else obj[key] = value;
3816
3839
  return obj;
3817
3840
  }
3818
- var AtscriptDoc = class {
3841
+ var AtscriptDoc = class AtscriptDoc {
3819
3842
  get name() {
3820
3843
  return this.id.split("/").pop();
3821
3844
  }
@@ -3946,7 +3969,16 @@ var AtscriptDoc = class {
3946
3969
  });
3947
3970
  }
3948
3971
  }
3972
+ /**
3973
+ * The document in which the given annotation was written (and whose imports
3974
+ * its type-reference arguments resolve against). Annotations inherited into
3975
+ * another document through a chain ref, `extends` or `annotate` keep the
3976
+ * declaring document. `undefined` for annotations never registered on a doc.
3977
+ */ annotationOrigin(annotation) {
3978
+ return AtscriptDoc.annotationOrigins.get(annotation);
3979
+ }
3949
3980
  registerAnnotation(annotationTokens) {
3981
+ AtscriptDoc.annotationOrigins.set(annotationTokens, this);
3950
3982
  this.annotations.push(annotationTokens);
3951
3983
  const mainToken = annotationTokens.token;
3952
3984
  const args = annotationTokens.args;
@@ -3968,6 +4000,7 @@ var AtscriptDoc = class {
3968
4000
  const dotIdx = refToken.text.indexOf(".");
3969
4001
  if (dotIdx > 0) {
3970
4002
  const typeToken = refToken.clone({ text: refToken.text.slice(0, dotIdx) });
4003
+ typeToken.annotationRef = refToken.annotationRef;
3971
4004
  typeToken.isReference = true;
3972
4005
  this.referred.push(typeToken);
3973
4006
  } else {
@@ -4119,6 +4152,40 @@ var AtscriptDoc = class {
4119
4152
  return block?.blockType === "annotate" && isAnnotate(block.parentNode) ? block.parentNode : void 0;
4120
4153
  }
4121
4154
  /**
4155
+ * The definition an annotated node stands for, with the document that declares it:
4156
+ * a prop / type declaration's own definition, or — for an `annotate` block entry —
4157
+ * the type of the target property the entry names (`annotate Host { name @x.y }`
4158
+ * → the type of `Host.name`). Plugin hooks use it to introspect the field an
4159
+ * annotation sits on, whether it is written inline or in an annotate block.
4160
+ * `undefined` when the node has no definition or the entry does not resolve.
4161
+ */ annotatedDefinition(node) {
4162
+ if (!node) return;
4163
+ if (!isRef(node)) {
4164
+ const def = node.getDefinition();
4165
+ return def ? {
4166
+ def,
4167
+ doc: this
4168
+ } : void 0;
4169
+ }
4170
+ const idToken = node.token("identifier");
4171
+ const block = idToken ? this.annotateBlockAt(idToken.range.start.line, idToken.range.start.character) : void 0;
4172
+ if (!block) return {
4173
+ def: node,
4174
+ doc: this
4175
+ };
4176
+ const chain = [node.id, ...node.chain.map((c) => c.text)];
4177
+ const unwound = this.unwindType(block.targetName, chain);
4178
+ if (unwound?.def && (!isGroup(unwound.def) || unwound.node?.id === chain.at(-1))) return {
4179
+ def: unwound.def,
4180
+ doc: unwound.doc
4181
+ };
4182
+ const merged = this.resolveChainWithMerge(block.targetName, chain);
4183
+ return merged ? {
4184
+ def: merged,
4185
+ doc: this
4186
+ } : void 0;
4187
+ }
4188
+ /**
4122
4189
  * Collects ad-hoc annotations from all `annotate` blocks targeting a given type/interface.
4123
4190
  * Returns a map of property path (dot-joined) → annotations array.
4124
4191
  * For entries without a chain (root-level), the key is the entry identifier.
@@ -4386,22 +4453,51 @@ var AtscriptDoc = class {
4386
4453
  } : void 0;
4387
4454
  }
4388
4455
  }
4456
+ /** Pushes an "Unknown identifier" error when `t` is not defined; returns whether it was. */ reportUnknownIdentifier(t) {
4457
+ if (this.registry.isDefined(t)) return false;
4458
+ this._allMessages.push({
4459
+ severity: 1,
4460
+ message: `Unknown identifier "${t.text}"`,
4461
+ range: t.range
4462
+ });
4463
+ return true;
4464
+ }
4465
+ /** Definition link from `token` to `target`, or to what the token resolves to when omitted. */ definitionLink(token, target) {
4466
+ const resolved = target ?? this.toRangeTarget(this.getDefinitionFor(token));
4467
+ if (!resolved) return;
4468
+ const range = resolved.range ?? zeroRange;
4469
+ return [{
4470
+ targetUri: resolved.uri,
4471
+ targetRange: range,
4472
+ targetSelectionRange: range,
4473
+ originSelectionRange: token.range
4474
+ }];
4475
+ }
4476
+ toRangeTarget(def) {
4477
+ return def && {
4478
+ uri: def.uri,
4479
+ range: def.token?.range
4480
+ };
4481
+ }
4389
4482
  getToDefinitionAt(line, character) {
4390
4483
  const token = this.tokensIndex.at(line, character);
4391
4484
  if (token) {
4485
+ if (token.annotationRef && token.isReference) return this.definitionLink(token);
4486
+ const declared = getDeclaredValue(token, this)?.definition;
4487
+ if (declared) return this.definitionLink(token, {
4488
+ uri: declared.doc.id,
4489
+ range: declared.token.range
4490
+ });
4392
4491
  const annotateBlock = this.annotateBlockAt(line, character);
4393
4492
  if (annotateBlock && isRef(token.parentNode)) {
4394
4493
  const targetName = annotateBlock.targetName;
4395
4494
  const entryRef = token.parentNode;
4396
4495
  const chain = token.isChain && typeof token.index === "number" ? [entryRef.id, ...entryRef.chain.slice(0, token.index).map((c) => c.text)] : [token.text];
4397
4496
  const unwound = this.unwindType(targetName, chain);
4398
- if (unwound?.node) return [{
4399
- targetUri: unwound.doc.id,
4400
- targetRange: unwound.node.token("identifier")?.range ?? zeroRange,
4401
- targetSelectionRange: unwound.node.token("identifier")?.range ?? zeroRange,
4402
- originSelectionRange: token.range
4403
- }];
4404
- return;
4497
+ return unwound?.node ? this.definitionLink(token, {
4498
+ uri: unwound.doc.id,
4499
+ range: unwound.node.token("identifier")?.range
4500
+ }) : void 0;
4405
4501
  }
4406
4502
  const fieldRef = resolveFieldRefAt(token, this, character);
4407
4503
  if (fieldRef) {
@@ -4415,41 +4511,21 @@ var AtscriptDoc = class {
4415
4511
  }
4416
4512
  if (isQueryFieldRef(token.parentNode)) {
4417
4513
  const { typeRef, queryArgToken } = token.parentNode;
4418
- if (token === typeRef && queryArgToken && getQueryScope(queryArgToken, this)) {
4419
- const result = this.getDefinitionFor(token);
4420
- return result ? [{
4421
- targetUri: result.uri,
4422
- targetRange: result.token?.range ?? zeroRange,
4423
- targetSelectionRange: result.token?.range ?? zeroRange,
4424
- originSelectionRange: token.range
4425
- }] : void 0;
4426
- }
4514
+ if (token === typeRef && queryArgToken && getQueryScope(queryArgToken, this)) return this.definitionLink(token);
4427
4515
  return;
4428
4516
  }
4429
- if (isProp(token.parentNode)) return [{
4430
- targetUri: this.id,
4431
- targetRange: token.range,
4432
- targetSelectionRange: token.range,
4433
- originSelectionRange: token.range
4434
- }];
4517
+ if (isProp(token.parentNode)) return this.definitionLink(token, {
4518
+ uri: this.id,
4519
+ range: token.range
4520
+ });
4435
4521
  if (token.isChain && isRef(token.parentNode) && typeof token.index === "number") {
4436
4522
  const id = token.parentNode.id;
4437
4523
  const unwound = this.unwindType(id, token.parentNode.chain.slice(0, token.index));
4438
- if (unwound?.node) return [{
4439
- targetUri: unwound.doc.id,
4440
- targetRange: unwound.node.token("identifier")?.range ?? zeroRange,
4441
- targetSelectionRange: unwound.node.token("identifier")?.range ?? zeroRange,
4442
- originSelectionRange: token.range
4443
- }];
4444
- } else {
4445
- const result = this.getDefinitionFor(token);
4446
- return result ? [{
4447
- targetUri: result.uri,
4448
- targetRange: result.token?.range ?? zeroRange,
4449
- targetSelectionRange: result.token?.range ?? zeroRange,
4450
- originSelectionRange: token.range
4451
- }] : void 0;
4452
- }
4524
+ if (unwound?.node) return this.definitionLink(token, {
4525
+ uri: unwound.doc.id,
4526
+ range: unwound.node.token("identifier")?.range
4527
+ });
4528
+ } else return this.definitionLink(token);
4453
4529
  }
4454
4530
  }
4455
4531
  registerImport({ from, imports, block }) {
@@ -4644,6 +4720,10 @@ var AtscriptDoc = class {
4644
4720
  }
4645
4721
  }
4646
4722
  for (const t of this.referred) {
4723
+ if (t.annotationRef || isQueryFieldRef(t.parentNode)) {
4724
+ this.reportUnknownIdentifier(t);
4725
+ continue;
4726
+ }
4647
4727
  const annotateBlock = this.annotateBlockAt(t.range.start.line, t.range.start.character);
4648
4728
  if (annotateBlock) {
4649
4729
  const targetName = annotateBlock.targetName;
@@ -4677,14 +4757,7 @@ var AtscriptDoc = class {
4677
4757
  }
4678
4758
  continue;
4679
4759
  }
4680
- if (!this.registry.isDefined(t)) {
4681
- this._allMessages.push({
4682
- severity: 1,
4683
- message: `Unknown identifier "${t.text}"`,
4684
- range: t.range
4685
- });
4686
- continue;
4687
- }
4760
+ if (this.reportUnknownIdentifier(t)) continue;
4688
4761
  if (isRef(t.parentNode)) {
4689
4762
  const def = this.unwindType(t.parentNode.id, t.parentNode.chain)?.def;
4690
4763
  if (isPrimitive(def) && !def.config.type) {
@@ -4732,6 +4805,10 @@ var AtscriptDoc = class {
4732
4805
  let def = this.unwindType(targetName)?.def;
4733
4806
  if (!def) return;
4734
4807
  for (const prop of chain) {
4808
+ if (isRef(def)) {
4809
+ def = this.unwindType(def.id, def.chain)?.def;
4810
+ if (!def) return;
4811
+ }
4735
4812
  def = this.mergeIntersection(def);
4736
4813
  if (isProp(def)) {
4737
4814
  const inner = def.getDefinition();
@@ -4934,6 +5011,10 @@ var AtscriptDoc = class {
4934
5011
  this.registry = new IdRegistry(Array.from(config.primitives?.keys() || []));
4935
5012
  }
4936
5013
  };
5014
+ /**
5015
+ * Annotation tokens survive merges across refs, chain refs and `extends`
5016
+ * unchanged, so they are keyed by identity to the document that declared them.
5017
+ */ _define_property$3(AtscriptDoc, "annotationOrigins", /* @__PURE__ */ new WeakMap());
4937
5018
  const zeroRange = {
4938
5019
  start: {
4939
5020
  line: 0,
@@ -6313,4 +6394,4 @@ function walkArrayElement(doc, arrayNode, prefix, parentOptional, flatMap, stack
6313
6394
  }
6314
6395
 
6315
6396
  //#endregion
6316
- export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BACKTICK_ARG_VOCABULARY, BuildRepo, DB_ENTITY_ANNOTATIONS, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticExprBinaryNode, SemanticExprCallNode, SemanticExprNode, SemanticExprNumberNode, SemanticExprUnaryNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticOrderNode, SemanticPrimitiveNode, SemanticPropNode, SemanticQueryComparisonNode, SemanticQueryFieldRefNode, SemanticQueryLogicalNode, SemanticQueryNode, SemanticQueryValueListNode, SemanticQueryValueNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, analyzeQueryCursorContext, build, clearResolveBareCache, createAtscriptPlugin, defineConfig, fileUriToPath, flattenInterfaceNode, getFieldPathCompletionScope, getFieldsForType, getQueryCompletionScope, getQueryScope, getRelPath, hasNavPropAnnotation, isAnnotate, isAnnotationSpec, isArray, isBacktickArgType, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isQueryComparison, isQueryFieldRef, isQueryLogical, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, parseBareSpecifier, resolveAnnotation, resolveAtscriptFromPath, resolveBareSpecifier, resolveConfigFile, resolveFieldRefAt, resolveQueryFieldRef };
6397
+ export { $n, AnnotationSpec, AtscriptDoc, AtscriptRepo, BACKTICK_ARG_VOCABULARY, BuildRepo, DB_ENTITY_ANNOTATIONS, DEFAULT_FORMAT, PluginManager, SemanticAnnotateNode, SemanticArrayNode, SemanticConstNode, SemanticExprBinaryNode, SemanticExprCallNode, SemanticExprNode, SemanticExprNumberNode, SemanticExprUnaryNode, SemanticGroup, SemanticImportNode, SemanticInterfaceNode, SemanticNode, SemanticOrderNode, SemanticPrimitiveNode, SemanticPropNode, SemanticQueryComparisonNode, SemanticQueryFieldRefNode, SemanticQueryLogicalNode, SemanticQueryNode, SemanticQueryValueListNode, SemanticQueryValueNode, SemanticRefNode, SemanticStructureNode, SemanticTupleNode, SemanticTypeNode, Token, analyzeQueryCursorContext, build, clearResolveBareCache, createAtscriptPlugin, defineConfig, fileUriToPath, flattenInterfaceNode, getDeclaredValue, getFieldPathCompletionScope, getFieldsForType, getQueryCompletionScope, getQueryScope, getRelPath, getSiblingAnnotation, getValueCandidates, hasNavPropAnnotation, isAnnotate, isAnnotationSpec, isArray, isBacktickArgType, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isImport, isInterface, isPhantomNode, isPrimitive, isProp, isQueryComparison, isQueryFieldRef, isQueryLogical, isRef, isStructure, isTuple, isType, loadConfig, loadTsConfig, parseBareSpecifier, resolveAnnotation, resolveAtscriptFromPath, resolveBareSpecifier, resolveConfigFile, resolveFieldRefAt, resolveQueryFieldRef };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.99",
3
+ "version": "0.1.100",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",