Feat: Add file module with Jread, Jedit, Jsplit , Jvsplit and Jtabedit similar to fugitive's (#105)

Co-authored-by: NicolasGB <ngou0210@gmail.com>
This commit is contained in:
Björn Steinbrink
2026-04-26 09:50:26 -07:00
committed by GitHub
co-authored by NicolasGB
parent 2dbe2c73c5
commit 53a3196210
8 changed files with 527 additions and 68 deletions
+40 -24
View File
@@ -1,46 +1,60 @@
local utils = require("jj.utils")
local buffer = require("jj.core.buffer")
local runner = require("jj.core.runner")
local diff = require("jj.diff")
local file = require("jj.file")
--- Get the content of a file at a specific revision
--- Open a writable buffer for a specific revision of a file.
--- @param rev string The revision
--- @param path string The file path
--- @return table lines The file content
local function get_file_content(rev, path)
local cmd = string.format("jj file show -r %s %s", vim.fn.shellescape(rev), vim.fn.shellescape(path))
local content = vim.fn.system(cmd)
local success = vim.v.shell_error == 0
if success then
return vim.split(content, "\n", { trimempty = true })
else
return {}
end
end
--- Open a read-only buffer for a specific revision of a file
--- @param rev string The revision
--- @param path string The file path
--- @param path string The file path (absolute or repo-relative)
local function open_revision(rev, path)
local lines = get_file_content(rev, path)
local raw_ids, ok = runner.execute_command(
string.format([[jj log --no-graph -r %s -T 'change_id ++ "\n"' --quiet]], vim.fn.shellescape(rev)),
"jj: failed to resolve revision"
)
if not ok then return end
local ids = vim.split(vim.trim(raw_ids), "\n", { trimempty = true })
if #ids ~= 1 then
utils.notify(string.format("Revision '%s' is ambiguous", rev), vim.log.levels.ERROR)
return
end
local change_id = ids[1]
local rel_path, err = utils.normalize_repo_path(path)
if not rel_path then
utils.notify(err or "Could not resolve path", vim.log.levels.ERROR)
return
end
local lines, had_eol = file.get_file_content(change_id, rel_path)
local buf = vim.api.nvim_create_buf(false, true)
local buf_name = string.format("jj://%s/%s", rev, path)
local buf_name = string.format("jj://%s/%s", change_id, rel_path)
vim.api.nvim_buf_set_name(buf, buf_name)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].eol = had_eol
local ft = vim.filetype.match({ filename = path })
local ft = vim.filetype.match({ filename = rel_path })
if ft then
vim.bo[buf].filetype = ft
end
vim.bo[buf].buftype = "nofile"
vim.bo[buf].buftype = "acwrite"
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].readonly = true
vim.bo[buf].readonly = false
vim.bo[buf].swapfile = false
vim.bo[buf].modifiable = true
vim.api.nvim_create_autocmd("BufWriteCmd", {
buffer = buf,
callback = function()
file.write_revision_file(buf, change_id, rel_path)
end,
})
vim.bo[buf].modified = false
vim.api.nvim_win_set_buf(0, buf)
end
@@ -61,8 +75,10 @@ diff.register_backend("native", {
local prev_buf = vim.api.nvim_get_current_buf()
local prev_cur_pos = buffer.get_cursor(prev_buf) or { 1, 0 }
local rev = opts.rev or "@-"
local path = opts.path or vim.api.nvim_buf_get_name(0)
local buf_name = vim.api.nvim_buf_get_name(0)
local change_id, jj_path = utils.parse_jj_uri(buf_name)
local rev = opts.rev or (change_id and (change_id .. "-")) or "@-"
local path = opts.path or jj_path or buf_name
local layout = opts.layout or "vertical"
local split_fun = layout == "horizontal" and vim.cmd.split or vim.cmd.vsplit