From 59c0750437b5d74e615abf65c67cabf65f97e4b7 Mon Sep 17 00:00:00 2001 From: NicolasGB Date: Wed, 2 Jul 2025 18:14:31 +0200 Subject: [PATCH] feat(diff): Add options to diff command to allow diffing only current buffer --- lua/jj/cmd.lua | 21 ++++++++++++++++++--- lua/jj/picker.lua | 1 + lua/jj/utils.lua | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lua/jj/cmd.lua b/lua/jj/cmd.lua index d2d3173..53a0f98 100644 --- a/lua/jj/cmd.lua +++ b/lua/jj/cmd.lua @@ -37,7 +37,6 @@ local function handle_status_enter() if renamed_file then -- For renamed files, we need to construct the full path - -- Extract the directory part before the "{" local dir_pattern = "^R (.*)/{.*}$" local dir_path = line:match(dir_pattern) if dir_path then @@ -46,7 +45,7 @@ local function handle_status_enter() filepath = renamed_file end else - -- jj status format: "M filename" or "A filename" or "D filename" etc. + -- jj status format: "M filename" or "A filename" -- Match lines that start with status letter followed by space and filename local pattern = "^[MA?!] (.+)$" filepath = line:match(pattern) @@ -403,13 +402,29 @@ function M.log(opts) run(cmd) end +---@class jj.cmd.diff_opts +---@field current boolean Wether or not to only diff the current buffer + --- Jujutsu diff -function M.diff() +--- @param opts? jj.cmd.diff_opts The options for the diff command +function M.diff(opts) if not utils.ensure_jj() then return end local cmd = "jj diff" + + if opts and opts.current then + local file = vim.fn.expand("%:p") + utils.notify(file) + if file and file ~= "" then + cmd = string.format("%s %s", cmd, vim.fn.fnameescape(file)) + else + utils.notify("Current buffer is not a file", vim.log.levels.ERROR) + return + end + end + run(cmd) end diff --git a/lua/jj/picker.lua b/lua/jj/picker.lua index ba5206e..0aab49d 100644 --- a/lua/jj/picker.lua +++ b/lua/jj/picker.lua @@ -44,6 +44,7 @@ local function get_files() local change, file_path = line:match("^(%a)%s(.+)$") table.insert(files, { + text = file_path, file = file_path, change = change, diff_cmd = string.format("jj diff %s", file_path), diff --git a/lua/jj/utils.lua b/lua/jj/utils.lua index 5927092..42d0c29 100644 --- a/lua/jj/utils.lua +++ b/lua/jj/utils.lua @@ -101,7 +101,7 @@ end ---- Notify function to display messages with a title --- @param message string The message to display ---- @param level number The log level (default: INFO) +--- @param level? number The log level (default: INFO) function M.notify(message, level) level = level or vim.log.levels.INFO vim.notify(message, level, { title = "JJ", timeout = 3000 })