@willbooster/web-tree-sitter 2.0.20 → 2.0.22

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.
@@ -3861,84 +3861,72 @@ var Query = class {
3861
3861
  * @param {QueryOptions} options - Options for query execution.
3862
3862
  */
3863
3863
  matches(node, options = {}) {
3864
- const startPosition = options.startPosition ?? ZERO_POINT;
3865
- const endPosition = options.endPosition ?? ZERO_POINT;
3866
- const startIndex = options.startIndex ?? 0;
3867
- const endIndex = options.endIndex ?? 0;
3868
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
3869
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
3870
- const startContainingIndex = options.startContainingIndex ?? 0;
3871
- const endContainingIndex = options.endContainingIndex ?? 0;
3872
- const matchLimit = options.matchLimit ?? 4294967295;
3873
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
3874
- const progressCallback = options.progressCallback;
3875
- if (typeof matchLimit !== "number") {
3876
- throw new Error("Arguments must be numbers");
3877
- }
3878
- this.matchLimit = matchLimit;
3879
- if (endIndex !== 0 && startIndex > endIndex) {
3880
- throw new Error("`startIndex` cannot be greater than `endIndex`");
3881
- }
3882
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
3883
- throw new Error("`startPosition` cannot be greater than `endPosition`");
3884
- }
3885
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
3886
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
3887
- }
3888
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
3889
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
3890
- }
3891
- if (progressCallback) {
3892
- C.currentQueryProgressCallback = progressCallback;
3893
- }
3894
- marshalNode(node);
3895
- C._ts_query_matches_wasm(
3896
- this[0],
3897
- node.tree[0],
3898
- startPosition.row,
3899
- startPosition.column,
3900
- endPosition.row,
3901
- endPosition.column,
3864
+ const {
3865
+ startPosition,
3866
+ endPosition,
3902
3867
  startIndex,
3903
3868
  endIndex,
3904
- startContainingPosition.row,
3905
- startContainingPosition.column,
3906
- endContainingPosition.row,
3907
- endContainingPosition.column,
3869
+ startContainingPosition,
3870
+ endContainingPosition,
3908
3871
  startContainingIndex,
3909
3872
  endContainingIndex,
3910
3873
  matchLimit,
3911
- maxStartDepth
3912
- );
3913
- const rawCount = C.getValue(TRANSFER_BUFFER, "i32");
3914
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
3915
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
3916
- const result = new Array(rawCount);
3917
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
3918
- let filteredCount = 0;
3919
- let address = startAddress;
3920
- for (let i2 = 0; i2 < rawCount; i2++) {
3921
- const patternIndex = C.getValue(address, "i32");
3922
- address += SIZE_OF_INT;
3923
- const captureCount = C.getValue(address, "i32");
3924
- address += SIZE_OF_INT;
3925
- const captures = new Array(captureCount);
3926
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
3927
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
3928
- result[filteredCount] = { patternIndex, captures };
3929
- const setProperties = this.setProperties[patternIndex];
3930
- result[filteredCount].setProperties = setProperties;
3931
- const assertedProperties = this.assertedProperties[patternIndex];
3932
- result[filteredCount].assertedProperties = assertedProperties;
3933
- const refutedProperties = this.refutedProperties[patternIndex];
3934
- result[filteredCount].refutedProperties = refutedProperties;
3935
- filteredCount++;
3874
+ maxStartDepth,
3875
+ progressCallback
3876
+ } = prepareQuery(this, options);
3877
+ return executeQuery(
3878
+ progressCallback,
3879
+ () => {
3880
+ marshalNode(node);
3881
+ C._ts_query_matches_wasm(
3882
+ this[0],
3883
+ node.tree[0],
3884
+ startPosition.row,
3885
+ startPosition.column,
3886
+ endPosition.row,
3887
+ endPosition.column,
3888
+ startIndex,
3889
+ endIndex,
3890
+ startContainingPosition.row,
3891
+ startContainingPosition.column,
3892
+ endContainingPosition.row,
3893
+ endContainingPosition.column,
3894
+ startContainingIndex,
3895
+ endContainingIndex,
3896
+ matchLimit,
3897
+ maxStartDepth
3898
+ );
3899
+ },
3900
+ (exceededMatchLimit) => {
3901
+ this.matchLimit = matchLimit;
3902
+ this.exceededMatchLimit = exceededMatchLimit;
3903
+ },
3904
+ (rawCount, startAddress) => {
3905
+ const result = new Array(rawCount);
3906
+ let filteredCount = 0;
3907
+ let address = startAddress;
3908
+ for (let i2 = 0; i2 < rawCount; i2++) {
3909
+ const patternIndex = C.getValue(address, "i32");
3910
+ address += SIZE_OF_INT;
3911
+ const captureCount = C.getValue(address, "i32");
3912
+ address += SIZE_OF_INT;
3913
+ const captures = new Array(captureCount);
3914
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
3915
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
3916
+ result[filteredCount] = { patternIndex, captures };
3917
+ const setProperties = this.setProperties[patternIndex];
3918
+ result[filteredCount].setProperties = setProperties;
3919
+ const assertedProperties = this.assertedProperties[patternIndex];
3920
+ result[filteredCount].assertedProperties = assertedProperties;
3921
+ const refutedProperties = this.refutedProperties[patternIndex];
3922
+ result[filteredCount].refutedProperties = refutedProperties;
3923
+ filteredCount++;
3924
+ }
3925
+ }
3926
+ result.length = filteredCount;
3927
+ return result;
3936
3928
  }
3937
- }
3938
- result.length = filteredCount;
3939
- C._free(startAddress);
3940
- C.currentQueryProgressCallback = null;
3941
- return result;
3929
+ );
3942
3930
  }
3943
3931
  /**
3944
3932
  * Iterate over all of the individual captures in the order that they
@@ -3952,85 +3940,71 @@ var Query = class {
3952
3940
  * @param {QueryOptions} options - Options for query execution.
3953
3941
  */
3954
3942
  captures(node, options = {}) {
3955
- const startPosition = options.startPosition ?? ZERO_POINT;
3956
- const endPosition = options.endPosition ?? ZERO_POINT;
3957
- const startIndex = options.startIndex ?? 0;
3958
- const endIndex = options.endIndex ?? 0;
3959
- const startContainingPosition = options.startContainingPosition ?? ZERO_POINT;
3960
- const endContainingPosition = options.endContainingPosition ?? ZERO_POINT;
3961
- const startContainingIndex = options.startContainingIndex ?? 0;
3962
- const endContainingIndex = options.endContainingIndex ?? 0;
3963
- const matchLimit = options.matchLimit ?? 4294967295;
3964
- const maxStartDepth = options.maxStartDepth ?? 4294967295;
3965
- const progressCallback = options.progressCallback;
3966
- if (typeof matchLimit !== "number") {
3967
- throw new Error("Arguments must be numbers");
3968
- }
3969
- this.matchLimit = matchLimit;
3970
- if (endIndex !== 0 && startIndex > endIndex) {
3971
- throw new Error("`startIndex` cannot be greater than `endIndex`");
3972
- }
3973
- if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
3974
- throw new Error("`startPosition` cannot be greater than `endPosition`");
3975
- }
3976
- if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
3977
- throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
3978
- }
3979
- if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
3980
- throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
3981
- }
3982
- if (progressCallback) {
3983
- C.currentQueryProgressCallback = progressCallback;
3984
- }
3985
- marshalNode(node);
3986
- C._ts_query_captures_wasm(
3987
- this[0],
3988
- node.tree[0],
3989
- startPosition.row,
3990
- startPosition.column,
3991
- endPosition.row,
3992
- endPosition.column,
3943
+ const {
3944
+ startPosition,
3945
+ endPosition,
3993
3946
  startIndex,
3994
3947
  endIndex,
3995
- startContainingPosition.row,
3996
- startContainingPosition.column,
3997
- endContainingPosition.row,
3998
- endContainingPosition.column,
3948
+ startContainingPosition,
3949
+ endContainingPosition,
3999
3950
  startContainingIndex,
4000
3951
  endContainingIndex,
4001
3952
  matchLimit,
4002
- maxStartDepth
4003
- );
4004
- const count = C.getValue(TRANSFER_BUFFER, "i32");
4005
- const startAddress = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4006
- const didExceedMatchLimit = C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32");
4007
- const result = new Array();
4008
- this.exceededMatchLimit = Boolean(didExceedMatchLimit);
4009
- const captures = new Array();
4010
- let address = startAddress;
4011
- for (let i2 = 0; i2 < count; i2++) {
4012
- const patternIndex = C.getValue(address, "i32");
4013
- address += SIZE_OF_INT;
4014
- const captureCount = C.getValue(address, "i32");
4015
- address += SIZE_OF_INT;
4016
- const captureIndex = C.getValue(address, "i32");
4017
- address += SIZE_OF_INT;
4018
- captures.length = captureCount;
4019
- address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
4020
- if (this.textPredicates[patternIndex].every((p) => p(captures))) {
4021
- const capture = captures[captureIndex];
4022
- const setProperties = this.setProperties[patternIndex];
4023
- capture.setProperties = setProperties;
4024
- const assertedProperties = this.assertedProperties[patternIndex];
4025
- capture.assertedProperties = assertedProperties;
4026
- const refutedProperties = this.refutedProperties[patternIndex];
4027
- capture.refutedProperties = refutedProperties;
4028
- result.push(capture);
3953
+ maxStartDepth,
3954
+ progressCallback
3955
+ } = prepareQuery(this, options);
3956
+ const callback = progressCallback;
3957
+ return executeQuery(
3958
+ callback ? (state) => callback(state) === true : void 0,
3959
+ () => {
3960
+ marshalNode(node);
3961
+ C._ts_query_captures_wasm(
3962
+ this[0],
3963
+ node.tree[0],
3964
+ startPosition.row,
3965
+ startPosition.column,
3966
+ endPosition.row,
3967
+ endPosition.column,
3968
+ startIndex,
3969
+ endIndex,
3970
+ startContainingPosition.row,
3971
+ startContainingPosition.column,
3972
+ endContainingPosition.row,
3973
+ endContainingPosition.column,
3974
+ startContainingIndex,
3975
+ endContainingIndex,
3976
+ matchLimit,
3977
+ maxStartDepth
3978
+ );
3979
+ },
3980
+ (exceededMatchLimit) => {
3981
+ this.matchLimit = matchLimit;
3982
+ this.exceededMatchLimit = exceededMatchLimit;
3983
+ },
3984
+ (count, startAddress) => {
3985
+ const result = new Array();
3986
+ const captures = new Array();
3987
+ let address = startAddress;
3988
+ for (let i2 = 0; i2 < count; i2++) {
3989
+ const patternIndex = C.getValue(address, "i32");
3990
+ address += SIZE_OF_INT;
3991
+ const captureCount = C.getValue(address, "i32");
3992
+ address += SIZE_OF_INT;
3993
+ const captureIndex = C.getValue(address, "i32");
3994
+ address += SIZE_OF_INT;
3995
+ captures.length = captureCount;
3996
+ address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
3997
+ if (this.textPredicates[patternIndex].every((p) => p(captures))) {
3998
+ const capture = captures[captureIndex];
3999
+ capture.setProperties = this.setProperties[patternIndex];
4000
+ capture.assertedProperties = this.assertedProperties[patternIndex];
4001
+ capture.refutedProperties = this.refutedProperties[patternIndex];
4002
+ result.push(capture);
4003
+ }
4004
+ }
4005
+ return result;
4029
4006
  }
4030
- }
4031
- C._free(startAddress);
4032
- C.currentQueryProgressCallback = null;
4033
- return result;
4007
+ );
4034
4008
  }
4035
4009
  /** Get the predicates for a given pattern. */
4036
4010
  predicatesForPattern(patternIndex) {
@@ -4115,6 +4089,81 @@ var Query = class {
4115
4089
  return C._ts_query_is_pattern_guaranteed_at_step(this[0], byteIndex) === 1;
4116
4090
  }
4117
4091
  };
4092
+ function prepareQuery(query, options) {
4093
+ const parameters = {
4094
+ startPosition: options.startPosition ?? ZERO_POINT,
4095
+ endPosition: options.endPosition ?? ZERO_POINT,
4096
+ startIndex: options.startIndex ?? 0,
4097
+ endIndex: options.endIndex ?? 0,
4098
+ startContainingPosition: options.startContainingPosition ?? ZERO_POINT,
4099
+ endContainingPosition: options.endContainingPosition ?? ZERO_POINT,
4100
+ startContainingIndex: options.startContainingIndex ?? 0,
4101
+ endContainingIndex: options.endContainingIndex ?? 0,
4102
+ matchLimit: options.matchLimit ?? 4294967295,
4103
+ maxStartDepth: options.maxStartDepth ?? 4294967295,
4104
+ progressCallback: options.progressCallback
4105
+ };
4106
+ const {
4107
+ startPosition,
4108
+ endPosition,
4109
+ startIndex,
4110
+ endIndex,
4111
+ startContainingPosition,
4112
+ endContainingPosition,
4113
+ startContainingIndex,
4114
+ endContainingIndex,
4115
+ matchLimit
4116
+ } = parameters;
4117
+ if (typeof matchLimit !== "number") {
4118
+ throw new Error("Arguments must be numbers");
4119
+ }
4120
+ query.matchLimit = matchLimit;
4121
+ if (endIndex !== 0 && startIndex > endIndex) {
4122
+ throw new Error("`startIndex` cannot be greater than `endIndex`");
4123
+ }
4124
+ if (endPosition !== ZERO_POINT && (startPosition.row > endPosition.row || startPosition.row === endPosition.row && startPosition.column > endPosition.column)) {
4125
+ throw new Error("`startPosition` cannot be greater than `endPosition`");
4126
+ }
4127
+ if (endContainingIndex !== 0 && startContainingIndex > endContainingIndex) {
4128
+ throw new Error("`startContainingIndex` cannot be greater than `endContainingIndex`");
4129
+ }
4130
+ if (endContainingPosition !== ZERO_POINT && (startContainingPosition.row > endContainingPosition.row || startContainingPosition.row === endContainingPosition.row && startContainingPosition.column > endContainingPosition.column)) {
4131
+ throw new Error("`startContainingPosition` cannot be greater than `endContainingPosition`");
4132
+ }
4133
+ return parameters;
4134
+ }
4135
+ __name(prepareQuery, "prepareQuery");
4136
+ function executeQuery(progressCallback, execute, updateState, readResult) {
4137
+ const previousCallback = C.currentQueryProgressCallback;
4138
+ let callbackFailure;
4139
+ C.currentQueryProgressCallback = progressCallback ? (state) => {
4140
+ try {
4141
+ return progressCallback(state);
4142
+ } catch (error) {
4143
+ callbackFailure = { error };
4144
+ return true;
4145
+ }
4146
+ } : null;
4147
+ try {
4148
+ execute();
4149
+ } finally {
4150
+ C.currentQueryProgressCallback = previousCallback;
4151
+ }
4152
+ const count = C.getValue(TRANSFER_BUFFER, "i32");
4153
+ const address = C.getValue(TRANSFER_BUFFER + SIZE_OF_INT, "i32");
4154
+ const exceededMatchLimit = Boolean(C.getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, "i32"));
4155
+ try {
4156
+ updateState(exceededMatchLimit);
4157
+ if (callbackFailure) {
4158
+ throw callbackFailure.error;
4159
+ }
4160
+ return readResult(count, address);
4161
+ } finally {
4162
+ C._free(address);
4163
+ updateState(exceededMatchLimit);
4164
+ }
4165
+ }
4166
+ __name(executeQuery, "executeQuery");
4118
4167
  export {
4119
4168
  CaptureQuantifier,
4120
4169
  Edit,