nvim-treesitter / nvim-treesitter-textobjects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swap keymap results in error "bad argument to ipairs (table expected, got nil)", but :TSTextobjectSwapNext works

mr-majkel opened this issue · comments

Describe the bug
I tried to make the node swapping work, but in the most recent commit e63c2ff, the keybind set in the config results in an error. I have tried some previous commits and the keymap set in the config was also not working, but no error was coming up. Keymap for textobject for select "action" works.

The command :TSTextobjectSwapNext @parameter.inner works ok.

I'm on Windows 10.

To Reproduce
Steps to reproduce the behavior:

  1. Open new buffer, set the filetype to python to start python parser
  2. Enter list definition: [aaaa, bbbb, cccc]
  3. Put cursor on aaaa
  4. Press keymap from swap config
  5. Get an error:
E5108: Error executing lua: ...ter-textobjects/lua/nvim-treesitter/textobjects/swap.lua:10: bad argument #1 to 'ipairs' (table expected, got nil)
stack traceback:
        [C]: in function 'ipairs'
        ...ter-textobjects/lua/nvim-treesitter/textobjects/swap.lua:10: in function 'swap_textobject'
        ...ter-textobjects/lua/nvim-treesitter/textobjects/swap.lua:32: in function <...ter-textobjects/lua/nvim-treesitter/textobjects/swap.lua:31>
        ...r-textobjects/lua/nvim-treesitter/textobjects/attach.lua:29: in function <...r-textobjects/lua/nvim-treesitter/textobjects/attach.lua:28>

Expected behavior
A keymap should swap nodes as TSTextobjectSwapNext does.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - WARNING: `tree-sitter` executable not found (parser generator, only needed for :TSInstallFromGrammar, not required for :TSInstall)
  - OK: `node` found v18.4.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `clang` executable found. Selected from { "clang" }
    Version: clang version 14.0.4
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## Parser/Features H L F I J
  - lua            ✓ ✓ ✓ ✓ ✓
  - javascript     ✓ ✓ ✓ ✓ ✓
  - markdown       ✓ . ✓ . ✓
  - bash           ✓ ✓ ✓ . ✓
  - http           ✓ . . . ✓
  - python         ✓ ✓ ✓ ✓ ✓
  - norg           ✓ . ✓ . ✓
  - query          ✓ ✓ ✓ ✓ ✓
  - dockerfile     ✓ . . . ✓
  - go             ✓ ✓ ✓ ✓ ✓
  - r              ✓ ✓ . ✓ ✓
  - vim            ✓ ✓ ✓ . ✓
  - c              ✓ ✓ ✓ ✓ ✓

  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

:version
NVIM v0.7.2
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compiled by runneradmin@fv-az276-503

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

   system vimrc file: "$VIM\sysinit.vim"
  fall-back for $VIM: "C:/Program Files/nvim/share/nvim"

Run :checkhealth for more info

treesitter textobjects config

textobjects = {
    select = {
      enable=true,
      keymaps={
        ["is"] = "@parameter.inner",
        ["as"] = "@parameter.outer",
      },
    },
    swap = {
      enable = true,
      swap_next = {
        ["gs"] = {"@parameter.inner"},
      },
      swap_previous = {
        ["gS"] = {"@parameter.inner"},
      },
    },
  }

I tried to reproduce but couldn't, using your neovim and your treesitter-textobjects version with the same config.
I am on macos though so that is the only change that could be the root problem here.

I think I'm seeing this with move as well. Looks like the issue is this block in make_attach:

if type(query_metadata) == "table" then
    query = query_metadata.query
    mapping_description = query_metadata.desc
else
    query = query_metadata
    mapping_description = function_description .. " " .. query_metadata
end

If you provide a list of query captures like in the config above, it will expect there to be query and desc subkeys.

It seems the README doesn't specify that you can pass lists there, but the help file does, so either that or the code should be updated.

Specifying the query field like below fixes this issue:

move = {
    enable = true,
    set_jumps = true,
    goto_next_start = {
        [']]'] = {query = {'@function.outer', '@class.outer'}},
    },
},

@binyomen Ah my bad, you are right, I did not pass a table trying to reproduce it.

So it is either:

textobjects = {
    swap = {
      enable = true,
      swap_next = {
        ["gs"] = "@parameter.inner",
      },
      swap_previous = {
        ["gS"] = "@parameter.inner",
      },
    },
  }

or:

textobjects = {
    swap = {
      enable = true,
      swap_next = {
        ["gs"] = { query = "@parameter.inner", desc = "" },
      },
      swap_previous = {
        ["gS"] = { query = "@parameter.inner", desc = "" },
      },
    },
  }

I think this is pretty clear in the readme though.

I think the help file should still be updated, since it has the incorrect syntax.