1 vim.api.nvim_create_user_command("Jopen", function(opts)
2 local revset = #opts.fargs > 0 and opts.fargs[1] or "@"
4 -- Get the files changed by given revset
6 vim.fn.system(string.format("jj diff --name-only --revisions %s 2>/dev/null", vim.fn.shellescape(revset)))
7 local ok = vim.v.shell_error == 0
9 vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" })
13 -- Split the output into lines and open each file
14 for file in output:gmatch("[^\r\n]+") do
16 if vim.fn.bufexists(file) ~= 0 then
17 buf = vim.fn.bufnr(file)
19 buf = vim.fn.bufadd(file)
21 -- Explanation: loading many buffers here causes slow down and even Git errors in Neo-tree
22 -- so let any needed loading happen whenever the user browses the buffers.
23 -- if not vim.api.nvim_buf_is_loaded(buf) then
24 -- vim.fn.bufload(buf)
26 vim.api.nvim_set_option_value("buflisted", true, { buf = buf })
28 end, { desc = "Open files changed by given revset", nargs = "?" })
30 vim.api.nvim_create_user_command("Jclose", function(opts)
31 local revset = #opts.fargs > 0 and opts.fargs[1] or "@"
34 vim.fn.system(string.format("jj diff --name-only --revisions %s 2>/dev/null", vim.fn.shellescape(revset)))
35 local ok = vim.v.shell_error == 0
37 vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" })
41 -- Split the output into lines and close each file
42 for file in output:gmatch("[^\r\n]+") do
44 if vim.fn.bufexists(file) ~= 0 then
45 vim.api.nvim_buf_delete(vim.fn.bufnr(file), { force = false, unload = false })
49 -- Refresh Neo-tree of buffers
50 local manager = require("neo-tree.sources.manager")
51 local utils = require("neo-tree.utils")
52 local refresh = utils.wrap(manager.refresh, "buffers")
54 end, { desc = "Close buffers for files changed by given revset", nargs = "?" })
56 vim.api.nvim_create_user_command("Jview", function(opts)
59 local utils = require("jj.utils")
60 if #opts.fargs <= 1 then
61 if #opts.fargs == 0 then
65 left, right = string.gmatch(opts.fargs[1], "([^.]+)[.][.](.+)$")()
68 -- FixMe(completeness): does not work on merge commits
69 left = string.format("%s-", opts.fargs[1])
73 left = utils.get_commit_id(left)
74 right = utils.get_commit_id(right)
75 vim.cmd(string.format("DiffviewOpen %s..%s", left, right))
76 elseif #opts.fargs == 2 then
77 left = utils.get_commit_id(opts.fargs[1])
78 right = utils.get_commit_id(opts.fargs[2])
79 vim.cmd(string.format("DiffviewFileHistory --range=%s...%s --right-only --no-merges", left, right))
82 desc = "Diff a revset and its parent (must be single), a revset range, or show history between two revsets",