@lezer/common 0.15.9 → 0.15.10

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
@@ -213,7 +213,7 @@ class NodeSet {
213
213
  return new NodeSet(newTypes);
214
214
  }
215
215
  }
216
- const CachedNode = new WeakMap();
216
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
217
217
  /// A piece of syntax tree. There are two ways to approach these
218
218
  /// trees: the way they are actually stored in memory, and the
219
219
  /// convenient way.
@@ -302,7 +302,9 @@ class Tree {
302
302
  /// position. With 0, it'll only enter nodes that cover the position
303
303
  /// from both sides.
304
304
  resolve(pos, side = 0) {
305
- return this.cursor(pos, side).node;
305
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
306
+ CachedNode.set(this, node);
307
+ return node;
306
308
  }
307
309
  /// Like [`resolve`](#common.Tree.resolve), but will enter
308
310
  /// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
@@ -310,13 +312,9 @@ class Tree {
310
312
  /// (with parent links going through all parent structure, including
311
313
  /// the host trees).
312
314
  resolveInner(pos, side = 0) {
313
- let result = this.topNode;
314
- for (;;) {
315
- let inner = result.enter(pos, side);
316
- if (!inner)
317
- return result;
318
- result = inner;
319
- }
315
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
316
+ CachedInnerNode.set(this, node);
317
+ return node;
320
318
  }
321
319
  /// Iterate over the tree and its children, calling `enter` for any
322
320
  /// node that touches the `from`/`to` region (if given) before
@@ -481,6 +479,30 @@ function enterUnfinishedNodesBefore(node, pos) {
481
479
  }
482
480
  return node;
483
481
  }
482
+ function resolveNode(node, pos, side, overlays) {
483
+ var _a;
484
+ // Move up to a node that actually holds the position, if possible
485
+ while (node.from == node.to ||
486
+ (side < 1 ? node.from >= pos : node.from > pos) ||
487
+ (side > -1 ? node.to <= pos : node.to < pos)) {
488
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
489
+ if (!parent)
490
+ return node;
491
+ node = parent;
492
+ }
493
+ // Must go up out of overlays when those do not overlap with pos
494
+ if (overlays)
495
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
496
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, true)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
497
+ node = parent;
498
+ }
499
+ for (;;) {
500
+ let inner = node.enter(pos, side, overlays);
501
+ if (!inner)
502
+ return node;
503
+ node = inner;
504
+ }
505
+ }
484
506
  class TreeNode {
485
507
  constructor(node, _from,
486
508
  // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
@@ -562,7 +584,10 @@ class TreeNode {
562
584
  get tree() { return this.node; }
563
585
  toTree() { return this.node; }
564
586
  resolve(pos, side = 0) {
565
- return this.cursor.moveTo(pos, side).node;
587
+ return resolveNode(this, pos, side, false);
588
+ }
589
+ resolveInner(pos, side = 0) {
590
+ return resolveNode(this, pos, side, true);
566
591
  }
567
592
  enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
568
593
  getChild(type, before = null, after = null) {
@@ -660,7 +685,10 @@ class BufferNode {
660
685
  return new Tree(this.type, children, positions, this.to - this.from);
661
686
  }
662
687
  resolve(pos, side = 0) {
663
- return this.cursor.moveTo(pos, side).node;
688
+ return resolveNode(this, pos, side, false);
689
+ }
690
+ resolveInner(pos, side = 0) {
691
+ return resolveNode(this, pos, side, true);
664
692
  }
665
693
  enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
666
694
  /// @internal
@@ -1317,6 +1345,9 @@ class MixedParse {
1317
1345
  this.baseParse = null;
1318
1346
  this.baseTree = done;
1319
1347
  this.startInner();
1348
+ if (this.stoppedAt != null)
1349
+ for (let inner of this.inner)
1350
+ inner.parse.stopAt(this.stoppedAt);
1320
1351
  }
1321
1352
  if (this.innerDone == this.inner.length) {
1322
1353
  let result = this.baseTree;
@@ -1360,7 +1391,7 @@ class MixedParse {
1360
1391
  let overlay = null;
1361
1392
  let covered = null;
1362
1393
  let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1363
- scan: for (let nest, isCovered;;) {
1394
+ scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
1364
1395
  let enter = true, range;
1365
1396
  if (fragmentCursor.hasNode(cursor)) {
1366
1397
  if (overlay) {
package/dist/index.js CHANGED
@@ -209,7 +209,7 @@ class NodeSet {
209
209
  return new NodeSet(newTypes);
210
210
  }
211
211
  }
212
- const CachedNode = new WeakMap();
212
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
213
213
  /// A piece of syntax tree. There are two ways to approach these
214
214
  /// trees: the way they are actually stored in memory, and the
215
215
  /// convenient way.
@@ -298,7 +298,9 @@ class Tree {
298
298
  /// position. With 0, it'll only enter nodes that cover the position
299
299
  /// from both sides.
300
300
  resolve(pos, side = 0) {
301
- return this.cursor(pos, side).node;
301
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
302
+ CachedNode.set(this, node);
303
+ return node;
302
304
  }
303
305
  /// Like [`resolve`](#common.Tree.resolve), but will enter
304
306
  /// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
@@ -306,13 +308,9 @@ class Tree {
306
308
  /// (with parent links going through all parent structure, including
307
309
  /// the host trees).
308
310
  resolveInner(pos, side = 0) {
309
- let result = this.topNode;
310
- for (;;) {
311
- let inner = result.enter(pos, side);
312
- if (!inner)
313
- return result;
314
- result = inner;
315
- }
311
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
312
+ CachedInnerNode.set(this, node);
313
+ return node;
316
314
  }
317
315
  /// Iterate over the tree and its children, calling `enter` for any
318
316
  /// node that touches the `from`/`to` region (if given) before
@@ -477,6 +475,30 @@ function enterUnfinishedNodesBefore(node, pos) {
477
475
  }
478
476
  return node;
479
477
  }
478
+ function resolveNode(node, pos, side, overlays) {
479
+ var _a;
480
+ // Move up to a node that actually holds the position, if possible
481
+ while (node.from == node.to ||
482
+ (side < 1 ? node.from >= pos : node.from > pos) ||
483
+ (side > -1 ? node.to <= pos : node.to < pos)) {
484
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
485
+ if (!parent)
486
+ return node;
487
+ node = parent;
488
+ }
489
+ // Must go up out of overlays when those do not overlap with pos
490
+ if (overlays)
491
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
492
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, true)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
493
+ node = parent;
494
+ }
495
+ for (;;) {
496
+ let inner = node.enter(pos, side, overlays);
497
+ if (!inner)
498
+ return node;
499
+ node = inner;
500
+ }
501
+ }
480
502
  class TreeNode {
481
503
  constructor(node, _from,
482
504
  // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
@@ -558,7 +580,10 @@ class TreeNode {
558
580
  get tree() { return this.node; }
559
581
  toTree() { return this.node; }
560
582
  resolve(pos, side = 0) {
561
- return this.cursor.moveTo(pos, side).node;
583
+ return resolveNode(this, pos, side, false);
584
+ }
585
+ resolveInner(pos, side = 0) {
586
+ return resolveNode(this, pos, side, true);
562
587
  }
563
588
  enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
564
589
  getChild(type, before = null, after = null) {
@@ -656,7 +681,10 @@ class BufferNode {
656
681
  return new Tree(this.type, children, positions, this.to - this.from);
657
682
  }
658
683
  resolve(pos, side = 0) {
659
- return this.cursor.moveTo(pos, side).node;
684
+ return resolveNode(this, pos, side, false);
685
+ }
686
+ resolveInner(pos, side = 0) {
687
+ return resolveNode(this, pos, side, true);
660
688
  }
661
689
  enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
662
690
  /// @internal
@@ -1313,6 +1341,9 @@ class MixedParse {
1313
1341
  this.baseParse = null;
1314
1342
  this.baseTree = done;
1315
1343
  this.startInner();
1344
+ if (this.stoppedAt != null)
1345
+ for (let inner of this.inner)
1346
+ inner.parse.stopAt(this.stoppedAt);
1316
1347
  }
1317
1348
  if (this.innerDone == this.inner.length) {
1318
1349
  let result = this.baseTree;
@@ -1356,7 +1387,7 @@ class MixedParse {
1356
1387
  let overlay = null;
1357
1388
  let covered = null;
1358
1389
  let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1359
- scan: for (let nest, isCovered;;) {
1390
+ scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
1360
1391
  let enter = true, range;
1361
1392
  if (fragmentCursor.hasNode(cursor)) {
1362
1393
  if (overlay) {