add Range:tsquery() to query TS within a range
Some checks failed
NeoVim tests / code-quality (push) Has been cancelled

This commit is contained in:
2025-08-13 20:58:17 -06:00
parent bebefb601a
commit b74afdbfd5
2 changed files with 121 additions and 35 deletions

View File

@@ -289,6 +289,77 @@ describe('Range', function()
end)
end)
it('from_tsquery_caps with string array filter', function()
withbuf({
'{',
' "sample-key1": "sample-value1",',
' "sample-key2": "sample-value2"',
'}',
}, function()
vim.cmd.setfiletype 'json'
-- Place cursor in "sample-value1"
Pos.new(0, 2, 25):save_to_pos '.'
-- Query that captures both keys and values in pairs
local query = [[
(pair
key: _ @key
value: _ @value)
]]
local ranges = Range.from_line(0, 2):tsquery(query)
-- Should have both @key and @value captures for the first pair only
-- (since cursor is in sample-value1)
assert(ranges, 'Range should not be nil')
assert(ranges.key, 'Range.key should not be nil')
assert(ranges.value, 'Range.value should not be nil')
-- Should have exactly one key and one value
assert.are.same(#ranges.key, 1)
assert.are.same(#ranges.value, 1)
-- Check that we got sample-key1 and sample-value1
assert.are.same(ranges.key[1]:text(), '"sample-key1"')
assert.are.same(ranges.value[1]:text(), '"sample-value1"')
end)
-- Make sure this works when the match is on the last line:
withbuf({
'{"sample-key1": "sample-value1",',
'"sample-key2": "sample-value2"}',
}, function()
vim.cmd.setfiletype 'json'
-- Place cursor in "sample-value1"
Pos.new(0, 2, 25):save_to_pos '.'
-- Query that captures both keys and values in pairs
local query = [[
(pair
key: _ @key
value: _ @value)
]]
local ranges = Range.from_line(0, 2):tsquery(query)
-- Should have both @key and @value captures for the first pair only
-- (since cursor is in sample-value1)
assert(ranges, 'Range should not be nil')
assert(ranges.key, 'Range.key should not be nil')
assert(ranges.value, 'Range.value should not be nil')
-- Should have exactly one key and one value
assert.are.same(#ranges.key, 1)
assert.are.same(#ranges.value, 1)
-- Check that we got sample-key2 and sample-value2
assert.are.same(ranges.key[1]:text(), '"sample-key2"')
assert.are.same(ranges.value[1]:text(), '"sample-value2"')
end)
end)
it('should get nearest block', function()
withbuf({
'this is a {',