nvim-treesitter / nvim-treesitter-textobjects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] How to jump to next argument without going into nested function calls

mnussbaum opened this issue · comments

I'm hoping to write a macro that involves jumping to a second argument of a function call and deleting it. The function arguments will sometimes include nested function calls, sometimes not, will sometimes be on multiple lines, and sometimes all on one line. I'm struggling to figure out how to use nvim-treesitter-textobjects to achieve the desired jumps.

My move configuration currently looks like this:

require'nvim-treesitter.configs'.setup {
  textobjects = {
     move = {
       enable = true,
       set_jumps = true,

       goto_previous_start = {
         ["[["] = "@parameter.inner",
       },
       goto_next_start = {
         ["]]"] = "@parameter.inner",
       },
     },
  },
}

And then for example, with a code snippet like this:

func foo(a, b int) int {
  return a * b
}

func bar(a, b int) int {
  return a + b
}

func main() {
  foo(
    bar(
      1, 
      2,
    ),
    3,
  )
}

When my cursor is on bar, and I hit ]], I'd like it to jump to 3, the next argument at the same level as the current node. However, it currently jumps to 1, the next argument at the lower level of the nested function call.

Any suggestions for how to achieve this desired jump? Thank you for your help!

On bar, hit % will jump to bar's closing parenthesis (this is the default behavior of matchit, see :h matchit for details), and then hit ]] will jump to 3. This workaround just one more key to press so I think it's acceptable.