diff --git a/README.md b/README.md index cb6ccdb..a07e18c 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,11 @@ The plugin also provides `:Jdiff`, `:Jvdiff`, and `:Jhdiff` commands for diffing } }, + -- Configure log command behavior + log = { + close_on_edit = false, -- Close log buffer after editing a change + }, + -- Configure keymaps for command buffers keymaps = { -- Log buffer keymaps (set to nil to disable) diff --git a/lua/jj/cmd/init.lua b/lua/jj/cmd/init.lua index d560e1c..c2b0f4a 100644 --- a/lua/jj/cmd/init.lua +++ b/lua/jj/cmd/init.lua @@ -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 = "", @@ -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 + diff --git a/lua/jj/cmd/log.lua b/lua/jj/cmd/log.lua index 02f7373..d9fa252 100644 --- a/lua/jj/cmd/log.lua +++ b/lua/jj/cmd/log.lua @@ -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