I want to filter string nodes using treesitter like this:
bar 'foo' 2
> bar 2
But when the closing delimiter is not present then don’t filter.
bar 'foo
> bar 'foo
This works fine in other languages, like python, but in lua, it treats a non-closed delimiter as a string
bar 'foo
> bar
(in lua)
Are there any ways to not have this behavior or do I need to write a special case for lua?
That’s just the way the Lua grammar implementation works. Let’s take a sample. Here is a test buffer:
bar 'foo'
bar 'foo
We can now loot at the tree:
function_call [0, 0] - [0, 9]
name: identifier [0, 0] - [0, 3]
arguments: arguments [0, 4] - [0, 9]
string [0, 4] - [0, 9]
start: "string_start" [0, 4] - [0, 5]
content: "string_content" [0, 5] - [0, 8]
end: "string_end" [0, 8] - [0, 9]
function_call [1, 0] - [1, 8]
name: identifier [1, 0] - [1, 3]
arguments: arguments [1, 4] - [1, 8]
string [1, 4] - [1, 8]
start: "string_start" [1, 4] - [1, 5]
content: "string_content" [1, 5] - [1, 8]
end: "string_end" [1, 8] - [1, 8]
As you can see the grammar has recognized 'foo
as a string, and as far as far as Neovim is concerned that’s a string
node. However, note the anonymous node string_end
: it has length zero, so you could define a predicate and add it to your query so the string
node only matches if the string_end
child has a non-zero length.
See :h treesitter-predicates
for details. You can use either an existing predicate like match?
or define your own.