@willbooster/web-tree-sitter 2.0.20 → 2.0.21

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.
@@ -4429,84 +4429,72 @@ var Query = class {
4429
4429
  * @param {QueryOptions} options - Options for query execution.
4430
4430
  */
4431
4431
  matches(node, options = {}) {
4432
- const startPosition = options.startPosition ?? ZERO_POINT;
4433
- const endPosition = options.endPosition ?? ZERO_POINT;
4434
- const startIndex = options.startIndex ?? 0;
4435
- const endIndex = options.endIndex ?? 0;
4436
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4437
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4438
- const startContainingIndex = options.startContainingIndex ?? 0;
4439
- const endContainingIndex = options.endContainingIndex ?? 0;
4440
- const matchLimit = options.matchLimit ?? 4294967295;
4441
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
4442
- const progressCallback = options.progressCallback;
4443
- if (typeof matchLimit !== "number") {
4444
- throw new Error("Arguments must be numbers");
4445
- }
4446
- this.matchLimit = matchLimit;
4447
- if (endIndex !== 0 && startIndex > endIndex) {
4448
- throw new Error("`startIndex` cannot be greater than `endIndex`");
4449
- }
4450
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4451
- throw new Error("`startPosition` cannot be greater than `endPosition`");
4452
- }
4453
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4454
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4455
- }
4456
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4457
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4458
- }
4459
- if (progressCallback) {
4460
- C.currentQueryProgressCallback = progressCallback;
4461
- }
4462
- marshalNode(node);
4463
- C._ts_query_matches_wasm(
4464
- this[0],
4465
- node.tree[0],
4466
- startPosition.row,
4467
- startPosition.column,
4468
- endPosition.row,
4469
- endPosition.column,
4432
+ const {
4433
+ startPosition,
4434
+ endPosition,
4470
4435
  startIndex,
4471
4436
  endIndex,
4472
- startContainingPosition.row,
4473
- startContainingPosition.column,
4474
- endContainingPosition.row,
4475
- endContainingPosition.column,
4437
+ startContainingPosition,
4438
+ endContainingPosition,
4476
4439
  startContainingIndex,
4477
4440
  endContainingIndex,
4478
4441
  matchLimit,
4479
- maxStartDepth
4480
- );
4481
- const rawCount = C.getValue(TRANSFER_BUFFER, "i32");
4482
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4483
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4484
- const result = new Array(rawCount);
4485
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4486
- let filteredCount = 0;
4487
- let address = startAddress;
4488
- for (let i2 = 0; i2 < rawCount; i2++) {
4489
- const patternIndex = C.getValue(address, "i32");
4490
- address += SIZE_OF_INT;
4491
- const captureCount = C.getValue(address, "i32");
4492
- address += SIZE_OF_INT;
4493
- const captures = new Array(captureCount);
4494
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4495
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4496
- result[filteredCount] = { patternIndex, captures };
4497
- const setProperties = this.setProperties[patternIndex];
4498
- result[filteredCount].setProperties = setProperties;
4499
- const assertedProperties = this.assertedProperties[patternIndex];
4500
- result[filteredCount].assertedProperties = assertedProperties;
4501
- const refutedProperties = this.refutedProperties[patternIndex];
4502
- result[filteredCount].refutedProperties = refutedProperties;
4503
- filteredCount++;
4442
+ maxStartDepth,
4443
+ progressCallback
4444
+ } = prepareQuery(this, options);
4445
+ return executeQuery(
4446
+ progressCallback,
4447
+ () => {
4448
+ marshalNode(node);
4449
+ C._ts_query_matches_wasm(
4450
+ this[0],
4451
+ node.tree[0],
4452
+ startPosition.row,
4453
+ startPosition.column,
4454
+ endPosition.row,
4455
+ endPosition.column,
4456
+ startIndex,
4457
+ endIndex,
4458
+ startContainingPosition.row,
4459
+ startContainingPosition.column,
4460
+ endContainingPosition.row,
4461
+ endContainingPosition.column,
4462
+ startContainingIndex,
4463
+ endContainingIndex,
4464
+ matchLimit,
4465
+ maxStartDepth
4466
+ );
4467
+ },
4468
+ (exceededMatchLimit) => {
4469
+ this.matchLimit = matchLimit;
4470
+ this.exceededMatchLimit = exceededMatchLimit;
4471
+ },
4472
+ (rawCount, startAddress) => {
4473
+ const result = new Array(rawCount);
4474
+ let filteredCount = 0;
4475
+ let address = startAddress;
4476
+ for (let i2 = 0; i2 < rawCount; i2++) {
4477
+ const patternIndex = C.getValue(address, "i32");
4478
+ address += SIZE_OF_INT;
4479
+ const captureCount = C.getValue(address, "i32");
4480
+ address += SIZE_OF_INT;
4481
+ const captures = new Array(captureCount);
4482
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4483
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4484
+ result[filteredCount] = { patternIndex, captures };
4485
+ const setProperties = this.setProperties[patternIndex];
4486
+ result[filteredCount].setProperties = setProperties;
4487
+ const assertedProperties = this.assertedProperties[patternIndex];
4488
+ result[filteredCount].assertedProperties = assertedProperties;
4489
+ const refutedProperties = this.refutedProperties[patternIndex];
4490
+ result[filteredCount].refutedProperties = refutedProperties;
4491
+ filteredCount++;
4492
+ }
4493
+ }
4494
+ result.length = filteredCount;
4495
+ return result;
4504
4496
  }
4505
- }
4506
- result.length = filteredCount;
4507
- C._free(startAddress);
4508
- C.currentQueryProgressCallback = null;
4509
- return result;
4497
+ );
4510
4498
  }
4511
4499
  /**
4512
4500
  * Iterate over all of the individual captures in the order that they
@@ -4520,85 +4508,71 @@ var Query = class {
4520
4508
  * @param {QueryOptions} options - Options for query execution.
4521
4509
  */
4522
4510
  captures(node, options = {}) {
4523
- const startPosition = options.startPosition ?? ZERO_POINT;
4524
- const endPosition = options.endPosition ?? ZERO_POINT;
4525
- const startIndex = options.startIndex ?? 0;
4526
- const endIndex = options.endIndex ?? 0;
4527
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
4528
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
4529
- const startContainingIndex = options.startContainingIndex ?? 0;
4530
- const endContainingIndex = options.endContainingIndex ?? 0;
4531
- const matchLimit = options.matchLimit ?? 4294967295;
4532
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
4533
- const progressCallback = options.progressCallback;
4534
- if (typeof matchLimit !== "number") {
4535
- throw new Error("Arguments must be numbers");
4536
- }
4537
- this.matchLimit = matchLimit;
4538
- if (endIndex !== 0 && startIndex > endIndex) {
4539
- throw new Error("`startIndex` cannot be greater than `endIndex`");
4540
- }
4541
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4542
- throw new Error("`startPosition` cannot be greater than `endPosition`");
4543
- }
4544
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4545
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4546
- }
4547
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4548
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4549
- }
4550
- if (progressCallback) {
4551
- C.currentQueryProgressCallback = progressCallback;
4552
- }
4553
- marshalNode(node);
4554
- C._ts_query_captures_wasm(
4555
- this[0],
4556
- node.tree[0],
4557
- startPosition.row,
4558
- startPosition.column,
4559
- endPosition.row,
4560
- endPosition.column,
4511
+ const {
4512
+ startPosition,
4513
+ endPosition,
4561
4514
  startIndex,
4562
4515
  endIndex,
4563
- startContainingPosition.row,
4564
- startContainingPosition.column,
4565
- endContainingPosition.row,
4566
- endContainingPosition.column,
4516
+ startContainingPosition,
4517
+ endContainingPosition,
4567
4518
  startContainingIndex,
4568
4519
  endContainingIndex,
4569
4520
  matchLimit,
4570
- maxStartDepth
4571
- );
4572
- const count = C.getValue(TRANSFER_BUFFER, "i32");
4573
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4574
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4575
- const result = new Array();
4576
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4577
- const captures = new Array();
4578
- let address = startAddress;
4579
- for (let i2 = 0; i2 < count; i2++) {
4580
- const patternIndex = C.getValue(address, "i32");
4581
- address += SIZE_OF_INT;
4582
- const captureCount = C.getValue(address, "i32");
4583
- address += SIZE_OF_INT;
4584
- const captureIndex = C.getValue(address, "i32");
4585
- address += SIZE_OF_INT;
4586
- captures.length = captureCount;
4587
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4588
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4589
- const capture = captures[captureIndex];
4590
- const setProperties = this.setProperties[patternIndex];
4591
- capture.setProperties = setProperties;
4592
- const assertedProperties = this.assertedProperties[patternIndex];
4593
- capture.assertedProperties = assertedProperties;
4594
- const refutedProperties = this.refutedProperties[patternIndex];
4595
- capture.refutedProperties = refutedProperties;
4596
- result.push(capture);
4521
+ maxStartDepth,
4522
+ progressCallback
4523
+ } = prepareQuery(this, options);
4524
+ const callback = progressCallback;
4525
+ return executeQuery(
4526
+ callback ? (state) => callback(state) === true : void 0,
4527
+ () => {
4528
+ marshalNode(node);
4529
+ C._ts_query_captures_wasm(
4530
+ this[0],
4531
+ node.tree[0],
4532
+ startPosition.row,
4533
+ startPosition.column,
4534
+ endPosition.row,
4535
+ endPosition.column,
4536
+ startIndex,
4537
+ endIndex,
4538
+ startContainingPosition.row,
4539
+ startContainingPosition.column,
4540
+ endContainingPosition.row,
4541
+ endContainingPosition.column,
4542
+ startContainingIndex,
4543
+ endContainingIndex,
4544
+ matchLimit,
4545
+ maxStartDepth
4546
+ );
4547
+ },
4548
+ (exceededMatchLimit) => {
4549
+ this.matchLimit = matchLimit;
4550
+ this.exceededMatchLimit = exceededMatchLimit;
4551
+ },
4552
+ (count, startAddress) => {
4553
+ const result = new Array();
4554
+ const captures = new Array();
4555
+ let address = startAddress;
4556
+ for (let i2 = 0; i2 < count; i2++) {
4557
+ const patternIndex = C.getValue(address, "i32");
4558
+ address += SIZE_OF_INT;
4559
+ const captureCount = C.getValue(address, "i32");
4560
+ address += SIZE_OF_INT;
4561
+ const captureIndex = C.getValue(address, "i32");
4562
+ address += SIZE_OF_INT;
4563
+ captures.length = captureCount;
4564
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4565
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4566
+ const capture = captures[captureIndex];
4567
+ capture.setProperties = this.setProperties[patternIndex];
4568
+ capture.assertedProperties = this.assertedProperties[patternIndex];
4569
+ capture.refutedProperties = this.refutedProperties[patternIndex];
4570
+ result.push(capture);
4571
+ }
4572
+ }
4573
+ return result;
4597
4574
  }
4598
- }
4599
- C._free(startAddress);
4600
- C.currentQueryProgressCallback = null;
4601
- return result;
4575
+ );
4602
4576
  }
4603
4577
  /** Get the predicates for a given pattern. */
4604
4578
  predicatesForPattern(patternIndex) {
@@ -4683,6 +4657,81 @@ var Query = class {
4683
4657
  return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteIndex) === 1;
4684
4658
  }
4685
4659
  };
4660
+ function prepareQuery(query, options) {
4661
+ const parameters = {
4662
+ startPosition: options.startPosition ?? ZERO_POINT,
4663
+ endPosition: options.endPosition ?? ZERO_POINT,
4664
+ startIndex: options.startIndex ?? 0,
4665
+ endIndex: options.endIndex ?? 0,
4666
+ startContainingPosition: options.startContainingPosition ?? ZERO_POINT,
4667
+ endContainingPosition: options.endContainingPosition ?? ZERO_POINT,
4668
+ startContainingIndex: options.startContainingIndex ?? 0,
4669
+ endContainingIndex: options.endContainingIndex ?? 0,
4670
+ matchLimit: options.matchLimit ?? 4294967295,
4671
+ maxStartDepth: options.maxStartDepth ?? 4294967295,
4672
+ progressCallback: options.progressCallback
4673
+ };
4674
+ const {
4675
+ startPosition,
4676
+ endPosition,
4677
+ startIndex,
4678
+ endIndex,
4679
+ startContainingPosition,
4680
+ endContainingPosition,
4681
+ startContainingIndex,
4682
+ endContainingIndex,
4683
+ matchLimit
4684
+ } = parameters;
4685
+ if (typeof matchLimit !== "number") {
4686
+ throw new Error("Arguments must be numbers");
4687
+ }
4688
+ query.matchLimit = matchLimit;
4689
+ if (endIndex !== 0 && startIndex > endIndex) {
4690
+ throw new Error("`startIndex` cannot be greater than `endIndex`");
4691
+ }
4692
+ if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4693
+ throw new Error("`startPosition` cannot be greater than `endPosition`");
4694
+ }
4695
+ if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4696
+ throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4697
+ }
4698
+ if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4699
+ throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4700
+ }
4701
+ return parameters;
4702
+ }
4703
+ __name(prepareQuery, "prepareQuery");
4704
+ function executeQuery(progressCallback, execute, updateState, readResult) {
4705
+ const previousCallback = C.currentQueryProgressCallback;
4706
+ let callbackFailure;
4707
+ C.currentQueryProgressCallback = progressCallback ? (state) => {
4708
+ try {
4709
+ return progressCallback(state);
4710
+ } catch (error) {
4711
+ callbackFailure = { error };
4712
+ return true;
4713
+ }
4714
+ } : null;
4715
+ try {
4716
+ execute();
4717
+ } finally {
4718
+ C.currentQueryProgressCallback = previousCallback;
4719
+ }
4720
+ const count = C.getValue(TRANSFER_BUFFER, "i32");
4721
+ const address = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4722
+ const exceededMatchLimit = Boolean(C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32"));
4723
+ try {
4724
+ updateState(exceededMatchLimit);
4725
+ if (callbackFailure) {
4726
+ throw callbackFailure.error;
4727
+ }
4728
+ return readResult(count, address);
4729
+ } finally {
4730
+ C._free(address);
4731
+ updateState(exceededMatchLimit);
4732
+ }
4733
+ }
4734
+ __name(executeQuery, "executeQuery");
4686
4735
  export {
4687
4736
  CaptureQuantifier,
4688
4737
  Edit,