Wansmer / sibling-swap.nvim

Neovim plugin for swaps closest siblings with Tree-Sitter
MIT License
155 stars 1 forks source link

swap didn't work #17

Open edte opened 3 months ago

edte commented 3 months ago

nvim version

$ nvim -v
NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713773202
Run "nvim -V1 -v" for more info

plugin version

image

plugin configuration

i use lazy.nvim plugin manager

    {
        "Wansmer/sibling-swap.nvim",
        dependencies = { "nvim-treesitter" },
        config = function()
            require("sibling-swap").setup({
                allowed_separators = {
                    ",",
                    ":",
                    ";",
                    "and",
                    "or",
                    "&&",
                    "&",
                    "||",
                    "|",
                    "==",
                    "===",
                    "!=",
                    "!==",
                    "-",
                    "+",
                    ["<"] = ">",
                    ["<="] = ">=",
                    [">"] = "<",
                    [">="] = "<=",
                },
                use_default_keymaps = true,
                -- Highlight recently swapped node. Can be boolean or table
                -- If table: { ms = 500, hl_opts = { link = 'IncSearch' } }
                -- `hl_opts` is a `val` from `nvim_set_hl()`
                highlight_node_at_cursor = true,
                -- keybinding for movements to right or left (and up or down, if `allow_interline_swaps` is true)
                -- (`<C-,>` and `<C-.>` may not map to control chars at system level, so are sent by certain terminals as just `,` and `.`. In this case, just add the mappings you want.)
                keymaps = {
                    ["<C-.>"] = "swap_with_right",
                    ["<C-,>"] = "swap_with_left",
                    ["<space>."] = "swap_with_right_with_opp",
                    ["<space>,"] = "swap_with_left_with_opp",
                },
                ignore_injected_langs = false,
                -- allow swaps across lines
                allow_interline_swaps = true,
                -- swaps interline siblings without separators (no recommended, helpful for swaps html-like attributes)
                interline_swaps_without_separator = false,
                -- Fallbacs for tiny settings for langs and nodes. See #fallback
                fallback = {},
            })
        end,
    },

Swap doesn't work

https://github.com/Wansmer/sibling-swap.nvim/assets/50194671/073720c5-2c35-4310-920d-dd88266f056e

Wansmer commented 3 months ago

I can't reproduce it.

Did this happen with minimal config (just clean nvim and sibling-spaw only)? What is the output of :map <C-.>/:map <C-,>?

edte commented 3 months ago

minimal config

-- **DO NOT change the paths and don't remove the colorscheme**
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

vim.cmd.color("elflord")
--------------------------------------------------------------------------------
local plugins = {
    {
        "Wansmer/sibling-swap.nvim",
        dependencies = "nvim-treesitter/nvim-treesitter",
        config = function()
            require("sibling-swap").setup({
                allowed_separators = {
                    ",",
                    ":",
                    ";",
                    "and",
                    "or",
                    "&&",
                    "&",
                    "||",
                    "|",
                    "==",
                    "===",
                    "!=",
                    "!==",
                    "-",
                    "+",
                    ["<"] = ">",
                    ["<="] = ">=",
                    [">"] = "<",
                    [">="] = "<=",
                },
                use_default_keymaps = true,
                -- Highlight recently swapped node. Can be boolean or table
                -- If table: { ms = 500, hl_opts = { link = 'IncSearch' } }
                -- `hl_opts` is a `val` from `nvim_set_hl()`
                highlight_node_at_cursor = true,
                -- keybinding for movements to right or left (and up or down, if `allow_interline_swaps` is true)
                -- (`<C-,>` and `<C-.>` may not map to control chars at system level, so are sent by certain terminals as just `,` and `.`. In this case, just add the mappings you want.)
                keymaps = {
                    ["<C-.>"] = "swap_with_right",
                    ["<C-,>"] = "swap_with_left",
                    ["<space>."] = "swap_with_right_with_opp",
                    ["<space>,"] = "swap_with_left_with_opp",
                },
                ignore_injected_langs = false,
                -- allow swaps across lines
                allow_interline_swaps = true,
                -- swaps interline siblings without separators (no recommended, helpful for swaps html-like attributes)
                interline_swaps_without_separator = false,
                -- Fallbacs for tiny settings for langs and nodes. See #fallback
                fallback = {},
            })
        end,
    },
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

output:

image image

not work

nvim -u a.lua main.go

https://github.com/Wansmer/sibling-swap.nvim/assets/50194671/ea34f1f8-b7a2-4b1f-9b45-5f7b5542e1e3

Wansmer commented 3 months ago

Looks like you just did not install the parser for go. Run :TSInstall go.

edte commented 3 months ago

actually I have already installed it,Already restarted

https://github.com/Wansmer/sibling-swap.nvim/assets/50194671/31850847-8e91-4279-93b0-4cf5b166c1a2

Wansmer commented 3 months ago

Does this happen if you change the mappings? Some terminals may not handle CTRL keys correctly.

I run minimal config with the same Neovim version and everything works correct. (kitty/iTerm2)

edte commented 3 months ago

I use kitty terminal then I tried changing the shortcut key to ctrl+1 or alt+1 but it didn't work.

Wansmer commented 3 months ago

So far I have no idea why plugin doesn't work. I can't reproduce this bug under the same conditions.

Try calling the plugin directly: lua require('sibling-swap').swap_with_right() being on the first argument in fmt.Println. If it works, then the problem is in the mappings on your system. If it doesn't, we'll look into it further.

dlyongemallo commented 2 months ago

I also had trouble with the keymaps field in the config, but I was able to get the keymaps to work using nvim_set_keymap.

@edte Can you try the following?

vim.api.nvim_set_keymap("n", "<a-2>", "<cmd>lua require('sibling-swap').swap_with_right()<CR>")
vim.api.nvim_set_keymap("n", "<a-1>", "<cmd>lua require('sibling-swap').swap_with_left()<CR>")

(Another useful thing to try is the WhichKey plugin. Running ":WhichKey" will let you know if your keymapping was successful or not, so you can rule out if that's the cause of the problem.)