mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 06:21:13 +01:00
Fix: Save as with <leader>W on existing files.
This commit is contained in:
@@ -177,7 +177,23 @@ local function smart_save(force_save_as)
|
|||||||
-- For no-name buffers or Save As, write and set buffer name
|
-- For no-name buffers or Save As, write and set buffer name
|
||||||
local write_cmd = overwrite and 'write!' or 'write'
|
local write_cmd = overwrite and 'write!' or 'write'
|
||||||
vim.cmd(write_cmd .. ' ' .. vim.fn.fnameescape(filename))
|
vim.cmd(write_cmd .. ' ' .. vim.fn.fnameescape(filename))
|
||||||
|
if current_path ~= '' then
|
||||||
|
-- Preserve cursor position and undo history
|
||||||
|
local old_buf = vim.api.nvim_get_current_buf()
|
||||||
|
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||||||
|
local undo_history = vim.fn.getbufinfo(old_buf)[1].changedtick
|
||||||
|
-- Open the new file in a fresh buffer
|
||||||
|
vim.cmd('edit ' .. vim.fn.fnameescape(filename))
|
||||||
|
-- Restore cursor
|
||||||
|
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
||||||
|
-- Restore undo history
|
||||||
|
vim.cmd 'undojoin'
|
||||||
|
-- Delete the old buffer without saving
|
||||||
|
vim.api.nvim_buf_delete(old_buf, { force = true })
|
||||||
|
else
|
||||||
|
-- No-name buffer, just set its name
|
||||||
vim.api.nvim_buf_set_name(0, filename)
|
vim.api.nvim_buf_set_name(0, filename)
|
||||||
|
end
|
||||||
print('Saved as ' .. filename)
|
print('Saved as ' .. filename)
|
||||||
else
|
else
|
||||||
-- Buffer already has a name, just save it
|
-- Buffer already has a name, just save it
|
||||||
|
|||||||
Reference in New Issue
Block a user