Skip to content

Commit 76751e3

Browse files
committed
feat: savable scratch buffers (issue: #90)
Code commands such as GpEnew as well as hooks which generate new buffer will start as listed scratch buffers to ease neovim exit. BufWritePre callback will change it to a normal buffer if user decides to save given buf.
1 parent 76ad40a commit 76751e3

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lua/gp/init.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,14 +2718,25 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
27182718
vim.cmd("tabnew")
27192719
win = vim.api.nvim_get_current_win()
27202720
end
2721-
-- create a new buffer
2722-
buf = vim.api.nvim_create_buf(true, false)
2723-
-- set the created buffer as the current buffer
2721+
2722+
buf = vim.api.nvim_create_buf(true, true)
27242723
vim.api.nvim_set_current_buf(buf)
2725-
-- set the filetype
2724+
2725+
local group = M._H.create_augroup("GpScratchSave" .. _H.uuid(), { clear = true })
2726+
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
2727+
buffer = buf,
2728+
group = group,
2729+
callback = function(ctx)
2730+
vim.api.nvim_buf_set_option(ctx.buf, "buftype", "")
2731+
vim.api.nvim_buf_set_name(ctx.buf, ctx.file)
2732+
vim.api.nvim_command("w!")
2733+
vim.api.nvim_del_augroup_by_id(ctx.group)
2734+
end,
2735+
})
2736+
27262737
local ft = target.filetype or filetype
27272738
vim.api.nvim_buf_set_option(buf, "filetype", ft)
2728-
-- prepare handler
2739+
27292740
handler = M.create_handler(buf, win, 0, false, "", cursor)
27302741
end
27312742

0 commit comments

Comments
 (0)