tree-sitter-cmake icon indicating copy to clipboard operation
tree-sitter-cmake copied to clipboard

Adding a locals query

Open alcroito opened this issue 5 months ago • 2 comments

Hi,

Would it be possible to add a locals query either to this repo and possibly to the nvim-treesitter repo afterwards?

I'm using neovim + nvim-treesitter + snacks plugins, and snacks provides a picker to list local treesitter symbols in the currently opened file.

To do that it runs the locals query: https://github.com/folke/snacks.nvim/blob/main/lua/snacks/picker/source/treesitter.lua#L52

nvim-treesitter ships multiple locals.scm query files for various languages, but not for cmake.

If cmake were also shipped, it would make symbol navigation a bit nicer.

I imagine it should include locally defined functions, macros, variables, include statements, likely also function calls, and if()-like command blocks.

alcroito avatar Aug 01 '25 11:08 alcroito

It's possible, but I currently don't really have time to dig in to the document for locals, so a PR is welcome.

uyha avatar Aug 01 '25 12:08 uyha

I'm very new to tree sitter.

But I was able to scrounge up showing function definitions, include calls, and set assignments.

For any future travellers:

$ cat nvim-treesitter/queries/cmake/locals.scm
(source_file) @local.scope

(body) @local.scope

(function_command
  (function)
  (argument_list
    .
    (argument) @local.definition.function
    (argument)*)
  (#set! definition.function.scope "parent")
)

(normal_command
  (identifier) @include_identifier
  (argument_list
    .
    (argument) @include_argument)
  (#eq? @include_identifier "include")
) @local.definition.import

(normal_command
  (identifier) @_function
  (argument_list
    .
    (argument) @local.definition.var)
  (#match? @_function "\\c^set$")
)

alcroito avatar Aug 01 '25 15:08 alcroito