From 920b3476848e516cdc3e928f1604be32758c1dc0 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 14 Jul 2023 17:15:47 -0500 Subject: [PATCH 1/8] fix: ensure floating popup windows are auto closed when they lose focus Fixes #591 --- lua/neogit/lib/popup.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/neogit/lib/popup.lua b/lua/neogit/lib/popup.lua index 69a092f6d..fbccf7abf 100644 --- a/lua/neogit/lib/popup.lua +++ b/lua/neogit/lib/popup.lua @@ -575,6 +575,19 @@ function M:show() } end, } + + -- Closes the window if it loses focus and it is a floating buffer + if self.buffer.kind == "floating" then + vim.api.nvim_create_autocmd("WinLeave", { + callback = function(win) + if win.buf == self.buffer.handle then + -- We pcall this because it's possible the window was closed by a command invocation, e.g. "cc" for commits + pcall(self.close) + end + end, + once = true, + }) + end end return M From 5884423d8f2b97da20730722d34836597d5d1a21 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 09:41:16 -0500 Subject: [PATCH 2/8] refactor: use in-built autocmd registration functionality --- lua/neogit/lib/popup.lua | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lua/neogit/lib/popup.lua b/lua/neogit/lib/popup.lua index fbccf7abf..2dca4d86e 100644 --- a/lua/neogit/lib/popup.lua +++ b/lua/neogit/lib/popup.lua @@ -574,20 +574,15 @@ function M:show() }, } end, - } - - -- Closes the window if it loses focus and it is a floating buffer - if self.buffer.kind == "floating" then - vim.api.nvim_create_autocmd("WinLeave", { - callback = function(win) - if win.buf == self.buffer.handle then + autocmds = { + ["WinLeave"] = function() + if self.buffer.kind == "floating" then -- We pcall this because it's possible the window was closed by a command invocation, e.g. "cc" for commits - pcall(self.close) + pcall(self.close, self) end end, - once = true, - }) - end + }, + } end return M From c3a2265036364529d4cd52e6414a6a0686740dea Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 12:31:53 -0500 Subject: [PATCH 3/8] fix: ensure all created autocmds are part of the Neogit augroup --- lua/neogit/autocmds.lua | 4 ++-- lua/neogit/lib/buffer.lua | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lua/neogit/autocmds.lua b/lua/neogit/autocmds.lua index 522489987..2b1d407e3 100644 --- a/lua/neogit/autocmds.lua +++ b/lua/neogit/autocmds.lua @@ -1,7 +1,7 @@ local M = {} local api = vim.api -local group = api.nvim_create_augroup("Neogit", { clear = true }) +M.group = api.nvim_create_augroup("Neogit", { clear = true }) local a = require("plenary.async") local status = require("neogit.status") @@ -28,7 +28,7 @@ function M.setup() status.refresh({ status = true, diffs = { "*:" .. path } }, string.format("%s:%s", o.event, o.file)) end, function() end) end, - group = group, + group = M.group, }) end diff --git a/lua/neogit/lib/buffer.lua b/lua/neogit/lib/buffer.lua index b2ee917e7..9559d5941 100644 --- a/lua/neogit/lib/buffer.lua +++ b/lua/neogit/lib/buffer.lua @@ -401,8 +401,9 @@ function Buffer.create(config) buffer.ui:render(unpack(config.render(buffer))) end + local neogit_augroup = require("neogit.autocmds").group for event, callback in pairs(config.autocmds or {}) do - api.nvim_create_autocmd(event, { callback = callback, buffer = buffer.handle }) + api.nvim_create_autocmd(event, { callback = callback, buffer = buffer.handle, group = neogit_augroup }) end buffer.mmanager.register() From b8d08dbbf699d4f3018ada4f06cd8cf3cf99e72b Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 15 Jul 2023 20:59:57 +0200 Subject: [PATCH 4/8] Extract pad_right into util's module --- lua/neogit/lib/util.lua | 5 +++++ lua/neogit/status.lua | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lua/neogit/lib/util.lua b/lua/neogit/lib/util.lua index 42a8d418a..d0ebc33a6 100644 --- a/lua/neogit/lib/util.lua +++ b/lua/neogit/lib/util.lua @@ -249,6 +249,10 @@ local function build_reverse_lookup(tbl) return result end +local function pad_right(s, len) + return s .. string.rep(" ", math.max(len - #s, 0)) +end + return { time = time, time_async = time_async, @@ -276,4 +280,5 @@ return { merge = merge, str_min_width = str_min_width, str_clamp = str_clamp, + pad_right = pad_right, } diff --git a/lua/neogit/status.lua b/lua/neogit/status.lua index bd7e650c3..7a083f13f 100644 --- a/lua/neogit/status.lua +++ b/lua/neogit/status.lua @@ -12,6 +12,7 @@ local F = require("neogit.lib.functional") local LineBuffer = require("neogit.lib.line_buffer") local fs = require("neogit.lib.fs") local input = require("neogit.lib.input") +local util = require("neogit.lib.util") local map = require("neogit.lib.util").map local api = vim.api @@ -145,10 +146,6 @@ local function format_mode(mode) return mode end -local function pad_right(s, len) - return s .. string.rep(" ", math.max(len - #s, 0)) -end - local function draw_buffer() M.status_buffer:clear_sign_group("hl") M.status_buffer:clear_sign_group("fold_markers") @@ -207,7 +204,7 @@ local function draw_buffer() location.files = {} for _, f in ipairs(data.items) do - local label = pad_right(format_mode(f.mode), max_len) + local label = util.pad_right(format_mode(f.mode), max_len) if label and vim.o.columns < 120 then label = vim.trim(label) end From 1c592672d6f3f34aec4f842d40963b0ba7d1ecca Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 15 Jul 2023 21:02:18 +0200 Subject: [PATCH 5/8] highlight base branch in rebase popup --- lua/neogit/popups/rebase/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/neogit/popups/rebase/init.lua b/lua/neogit/popups/rebase/init.lua index b6f2d61b2..73743ec59 100644 --- a/lua/neogit/popups/rebase/init.lua +++ b/lua/neogit/popups/rebase/init.lua @@ -42,7 +42,7 @@ function M.create(commit) :action_if(not in_rebase, "f", "to autosquash") :env({ commit = commit, - highlight = { branch, git.repo.upstream.ref }, + highlight = { branch, git.repo.upstream.ref, base_branch }, bold = { "@{upstream}", "pushRemote" }, }) :build() From 0ec5c945aeaadd634247bd2bbc8f8a6e992b8769 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sat, 15 Jul 2023 21:03:25 +0200 Subject: [PATCH 6/8] These tend to be a bit noisy when reading logs, so lower it from debug to trace --- lua/neogit/process.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neogit/process.lua b/lua/neogit/process.lua index 5954c2013..a5c489869 100644 --- a/lua/neogit/process.lua +++ b/lua/neogit/process.lua @@ -66,7 +66,7 @@ local function create_preview_buffer() -- May be called multiple times due to scheduling if preview_buffer then if preview_buffer.buffer then - logger.debug("Preview buffer already exists. Focusing the existing one") + logger.trace("[PROCESS] Preview buffer already exists. Focusing the existing one") preview_buffer.buffer:focus() end return @@ -360,7 +360,7 @@ function Process:spawn(cb) end end - logger.debug("Spawning: " .. vim.inspect(self.cmd)) + logger.trace("[PROCESS] Spawning: " .. vim.inspect(self.cmd)) local job = vim.fn.jobstart(self.cmd, { cwd = self.cwd, env = self.env, From baadc1b1f55b7ca92c54cfded1c68e1c7fcc5a23 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 14:32:44 -0500 Subject: [PATCH 7/8] refactor: create augroup at base neogit file and export --- lua/neogit.lua | 1 + lua/neogit/autocmds.lua | 5 ++--- lua/neogit/lib/buffer.lua | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/neogit.lua b/lua/neogit.lua index 7be5f8909..3d497a664 100644 --- a/lua/neogit.lua +++ b/lua/neogit.lua @@ -123,4 +123,5 @@ return { close = status.close, setup = setup, complete = complete, + autocmd_group = vim.api.nvim_create_augroup("Neogit", { clear = false }), } diff --git a/lua/neogit/autocmds.lua b/lua/neogit/autocmds.lua index 2b1d407e3..9a990b4e9 100644 --- a/lua/neogit/autocmds.lua +++ b/lua/neogit/autocmds.lua @@ -1,11 +1,10 @@ local M = {} local api = vim.api -M.group = api.nvim_create_augroup("Neogit", { clear = true }) - local a = require("plenary.async") local status = require("neogit.status") local fs = require("neogit.lib.fs") +local group = require("neogit").autocmd_group function M.setup() api.nvim_create_autocmd({ "BufWritePost", "ShellCmdPost", "VimResume" }, { @@ -28,7 +27,7 @@ function M.setup() status.refresh({ status = true, diffs = { "*:" .. path } }, string.format("%s:%s", o.event, o.file)) end, function() end) end, - group = M.group, + group = group, }) end diff --git a/lua/neogit/lib/buffer.lua b/lua/neogit/lib/buffer.lua index 9559d5941..14461d905 100644 --- a/lua/neogit/lib/buffer.lua +++ b/lua/neogit/lib/buffer.lua @@ -401,7 +401,7 @@ function Buffer.create(config) buffer.ui:render(unpack(config.render(buffer))) end - local neogit_augroup = require("neogit.autocmds").group + local neogit_augroup = require("neogit").autocmd_group for event, callback in pairs(config.autocmds or {}) do api.nvim_create_autocmd(event, { callback = callback, buffer = buffer.handle, group = neogit_augroup }) end From b57d5ee08be53a2c4ca35c1a7d4efe7cac0498a7 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Sat, 15 Jul 2023 16:50:28 -0500 Subject: [PATCH 8/8] fix: show the correctly bound key for the top Status Hint Fixes #564 (partially) --- lua/neogit/config.lua | 4 ++++ lua/neogit/popups/help/actions.lua | 4 +--- lua/neogit/status.lua | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lua/neogit/config.lua b/lua/neogit/config.lua index 04ff05d30..38ed2abcc 100644 --- a/lua/neogit/config.lua +++ b/lua/neogit/config.lua @@ -1,5 +1,9 @@ local M = {} +function M.get_reversed_status_maps() + return vim.tbl_add_reverse_lookup(vim.tbl_deep_extend("force", M.values.mappings.status, {})) +end + M.values = { disable_hint = false, disable_context_highlighting = false, diff --git a/lua/neogit/popups/help/actions.lua b/lua/neogit/popups/help/actions.lua index 1651802ca..361a9bb29 100644 --- a/lua/neogit/popups/help/actions.lua +++ b/lua/neogit/popups/help/actions.lua @@ -4,9 +4,7 @@ local util = require("neogit.lib.util") local NONE = function() end -- Using deep extend this way creates a copy of the mapping values -local status_mappings = vim.tbl_add_reverse_lookup( - vim.tbl_deep_extend("force", require("neogit.config").values.mappings.status, {}) -) +local status_mappings = require("neogit.config").get_reversed_status_maps() local function present(commands) local presenter = util.map(commands, function(command) diff --git a/lua/neogit/status.lua b/lua/neogit/status.lua index 7a083f13f..ca7db5f77 100644 --- a/lua/neogit/status.lua +++ b/lua/neogit/status.lua @@ -152,7 +152,22 @@ local function draw_buffer() local output = LineBuffer.new() if not config.values.disable_hint then - output:append("Hint: [] toggle diff | [s]tage | [u]nstage | [x] discard | [c]ommit | [?] more help") + local reversed_status_map = config.get_reversed_status_maps() + + local function hint_label(map_name, hint) + return "[" .. reversed_status_map[map_name] .. "] " .. hint + end + + local hints = { + hint_label("Toggle", "toggle diff"), + hint_label("Stage", "stage"), + hint_label("Unstage", "unstage"), + hint_label("Discard", "discard"), + hint_label("CommitPopup", "commit"), + hint_label("HelpPopup", "help"), + } + + output:append("Hint: " .. table.concat(hints, " | ")) output:append("") end