From 2b99f1b391a6d5a1b966f155572c6294f360852f Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 13:02:54 -0500 Subject: [PATCH 1/2] feat: do not close commit message with `q` until saved Fixes #552 --- lua/neogit/buffers/commit_editor/init.lua | 10 +++++++++- lua/neogit/lib/buffer.lua | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lua/neogit/buffers/commit_editor/init.lua b/lua/neogit/buffers/commit_editor/init.lua index 6abf89e97..e1ae6b191 100644 --- a/lua/neogit/buffers/commit_editor/init.lua +++ b/lua/neogit/buffers/commit_editor/init.lua @@ -67,7 +67,15 @@ function M:open() mappings = { n = { ["q"] = function(buffer) - buffer:close(true) + if buffer:get_option("modified") then + require("neogit.lib.notification").create( + "Commit message has not been saved! Try `:w`.", + vim.log.levels.WARN + ) + else + self:close() + buffer:close(true) + end end, }, }, diff --git a/lua/neogit/lib/buffer.lua b/lua/neogit/lib/buffer.lua index b2ee917e7..615f52956 100644 --- a/lua/neogit/lib/buffer.lua +++ b/lua/neogit/lib/buffer.lua @@ -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) From f91630843b5765e5f9ea3db108011349f488a279 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 14:01:16 -0500 Subject: [PATCH 2/2] refactor: get user confirmation for aborting a commit --- lua/neogit/buffers/commit_editor/init.lua | 29 ++++++++++------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lua/neogit/buffers/commit_editor/init.lua b/lua/neogit/buffers/commit_editor/init.lua index e1ae6b191..9aac32990 100644 --- a/lua/neogit/buffers/commit_editor/init.lua +++ b/lua/neogit/buffers/commit_editor/init.lua @@ -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", @@ -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, + } + 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() @@ -67,13 +67,10 @@ function M:open() mappings = { n = { ["q"] = function(buffer) - if buffer:get_option("modified") then - require("neogit.lib.notification").create( - "Commit message has not been saved! Try `:w`.", - vim.log.levels.WARN - ) - else - self:close() + 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,