repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/tests/bugabinga/cursor_restore_spec.lua
Rawlocal auto = require('std.auto')
local ignored = require('std.ignored')
describe('cursor restore behaviour', function()
after_each(function()
pcall(vim.api.nvim_del_augroup_by_name, 'restore_cursor_position')
pcall(vim.api.nvim_del_augroup_by_name, 'restore_cursor_stage2')
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
pcall(vim.api.nvim_buf_delete, buf, { force = true })
end
end)
local function make_buffer_with_lines(buf, lines)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
end
local function set_shada_mark(buf, line, col)
vim.api.nvim_set_current_buf(buf)
vim.cmd.normal { string.format('%dG', line), bang = true }
vim.cmd.normal { string.format('%dl', col - 1), bang = true }
vim.cmd([[normal! m"]])
end
local function restore_cursor_logic(ev)
local ft = vim.bo[ev.buf].filetype
local bt = vim.bo[ev.buf].buftype
if ignored.buftypes_kv[bt] then return end
if ignored.filetypes_kv[ft] then return end
for _, pattern in ipairs(ignored.filetypes_patterns) do
if ft:find(pattern) then return end
end
if vim.fn.line('.') > 1 then return end
local line = vim.fn.line([['"]])
local col = vim.fn.col([['"]]) - 1
if line > 0 and line <= vim.fn.line('$') then
auto 'restore_cursor_stage2' {
events = 'BufWinEnter',
buffer = ev.buf,
once = true,
command = function()
vim.api.nvim_win_set_cursor(0, { line, col })
vim.cmd.normal { 'zvzz', bang = true }
end,
}
end
end
describe('BufReadPost stage', function()
it('should NOT create stage2 when buftype is nofile', function()
local buf = vim.api.nvim_create_buf(false, false)
vim.bo[buf].buftype = 'nofile'
make_buffer_with_lines(buf, { 'line 1', 'line 2', 'line 3' })
set_shada_mark(buf, 3, 5)
auto 'restore_cursor_position' {
events = 'BufReadPost',
command = restore_cursor_logic,
}
vim.api.nvim_exec_autocmds('BufReadPost', { buffer = buf })
local ok, stage2 = pcall(vim.api.nvim_get_autocmds, { group = 'restore_cursor_stage2' })
if ok then
assert.equals(0, #stage2)
end
end)
it('should NOT create stage2 when filetype is help', function()
local buf = vim.api.nvim_create_buf(false, false)
vim.bo[buf].filetype = 'help'
make_buffer_with_lines(buf, { 'line 1', 'line 2', 'line 3' })
set_shada_mark(buf, 3, 5)
auto 'restore_cursor_position' {
events = 'BufReadPost',
command = restore_cursor_logic,
}
vim.api.nvim_exec_autocmds('BufReadPost', { buffer = buf })
local ok, stage2 = pcall(vim.api.nvim_get_autocmds, { group = 'restore_cursor_stage2' })
if ok then
assert.equals(0, #stage2)
end
end)
it('should NOT create stage2 when cursor already at line > 1', function()
local buf = vim.api.nvim_create_buf(false, false)
make_buffer_with_lines(buf, { 'line 1', 'line 2', 'line 3', 'line 4', 'line 5' })
set_shada_mark(buf, 3, 5)
-- simulate nvim +3 file.lua - cursor already at line 3
vim.api.nvim_set_current_buf(buf)
vim.cmd.normal { '3G' }
auto 'restore_cursor_position' {
events = 'BufReadPost',
command = restore_cursor_logic,
}
vim.api.nvim_exec_autocmds('BufReadPost', { buffer = buf })
local ok, stage2 = pcall(vim.api.nvim_get_autocmds, { group = 'restore_cursor_stage2' })
if ok then
assert.equals(0, #stage2)
end
end)
it('should create stage2 autocmd when conditions are met', function()
local buf = vim.api.nvim_create_buf(false, false)
make_buffer_with_lines(buf, { 'line 1', 'line 2', 'line 3', 'line 4', 'line 5' })
set_shada_mark(buf, 3, 5)
-- cursor at line 1
vim.api.nvim_set_current_buf(buf)
vim.cmd.normal { 'gg' }
auto 'restore_cursor_position' {
events = 'BufReadPost',
command = restore_cursor_logic,
}
vim.api.nvim_exec_autocmds('BufReadPost', { buffer = buf })
local stage2 = vim.api.nvim_get_autocmds({ group = 'restore_cursor_stage2' })
assert.equals(1, #stage2)
assert.equals(buf, stage2[1].buffer)
end)
end)
describe('BufWinEnter stage', function()
it('should restore cursor to shada position', function()
local buf = vim.api.nvim_create_buf(false, false)
make_buffer_with_lines(buf, { 'line 1', 'line 2', 'line 3', 'line 4', 'line 5' })
set_shada_mark(buf, 3, 5)
-- create a window with this buffer
vim.cmd.split()
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, buf)
vim.api.nvim_set_current_win(win)
vim.cmd.normal { 'gg' } -- cursor at line 1
auto 'restore_cursor_position' {
events = 'BufReadPost',
command = restore_cursor_logic,
}
vim.api.nvim_exec_autocmds('BufReadPost', { buffer = buf })
vim.api.nvim_exec_autocmds('BufWinEnter', { buffer = buf })
local cursor = vim.api.nvim_win_get_cursor(win)
assert.equals(3, cursor[1])
assert.equals(4, cursor[2])
end)
end)
end)