@lezer/common 0.15.5 → 0.15.9

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
@@ -286,7 +286,8 @@ class Tree {
286
286
  }
287
287
  /// Get a [tree cursor](#common.TreeCursor) that, unlike regular
288
288
  /// cursors, doesn't skip through
289
- /// [anonymous](#common.NodeType.isAnonymous) nodes.
289
+ /// [anonymous](#common.NodeType.isAnonymous) nodes and doesn't
290
+ /// automatically enter mounted nodes.
290
291
  fullCursor() {
291
292
  return new TreeCursor(this.topNode, 1 /* Full */);
292
293
  }
@@ -364,7 +365,7 @@ class Tree {
364
365
  /// [`NodeType.none`](#common.NodeType^none).
365
366
  balance(config = {}) {
366
367
  return this.children.length <= 8 /* BranchFactor */ ? this :
367
- balanceRange(this.type, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
368
+ balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
368
369
  }
369
370
  /// Build a tree from a postfix-ordered buffer of node information,
370
371
  /// or a cursor over such a buffer.
@@ -508,7 +509,7 @@ class TreeNode {
508
509
  }
509
510
  else if ((mode & 1 /* Full */) || (!next.type.isAnonymous || hasChild(next))) {
510
511
  let mounted;
511
- if (next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
512
+ if (!(mode & 1 /* Full */) && next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
512
513
  return new TreeNode(mounted.tree, start, i, parent);
513
514
  let inner = new TreeNode(next, start, i, parent);
514
515
  return (mode & 1 /* Full */) || !inner.type.isAnonymous ? inner
@@ -756,10 +757,10 @@ class TreeCursor {
756
757
  /// set to false.
757
758
  enter(pos, side, overlays = true, buffers = true) {
758
759
  if (!this.buffer)
759
- return this.yield(this._tree.enter(pos, side, overlays, buffers));
760
+ return this.yield(this._tree.enter(pos, side, overlays && !(this.mode & 1 /* Full */), buffers));
760
761
  return buffers ? this.enterChild(1, pos, side) : false;
761
762
  }
762
- /// Move the node's parent node, if this isn't the top node.
763
+ /// Move to the node's parent node, if this isn't the top node.
763
764
  parent() {
764
765
  if (!this.buffer)
765
766
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1081,15 +1082,21 @@ function nodeSize(balanceType, node) {
1081
1082
  return 1;
1082
1083
  let size = nodeSizeCache.get(node);
1083
1084
  if (size == null) {
1084
- size = node.children.reduce((s, ch) => s + nodeSize(balanceType, ch), 1);
1085
+ size = 1;
1086
+ for (let child of node.children) {
1087
+ if (child.type != balanceType || !(child instanceof Tree)) {
1088
+ size = 1;
1089
+ break;
1090
+ }
1091
+ size += nodeSize(balanceType, child);
1092
+ }
1085
1093
  nodeSizeCache.set(node, size);
1086
1094
  }
1087
1095
  return size;
1088
1096
  }
1089
1097
  function balanceRange(
1090
- // The type to tag the resulting tree with. Will also be used for
1091
- // internal nodes when it is an anonymous type
1092
- type,
1098
+ // The type the balanced tree's inner nodes.
1099
+ balanceType,
1093
1100
  // The direct children and their positions
1094
1101
  children, positions,
1095
1102
  // The index range in children/positions to use
@@ -1104,22 +1111,22 @@ mkTop,
1104
1111
  mkTree) {
1105
1112
  let total = 0;
1106
1113
  for (let i = from; i < to; i++)
1107
- total += nodeSize(type, children[i]);
1114
+ total += nodeSize(balanceType, children[i]);
1108
1115
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1109
1116
  let localChildren = [], localPositions = [];
1110
1117
  function divide(children, positions, from, to, offset) {
1111
1118
  for (let i = from; i < to;) {
1112
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1119
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1113
1120
  i++;
1114
1121
  for (; i < to; i++) {
1115
- let nextSize = nodeSize(type, children[i]);
1122
+ let nextSize = nodeSize(balanceType, children[i]);
1116
1123
  if (groupSize + nextSize >= maxChild)
1117
1124
  break;
1118
1125
  groupSize += nextSize;
1119
1126
  }
1120
1127
  if (i == groupFrom + 1) {
1121
1128
  if (groupSize > maxChild) {
1122
- let only = children[groupFrom];
1129
+ let only = children[groupFrom]; // Only trees can have a size > 1
1123
1130
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1124
1131
  continue;
1125
1132
  }
@@ -1127,7 +1134,7 @@ mkTree) {
1127
1134
  }
1128
1135
  else {
1129
1136
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1130
- localChildren.push(balanceRange(type, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1137
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1131
1138
  }
1132
1139
  localPositions.push(groupStart + offset - start);
1133
1140
  }
@@ -1268,11 +1275,12 @@ function parseMixed(nest) {
1268
1275
  return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
1269
1276
  }
1270
1277
  class InnerParse {
1271
- constructor(parser, parse, overlay, target) {
1278
+ constructor(parser, parse, overlay, target, ranges) {
1272
1279
  this.parser = parser;
1273
1280
  this.parse = parse;
1274
1281
  this.overlay = overlay;
1275
1282
  this.target = target;
1283
+ this.ranges = ranges;
1276
1284
  }
1277
1285
  }
1278
1286
  class ActiveOverlay {
@@ -1288,6 +1296,7 @@ class ActiveOverlay {
1288
1296
  this.ranges = [];
1289
1297
  }
1290
1298
  }
1299
+ const stoppedInner = new NodeProp({ perNode: true });
1291
1300
  class MixedParse {
1292
1301
  constructor(base, nest, input, fragments, ranges) {
1293
1302
  this.nest = nest;
@@ -1309,8 +1318,12 @@ class MixedParse {
1309
1318
  this.baseTree = done;
1310
1319
  this.startInner();
1311
1320
  }
1312
- if (this.innerDone == this.inner.length)
1313
- return this.baseTree;
1321
+ if (this.innerDone == this.inner.length) {
1322
+ let result = this.baseTree;
1323
+ if (this.stoppedAt != null)
1324
+ result = new Tree(result.type, result.children, result.positions, result.length, result.propValues.concat([[stoppedInner, this.stoppedAt]]));
1325
+ return result;
1326
+ }
1314
1327
  let inner = this.inner[this.innerDone], done = inner.parse.advance();
1315
1328
  if (done) {
1316
1329
  this.innerDone++;
@@ -1327,8 +1340,12 @@ class MixedParse {
1327
1340
  get parsedPos() {
1328
1341
  if (this.baseParse)
1329
1342
  return 0;
1330
- let next = this.inner[this.innerDone];
1331
- return next ? next.parse.parsedPos : this.input.length;
1343
+ let pos = this.input.length;
1344
+ for (let i = this.innerDone; i < this.inner.length; i++) {
1345
+ if (this.inner[i].ranges[0].from < pos)
1346
+ pos = Math.min(pos, this.inner[i].parse.parsedPos);
1347
+ }
1348
+ return pos;
1332
1349
  }
1333
1350
  stopAt(pos) {
1334
1351
  this.stoppedAt = pos;
@@ -1342,7 +1359,8 @@ class MixedParse {
1342
1359
  let fragmentCursor = new FragmentCursor(this.fragments);
1343
1360
  let overlay = null;
1344
1361
  let covered = null;
1345
- scan: for (let cursor = this.baseTree.fullCursor(), nest, isCovered;;) {
1362
+ let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1363
+ scan: for (let nest, isCovered;;) {
1346
1364
  let enter = true, range;
1347
1365
  if (fragmentCursor.hasNode(cursor)) {
1348
1366
  if (overlay) {
@@ -1369,7 +1387,7 @@ class MixedParse {
1369
1387
  else {
1370
1388
  let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
1371
1389
  if (ranges.length)
1372
- this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree));
1390
+ this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
1373
1391
  if (!nest.overlay)
1374
1392
  enter = false;
1375
1393
  else if (ranges.length)
@@ -1397,7 +1415,7 @@ class MixedParse {
1397
1415
  if (overlay && !--overlay.depth) {
1398
1416
  let ranges = punchRanges(this.ranges, overlay.ranges);
1399
1417
  if (ranges.length)
1400
- this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target));
1418
+ this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
1401
1419
  overlay = overlay.prev;
1402
1420
  }
1403
1421
  if (covered && !--covered.depth)
@@ -1482,7 +1500,7 @@ class StructureCursor {
1482
1500
  }
1483
1501
  hasNode(cursor) {
1484
1502
  this.moveTo(cursor.from);
1485
- if (!this.done && this.cursor.from + this.offset == cursor.from) {
1503
+ if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
1486
1504
  for (let tree = this.cursor.tree;;) {
1487
1505
  if (tree == cursor.tree)
1488
1506
  return true;
@@ -1497,10 +1515,13 @@ class StructureCursor {
1497
1515
  }
1498
1516
  class FragmentCursor {
1499
1517
  constructor(fragments) {
1518
+ var _a;
1500
1519
  this.fragments = fragments;
1520
+ this.curTo = 0;
1501
1521
  this.fragI = 0;
1502
1522
  if (fragments.length) {
1503
1523
  let first = this.curFrag = fragments[0];
1524
+ this.curTo = (_a = first.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : first.to;
1504
1525
  this.inner = new StructureCursor(first.tree, -first.offset);
1505
1526
  }
1506
1527
  else {
@@ -1508,17 +1529,19 @@ class FragmentCursor {
1508
1529
  }
1509
1530
  }
1510
1531
  hasNode(node) {
1511
- while (this.curFrag && node.from >= this.curFrag.to)
1532
+ while (this.curFrag && node.from >= this.curTo)
1512
1533
  this.nextFrag();
1513
- return this.curFrag && this.curFrag.from <= node.from && this.curFrag.to >= node.to && this.inner.hasNode(node);
1534
+ return this.curFrag && this.curFrag.from <= node.from && this.curTo >= node.to && this.inner.hasNode(node);
1514
1535
  }
1515
1536
  nextFrag() {
1537
+ var _a;
1516
1538
  this.fragI++;
1517
1539
  if (this.fragI == this.fragments.length) {
1518
1540
  this.curFrag = this.inner = null;
1519
1541
  }
1520
1542
  else {
1521
1543
  let frag = this.curFrag = this.fragments[this.fragI];
1544
+ this.curTo = (_a = frag.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : frag.to;
1522
1545
  this.inner = new StructureCursor(frag.tree, -frag.offset);
1523
1546
  }
1524
1547
  }
package/dist/index.js CHANGED
@@ -282,7 +282,8 @@ class Tree {
282
282
  }
283
283
  /// Get a [tree cursor](#common.TreeCursor) that, unlike regular
284
284
  /// cursors, doesn't skip through
285
- /// [anonymous](#common.NodeType.isAnonymous) nodes.
285
+ /// [anonymous](#common.NodeType.isAnonymous) nodes and doesn't
286
+ /// automatically enter mounted nodes.
286
287
  fullCursor() {
287
288
  return new TreeCursor(this.topNode, 1 /* Full */);
288
289
  }
@@ -360,7 +361,7 @@ class Tree {
360
361
  /// [`NodeType.none`](#common.NodeType^none).
361
362
  balance(config = {}) {
362
363
  return this.children.length <= 8 /* BranchFactor */ ? this :
363
- balanceRange(this.type, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
364
+ balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
364
365
  }
365
366
  /// Build a tree from a postfix-ordered buffer of node information,
366
367
  /// or a cursor over such a buffer.
@@ -504,7 +505,7 @@ class TreeNode {
504
505
  }
505
506
  else if ((mode & 1 /* Full */) || (!next.type.isAnonymous || hasChild(next))) {
506
507
  let mounted;
507
- if (next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
508
+ if (!(mode & 1 /* Full */) && next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
508
509
  return new TreeNode(mounted.tree, start, i, parent);
509
510
  let inner = new TreeNode(next, start, i, parent);
510
511
  return (mode & 1 /* Full */) || !inner.type.isAnonymous ? inner
@@ -752,10 +753,10 @@ class TreeCursor {
752
753
  /// set to false.
753
754
  enter(pos, side, overlays = true, buffers = true) {
754
755
  if (!this.buffer)
755
- return this.yield(this._tree.enter(pos, side, overlays, buffers));
756
+ return this.yield(this._tree.enter(pos, side, overlays && !(this.mode & 1 /* Full */), buffers));
756
757
  return buffers ? this.enterChild(1, pos, side) : false;
757
758
  }
758
- /// Move the node's parent node, if this isn't the top node.
759
+ /// Move to the node's parent node, if this isn't the top node.
759
760
  parent() {
760
761
  if (!this.buffer)
761
762
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1077,15 +1078,21 @@ function nodeSize(balanceType, node) {
1077
1078
  return 1;
1078
1079
  let size = nodeSizeCache.get(node);
1079
1080
  if (size == null) {
1080
- size = node.children.reduce((s, ch) => s + nodeSize(balanceType, ch), 1);
1081
+ size = 1;
1082
+ for (let child of node.children) {
1083
+ if (child.type != balanceType || !(child instanceof Tree)) {
1084
+ size = 1;
1085
+ break;
1086
+ }
1087
+ size += nodeSize(balanceType, child);
1088
+ }
1081
1089
  nodeSizeCache.set(node, size);
1082
1090
  }
1083
1091
  return size;
1084
1092
  }
1085
1093
  function balanceRange(
1086
- // The type to tag the resulting tree with. Will also be used for
1087
- // internal nodes when it is an anonymous type
1088
- type,
1094
+ // The type the balanced tree's inner nodes.
1095
+ balanceType,
1089
1096
  // The direct children and their positions
1090
1097
  children, positions,
1091
1098
  // The index range in children/positions to use
@@ -1100,22 +1107,22 @@ mkTop,
1100
1107
  mkTree) {
1101
1108
  let total = 0;
1102
1109
  for (let i = from; i < to; i++)
1103
- total += nodeSize(type, children[i]);
1110
+ total += nodeSize(balanceType, children[i]);
1104
1111
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1105
1112
  let localChildren = [], localPositions = [];
1106
1113
  function divide(children, positions, from, to, offset) {
1107
1114
  for (let i = from; i < to;) {
1108
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1115
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1109
1116
  i++;
1110
1117
  for (; i < to; i++) {
1111
- let nextSize = nodeSize(type, children[i]);
1118
+ let nextSize = nodeSize(balanceType, children[i]);
1112
1119
  if (groupSize + nextSize >= maxChild)
1113
1120
  break;
1114
1121
  groupSize += nextSize;
1115
1122
  }
1116
1123
  if (i == groupFrom + 1) {
1117
1124
  if (groupSize > maxChild) {
1118
- let only = children[groupFrom];
1125
+ let only = children[groupFrom]; // Only trees can have a size > 1
1119
1126
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1120
1127
  continue;
1121
1128
  }
@@ -1123,7 +1130,7 @@ mkTree) {
1123
1130
  }
1124
1131
  else {
1125
1132
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1126
- localChildren.push(balanceRange(type, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1133
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1127
1134
  }
1128
1135
  localPositions.push(groupStart + offset - start);
1129
1136
  }
@@ -1264,11 +1271,12 @@ function parseMixed(nest) {
1264
1271
  return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
1265
1272
  }
1266
1273
  class InnerParse {
1267
- constructor(parser, parse, overlay, target) {
1274
+ constructor(parser, parse, overlay, target, ranges) {
1268
1275
  this.parser = parser;
1269
1276
  this.parse = parse;
1270
1277
  this.overlay = overlay;
1271
1278
  this.target = target;
1279
+ this.ranges = ranges;
1272
1280
  }
1273
1281
  }
1274
1282
  class ActiveOverlay {
@@ -1284,6 +1292,7 @@ class ActiveOverlay {
1284
1292
  this.ranges = [];
1285
1293
  }
1286
1294
  }
1295
+ const stoppedInner = new NodeProp({ perNode: true });
1287
1296
  class MixedParse {
1288
1297
  constructor(base, nest, input, fragments, ranges) {
1289
1298
  this.nest = nest;
@@ -1305,8 +1314,12 @@ class MixedParse {
1305
1314
  this.baseTree = done;
1306
1315
  this.startInner();
1307
1316
  }
1308
- if (this.innerDone == this.inner.length)
1309
- return this.baseTree;
1317
+ if (this.innerDone == this.inner.length) {
1318
+ let result = this.baseTree;
1319
+ if (this.stoppedAt != null)
1320
+ result = new Tree(result.type, result.children, result.positions, result.length, result.propValues.concat([[stoppedInner, this.stoppedAt]]));
1321
+ return result;
1322
+ }
1310
1323
  let inner = this.inner[this.innerDone], done = inner.parse.advance();
1311
1324
  if (done) {
1312
1325
  this.innerDone++;
@@ -1323,8 +1336,12 @@ class MixedParse {
1323
1336
  get parsedPos() {
1324
1337
  if (this.baseParse)
1325
1338
  return 0;
1326
- let next = this.inner[this.innerDone];
1327
- return next ? next.parse.parsedPos : this.input.length;
1339
+ let pos = this.input.length;
1340
+ for (let i = this.innerDone; i < this.inner.length; i++) {
1341
+ if (this.inner[i].ranges[0].from < pos)
1342
+ pos = Math.min(pos, this.inner[i].parse.parsedPos);
1343
+ }
1344
+ return pos;
1328
1345
  }
1329
1346
  stopAt(pos) {
1330
1347
  this.stoppedAt = pos;
@@ -1338,7 +1355,8 @@ class MixedParse {
1338
1355
  let fragmentCursor = new FragmentCursor(this.fragments);
1339
1356
  let overlay = null;
1340
1357
  let covered = null;
1341
- scan: for (let cursor = this.baseTree.fullCursor(), nest, isCovered;;) {
1358
+ let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1359
+ scan: for (let nest, isCovered;;) {
1342
1360
  let enter = true, range;
1343
1361
  if (fragmentCursor.hasNode(cursor)) {
1344
1362
  if (overlay) {
@@ -1365,7 +1383,7 @@ class MixedParse {
1365
1383
  else {
1366
1384
  let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
1367
1385
  if (ranges.length)
1368
- this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree));
1386
+ this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
1369
1387
  if (!nest.overlay)
1370
1388
  enter = false;
1371
1389
  else if (ranges.length)
@@ -1393,7 +1411,7 @@ class MixedParse {
1393
1411
  if (overlay && !--overlay.depth) {
1394
1412
  let ranges = punchRanges(this.ranges, overlay.ranges);
1395
1413
  if (ranges.length)
1396
- this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target));
1414
+ this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
1397
1415
  overlay = overlay.prev;
1398
1416
  }
1399
1417
  if (covered && !--covered.depth)
@@ -1478,7 +1496,7 @@ class StructureCursor {
1478
1496
  }
1479
1497
  hasNode(cursor) {
1480
1498
  this.moveTo(cursor.from);
1481
- if (!this.done && this.cursor.from + this.offset == cursor.from) {
1499
+ if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
1482
1500
  for (let tree = this.cursor.tree;;) {
1483
1501
  if (tree == cursor.tree)
1484
1502
  return true;
@@ -1493,10 +1511,13 @@ class StructureCursor {
1493
1511
  }
1494
1512
  class FragmentCursor {
1495
1513
  constructor(fragments) {
1514
+ var _a;
1496
1515
  this.fragments = fragments;
1516
+ this.curTo = 0;
1497
1517
  this.fragI = 0;
1498
1518
  if (fragments.length) {
1499
1519
  let first = this.curFrag = fragments[0];
1520
+ this.curTo = (_a = first.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : first.to;
1500
1521
  this.inner = new StructureCursor(first.tree, -first.offset);
1501
1522
  }
1502
1523
  else {
@@ -1504,17 +1525,19 @@ class FragmentCursor {
1504
1525
  }
1505
1526
  }
1506
1527
  hasNode(node) {
1507
- while (this.curFrag && node.from >= this.curFrag.to)
1528
+ while (this.curFrag && node.from >= this.curTo)
1508
1529
  this.nextFrag();
1509
- return this.curFrag && this.curFrag.from <= node.from && this.curFrag.to >= node.to && this.inner.hasNode(node);
1530
+ return this.curFrag && this.curFrag.from <= node.from && this.curTo >= node.to && this.inner.hasNode(node);
1510
1531
  }
1511
1532
  nextFrag() {
1533
+ var _a;
1512
1534
  this.fragI++;
1513
1535
  if (this.fragI == this.fragments.length) {
1514
1536
  this.curFrag = this.inner = null;
1515
1537
  }
1516
1538
  else {
1517
1539
  let frag = this.curFrag = this.fragments[this.fragI];
1540
+ this.curTo = (_a = frag.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : frag.to;
1518
1541
  this.inner = new StructureCursor(frag.tree, -frag.offset);
1519
1542
  }
1520
1543
  }
package/dist/tree.d.ts CHANGED
@@ -134,6 +134,80 @@ export interface SyntaxNode {
134
134
  getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode | null;
135
135
  getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
136
136
  }
137
+ declare const enum Side {
138
+ Before = -2,
139
+ AtOrBefore = -1,
140
+ Around = 0,
141
+ AtOrAfter = 1,
142
+ After = 2,
143
+ DontCare = 4
144
+ }
145
+ export declare const enum Mode {
146
+ Full = 1,
147
+ NoEnterBuffer = 2
148
+ }
149
+ export declare class TreeNode implements SyntaxNode {
150
+ readonly node: Tree;
151
+ readonly _from: number;
152
+ readonly index: number;
153
+ readonly _parent: TreeNode | null;
154
+ constructor(node: Tree, _from: number, index: number, _parent: TreeNode | null);
155
+ get type(): NodeType;
156
+ get name(): string;
157
+ get from(): number;
158
+ get to(): number;
159
+ nextChild(i: number, dir: 1 | -1, pos: number, side: Side, mode?: Mode): TreeNode | BufferNode | null;
160
+ get firstChild(): TreeNode | BufferNode;
161
+ get lastChild(): TreeNode | BufferNode;
162
+ childAfter(pos: number): TreeNode | BufferNode;
163
+ childBefore(pos: number): TreeNode | BufferNode;
164
+ enter(pos: number, side: -1 | 0 | 1, overlays?: boolean, buffers?: boolean): TreeNode | BufferNode;
165
+ nextSignificantParent(): TreeNode;
166
+ get parent(): TreeNode;
167
+ get nextSibling(): TreeNode | BufferNode;
168
+ get prevSibling(): TreeNode | BufferNode;
169
+ get cursor(): TreeCursor;
170
+ get tree(): Tree;
171
+ toTree(): Tree;
172
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
173
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
174
+ getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode;
175
+ getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
176
+ }
177
+ declare class BufferContext {
178
+ readonly parent: TreeNode;
179
+ readonly buffer: TreeBuffer;
180
+ readonly index: number;
181
+ readonly start: number;
182
+ constructor(parent: TreeNode, buffer: TreeBuffer, index: number, start: number);
183
+ }
184
+ declare class BufferNode implements SyntaxNode {
185
+ readonly context: BufferContext;
186
+ readonly _parent: BufferNode | null;
187
+ readonly index: number;
188
+ type: NodeType;
189
+ get name(): string;
190
+ get from(): number;
191
+ get to(): number;
192
+ constructor(context: BufferContext, _parent: BufferNode | null, index: number);
193
+ child(dir: 1 | -1, pos: number, side: Side): BufferNode | null;
194
+ get firstChild(): BufferNode;
195
+ get lastChild(): BufferNode;
196
+ childAfter(pos: number): BufferNode;
197
+ childBefore(pos: number): BufferNode;
198
+ enter(pos: number, side: -1 | 0 | 1, overlays?: boolean, buffers?: boolean): BufferNode;
199
+ get parent(): TreeNode | BufferNode;
200
+ externalSibling(dir: 1 | -1): TreeNode | BufferNode;
201
+ get nextSibling(): SyntaxNode | null;
202
+ get prevSibling(): SyntaxNode | null;
203
+ get cursor(): TreeCursor;
204
+ get tree(): any;
205
+ toTree(): Tree;
206
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
207
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
208
+ getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode;
209
+ getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
210
+ }
137
211
  export declare class TreeCursor {
138
212
  type: NodeType;
139
213
  get name(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lezer/common",
3
- "version": "0.15.5",
3
+ "version": "0.15.9",
4
4
  "description": "Syntax tree data structure and parser interfaces for the lezer parser",
5
5
  "main": "dist/index.cjs",
6
6
  "type": "module",