@willbooster/web-tree-sitter 15.5.3 → 16.0.0

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.
@@ -4215,10 +4215,6 @@ function parsePattern(index, stepType, stepValueId, captureNames, stringValues,
4215
4215
  }
4216
4216
  }
4217
4217
  __name(parsePattern, "parsePattern");
4218
- function indexForByteOffset(source, byteOffset) {
4219
- return new TextEncoder().encodeInto(source, new Uint8Array(byteOffset)).read;
4220
- }
4221
- __name(indexForByteOffset, "indexForByteOffset");
4222
4218
  var finalizer5 = newFinalizer((address) => {
4223
4219
  C._ts_query_delete(address);
4224
4220
  });
@@ -4233,8 +4229,6 @@ var Query = class {
4233
4229
  exceededMatchLimit;
4234
4230
  /** @internal */
4235
4231
  textPredicates;
4236
- /** @internal */
4237
- source;
4238
4232
  /** The names of the captures used in the query. */
4239
4233
  captureNames;
4240
4234
  /** The quantifiers of the captures used in the query. */
@@ -4374,7 +4368,6 @@ var Query = class {
4374
4368
  this.captureNames = captureNames;
4375
4369
  this.captureQuantifiers = captureQuantifiers;
4376
4370
  this.textPredicates = textPredicates;
4377
- this.source = source;
4378
4371
  this.predicates = predicates;
4379
4372
  this.setProperties = setProperties;
4380
4373
  this.assertedProperties = assertedProperties;
@@ -4401,72 +4394,84 @@ var Query = class {
4401
4394
  * @param {QueryOptions} options - Options for query execution.
4402
4395
  */
4403
4396
  matches(node, options = {}) {
4404
- const {
4405
- startPosition,
4406
- endPosition,
4397
+ const startPosition = options.startPosition ?? ZERO_POINT;
4398
+ const endPosition = options.endPosition ?? ZERO_POINT;
4399
+ const startIndex = options.startIndex ?? 0;
4400
+ const endIndex = options.endIndex ?? 0;
4401
+ const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4402
+ const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4403
+ const startContainingIndex = options.startContainingIndex ?? 0;
4404
+ const endContainingIndex = options.endContainingIndex ?? 0;
4405
+ const matchLimit = options.matchLimit ?? 4294967295;
4406
+ const maxStartDepth = options.maxStartDepth ?? 4294967295;
4407
+ const progressCallback = options.progressCallback;
4408
+ if (typeof matchLimit !== "number") {
4409
+ throw new Error("Arguments must be numbers");
4410
+ }
4411
+ this.matchLimit = matchLimit;
4412
+ if (endIndex !== 0 && startIndex > endIndex) {
4413
+ throw new Error("`startIndex` cannot be greater than `endIndex`");
4414
+ }
4415
+ if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4416
+ throw new Error("`startPosition` cannot be greater than `endPosition`");
4417
+ }
4418
+ if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4419
+ throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4420
+ }
4421
+ if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4422
+ throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4423
+ }
4424
+ if (progressCallback) {
4425
+ C.currentQueryProgressCallback = progressCallback;
4426
+ }
4427
+ marshalNode(node);
4428
+ C._ts_query_matches_wasm(
4429
+ this[0],
4430
+ node.tree[0],
4431
+ startPosition.row,
4432
+ startPosition.column,
4433
+ endPosition.row,
4434
+ endPosition.column,
4407
4435
  startIndex,
4408
4436
  endIndex,
4409
- startContainingPosition,
4410
- endContainingPosition,
4437
+ startContainingPosition.row,
4438
+ startContainingPosition.column,
4439
+ endContainingPosition.row,
4440
+ endContainingPosition.column,
4411
4441
  startContainingIndex,
4412
4442
  endContainingIndex,
4413
4443
  matchLimit,
4414
- maxStartDepth,
4415
- progressCallback
4416
- } = prepareQuery(this, options);
4417
- return executeQuery(
4418
- progressCallback,
4419
- () => {
4420
- marshalNode(node);
4421
- C._ts_query_matches_wasm(
4422
- this[0],
4423
- node.tree[0],
4424
- startPosition.row,
4425
- startPosition.column,
4426
- endPosition.row,
4427
- endPosition.column,
4428
- startIndex,
4429
- endIndex,
4430
- startContainingPosition.row,
4431
- startContainingPosition.column,
4432
- endContainingPosition.row,
4433
- endContainingPosition.column,
4434
- startContainingIndex,
4435
- endContainingIndex,
4436
- matchLimit,
4437
- maxStartDepth
4438
- );
4439
- },
4440
- (exceededMatchLimit) => {
4441
- this.matchLimit = matchLimit;
4442
- this.exceededMatchLimit = exceededMatchLimit;
4443
- },
4444
- (rawCount, startAddress) => {
4445
- const result = new Array(rawCount);
4446
- let filteredCount = 0;
4447
- let address = startAddress;
4448
- for (let i2 = 0; i2 < rawCount; i2++) {
4449
- const patternIndex = C.getValue(address, "i32");
4450
- address += SIZE_OF_INT;
4451
- const captureCount = C.getValue(address, "i32");
4452
- address += SIZE_OF_INT;
4453
- const captures = new Array(captureCount);
4454
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4455
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4456
- result[filteredCount] = { patternIndex, captures };
4457
- const setProperties = this.setProperties[patternIndex];
4458
- result[filteredCount].setProperties = setProperties;
4459
- const assertedProperties = this.assertedProperties[patternIndex];
4460
- result[filteredCount].assertedProperties = assertedProperties;
4461
- const refutedProperties = this.refutedProperties[patternIndex];
4462
- result[filteredCount].refutedProperties = refutedProperties;
4463
- filteredCount++;
4464
- }
4465
- }
4466
- result.length = filteredCount;
4467
- return result;
4468
- }
4444
+ maxStartDepth
4469
4445
  );
4446
+ const rawCount = C.getValue(TRANSFER_BUFFER, "i32");
4447
+ const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4448
+ const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4449
+ const result = new Array(rawCount);
4450
+ this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4451
+ let filteredCount = 0;
4452
+ let address = startAddress;
4453
+ for (let i2 = 0; i2 < rawCount; i2++) {
4454
+ const patternIndex = C.getValue(address, "i32");
4455
+ address += SIZE_OF_INT;
4456
+ const captureCount = C.getValue(address, "i32");
4457
+ address += SIZE_OF_INT;
4458
+ const captures = new Array(captureCount);
4459
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4460
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4461
+ result[filteredCount] = { patternIndex, captures };
4462
+ const setProperties = this.setProperties[patternIndex];
4463
+ result[filteredCount].setProperties = setProperties;
4464
+ const assertedProperties = this.assertedProperties[patternIndex];
4465
+ result[filteredCount].assertedProperties = assertedProperties;
4466
+ const refutedProperties = this.refutedProperties[patternIndex];
4467
+ result[filteredCount].refutedProperties = refutedProperties;
4468
+ filteredCount++;
4469
+ }
4470
+ }
4471
+ result.length = filteredCount;
4472
+ C._free(startAddress);
4473
+ C.currentQueryProgressCallback = null;
4474
+ return result;
4470
4475
  }
4471
4476
  /**
4472
4477
  * Iterate over all of the individual captures in the order that they
@@ -4480,71 +4485,85 @@ var Query = class {
4480
4485
  * @param {QueryOptions} options - Options for query execution.
4481
4486
  */
4482
4487
  captures(node, options = {}) {
4483
- const {
4484
- startPosition,
4485
- endPosition,
4488
+ const startPosition = options.startPosition ?? ZERO_POINT;
4489
+ const endPosition = options.endPosition ?? ZERO_POINT;
4490
+ const startIndex = options.startIndex ?? 0;
4491
+ const endIndex = options.endIndex ?? 0;
4492
+ const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4493
+ const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4494
+ const startContainingIndex = options.startContainingIndex ?? 0;
4495
+ const endContainingIndex = options.endContainingIndex ?? 0;
4496
+ const matchLimit = options.matchLimit ?? 4294967295;
4497
+ const maxStartDepth = options.maxStartDepth ?? 4294967295;
4498
+ const progressCallback = options.progressCallback;
4499
+ if (typeof matchLimit !== "number") {
4500
+ throw new Error("Arguments must be numbers");
4501
+ }
4502
+ this.matchLimit = matchLimit;
4503
+ if (endIndex !== 0 && startIndex > endIndex) {
4504
+ throw new Error("`startIndex` cannot be greater than `endIndex`");
4505
+ }
4506
+ if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4507
+ throw new Error("`startPosition` cannot be greater than `endPosition`");
4508
+ }
4509
+ if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4510
+ throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4511
+ }
4512
+ if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4513
+ throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4514
+ }
4515
+ if (progressCallback) {
4516
+ C.currentQueryProgressCallback = progressCallback;
4517
+ }
4518
+ marshalNode(node);
4519
+ C._ts_query_captures_wasm(
4520
+ this[0],
4521
+ node.tree[0],
4522
+ startPosition.row,
4523
+ startPosition.column,
4524
+ endPosition.row,
4525
+ endPosition.column,
4486
4526
  startIndex,
4487
4527
  endIndex,
4488
- startContainingPosition,
4489
- endContainingPosition,
4528
+ startContainingPosition.row,
4529
+ startContainingPosition.column,
4530
+ endContainingPosition.row,
4531
+ endContainingPosition.column,
4490
4532
  startContainingIndex,
4491
4533
  endContainingIndex,
4492
4534
  matchLimit,
4493
- maxStartDepth,
4494
- progressCallback
4495
- } = prepareQuery(this, options);
4496
- const callback = progressCallback;
4497
- return executeQuery(
4498
- callback ? (state) => callback(state) === true : void 0,
4499
- () => {
4500
- marshalNode(node);
4501
- C._ts_query_captures_wasm(
4502
- this[0],
4503
- node.tree[0],
4504
- startPosition.row,
4505
- startPosition.column,
4506
- endPosition.row,
4507
- endPosition.column,
4508
- startIndex,
4509
- endIndex,
4510
- startContainingPosition.row,
4511
- startContainingPosition.column,
4512
- endContainingPosition.row,
4513
- endContainingPosition.column,
4514
- startContainingIndex,
4515
- endContainingIndex,
4516
- matchLimit,
4517
- maxStartDepth
4518
- );
4519
- },
4520
- (exceededMatchLimit) => {
4521
- this.matchLimit = matchLimit;
4522
- this.exceededMatchLimit = exceededMatchLimit;
4523
- },
4524
- (count, startAddress) => {
4525
- const result = new Array();
4526
- const captures = new Array();
4527
- let address = startAddress;
4528
- for (let i2 = 0; i2 < count; i2++) {
4529
- const patternIndex = C.getValue(address, "i32");
4530
- address += SIZE_OF_INT;
4531
- const captureCount = C.getValue(address, "i32");
4532
- address += SIZE_OF_INT;
4533
- const captureIndex = C.getValue(address, "i32");
4534
- address += SIZE_OF_INT;
4535
- captures.length = captureCount;
4536
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4537
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4538
- const capture = captures[captureIndex];
4539
- capture.setProperties = this.setProperties[patternIndex];
4540
- capture.assertedProperties = this.assertedProperties[patternIndex];
4541
- capture.refutedProperties = this.refutedProperties[patternIndex];
4542
- result.push(capture);
4543
- }
4544
- }
4545
- return result;
4546
- }
4535
+ maxStartDepth
4547
4536
  );
4537
+ const count = C.getValue(TRANSFER_BUFFER, "i32");
4538
+ const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4539
+ const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4540
+ const result = new Array();
4541
+ this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4542
+ const captures = new Array();
4543
+ let address = startAddress;
4544
+ for (let i2 = 0; i2 < count; i2++) {
4545
+ const patternIndex = C.getValue(address, "i32");
4546
+ address += SIZE_OF_INT;
4547
+ const captureCount = C.getValue(address, "i32");
4548
+ address += SIZE_OF_INT;
4549
+ const captureIndex = C.getValue(address, "i32");
4550
+ address += SIZE_OF_INT;
4551
+ captures.length = captureCount;
4552
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4553
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4554
+ const capture = captures[captureIndex];
4555
+ const setProperties = this.setProperties[patternIndex];
4556
+ capture.setProperties = setProperties;
4557
+ const assertedProperties = this.assertedProperties[patternIndex];
4558
+ capture.assertedProperties = assertedProperties;
4559
+ const refutedProperties = this.refutedProperties[patternIndex];
4560
+ capture.refutedProperties = refutedProperties;
4561
+ result.push(capture);
4562
+ }
4563
+ }
4564
+ C._free(startAddress);
4565
+ C.currentQueryProgressCallback = null;
4566
+ return result;
4548
4567
  }
4549
4568
  /** Get the predicates for a given pattern. */
4550
4569
  predicatesForPattern(patternIndex) {
@@ -4585,25 +4604,23 @@ var Query = class {
4585
4604
  didExceedMatchLimit() {
4586
4605
  return this.exceededMatchLimit;
4587
4606
  }
4588
- /** Get the index where the given pattern starts in the query's source. */
4607
+ /** Get the byte offset where the given pattern starts in the query's source. */
4589
4608
  startIndexForPattern(patternIndex) {
4590
4609
  if (patternIndex >= this.predicates.length) {
4591
4610
  throw new Error(
4592
4611
  `Pattern index is ${patternIndex} but the pattern count is ${this.predicates.length}`
4593
4612
  );
4594
4613
  }
4595
- const byteOffset = C._ts_query_start_byte_for_pattern(this[0], patternIndex);
4596
- return indexForByteOffset(this.source, byteOffset);
4614
+ return C._ts_query_start_byte_for_pattern(this[0], patternIndex);
4597
4615
  }
4598
- /** Get the index where the given pattern ends in the query's source. */
4616
+ /** Get the byte offset where the given pattern ends in the query's source. */
4599
4617
  endIndexForPattern(patternIndex) {
4600
4618
  if (patternIndex >= this.predicates.length) {
4601
4619
  throw new Error(
4602
4620
  `Pattern index is ${patternIndex} but the pattern count is ${this.predicates.length}`
4603
4621
  );
4604
4622
  }
4605
- const byteOffset = C._ts_query_end_byte_for_pattern(this[0], patternIndex);
4606
- return indexForByteOffset(this.source, byteOffset);
4623
+ return C._ts_query_end_byte_for_pattern(this[0], patternIndex);
4607
4624
  }
4608
4625
  /** Get the number of patterns in the query. */
4609
4626
  patternCount() {
@@ -4622,91 +4639,15 @@ var Query = class {
4622
4639
  return C._ts_query_is_pattern_non_local(this[0], patternIndex) === 1;
4623
4640
  }
4624
4641
  /**
4625
- * Check if the step at the given index in the query's source is 'definite'.
4642
+ * Check if a given step in a query is 'definite'.
4626
4643
  *
4627
4644
  * A query step is 'definite' if its parent pattern will be guaranteed to
4628
4645
  * match successfully once it reaches the step.
4629
4646
  */
4630
- isPatternGuaranteedAtStep(index) {
4631
- const byteOffset = C.lengthBytesUTF8(this.source.slice(0, index));
4632
- return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteOffset) === 1;
4647
+ isPatternGuaranteedAtStep(byteIndex) {
4648
+ return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteIndex) === 1;
4633
4649
  }
4634
4650
  };
4635
- function prepareQuery(query, options) {
4636
- const parameters = {
4637
- startPosition: options.startPosition ?? ZERO_POINT,
4638
- endPosition: options.endPosition ?? ZERO_POINT,
4639
- startIndex: options.startIndex ?? 0,
4640
- endIndex: options.endIndex ?? 0,
4641
- startContainingPosition: options.startContainingPosition ?? ZERO_POINT,
4642
- endContainingPosition: options.endContainingPosition ?? ZERO_POINT,
4643
- startContainingIndex: options.startContainingIndex ?? 0,
4644
- endContainingIndex: options.endContainingIndex ?? 0,
4645
- matchLimit: options.matchLimit ?? 4294967295,
4646
- maxStartDepth: options.maxStartDepth ?? 4294967295,
4647
- progressCallback: options.progressCallback
4648
- };
4649
- const {
4650
- startPosition,
4651
- endPosition,
4652
- startIndex,
4653
- endIndex,
4654
- startContainingPosition,
4655
- endContainingPosition,
4656
- startContainingIndex,
4657
- endContainingIndex,
4658
- matchLimit
4659
- } = parameters;
4660
- if (typeof matchLimit !== "number") {
4661
- throw new Error("Arguments must be numbers");
4662
- }
4663
- query.matchLimit = matchLimit;
4664
- if (endIndex !== 0 && startIndex > endIndex) {
4665
- throw new Error("`startIndex` cannot be greater than `endIndex`");
4666
- }
4667
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4668
- throw new Error("`startPosition` cannot be greater than `endPosition`");
4669
- }
4670
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4671
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4672
- }
4673
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4674
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4675
- }
4676
- return parameters;
4677
- }
4678
- __name(prepareQuery, "prepareQuery");
4679
- function executeQuery(progressCallback, execute, updateState, readResult) {
4680
- const previousCallback = C.currentQueryProgressCallback;
4681
- let callbackFailure;
4682
- C.currentQueryProgressCallback = progressCallback ? (state) => {
4683
- try {
4684
- return progressCallback(state);
4685
- } catch (error) {
4686
- callbackFailure = { error };
4687
- return true;
4688
- }
4689
- } : null;
4690
- try {
4691
- execute();
4692
- } finally {
4693
- C.currentQueryProgressCallback = previousCallback;
4694
- }
4695
- const count = C.getValue(TRANSFER_BUFFER, "i32");
4696
- const address = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4697
- const exceededMatchLimit = Boolean(C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32"));
4698
- try {
4699
- updateState(exceededMatchLimit);
4700
- if (callbackFailure) {
4701
- throw callbackFailure.error;
4702
- }
4703
- return readResult(count, address);
4704
- } finally {
4705
- C._free(address);
4706
- updateState(exceededMatchLimit);
4707
- }
4708
- }
4709
- __name(executeQuery, "executeQuery");
4710
4651
  export {
4711
4652
  CaptureQuantifier,
4712
4653
  Edit,