feat: add close_on_edit config for log buffer (#48)

- Add close_on_edit option to close log buffer after editing a change (default: false)
- Rename handle_log_enter to handle_log_edit with close_on_exit parameter
- Update log keymaps to pass close_on_edit config to handlers
- Update config types and defaults in init.lua
This commit is contained in:
Nicolas GB
2025-11-26 15:48:56 +01:00
committed by GitHub
parent 39de3c6bc9
commit 10c611eaab
3 changed files with 38 additions and 14 deletions
+18 -5
View File
@@ -21,6 +21,9 @@ local status_module = require("jj.cmd.status")
--- @class jj.cmd.describe
--- @field editor? jj.cmd.describe.editor Options for the describe message editor
--- @class jj.cmd.log
--- @field close_on_edit? boolean Whether to close the log buffer when editing a change
--- @class jj.cmd.log.keymaps
--- @field checkout? string|string[] Keymaps for the log command buffer, setting a keymap to nil will disable it
--- @field checkout_immutable? string|string[]
@@ -49,6 +52,7 @@ local status_module = require("jj.cmd.status")
--- @class jj.cmd.opts
--- @field describe? jj.cmd.describe
--- @field log? jj.cmd.log
--- @field keymaps? jj.cmd.keymaps Keymaps for the buffers containing the output of the commands
---
--- @class jj.cmd.keymap_spec
@@ -68,6 +72,9 @@ M.config = {
},
},
},
log = {
close_on_edit = false,
},
keymaps = {
log = {
edit = "<CR>",
@@ -131,15 +138,20 @@ function M.resolve_keymaps_from_specs(cfg, specs)
for key, spec in pairs(specs) do
local lhs = cfg[key]
if lhs and spec.handler then
-- Create the handler, wrapping it with args if provided
local handler = spec.handler
if spec.args then
handler = function()
spec.handler(unpack(spec.args))
end
end
if type(lhs) == "table" then
for _, key_lhs in ipairs(lhs) do
table.insert(
keymaps,
{ modes = "n", lhs = key_lhs, rhs = spec.handler, opts = { desc = spec.desc } }
)
table.insert(keymaps, { modes = "n", lhs = key_lhs, rhs = handler, opts = { desc = spec.desc } })
end
else
table.insert(keymaps, { modes = "n", lhs = lhs, rhs = spec.handler, opts = { desc = spec.desc } })
table.insert(keymaps, { modes = "n", lhs = lhs, rhs = handler, opts = { desc = spec.desc } })
end
end
end
@@ -543,3 +555,4 @@ function M.register_command()
end
return M
+15 -9
View File
@@ -134,7 +134,8 @@ end
--- Silently returns if no revision is found or the jj command fails.
--- On success, notifies and refreshes the log buffer.
--- @param ignore_immut? boolean Pass --ignore-immutable to jj edit when true.
function M.handle_log_enter(ignore_immut)
--- @param close_on_exit? boolean Close the log buffer after editing when true.
function M.handle_log_edit(ignore_immut, close_on_exit)
local line = vim.api.nvim_get_current_line()
local revset = parser.get_rev_from_log_line(line)
if not revset or revset == "" then
@@ -160,9 +161,13 @@ function M.handle_log_enter(ignore_immut)
return
end
utils.notify(string.format("Editing change: `%s`", revset), vim.log.levels.INFO)
-- Close the terminal buffer
terminal.close_terminal_buffer()
if close_on_exit then
utils.notify(string.format("Editing change: `%s`", revset), vim.log.levels.INFO)
terminal.close_terminal_buffer()
else
M.log({})
end
end
--- Resolve log keymaps from config, filtering out nil values
@@ -175,18 +180,19 @@ function M.log_keymaps()
-- desc: Keybind description
-- handler: function to call
-- args: optional list of arguments passed to handler
local cfg = cmd.config.keymaps.log or {}
local keymaps = cmd.config.keymaps.log or {}
local close_on_edit = cmd.config.log.close_on_edit or false
local specs = {
edit = {
desc = "Checkout revision under cursor",
handler = M.handle_log_enter,
args = { false },
handler = M.handle_log_edit,
args = { false, close_on_edit },
},
edit_immutable = {
desc = "Checkout revision under cursor (ignores immutability)",
handler = M.handle_log_enter,
args = { true },
handler = M.handle_log_edit,
args = { true, close_on_edit },
},
describe = {
desc = "Describe revision under cursor",
@@ -221,7 +227,7 @@ function M.log_keymaps()
},
}
return cmd.merge_keymaps(cmd.resolve_keymaps_from_specs(cfg, specs), cmd.terminal_keymaps())
return cmd.merge_keymaps(cmd.resolve_keymaps_from_specs(keymaps, specs), cmd.terminal_keymaps())
end
return M