feat: add a patternIndex field to QueryCapture
This lets users look up the predicates associated with a given `QueryCapture`, by using the `patternIndex` field and calling `predicatesForPattern`
This commit is contained in:
parent
d60c6f163d
commit
e874d2d8f8
2 changed files with 19 additions and 10 deletions
|
|
@ -2,7 +2,7 @@ import { Edit, INTERNAL, Point, Range, SIZE_OF_INT, SIZE_OF_NODE, SIZE_OF_POINT,
|
|||
import { Node } from "./node";
|
||||
import { Tree } from "./tree";
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
import { Query, type QueryMatch } from "./query";
|
||||
import { Query, QueryCapture, type QueryMatch } from "./query";
|
||||
import { TreeCursor } from "./tree_cursor";
|
||||
import { TRANSFER_BUFFER } from "./parser";
|
||||
|
||||
|
|
@ -11,13 +11,19 @@ import { TRANSFER_BUFFER } from "./parser";
|
|||
*
|
||||
* Unmarshals a {@link QueryMatch} to the transfer buffer.
|
||||
*/
|
||||
export function unmarshalCaptures(query: Query, tree: Tree, address: number, result: {name: string, node: Node}[]) {
|
||||
export function unmarshalCaptures(
|
||||
query: Query,
|
||||
tree: Tree,
|
||||
address: number,
|
||||
patternIndex: number,
|
||||
result: QueryCapture[]
|
||||
) {
|
||||
for (let i = 0, n = result.length; i < n; i++) {
|
||||
const captureIndex = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const node = unmarshalNode(tree, address)!;
|
||||
address += SIZE_OF_NODE;
|
||||
result[i] = {name: query.captureNames[captureIndex], node};
|
||||
result[i] = {patternIndex, name: query.captureNames[captureIndex], node};
|
||||
}
|
||||
return address;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,6 +88,9 @@ export interface QueryPredicate {
|
|||
* {@link Query}.
|
||||
*/
|
||||
export interface QueryCapture {
|
||||
/** The index of the pattern that matched. */
|
||||
patternIndex: number;
|
||||
|
||||
/** The name of the capture */
|
||||
name: string;
|
||||
|
||||
|
|
@ -307,7 +310,7 @@ export class Query {
|
|||
address += SIZE_OF_INT;
|
||||
|
||||
const captures = new Array<QueryCapture>(captureCount);
|
||||
address = unmarshalCaptures(this, node.tree, address, captures);
|
||||
address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
|
||||
|
||||
if (this.textPredicates[patternIndex].every((p) => p(captures))) {
|
||||
result[filteredCount] = { pattern: patternIndex, patternIndex, captures };
|
||||
|
|
@ -396,7 +399,7 @@ export class Query {
|
|||
const captures = new Array<QueryCapture>();
|
||||
let address = startAddress;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const pattern = C.getValue(address, 'i32');
|
||||
const patternIndex = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureCount = C.getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
|
|
@ -404,15 +407,15 @@ export class Query {
|
|||
address += SIZE_OF_INT;
|
||||
|
||||
captures.length = captureCount;
|
||||
address = unmarshalCaptures(this, node.tree, address, captures);
|
||||
address = unmarshalCaptures(this, node.tree, address, patternIndex, captures);
|
||||
|
||||
if (this.textPredicates[pattern].every(p => p(captures))) {
|
||||
if (this.textPredicates[patternIndex].every(p => p(captures))) {
|
||||
const capture = captures[captureIndex];
|
||||
const setProperties = this.setProperties[pattern];
|
||||
const setProperties = this.setProperties[patternIndex];
|
||||
capture.setProperties = setProperties;
|
||||
const assertedProperties = this.assertedProperties[pattern];
|
||||
const assertedProperties = this.assertedProperties[patternIndex];
|
||||
capture.assertedProperties = assertedProperties;
|
||||
const refutedProperties = this.refutedProperties[pattern];
|
||||
const refutedProperties = this.refutedProperties[patternIndex];
|
||||
capture.refutedProperties = refutedProperties;
|
||||
result.push(capture);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue