Fix: Save as with <leader>W on existing files.

This commit is contained in:
psychhim
2025-10-15 23:03:09 +05:30
parent e2a7d8dbad
commit 67ac620680

View File

@@ -177,7 +177,23 @@ local function smart_save(force_save_as)
-- For no-name buffers or Save As, write and set buffer name
local write_cmd = overwrite and 'write!' or 'write'
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)
end
print('Saved as ' .. filename)
else
-- Buffer already has a name, just save it