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
831 stars 33 forks source link

[Bug]: Invalid window id after closing window and navigating to previous window #172

Closed josh-nz closed 4 months ago

josh-nz commented 4 months ago

Similar Issues

Neovim Version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

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

Multiplexer Integration

tmux

Multiplexer Version

tmux 3.4

Steps to Reproduce

Open Neovim Create a split, C-w v Close the newly split window, :q Trigger the smart-splits move_cursor_previous function (via key bind, command line, etc). This is C-p if using the minimal repro config below.

Expected Behavior

Not entirely sure. Probably a no-op? Depending on how much history is tracked by smart-splits, there might be a legitimate window to move to (ie, the previous previous window). At the very least, I would not expect the Lua exception to surface to the user.

Actual Behavior

E5108: Error executing lua: ...are/nvim/lazy/smart-splits.nvim/lua/smart-splits/api.lua:496: Invalid window id: 1002
stack traceback:
        [C]: in function 'nvim_set_current_win'
        ...are/nvim/lazy/smart-splits.nvim/lua/smart-splits/api.lua:496: in function <...are/nvim/lazy/smart-splits.nvim/lua/smart-spl
its/api.lua:493>

Minimal Configuration to Reproduce

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',
    '--single-branch',
    'https://github.com/folke/lazy.nvim.git',
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  -- do not remove the colorscheme! it makes testing nicer
  'folke/tokyonight.nvim',
  'mrjones2014/smart-splits.nvim',
  -- add any other pugins here
}

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

require('smart-splits').setup({
  -- add any options here
})

-- recommended mappings
-- resizing splits
-- these keymaps will also accept a range,
-- for example `10<A-h>` will `resize_left` by `(10 * config.default_amount)`
vim.keymap.set('n', '<A-h>', require('smart-splits').resize_left)
vim.keymap.set('n', '<A-j>', require('smart-splits').resize_down)
vim.keymap.set('n', '<A-k>', require('smart-splits').resize_up)
vim.keymap.set('n', '<A-l>', require('smart-splits').resize_right)
-- moving between splits
vim.keymap.set('n', '<C-h>', require('smart-splits').move_cursor_left)
vim.keymap.set('n', '<C-j>', require('smart-splits').move_cursor_down)
vim.keymap.set('n', '<C-k>', require('smart-splits').move_cursor_up)
vim.keymap.set('n', '<C-l>', require('smart-splits').move_cursor_right)
vim.keymap.set('n', '<C-p>', require('smart-splits').move_cursor_previous)
-- swapping buffers between windows
vim.keymap.set('n', '<leader><leader>h', require('smart-splits').swap_buf_left)
vim.keymap.set('n', '<leader><leader>j', require('smart-splits').swap_buf_down)
vim.keymap.set('n', '<leader><leader>k', require('smart-splits').swap_buf_up)
vim.keymap.set('n', '<leader><leader>l', require('smart-splits').swap_buf_right)

-- add anything else here
vim.opt.termguicolors = true
-- do not remove the colorscheme! it makes testing nicer
vim.cmd([[colorscheme tokyonight]])

Additional Details and/or Screenshots

No response

mrjones2014 commented 4 months ago

Ah, yeah, it should probably just be a no-op. We just need to make sure the window exists before trying to move to it.