From a33cbba40f18393d47e2ac2c4b00c2bc3047b571 Mon Sep 17 00:00:00 2001 From: NicolasGB Date: Thu, 16 Oct 2025 19:11:05 +0200 Subject: [PATCH] refactor(diff): Improve api with lua types and more flexibility --- lua/jj/cmd.lua | 34 ++++++++++++++++++++++------------ lua/jj/diff.lua | 20 ++++++++++++++------ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lua/jj/cmd.lua b/lua/jj/cmd.lua index e6f93cf..deaca3b 100644 --- a/lua/jj/cmd.lua +++ b/lua/jj/cmd.lua @@ -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 diff --git a/lua/jj/diff.lua b/lua/jj/diff.lua index fe183aa..88a6cd2 100644 --- a/lua/jj/diff.lua +++ b/lua/jj/diff.lua @@ -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 -