]> Git — Sourcephile - julm/julm-nix.git/blob - home-manager/profiles/nvim/lua/config/commands/jj.lua
use/op(nvim)(jj)(eagle): fix config
[julm/julm-nix.git] / home-manager / profiles / nvim / lua / config / commands / jj.lua
1 vim.api.nvim_create_user_command("Jopen", function(opts)
2 local revset = #opts.fargs > 0 and opts.fargs[1] or "@"
3
4 -- Get the files changed by given revset
5 local output =
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
8 if not ok then
9 vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" })
10 return
11 end
12
13 -- Split the output into lines and open each file
14 for file in output:gmatch("[^\r\n]+") do
15 local buf
16 if vim.fn.bufexists(file) ~= 0 then
17 buf = vim.fn.bufnr(file)
18 else
19 buf = vim.fn.bufadd(file)
20 end
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)
25 -- end
26 vim.api.nvim_set_option_value("buflisted", true, { buf = buf })
27 end
28 end, { desc = "Open files changed by given revset", nargs = "?" })
29
30 vim.api.nvim_create_user_command("Jclose", function(opts)
31 local revset = #opts.fargs > 0 and opts.fargs[1] or "@"
32
33 local output =
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
36 if not ok then
37 vim.notify(string.format("Error getting diff of revset: %s", revset), vim.log.levels.ERROR, { title = "JJ" })
38 return
39 end
40
41 -- Split the output into lines and close each file
42 for file in output:gmatch("[^\r\n]+") do
43 local buf
44 if vim.fn.bufexists(file) ~= 0 then
45 vim.api.nvim_buf_delete(vim.fn.bufnr(file), { force = false, unload = false })
46 end
47 end
48
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")
53 refresh()
54 end, { desc = "Close buffers for files changed by given revset", nargs = "?" })
55
56 vim.api.nvim_create_user_command("Jview", function(opts)
57 local left
58 local right
59 local utils = require("jj.utils")
60 if #opts.fargs <= 1 then
61 if #opts.fargs == 0 then
62 left = "@-"
63 right = "@"
64 else
65 left, right = string.gmatch(opts.fargs[1], "([^.]+)[.][.](.+)$")()
66 end
67 if left == nil then
68 -- FixMe(completeness): does not work on merge commits
69 left = string.format("%s-", opts.fargs[1])
70 right = opts.fargs[1]
71 else
72 end
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))
80 end
81 end, {
82 desc = "Diff a revset and its parent (must be single), a revset range, or show history between two revsets",
83 nargs = "*",
84 })