@atscript/core 0.1.106 → 0.1.108

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
@@ -1640,7 +1640,7 @@ var NodeIterator = class NodeIterator {
1640
1640
  return new NodeIterator(nodes || this.nodes, this.messages, this.badNodes, this.issues, nodes ? this.$ : this.parent, nodes ? 0 : this.i).update();
1641
1641
  }
1642
1642
  skip(pun) {
1643
- while (this.$?.type === "comment" || this.$?.type === "punctuation" && pun?.length && pun.includes(this.$.text)) this.move();
1643
+ while (this.$?.type === "punctuation" && pun?.includes(this.$.text)) this.move();
1644
1644
  return this;
1645
1645
  }
1646
1646
  skipUntil(pun) {
@@ -3010,42 +3010,63 @@ QueryBlockToken.recognize(QueryRegExpToken, QueryOperatorToken, QueryBlockToken,
3010
3010
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
3011
3011
  * Replaces the v0.5 mapContent('children', callback) + global loop.
3012
3012
  *
3013
+ * Comments are dropped here, so the parser never sees them; their spans go to
3014
+ * `comments` (in source order) when given. A block comment spanning a line break
3015
+ * becomes a synthetic `'\n'` punctuation token, so it ends a line like the break
3016
+ * itself would.
3017
+ *
3013
3018
  * v0.6 Position has { offset, line, column }.
3014
3019
  * line is 1-based, column is 1-based.
3015
3020
  * VSCode expects 0-based line, 0-based character.
3016
- */ function extractTokens(node) {
3021
+ */ function extractTokens(node, comments) {
3017
3022
  const content = node.content;
3018
- return content.map((item, index) => {
3023
+ const tokens = [];
3024
+ content.forEach((item, index) => {
3019
3025
  if (typeof item === "string") {
3020
3026
  const prev = content[index - 1];
3021
3027
  const next = content[index + 1];
3022
3028
  const start = typeof prev === "object" ? prev.end : node.start;
3023
3029
  const end = typeof next === "object" ? next.start : node.end;
3024
- return {
3030
+ tokens.push({
3025
3031
  type: "unknown",
3026
3032
  text: item,
3027
3033
  getRange: () => ({
3028
3034
  start: toPos(start),
3029
3035
  end: toPos(end)
3030
3036
  })
3031
- };
3037
+ });
3038
+ return;
3032
3039
  }
3033
3040
  const data = item.data;
3034
3041
  data.getRange = () => ({
3035
3042
  start: toPos(item.start, data.startOffset),
3036
3043
  end: toPos(item.end, data.endOffset)
3037
3044
  });
3038
- data.children = extractTokens(item);
3039
- return data;
3045
+ if (data.type === "comment") {
3046
+ const closed = item.node === commentNodes.block && item.end.offset - item.start.offset === data.text.length + 4;
3047
+ comments?.push({
3048
+ start: item.start.offset,
3049
+ end: item.end.offset + (closed ? 0 : 1)
3050
+ });
3051
+ if (/[\n\r]/u.test(data.text)) tokens.push({
3052
+ type: "punctuation",
3053
+ text: "\n",
3054
+ getRange: data.getRange
3055
+ });
3056
+ return;
3057
+ }
3058
+ data.children = extractTokens(item, comments);
3059
+ tokens.push(data);
3040
3060
  });
3061
+ return tokens;
3041
3062
  }
3042
3063
 
3043
3064
  //#endregion
3044
3065
  //#region packages/core/src/tokenizer/index.ts
3045
- function tokenize(source, debug = false) {
3066
+ /** Lexes `source`; comment spans are collected into `comments` when given. */ function tokenize(source, debug = false, comments) {
3046
3067
  const result = root.parse(source);
3047
3068
  if (debug) console.log((0, _prostojs_parser.printTree)(result));
3048
- return extractTokens(result);
3069
+ return extractTokens(result, comments);
3049
3070
  }
3050
3071
 
3051
3072
  //#endregion
@@ -3417,7 +3438,7 @@ function annotations() {
3417
3438
  ni.accepted();
3418
3439
  ni.move();
3419
3440
  }
3420
- while (ni.satisfies(...opts.argument)) {
3441
+ while (ni.$ && ni.satisfies(...opts.argument)) {
3421
3442
  let argToken = new Token(ni.$);
3422
3443
  ni.accepted();
3423
3444
  ni.move();
@@ -3729,10 +3750,6 @@ const importPipe = $pipe("import", [
3729
3750
  const annotateEntry = $pipe("ref", [
3730
3751
  annotations(),
3731
3752
  refWithChain(),
3732
- { handler(ni) {
3733
- ni.skip();
3734
- return true;
3735
- } },
3736
3753
  pun(";", ",", "\n").orEob().lookBehind()
3737
3754
  ]).skip("\n", ";", ",");
3738
3755
  const annotatePipe = $pipe("annotate", [
@@ -3996,10 +4013,22 @@ var AtscriptDoc = class AtscriptDoc {
3996
4013
  get text() {
3997
4014
  return this._text;
3998
4015
  }
4016
+ /** Whether `offset` into `text` falls inside a comment (e.g. a cursor in `// …`). */ isInComment(offset) {
4017
+ let lo = 0;
4018
+ let hi = this.commentSpans.length;
4019
+ while (lo < hi) {
4020
+ const mid = lo + hi >>> 1;
4021
+ if (this.commentSpans[mid].start < offset) lo = mid + 1;
4022
+ else hi = mid;
4023
+ }
4024
+ const span = this.commentSpans[lo - 1];
4025
+ return span !== void 0 && offset < span.end;
4026
+ }
3999
4027
  update(text, debug = false) {
4000
4028
  this._text = text;
4001
4029
  this.cleanup();
4002
- const rawTokens = tokenize(text, debug);
4030
+ this.commentSpans = [];
4031
+ const rawTokens = tokenize(text, debug, this.commentSpans);
4003
4032
  const ni = new NodeIterator(rawTokens, []).move();
4004
4033
  this.nodes = runPipes([
4005
4034
  pipes.importPipe,
@@ -5106,6 +5135,7 @@ var AtscriptDoc = class AtscriptDoc {
5106
5135
  _define_property$3(this, "nodes", void 0);
5107
5136
  _define_property$3(this, "_text", void 0);
5108
5137
  _define_property$3(this, "_annotateNodesByTarget", void 0);
5138
+ /** Comment spans of `text`, in source order. */ _define_property$3(this, "commentSpans", void 0);
5109
5139
  /** Lexical backtick tokens already parsed by `registerAnnotation`. */ _define_property$3(this, "parsedBacktickArgs", void 0);
5110
5140
  _define_property$3(this, "resolvedAnnotations", void 0);
5111
5141
  _define_property$3(this, "annotations", void 0);
@@ -5131,6 +5161,7 @@ var AtscriptDoc = class AtscriptDoc {
5131
5161
  this._annotationSpecCache = /* @__PURE__ */ new Map();
5132
5162
  this.nodes = [];
5133
5163
  this._text = "";
5164
+ this.commentSpans = [];
5134
5165
  this.parsedBacktickArgs = /* @__PURE__ */ new WeakSet();
5135
5166
  this.resolvedAnnotations = [];
5136
5167
  this.annotations = [];
package/dist/index.d.ts CHANGED
@@ -538,6 +538,10 @@ declare class AtscriptDoc {
538
538
  private _text;
539
539
  private _annotateNodesByTarget;
540
540
  get text(): string;
541
+ /** Comment spans of `text`, in source order. */
542
+ private commentSpans;
543
+ /** Whether `offset` into `text` falls inside a comment (e.g. a cursor in `// …`). */
544
+ isInComment(offset: number): boolean;
541
545
  update(text: string, debug?: boolean): void;
542
546
  /** Lexical backtick tokens already parsed by `registerAnnotation`. */
543
547
  private parsedBacktickArgs;
package/dist/index.mjs CHANGED
@@ -1609,7 +1609,7 @@ var NodeIterator = class NodeIterator {
1609
1609
  return new NodeIterator(nodes || this.nodes, this.messages, this.badNodes, this.issues, nodes ? this.$ : this.parent, nodes ? 0 : this.i).update();
1610
1610
  }
1611
1611
  skip(pun) {
1612
- while (this.$?.type === "comment" || this.$?.type === "punctuation" && pun?.length && pun.includes(this.$.text)) this.move();
1612
+ while (this.$?.type === "punctuation" && pun?.includes(this.$.text)) this.move();
1613
1613
  return this;
1614
1614
  }
1615
1615
  skipUntil(pun) {
@@ -2979,42 +2979,63 @@ QueryBlockToken.recognize(QueryRegExpToken, QueryOperatorToken, QueryBlockToken,
2979
2979
  * Recursively extract TLexicalToken[] from a ParsedNode tree.
2980
2980
  * Replaces the v0.5 mapContent('children', callback) + global loop.
2981
2981
  *
2982
+ * Comments are dropped here, so the parser never sees them; their spans go to
2983
+ * `comments` (in source order) when given. A block comment spanning a line break
2984
+ * becomes a synthetic `'\n'` punctuation token, so it ends a line like the break
2985
+ * itself would.
2986
+ *
2982
2987
  * v0.6 Position has { offset, line, column }.
2983
2988
  * line is 1-based, column is 1-based.
2984
2989
  * VSCode expects 0-based line, 0-based character.
2985
- */ function extractTokens(node) {
2990
+ */ function extractTokens(node, comments) {
2986
2991
  const content = node.content;
2987
- return content.map((item, index) => {
2992
+ const tokens = [];
2993
+ content.forEach((item, index) => {
2988
2994
  if (typeof item === "string") {
2989
2995
  const prev = content[index - 1];
2990
2996
  const next = content[index + 1];
2991
2997
  const start = typeof prev === "object" ? prev.end : node.start;
2992
2998
  const end = typeof next === "object" ? next.start : node.end;
2993
- return {
2999
+ tokens.push({
2994
3000
  type: "unknown",
2995
3001
  text: item,
2996
3002
  getRange: () => ({
2997
3003
  start: toPos(start),
2998
3004
  end: toPos(end)
2999
3005
  })
3000
- };
3006
+ });
3007
+ return;
3001
3008
  }
3002
3009
  const data = item.data;
3003
3010
  data.getRange = () => ({
3004
3011
  start: toPos(item.start, data.startOffset),
3005
3012
  end: toPos(item.end, data.endOffset)
3006
3013
  });
3007
- data.children = extractTokens(item);
3008
- return data;
3014
+ if (data.type === "comment") {
3015
+ const closed = item.node === commentNodes.block && item.end.offset - item.start.offset === data.text.length + 4;
3016
+ comments?.push({
3017
+ start: item.start.offset,
3018
+ end: item.end.offset + (closed ? 0 : 1)
3019
+ });
3020
+ if (/[\n\r]/u.test(data.text)) tokens.push({
3021
+ type: "punctuation",
3022
+ text: "\n",
3023
+ getRange: data.getRange
3024
+ });
3025
+ return;
3026
+ }
3027
+ data.children = extractTokens(item, comments);
3028
+ tokens.push(data);
3009
3029
  });
3030
+ return tokens;
3010
3031
  }
3011
3032
 
3012
3033
  //#endregion
3013
3034
  //#region packages/core/src/tokenizer/index.ts
3014
- function tokenize(source, debug = false) {
3035
+ /** Lexes `source`; comment spans are collected into `comments` when given. */ function tokenize(source, debug = false, comments) {
3015
3036
  const result = root.parse(source);
3016
3037
  if (debug) console.log(printTree(result));
3017
- return extractTokens(result);
3038
+ return extractTokens(result, comments);
3018
3039
  }
3019
3040
 
3020
3041
  //#endregion
@@ -3386,7 +3407,7 @@ function annotations() {
3386
3407
  ni.accepted();
3387
3408
  ni.move();
3388
3409
  }
3389
- while (ni.satisfies(...opts.argument)) {
3410
+ while (ni.$ && ni.satisfies(...opts.argument)) {
3390
3411
  let argToken = new Token(ni.$);
3391
3412
  ni.accepted();
3392
3413
  ni.move();
@@ -3698,10 +3719,6 @@ const importPipe = $pipe("import", [
3698
3719
  const annotateEntry = $pipe("ref", [
3699
3720
  annotations(),
3700
3721
  refWithChain(),
3701
- { handler(ni) {
3702
- ni.skip();
3703
- return true;
3704
- } },
3705
3722
  pun(";", ",", "\n").orEob().lookBehind()
3706
3723
  ]).skip("\n", ";", ",");
3707
3724
  const annotatePipe = $pipe("annotate", [
@@ -3965,10 +3982,22 @@ var AtscriptDoc = class AtscriptDoc {
3965
3982
  get text() {
3966
3983
  return this._text;
3967
3984
  }
3985
+ /** Whether `offset` into `text` falls inside a comment (e.g. a cursor in `// …`). */ isInComment(offset) {
3986
+ let lo = 0;
3987
+ let hi = this.commentSpans.length;
3988
+ while (lo < hi) {
3989
+ const mid = lo + hi >>> 1;
3990
+ if (this.commentSpans[mid].start < offset) lo = mid + 1;
3991
+ else hi = mid;
3992
+ }
3993
+ const span = this.commentSpans[lo - 1];
3994
+ return span !== void 0 && offset < span.end;
3995
+ }
3968
3996
  update(text, debug = false) {
3969
3997
  this._text = text;
3970
3998
  this.cleanup();
3971
- const rawTokens = tokenize(text, debug);
3999
+ this.commentSpans = [];
4000
+ const rawTokens = tokenize(text, debug, this.commentSpans);
3972
4001
  const ni = new NodeIterator(rawTokens, []).move();
3973
4002
  this.nodes = runPipes([
3974
4003
  pipes.importPipe,
@@ -5075,6 +5104,7 @@ var AtscriptDoc = class AtscriptDoc {
5075
5104
  _define_property$3(this, "nodes", void 0);
5076
5105
  _define_property$3(this, "_text", void 0);
5077
5106
  _define_property$3(this, "_annotateNodesByTarget", void 0);
5107
+ /** Comment spans of `text`, in source order. */ _define_property$3(this, "commentSpans", void 0);
5078
5108
  /** Lexical backtick tokens already parsed by `registerAnnotation`. */ _define_property$3(this, "parsedBacktickArgs", void 0);
5079
5109
  _define_property$3(this, "resolvedAnnotations", void 0);
5080
5110
  _define_property$3(this, "annotations", void 0);
@@ -5100,6 +5130,7 @@ var AtscriptDoc = class AtscriptDoc {
5100
5130
  this._annotationSpecCache = /* @__PURE__ */ new Map();
5101
5131
  this.nodes = [];
5102
5132
  this._text = "";
5133
+ this.commentSpans = [];
5103
5134
  this.parsedBacktickArgs = /* @__PURE__ */ new WeakSet();
5104
5135
  this.resolvedAnnotations = [];
5105
5136
  this.annotations = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/core",
3
- "version": "0.1.106",
3
+ "version": "0.1.108",
4
4
  "description": "Core library for Atscript parsing and file generation.",
5
5
  "keywords": [
6
6
  "annotations",