mrjones2014 / smart-splits.nvim

🧠 Smart, seamless, directional navigation and resizing of Neovim + terminal multiplexer splits. Supports tmux, Wezterm, and Kitty. Think about splits in terms of "up/down/left/right".
MIT License
895 stars 37 forks source link

[Bug]: Resize mode is deleting my global keymaps that remap j and k to gj and gk #126

Closed serranomorante closed 1 year ago

serranomorante commented 1 year ago

Similar Issues

Neovim Version

NVIM v0.9.1

Multiplexer Integration

I don't use one

Multiplexer Version

No response

Steps to Reproduce

t-rec_12

Disclaimer

I have my global keymaps defined BEFORE my package manager (lazy.nvim) loads.

  1. Configure this global keymap (helps to move by display lines when I have the wrap set to on).
-- Navigate display lines
vim.keymap.set({ "n", "x" }, "j", function()
    return vim.v.count > 0 and "j" or "gj"
end, { noremap = true, expr = true })
vim.keymap.set({ "n", "x" }, "k", function()
    return vim.v.count > 0 and "k" or "gk"
end, { noremap = true, expr = true })
  1. Open a buffer (with any content) and make the text wrap (as in the screenshot). Pressing j or k should move your cursor by display lines (it will not skip the wrapped text lines). Good, your keymap is working normally.
  2. Enter resize mode and resize your window a little bit in any direction (as in the screenshot) and then exit resize mode.
  3. Try to move again between wrapped lines, now j and k are skipping the wrapped lines (this should not happen)

I think vim.api.nvim_del_keymap on file resize_mode.lua on line 47-51 are the ones to blame.

Expected Behavior

Entering resize mode should not delete my previous mapping.

Actual Behavior

Entering resize mode deletes my previous mapping.

Minimal Configuration to Reproduce

remap.lua -- loaded before my package manager

-- Navigate display lines
vim.keymap.set({ "n", "x" }, "j", function()
    return vim.v.count > 0 and "j" or "gj"
end, { noremap = true, expr = true })
vim.keymap.set({ "n", "x" }, "k", function()
    return vim.v.count > 0 and "k" or "gk"
end, { noremap = true, expr = true })

smart-splits.lua

local utils = require("serranomorante.utils")

return {
    {
        "kwkarlwang/bufresize.nvim",
        event = "VeryLazy",
        config = true,
    },

    {
        "mrjones2014/smart-splits.nvim",
        -- enabled = false,
        keys = {
            {
                "<leader>rs",
                function()
                    require("smart-splits").start_resize_mode()
                end,
            },
            {
                "<leader><leader>h",
                function()
                    -- Quick return if next window is neo-tree
                    local next_winid = vim.fn.win_getid(vim.fn.winnr(utils.DirectionKeys[utils.Direction.left]))
                    local filetype = utils.buf_filetype_from_winid(next_winid)
                    if filetype == "neo-tree" then
                        return
                    end

                    require("smart-splits").swap_buf_left()
                end,
            },
            {
                "<leader><leader>j",
                function()
                    require("smart-splits").swap_buf_down()
                end,
            },
            {
                "<leader><leader>k",
                function()
                    require("smart-splits").swap_buf_up()
                end,
            },
            {
                "<leader><leader>l",
                function()
                    -- Quick return if next window is neo-tree
                    local swap_direction = utils.Direction.right
                    local will_wrap = utils.win_at_edge(swap_direction)
                    if will_wrap then
                        local wrap_winid = utils.win_wrap_id(swap_direction)
                        local filetype = utils.buf_filetype_from_winid(wrap_winid)
                        if filetype == "neo-tree" then
                            return
                        end
                    end

                    require("smart-splits").swap_buf_right()
                end,
            },
        },
        opts = function()
            return {
                cursor_follows_swapped_bufs = true,
                ignored_filetypes = {
                    "nofile",
                    "quickfix",
                    "prompt",
                    "neo-tree",
                    "harpoon",
                    "NvimTree",
                },
                resize_mode = {
                    silent = true,
                    hooks = {
                        on_leave = function()
                            require("bufresize").register()
                            vim.notify("Smart Splits: RESIZE MODE OFF", vim.log.levels.INFO)
                        end,
                        on_enter = function()
                            vim.notify("Smart Splits: RESIZE MODE ON", vim.log.levels.WARN)
                        end,
                    },
                },
            }
        end,
    },
}

Additional Details and/or Screenshots

No response

mrjones2014 commented 1 year ago

Unfortunately there isn't a way the plugin can work around this directly. You can use this:

local function jkmaps()
  -- Navigate display lines
  vim.keymap.set({ "n", "x" }, "j", function()
    return vim.v.count > 0 and "j" or "gj"
  end, { noremap = true, expr = true })
  vim.keymap.set({ "n", "x" }, "k", function()
    return vim.v.count > 0 and "k" or "gk"
  end, { noremap = true, expr = true })
end

require('smart-splits').setup({
  resize_mode = {
    hooks = {
      on_leave = jkmaps,
    },
  },
})
serranomorante commented 1 year ago

That is good enough! thank you.