nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects

Question: How to write a query for comment + function

Open
#481 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Tree-sitter Query
Stars
2.8k
Forks
271
Avg merge
8d 8h
Merged PRs (30d)
1

Description

I've read through the docs on how to write TS queries but I just can't make it work. I'm trying to write a query for lua where it captures a luadoc followed by a function:

---test comment 5
---@param bool any
function M.test5(bool)
	-- oeu
	print("test5", bool)
end

These were my attempts:

(
  (comment) @comment.outer
  (function_declaration) @function.outer
    (comment) @comment.outer
    .
    (function_declaration) @function.outer) @function_context

what I kind of undestood is that by grouping comment and function within () you declare a capture group and the . means that a comment is a direct sibling to the function declaration.

Im using the query with the swap feature:

swap = {
			enable = true,
			swap_next = {
				["<leader>spn"] = "@parameter.inner",
				["<leader>sfn"] = {
					query = { "@function_context" },
					query_group = "context",
				},
			},
			swap_previous = {
				["<leader>spp"] = "@parameter.inner",
				["<leader>sfp"] = {
					query = { "@function_context" },
					query_group = "context",
				},
			},
		},

but whatever I do it just either swaps the functions without the comments or with different attempts it would swap the first function with one of the comments above the second function.

---test comment 5
---@param bool any
function M.test5(bool)
	-- oeu
	print("test5", bool)
end

---test comment 4
---@param num any
---@param str any
function M.test4(num, str)
	print("test4", num, str)
end

I did not mark this issue as a bug because there probably is a way to swap functions together with their comments. I appreciate any help or advise that I can get and maybe this could be a feature?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the swap configuration and a Tree-sitter query, but no source file or test is identified. Start by reproducing the query and swap behavior with the provided Lua examples, then determine whether the existing feature can capture each function with its preceding comments or whether support for that grouping is needed. Done means the paired function and comments swap together consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.