nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.01k stars 181 forks source link

[main] recommended setup for swap doesn't work #624

Open igorlfs opened 1 month ago

igorlfs commented 1 month ago

Describe the bug The recommended setup for swapping text objects on main is:

-- keymaps
local swap = require("nvim-treesitter-textobjects.swap")
vim.keymap.set("n", "<leader>a", swap.swap_next("@parameter.inner"))
vim.keymap.set("n", "<leader>A", swap.swap_next("@parameter.outer"))

But these keymaps don't work: nothing is swapped, and no error is thrown. Based on the setup for default branch, I think there's a typo and it should be

vim.keymap.set("n", "<leader>a", swap.swap_next("@parameter.inner"))
vim.keymap.set("n", "<leader>A", swap.swap_previous("@parameter.inner"))

However, these keymaps also don't work.

To Reproduce Steps to reproduce the behavior:

  1. Install the plugin and enable the recommended for swap
  2. <leader>a on an argument

Expected behavior Arguments should be swapped

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 v20.13.1 (only needed for :TSInstallFromGrammar) - OK `git` executable found. - OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" } Version: cc (GCC) 14.1.1 20240522 - 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.9.1-arch1-2", sysname = "Linux", version = "#1 SMP PREEMPT_DYNAMIC Wed, 22 May 2024 13:47:07 +0000" } ~ Parser/Features H L F I J - bash ✓ ✓ ✓ . ✓ - bibtex ✓ . ✓ ✓ . - c ✓ ✓ ✓ ✓ ✓ - comment ✓ . . . . - cpp ✓ ✓ ✓ ✓ ✓ - css ✓ . ✓ ✓ ✓ - diff ✓ . . . . - gdscript ✓ ✓ ✓ ✓ ✓ - gitcommit ✓ . . . ✓ - gitignore ✓ . . . . - html ✓ ✓ ✓ ✓ ✓ - hyprlang ✓ . ✓ ✓ ✓ - java ✓ ✓ ✓ ✓ ✓ - javascript ✓ ✓ ✓ ✓ ✓ - jsdoc ✓ . . . . - json ✓ ✓ ✓ ✓ . - jsonc ✓ ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ ✓ - make ✓ . ✓ . ✓ - markdown ✓ . ✓ ✓ ✓ - markdown_inline ✓ . . . ✓ - python ✓ ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ ✓ - regex ✓ . . . . - requirements ✓ . . . ✓ - rust ✓ ✓ ✓ ✓ ✓ - scss ✓ . ✓ ✓ . - sql ✓ . . ✓ ✓ - svelte ✓ ✓ ✓ ✓ ✓ - toml ✓ ✓ ✓ ✓ ✓ - tsx ✓ ✓ ✓ ✓ ✓ - typescript ✓ ✓ ✓ ✓ ✓ - typst ✓ . ✓ ✓ ✓ - vim ✓ ✓ ✓ . ✓ - vimdoc ✓ . . . ✓ - yaml ✓ ✓ ✓ ✓ ✓ - zathurarc ✓ . . . ✓ 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 v0.10.0
Build type: Release
LuaJIT 2.1.1713773202
igorlfs commented 1 month ago

EDIT: this might be an issue with lazy loading, since I was defining the keys via lazy.nvim's keys property 🤔

thetic commented 1 month ago

I see the same issue.

return {
    'nvim-treesitter/nvim-treesitter-textobjects',
    branch = 'main',
    opts = {
        select = {
            lookahead = true,
        },
        lsp_interop = {
            floating_preview_opts = {},
        },
        move = {
            set_jumps = true,
        },
    },
    keys = {
        --...
        {
            ']A',
            function()
                require('nvim-treesitter-textobjects.swap').swap_next(
                    '@parameter.inner'
                )
            end,
            desc = 'Swap argument',
        },
        -- ...
    },
}
DavidGamba commented 3 weeks ago

Mine works with this:

return {
  {
    "nvim-treesitter/nvim-treesitter",
    dependencies = {
      "nvim-treesitter/nvim-treesitter-textobjects",
    },
    branch = "master",
    lazy = false,
    build = ":TSUpdate",
    config = function()
      require("nvim-treesitter.configs").setup {
        ensure_installed = {
...
        },
        auto_install = true,
        highlight = {
          enable = true,
        },

        textobjects = {
          swap = {
            enable = true,
            swap_next = {
              ["<leader>a"] = "@parameter.inner",
            },
            swap_previous = {
              ["<leader>A"] = "@parameter.inner",
            },
          },
        },
      }
    end,
  },
}
thetic commented 3 weeks ago

Mine works with this:

return {
  {
    "nvim-treesitter/nvim-treesitter",
    dependencies = {
      "nvim-treesitter/nvim-treesitter-textobjects",
    },
    branch = "master",
    lazy = false,
    build = ":TSUpdate",
    config = function()
      require("nvim-treesitter.configs").setup {
        ensure_installed = {
...
        },
        auto_install = true,
        highlight = {
          enable = true,
        },

        textobjects = {
          swap = {
            enable = true,
            swap_next = {
              ["<leader>a"] = "@parameter.inner",
            },
            swap_previous = {
              ["<leader>A"] = "@parameter.inner",
            },
          },
        },
      }
    end,
  },
}

we're discussing the main branch (not master) here, which addresses #503.

DavidGamba commented 3 weeks ago

Trying to be helpful wasn't helpful 🤦‍♂️ 😁 sorry I didn't fully read the [main] part.