nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.19k stars 197 forks source link

Defining my own textobjects as shown in docs isn't working #65

Closed RRethy closed 2 years ago

RRethy commented 3 years ago

Describe the bug I am unable to define custom textobjects for the select submodule.

To Reproduce Config:

  require'nvim-treesitter.configs'.setup {
    textobjects = {
      select = {
        enable = true,
        keymaps = {
          -- Or you can define your own textobjects like this
          ["iF"] = {
            python = "(function_definition) @function",
            cpp = "(function_definition) @function",
            c = "(function_definition) @function",
            java = "(method_declaration) @function",
          },
        },
      },
    },
  }

C file:

int foo() {
}

Doing viF results in E5108: Error executing lua ...ckpack/opt/nvim-treesitter/lua/nvim-treesitter/query.lua:144: attempt to index local 'query' (a nil value).

Expected behavior

I expect the function to be selected.

Output of :checkhealth nvim_treesitter

health#nvim_treesitter#check ======================================================================== ## Installation - OK: `tree-sitter` found 0.19.4 (4e321f34a0688f2ad0ce6ec32d0700b039e8436c) (parser generator, only needed for :TSInstallFromGrammar) - OK: `node` found v12.16.3 (only needed for :TSInstallFromGrammar) - OK: `git` executable found. - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl" } - OK: Neovim was compiled with tree-sitter runtime ABI version 13 (required >=13). Parsers must be compatible with runtime ABI. ## Parser/Features H L F I - swift . . . . - fortran ✓ . ✓ . - c_sharp ✓ . . . - rust ✓ ✓ ✓ ✓ - go ✓ ✓ ✓ ✓ - ledger ✓ . ✓ ✓ - ruby ✓ ✓ ✓ ✓ - zig ✓ ✓ ✓ ✓ - rst ✓ ✓ . . - bibtex ✓ . ✓ ✓ - fennel ✓ ✓ . . - latex ✓ . ✓ . - teal ✓ ✓ ✓ ✓ - gomod ✓ . . . - beancount ✓ . ✓ . - graphql ✓ . . ✓ - r ✓ ✓ . . - glimmer ✓ . . . - verilog ✓ ✓ ✓ . - jsonc ✓ ✓ ✓ ✓ - bash ✓ ✓ ✓ . - elm . . . . - c ✓ ✓ ✓ ✓ - comment ✓ . . . - dart ✓ ✓ . ✓ - fish ✓ ✓ ✓ ✓ - sparql ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ - jsdoc ✓ . . . - php x x ✓ ✓ - regex ✓ . . . - ql ✓ ✓ . ✓ - gdscript x x . . - nix x x x . - typescript ✓ ✓ ✓ ✓ - yaml x x ✓ ✓ - turtle ✓ ✓ ✓ ✓ - html ✓ ✓ ✓ ✓ - devicetree x x x x - julia ✓ ✓ ✓ . - json ✓ ✓ ✓ ✓ - javascript ✓ ✓ ✓ ✓ - svelte x . ✓ ✓ - css ✓ . ✓ ✓ - supercollider x x x x - scss ✓ . . ✓ - haskell . . . . - erlang . . . . - toml ✓ ✓ ✓ ✓ - scala . . . . - elixir ✓ ✓ . ✓ - tsx ✓ ✓ ✓ ✓ - clojure ✓ ✓ ✓ . - kotlin ✓ . . . - ocaml x ✓ ✓ . - commonlisp ✓ ✓ ✓ . - vue ✓ . ✓ . - python ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ - ocaml_interfacex ✓ ✓ . - java x ✓ . ✓ - cpp ✓ ✓ ✓ ✓ - ocamllex x . . . - dockerfile ✓ . . . Legend: H[ighlight], L[ocals], F[olds], I[ndents] +) multiple parsers found, only one will be used x) errors found in the query, try to run :TSUpdate {lang} The following errors have been detected: - ERROR: php(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 100 - ERROR: php(locals): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 987 - ERROR: gdscript(highlights): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/gdscript.so: supported between 13 and 13, found 12 - ERROR: gdscript(locals): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/gdscript.so: supported between 13 and 13, found 12 - ERROR: nix(highlights): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/nix.so: supported between 13 and 13, found 12 - ERROR: nix(locals): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/nix.so: supported between 13 and 13, found 12 - ERROR: nix(folds): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/nix.so: supported between 13 and 13, found 12 - ERROR: yaml(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 291 - ERROR: yaml(locals): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 20 - ERROR: devicetree(highlights): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/devicetree.so: supported between 13 and 13, found 12 - ERROR: devicetree(locals): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/devicetree.so: supported between 13 and 13, found 12 - ERROR: devicetree(folds): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/devicetree.so: supported between 13 and 13, found 12 - ERROR: devicetree(indents): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/devicetree.so: supported between 13 and 13, found 12 - ERROR: svelte(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 17 - ERROR: supercollider(highlights): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/supercollider.so: supported between 13 and 13, found 12 - ERROR: supercollider(locals): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/supercollider.so: supported between 13 and 13, found 12 - ERROR: supercollider(folds): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/supercollider.so: supported between 13 and 13, found 12 - ERROR: supercollider(indents): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/supercollider.so: supported between 13 and 13, found 12 - ERROR: ocaml(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 2729 - ERROR: ocaml_interface(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 2729 - ERROR: java(highlights): /usr/local/share/nvim/runtime/lua/vim/treesitter/query.lua:161: query: invalid node type at position 2209 - ERROR: ocamllex(highlights): ...local/share/nvim/runtime/lua/vim/treesitter/language.lua:33: ABI version mismatch for /Users/rethy/.local/share/nvim/site/pack/backpack/opt/nvim-treesitter/parser/ocamllex.so: supported between 13 and 13, found 11

Output of nvim --version

NVIM v0.5.0-dev+1311-gf8173df4d
Build type: Debug
LuaJIT 2.1.0-beta3
Compilation: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=1 -I/Users/rethy/neovim/build/config -I/Users/rethy/neovim/src -I/Users/rethy/neovim/.deps/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include -I/Library/Frameworks/Mono.framework/Headers -I/Users/rethy/neovim/build/src/nvim/auto -I/Users/rethy/neovim/build/include
Compiled by rethy@Adams-MacBook-Pro.local

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info

Additional context I looked at the code and I don't think it should work unless I misunderstand the following snippet:

shared.lua

    local parser = parsers.get_parser(bufnr, lang)

    parser:for_each_tree(function(tree, lang_tree)
      local lang = lang_tree:lang()
      local start_row, _, end_row, _ = tree:root():range()
      local query = queries.get_query(lang, 'textobjects')
      for m in queries.iter_prepared_matches(query, tree:root(), bufnr, start_row, end_row) do
        for _, n in pairs(m) do
          if n.node then
            table.insert(matches, n)
          end
        end
      end
    end)
rfwatson commented 3 years ago

I experienced the same attempt to index local 'query' (a nil value) error when trying to define custom textobjects for the Go language. I threw in some debugging and discovered that the plugin was falling back to the comment language at some point during the text object flow. I've got no idea if this should be expected, but removing unused languages including the comment language silenced the error.

Unfortunately, the custom textobjects still don't seem to trigger for me after this change.

stsewd commented 3 years ago

I think this option should be deprecated and instead recommend putting your custom queries on ~/.config/nvim/after/queries/go/textobjects.scm

dlants commented 3 years ago

@stsewd could you elaborate on what you mean? I'm running into the same issue...

stsewd commented 3 years ago

@stsewd could you elaborate on what you mean? I'm running into the same issue...

@dlants create a file in ~/.config/nvim/after/queries/go/textobjects.scm (node) @mycustomobject with your custom queries, and then in your config

require'nvim-treesitter.configs'.setup {
  textobjects = {
    select = {
      enable = true,
      lookahead = true,

      keymaps = {
        -- You can use the capture groups defined in textobjects.scm
        ["af"] = "@mycustomobject",
     },
   },
}
mawkler commented 1 year ago

@stsewd I can't get your solution to work. I tried creating the following textobjects.csm file

;; ~/.config/nvim/after/queries/typescript/textobjects.scm
(method_definition) @mymethod

And I'm using the following minimal lazy.nvim config:

{
  'nvim-treesitter/nvim-treesitter',
  dependencies = 'nvim-treesitter/nvim-treesitter-textobjects',
  build = ':TSUpdate',
  config = function()
    require('nvim-treesitter.configs').setup({
      ensure_installed = 'all',
      highlight = {
        enable = true,
      },
      textobjects = {
        move = {
          enable = true,
          goto_next_start = {
            [']m'] = '@mymethod'
          }
        }
      }
    })
  end
}

Nothing happens when I press ]m inside a TypeScript file (with a bunch of methods). I want ]m to jump to the next method. What am I doing wrong?

kiyoon commented 1 year ago

@mawkler Maybe you need to put ; extends

mawkler commented 1 year ago

@kiyoon That works, thank you!

Do you have a link to some resource that explains what ; extends does? I couldn't find any info online.

sogaiu commented 1 year ago

FWIW, not sure if it's the same, but I learned about using ; extends in the context of nvim-treesitter here.