Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not close commit message with q until saved #607

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lua/neogit/buffers/commit_editor/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function M.new(filename, on_unload)
end

function M:open()
local written = false
local should_commit = false
self.buffer = Buffer.create {
name = self.filename,
filetype = "NeogitCommitMessage",
Expand All @@ -40,25 +40,25 @@ function M:open()
modifiable = true,
readonly = false,
autocmds = {
["BufWritePre"] = function()
written = true
end,
["BufUnload"] = function(o)
if written then
local buf = Buffer.create {
name = o.buf,
}
PriceHiller marked this conversation as resolved.
Show resolved Hide resolved
if not should_commit and buf:get_option("modified") then
if
not config.values.disable_commit_confirmation
and not input.get_confirmation("Are you sure you want to commit?")
then
-- Clear the buffer, without filling the register
vim.api.nvim_buf_set_lines(o.buf, 0, -1, false, {})
vim.api.nvim_buf_call(o.buf, function()
buf:set_lines(0, -1, false, {})
buf:call(function()
vim.cmd("silent w!")
end)
end
end

if self.on_unload then
self.on_unload(written)
if self.on_unload and not should_commit then
self.on_unload(true)
end

require("neogit.process").defer_show_preview_buffers()
Expand All @@ -67,7 +67,12 @@ function M:open()
mappings = {
n = {
["q"] = function(buffer)
buffer:close(true)
if not buffer:get_option("modified") then
buffer:close(true)
elseif input.get_confirmation("Commit message hasn't been saved. Abort?") then
should_commit = true
buffer:close(true)
end
end,
},
},
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/lib/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function Buffer:unlock()
end

function Buffer:get_option(name)
api.nvim_buf_get_option(self.handle, name)
return api.nvim_buf_get_option(self.handle, name)
end

function Buffer:set_option(name, value)
Expand Down