mirror of
https://github.com/zoriya/jj.nvim.git
synced 2026-08-05 02:36:07 +00:00
refactor: Make the floating buffer configurable keymaps too
This commit is contained in:
+33
-7
@@ -37,10 +37,15 @@ local status_module = require("jj.cmd.status")
|
||||
--- @field open_file? string|string[] Keymaps for the status command buffer, setting a keymap to nil will disable it
|
||||
--- @field restore_file? string|string[]
|
||||
|
||||
--- @class jj.cmd.floating.keymaps The floating buffer is the one shown when diffing from the log buffer
|
||||
--- @field close? string|string[] Keymaps to close the floating buffer
|
||||
--- @field hide? string|string[] Keymaps to hide the floating buffer
|
||||
|
||||
--- @class jj.cmd.keymaps
|
||||
--- @field log? jj.cmd.log.keymaps Keymaps for the log command buffer
|
||||
--- @field status? jj.cmd.status.keymaps Keymaps for the status command buffer
|
||||
--- @field close? string|string[] Keymaps for the close keybind
|
||||
--- @field floating? jj.cmd.floating.keymaps Keymaps for the floating buffer
|
||||
|
||||
--- @class jj.cmd.opts
|
||||
--- @field describe? jj.cmd.describe
|
||||
@@ -80,6 +85,10 @@ M.config = {
|
||||
restore_file = "<S-x>",
|
||||
},
|
||||
close = { "q", "<Esc>" },
|
||||
floating = {
|
||||
close = "q",
|
||||
hide = "<Esc>",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -138,9 +147,9 @@ function M.resolve_keymaps_from_specs(cfg, specs)
|
||||
return keymaps
|
||||
end
|
||||
|
||||
-- Resolve close keymaps from config
|
||||
-- Resolve terminal keymaps from config
|
||||
--- @return jj.core.buffer.keymap[]
|
||||
function M.close_keymaps()
|
||||
function M.terminal_keymaps()
|
||||
local cfg = M.config.keymaps.close or {}
|
||||
|
||||
return M.resolve_keymaps_from_specs({ close = cfg }, {
|
||||
@@ -151,6 +160,23 @@ function M.close_keymaps()
|
||||
})
|
||||
end
|
||||
|
||||
-- Resolve floating keymaps from config
|
||||
--- @return jj.core.buffer.keymap[]
|
||||
function M.floating_keymaps()
|
||||
local cfg = M.config.keymaps.floating or {}
|
||||
|
||||
return M.resolve_keymaps_from_specs(cfg, {
|
||||
close = {
|
||||
desc = "Close floating buffer",
|
||||
handler = terminal.close_floating_buffer,
|
||||
},
|
||||
hide = {
|
||||
desc = "Hide floating buffer",
|
||||
handler = terminal.hide_floating_buffer,
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
--- @class jj.cmd.new_opts
|
||||
--- @field show_log? boolean Whether or not to display the log command after creating a new
|
||||
--- @field with_input? boolean Whether or not to use nvim input to decide the parent of the new commit
|
||||
@@ -264,7 +290,7 @@ function M.diff(opts)
|
||||
end
|
||||
end
|
||||
|
||||
terminal.run(cmd, M.close_keymaps())
|
||||
terminal.run(cmd, M.terminal_keymaps())
|
||||
end
|
||||
|
||||
-- Jujutsu rebase
|
||||
@@ -384,13 +410,13 @@ function M.j(args)
|
||||
true
|
||||
)
|
||||
if not success then
|
||||
terminal.run("jj", M.close_keymaps())
|
||||
terminal.run("jj", M.terminal_keymaps())
|
||||
return
|
||||
end
|
||||
|
||||
local default_cmd = parser.parse_default_cmd(default_cmd_str and default_cmd_str or "")
|
||||
if default_cmd == nil then
|
||||
terminal.run("jj", M.close_keymaps())
|
||||
terminal.run("jj", M.terminal_keymaps())
|
||||
return
|
||||
end
|
||||
args = default_cmd
|
||||
@@ -416,7 +442,7 @@ function M.j(args)
|
||||
if #remaining_args == 0 then
|
||||
M.edit()
|
||||
else
|
||||
terminal.run(cmd)
|
||||
terminal.run(cmd, M.terminal_keymaps())
|
||||
end
|
||||
end,
|
||||
new = function()
|
||||
@@ -448,7 +474,7 @@ function M.j(args)
|
||||
if handlers[subcommand] then
|
||||
handlers[subcommand]()
|
||||
else
|
||||
terminal.run(cmd, M.close_keymaps())
|
||||
terminal.run(cmd, M.terminal_keymaps())
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+2
-2
@@ -107,7 +107,7 @@ function M.handle_log_diff()
|
||||
|
||||
if revset then
|
||||
local cmd = string.format("jj show %s", revset)
|
||||
terminal.run_floating(cmd)
|
||||
terminal.run_floating(cmd, require("jj.cmd").floating_keymaps())
|
||||
else
|
||||
utils.notify("No valid revision found in the log line", vim.log.levels.ERROR)
|
||||
end
|
||||
@@ -216,7 +216,7 @@ function M.log_keymaps()
|
||||
},
|
||||
}
|
||||
|
||||
return cmd.merge_keymaps(cmd.resolve_keymaps_from_specs(cfg, specs), cmd.close_keymaps())
|
||||
return cmd.merge_keymaps(cmd.resolve_keymaps_from_specs(cfg, specs), cmd.terminal_keymaps())
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -100,9 +100,9 @@ function M.status(opts)
|
||||
else
|
||||
-- Default behavior: show in buffer
|
||||
local cmd = require("jj.cmd")
|
||||
local keymaps = cmd.merge_keymaps(M.status_keymaps(), cmd.close_keymaps())
|
||||
local keymaps = cmd.merge_keymaps(M.status_keymaps(), cmd.terminal_keymaps())
|
||||
terminal.run(cmd_str, keymaps)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
return M
|
||||
|
||||
+16
-13
@@ -38,12 +38,12 @@ function M.close_terminal_buffer()
|
||||
end
|
||||
|
||||
--- Close the current terminal buffer if it exists
|
||||
local function close_floating_buffer()
|
||||
function M.close_floating_buffer()
|
||||
buffer.close(state.floating_buf)
|
||||
end
|
||||
|
||||
--- Hide the current floating window
|
||||
local function hide_floating_window()
|
||||
function M.hide_floating_buffer()
|
||||
if not state.floating_buf then
|
||||
return
|
||||
elseif state.floating_buf and vim.api.nvim_buf_is_valid(state.floating_buf) then
|
||||
@@ -53,7 +53,8 @@ end
|
||||
|
||||
--- Run the command in a floating window
|
||||
--- @param cmd string The command to run in the floating window
|
||||
function M.run_floating(cmd)
|
||||
--- @param keymaps jj.core.buffer.keymap[]|nil Additional keymaps to set for this floating buffer
|
||||
function M.run_floating(cmd, keymaps)
|
||||
-- Clean up previous state if invalid
|
||||
if state.floating_buf and not vim.api.nvim_buf_is_valid(state.floating_buf) then
|
||||
state.floating_buf = nil
|
||||
@@ -154,19 +155,21 @@ function M.run_floating(cmd)
|
||||
|
||||
-- Set keymaps only if they haven't been set for this buffer
|
||||
if not vim.b[state.floating_buf].jj_keymaps_set then
|
||||
buffer.set_keymaps(state.floating_buf, {
|
||||
local default_keymaps = {
|
||||
{ modes = { "n", "v" }, lhs = "i", rhs = function() end },
|
||||
{ modes = { "n", "v" }, lhs = "c", rhs = function() end },
|
||||
{ modes = { "n", "v" }, lhs = "a", rhs = function() end },
|
||||
{ modes = { "n", "v" }, lhs = "u", rhs = function() end },
|
||||
{
|
||||
modes = { "n", "v" },
|
||||
lhs = "q",
|
||||
rhs = close_floating_buffer,
|
||||
opts = { desc = "Close the floating buffer" },
|
||||
},
|
||||
{ modes = "n", lhs = "<ESC>", rhs = hide_floating_window, opts = { desc = "Hide the buffer" } },
|
||||
})
|
||||
}
|
||||
|
||||
-- Merge default keymaps with provided keymaps
|
||||
if keymaps and #keymaps > 0 then
|
||||
for _, km in ipairs(keymaps) do
|
||||
table.insert(default_keymaps, km)
|
||||
end
|
||||
end
|
||||
|
||||
buffer.set_keymaps(state.floating_buf, default_keymaps)
|
||||
vim.b[state.floating_buf].jj_keymaps_set = true
|
||||
end
|
||||
end
|
||||
@@ -325,4 +328,4 @@ function M.run(cmd, keymaps)
|
||||
vim.cmd("stopinsert")
|
||||
end
|
||||
|
||||
return M
|
||||
return M
|
||||
Reference in New Issue
Block a user