Add containing range APIs to query cursor

Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-authored-by: dino <dinojoaocosta@gmail.com>
Co-authored-by: John Tur <john-tur@outlook.com>
Co-authored-by: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com>
Co-authored-by: dino <dinojoaocosta@gmail.com>
Co-authored-by: Will Lillis <will.lillis24@gmail.com>
This commit is contained in:
Piotr Osiewicz 2025-10-02 19:02:05 +02:00 committed by Will Lillis
parent 7d3feeae9a
commit c0b1710f8a
13 changed files with 420 additions and 34 deletions

View file

@ -96,6 +96,64 @@ describe('Query', () => {
]);
});
it('can search in contained within point ranges', () => {
tree = parser.parse(`[
{"key1": "value1"},
{"key2": "value2"},
{"key3": "value3"},
{"key4": "value4"},
{"key5": "value5"},
{"key6": "value6"},
{"key7": "value7"},
{"key8": "value8"},
{"key9": "value9"},
{"key10": "value10"},
{"key11": "value11"},
{"key12": "value12"},
]`)!;
query = new Query(JavaScript, '("[" @l_bracket "]" @r_bracket) ("{" @l_brace "}" @r_brace)');
const matches = query.matches(
tree.rootNode,
{
startContainingPosition: { row: 5, column: 0 },
endContainingPosition: { row: 7, column: 0 },
}
);
expect(formatMatches(matches)).toEqual([
{ patternIndex: 1, captures: [{ patternIndex: 1, name: 'l_brace', text: '{' }, { patternIndex: 1, name: 'r_brace', text: '}' },] },
{ patternIndex: 1, captures: [{ patternIndex: 1, name: 'l_brace', text: '{' }, { patternIndex: 1, name: 'r_brace', text: '}' },] },
]);
});
it('can search in contained within byte ranges', () => {
tree = parser.parse(`[
{"key1": "value1"},
{"key2": "value2"},
{"key3": "value3"},
{"key4": "value4"},
{"key5": "value5"},
{"key6": "value6"},
{"key7": "value7"},
{"key8": "value8"},
{"key9": "value9"},
{"key10": "value10"},
{"key11": "value11"},
{"key12": "value12"},
]`)!;
query = new Query(JavaScript, '("[" @l_bracket "]" @r_bracket) ("{" @l_brace "}" @r_brace)');
const matches = query.matches(
tree.rootNode,
{
startContainingIndex: 290,
endContainingIndex: 432,
}
);
expect(formatMatches(matches)).toEqual([
{ patternIndex: 1, captures: [{ patternIndex: 1, name: 'l_brace', text: '{' }, { patternIndex: 1, name: 'r_brace', text: '}' },] },
{ patternIndex: 1, captures: [{ patternIndex: 1, name: 'l_brace', text: '{' }, { patternIndex: 1, name: 'r_brace', text: '}' },] },
]);
});
it('handles predicates that compare the text of capture to literal strings', () => {
tree = parser.parse(`
giraffe(1, 2, []);