Keep an eye on those revision descriptions.
1local M = {}
2
3local defaults = {
4 out_waiting = "...",
5 out_no_jj_repo = "~no_jj_repo~",
6 out_no_desc = "~no_desc~",
7 max_desc_length = 47,
8}
9
10M.config = vim.deepcopy(defaults)
11
12M.update_jj_status = function(bufnr)
13 vim.api.nvim_buf_set_var(bufnr, "jj_desc", M.config.out_waiting)
14
15 local current_file = vim.api.nvim_buf_get_name(bufnr)
16 if current_file == "" or current_file == nil then
17 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
18 return
19 end
20
21 if not vim.fn.filereadable(current_file) then
22 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
23 return
24 end
25
26 local jj_root = vim.fs.find(
27 ".jj", {
28 upward = true,
29 stop = vim.loop.os_homedir(),
30 path = current_file,
31 type = "directory",
32 }
33 )[1]
34
35 if not jj_root then
36 vim.api.nvim_buf_set_var(bufnr, "jj_desc", M.config.out_no_jj_repo)
37 return
38 end
39
40 local repo_root = vim.fs.dirname(jj_root)
41 local _cmd = string.format("jj log -R %s -r @ -T description --no-graph", repo_root)
42
43 local cmd = { "jj", "log", "-R", repo_root, "-r", "@", "-T", "description", "--no-graph", }
44
45 vim.system(cmd, { text = true, cwd = vim.fs.dirname(repo_root) }, function(obj)
46 if obj.code ~= 0 then
47 vim.api.nvim_buf_del_var(bufnr, "jj_desc")
48 return
49 end
50
51 local output = vim.trim(obj.stdout or "")
52 output = output:gsub("\n", ""):gsub("^%s*", ""):gsub("%s*$", "")
53
54 if output == "" then output = M.config.out_no_desc end
55 if #output > (M.config.max_desc_length + 3) then
56 output = string.sub(output, 1, M.config.max_desc_length) .. "..."
57 end
58
59 vim.schedule(function()
60 if vim.api.nvim_buf_is_valid(bufnr) then
61 vim.api.nvim_buf_set_var(bufnr, "jj_desc", output)
62
63 if bufnr == vim.api.nvim_get_current_buf() then
64 vim.cmd.redrawstatus()
65 end
66 end
67 end)
68 end)
69end
70
71M.setup = function(opts)
72 M.config = vim.tbl_deep_extend("force", M.config, opts or {})
73
74 local group = vim.api.nvim_create_augroup("JJLogPlugin", { clear = true })
75
76 vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "BufWritePost", "FocusGained" }, {
77 group = group,
78 pattern = "*",
79 callback = function(args)
80 M.update_jj_status(args.buf)
81 end,
82 })
83end
84
85return M