nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.18k stars 195 forks source link

example "scope" ]s mapping seemingly does nothing #604

Open aktau opened 6 months ago

aktau commented 6 months ago

Describe the bug

Simply put, the ]s, [s, ]S, [S mappings do nothing for me (no movement), when attempting to navigate around simple C and Go files (haven't tried others yet). Mappings like ]m, [m, ]M, [M do work.

I'm not sure if this problem is PEBKAC or not, but having seen locals.scm for Go/C/..., I suspect it isn't. The README gives this example:

["]s"] = { query = "@scope", query_group = "locals", desc = "Next scope" },

But there is no movement (see below). I've verified that nmap ]s shows me an anonymous Lua function is bound.

To Reproduce

-- reproducer.lua
vim.opt.runtimepath:append("$HOME/.vim/bundle/nvim-treesitter")
vim.opt.runtimepath:append("$HOME/.vim/bundle/nvim-treesitter-textobjects")

local treesitter_parsers = {
  "c",
  "cpp",
  "go",
  "lua",
}
require('nvim-treesitter.configs').setup({
  ensure_installed = treesitter_parsers,
  sync_install = false,
  highlight = {
    enable = true,
    additional_vim_regex_highlighting = false,
  },
  textobjects = {
    move = {
      enable = true,
      set_jumps = true, -- whether to set jumps in the jumplist
      goto_next_start = {
        [']m'] = '@function.outer',
        ["]s"] = { query = "@scope", query_group = "locals", desc = "Next scope" },
      },
      goto_next_end = {
        [']M'] = '@function.outer',
        ["]S"] = { query = "@scope", query_group = "locals", desc = "Next scope" },
      },
      goto_previous_start = {
        ['[m'] = '@function.outer',
        ["[s"] = { query = "@scope", query_group = "locals", desc = "Next scope" },
      },
      goto_previous_end = {
        ['[M'] = '@function.outer',
        ["[S"] = { query = "@scope", query_group = "locals", desc = "Next scope" },
      },
    },
  },
})
$ nvim -u reproducer.lua example.go

Expected behavior

I expected some movement to happen.v

Output of :checkhealth nvim-treesitter

Installation ~ - WARNING `tree-sitter` executable not found (parser generator, only needed for :TSInstallFromGrammar, not required for :TSInstall) - WARNING `node` executable not found (only needed for :TSInstallFromGrammar, not required for :TSInstall) - OK `git` executable found. - OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" } Version: cc (Debian 13.2.0-10) 13.2.0 - OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI. OS Info: { machine = "x86_64", release = "6.6.13-amd64", sysname = "Linux", version = "#1 SMP PREEMPT_DYNAMIC Debian 6.6.13" } ~ Parser/Features H L F I J - bash ✓ ✓ ✓ . ✓ - c ✓ ✓ ✓ ✓ ✓ - cpp ✓ ✓ ✓ ✓ ✓ - go ✓ ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ ✓ - markdown ✓ . ✓ ✓ ✓ - markdown_inline ✓ . . . ✓ - python ✓ ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ ✓ - sql ✓ . . ✓ ✓ - starlark ✓ ✓ ✓ ✓ ✓ - vim ✓ ✓ ✓ . ✓ - vimdoc ✓ . . . ✓ Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections +) multiple parsers found, only one will be used x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

$ nvim --version
NVIM v0.10.0-dev-2913+g5371ed36b
Build type: RelWithDebInfo
LuaJIT 2.1.1710088188
Run "nvim -V1 -v" for more info

nvim-treesitter: 1ba86026
nvim-treesitter-textobjects: 6e9bb56
Juhan280 commented 1 month ago

Try this

["]s"] = { query = "@local.scope", query_group = "locals", desc = "Next scope" },
aktau commented 1 month ago

That actually does something, which is much better than before!

It's not exactly what I want, for example with this C code I first move to the first letter of the if, then to the first brace { when I use ]s. I would prefer just moving to the first brace:

    int c = 3;    

    if (ptr == MAP_FAILED) {
/* 1                                      2 */
      perror("mmap");
      return 0;
    }

But it's already a lot better than before. Thanks.

Juhan280 commented 1 month ago

This happens because for statement, if statement, while statement, function definition, etc are also defined to make their own scope in https://github.com/nvim-treesitter/nvim-treesitter/blob/4770d9a1a77b0cc2b723c646c3dbe43a9133e5db/queries/c/locals.scm#L58-L67

The work around is to copy that file to ~/.config/nvim/queries/c/locals.scm and then comment out the blocks you don't want to jump to.

kiyoon commented 1 month ago

So the query names in nvim-treesitter seem to have been changed. I guess we need to update the README.