Tree-sitter: How to write query that returns strings from a table? (lua)

Hi there,

How do I write a tree-sitter query for lua that returns string field nodes from a table like the following one?


module.packages = {
  "aaa.nvim",
"bbb.nvim",
["not_this"] = { "ccc.nvim" }
}

I am reading the docs etc. but something is just not clicking in my understanding.

My goal is to be able to capture all of these strings (aaa.nvim,bbb.nvim,…) and put them all in a table and use telescope to pick from them so that I can use the picker to toggle configs/settings in a large table.

I should also mention that I know how to capture the whole table by copy pasting from the locals query from nvim-treesitter repo but I don’t really understand how it works so I am not sure how to modify it to only spit out the table string fields. Currently I am capturing the whole table chunk and then use get children and siblings functions in order to end up with the table fields.

##edit

This is my query so far but it is not working:

(assignment_statement
  (variable_list
    (dot_index_expression . (_) @def.assoc (identifier) @def.var)))
  (#eq? @def.var "packages")
  (expression_list
    (field . (field value: (string) @repostring)))
)

#edit

I have almost solved the query now for myself so I will post it when I am done.

https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax

I read through the query syntax docs a couple of more times until it clicked.

(assignment_statement 
   (variable_list
       (dot_index_expression . (_) @definition.associated (identifier) @definition.var)))
       (#eq? @definition.var "packages")
   (expression_list (table_constructor [
       (field value: (string) @package_string)
       (field value: (table_constructor (field value: (string) @package_string )))
])))