mirror of
https://github.com/zoriya/jj.nvim.git
synced 2026-08-09 12:46:15 +00:00
refactor(diff): Improve api with lua types and more flexibility
This commit is contained in:
+22
-12
@@ -975,18 +975,28 @@ function M.register_command()
|
||||
desc = "Execute jj commands with subcommand support",
|
||||
})
|
||||
|
||||
vim.api.nvim_create_user_command("Jdiff", diff.open_vdiff, {
|
||||
nargs = "?",
|
||||
desc = "Diff against jj revision",
|
||||
})
|
||||
vim.api.nvim_create_user_command("Jhdiff", diff.open_hdiff, {
|
||||
nargs = "?",
|
||||
desc = "Horizontal diff against jj revision",
|
||||
})
|
||||
vim.api.nvim_create_user_command("Jvdiff", diff.open_vdiff, {
|
||||
nargs = "?",
|
||||
desc = "Vertical diff against jj revision",
|
||||
})
|
||||
-- Unified creation of jj diff commands with optional revision argument
|
||||
local function create_diff_command(name, fn, desc)
|
||||
vim.api.nvim_create_user_command(name, function(opts)
|
||||
local rev = opts.fargs[1]
|
||||
if rev then
|
||||
fn({ rev = rev })
|
||||
else
|
||||
fn()
|
||||
end
|
||||
end, {
|
||||
nargs = "?",
|
||||
desc = desc .. " (optionally pass jj revision)",
|
||||
})
|
||||
end
|
||||
|
||||
-- Commands:
|
||||
-- Jdiff : vertical diff by default
|
||||
-- Jhdiff : horizontal diff
|
||||
-- Jvdiff : vertical diff (explicit)
|
||||
create_diff_command("Jdiff", diff.open_vdiff, "Vertical diff against jj revision")
|
||||
create_diff_command("Jhdiff", diff.open_hdiff, "Horizontal diff against jj revision")
|
||||
create_diff_command("Jvdiff", diff.open_vdiff, "Vertical diff against jj revision")
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
+14
-6
@@ -45,15 +45,20 @@ function M.open_revision(rev, path)
|
||||
vim.api.nvim_win_set_buf(0, buf)
|
||||
end
|
||||
|
||||
---@class jj.diff.diff_opts
|
||||
---@field rev string the revision to diff against
|
||||
|
||||
--- Open a diff split for a specific revision of the current file
|
||||
--- @param split_fun function Split function for the diff
|
||||
--- @param args table Any passed arguments
|
||||
function M.open_diff(split_fun, args)
|
||||
--- @param opts? jj.diff.diff_opts Any passed arguments
|
||||
function M.open_diff(split_fun, opts)
|
||||
if not utils.ensure_jj() then
|
||||
return
|
||||
end
|
||||
|
||||
local rev = args[1] or "@-"
|
||||
-- Ensure opts is a table to avoid indexing nil
|
||||
opts = opts or {}
|
||||
local rev = opts.rev or "@-"
|
||||
local path = vim.api.nvim_buf_get_name(0)
|
||||
|
||||
vim.cmd.diffthis()
|
||||
@@ -62,13 +67,16 @@ function M.open_diff(split_fun, args)
|
||||
vim.cmd.diffthis()
|
||||
end
|
||||
|
||||
-- Open a vertical diff split for a specific revision of the current file
|
||||
--- @param opts? jj.diff.diff_opts Any passed arguments
|
||||
function M.open_vdiff(opts)
|
||||
M.open_diff(vim.cmd.vsplit, opts.fargs)
|
||||
M.open_diff(vim.cmd.vsplit, opts)
|
||||
end
|
||||
|
||||
-- Open a horizontal diff split for a specific revision of the current file
|
||||
--- @param opts? jj.diff.diff_opts Any passed arguments
|
||||
function M.open_hdiff(opts)
|
||||
M.open_diff(vim.cmd.split, opts.fargs)
|
||||
M.open_diff(vim.cmd.split, opts)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
|
||||
Reference in New Issue
Block a user