vim.api.nvim_create_user_command("Jopen", function(opts) local revset = #opts.fargs > 0 and opts.fargs[1] or "@" -- Get the files changed by given revset local output = vim.fn.system(string.format("jj diff --name-only --revisions %s 2>/dev/null", vim.fn.shellescape(revset))) local ok = vim.v.shell_error == 0 if not ok then vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" }) return end -- Split the output into lines and open each file for file in output:gmatch("[^\r\n]+") do local buf if vim.fn.bufexists(file) ~= 0 then buf = vim.fn.bufnr(file) else buf = vim.fn.bufadd(file) end -- Explanation: loading many buffers here causes slow down and even Git errors in Neo-tree -- so let any needed loading happen whenever the user browses the buffers. -- if not vim.api.nvim_buf_is_loaded(buf) then -- vim.fn.bufload(buf) -- end vim.api.nvim_set_option_value("buflisted", true, { buf = buf }) end end, { desc = "Open files changed by given revset", nargs = "?" }) vim.api.nvim_create_user_command("Jclose", function(opts) local revset = #opts.fargs > 0 and opts.fargs[1] or "@" local output = vim.fn.system(string.format("jj diff --name-only --revisions %s 2>/dev/null", vim.fn.shellescape(revset))) local ok = vim.v.shell_error == 0 if not ok then vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" }) return end -- Split the output into lines and close each file for file in output:gmatch("[^\r\n]+") do local buf if vim.fn.bufexists(file) ~= 0 then vim.api.nvim_buf_delete(vim.fn.bufnr(file), { force = false, unload = false }) end end -- Refresh Neo-tree of buffers local manager = require("neo-tree.sources.manager") local utils = require("neo-tree.utils") local refresh = utils.wrap(manager.refresh, "buffers") refresh() end, { desc = "Close buffers for files changed by given revset", nargs = "?" }) vim.api.nvim_create_user_command("Jview", function(opts) local left local right local utils = require("jj.utils") if #opts.fargs <= 1 then if #opts.fargs == 0 then left = "@-" right = "@" else left, right = string.gmatch(opts.fargs[1], "([^.]+)[.][.](.+)$")() end if left == nil then -- FixMe(completeness): does not work on merge commits left = string.format("%s-", opts.fargs[1]) right = opts.fargs[1] else end left = utils.get_commit_id(left) right = utils.get_commit_id(right) vim.cmd(string.format("DiffviewOpen %s..%s", left, right)) elseif #opts.fargs == 2 then left = utils.get_commit_id(opts.fargs[1]) right = utils.get_commit_id(opts.fargs[2]) vim.cmd(string.format("DiffviewFileHistory --range=%s...%s --right-only --no-merges", left, right)) end end, { desc = "Diff a revset and its parent (must be single), a revset range, or show history between two revsets", nargs = "*", })