refactor(git): rework log_view, drop URI scheme
This commit is contained in:
+17
-58
@@ -3,8 +3,6 @@ local util = require("git.core.util")
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.URI_PREFIX = "gitlog://"
|
|
||||||
|
|
||||||
local LOG_FORMAT = "%h %ad {%an}%d %s"
|
local LOG_FORMAT = "%h %ad {%an}%d %s"
|
||||||
|
|
||||||
local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
|
local cr = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
|
||||||
@@ -56,50 +54,7 @@ local function populate(buf, r)
|
|||||||
if not stdout then
|
if not stdout then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local new_lines = util.split_lines(stdout)
|
util.set_buf_lines(buf, 0, -1, util.split_lines(stdout))
|
||||||
local old_str = table.concat(
|
|
||||||
vim.api.nvim_buf_get_lines(buf, 0, -1, false),
|
|
||||||
"\n"
|
|
||||||
) .. "\n"
|
|
||||||
local new_str = table.concat(new_lines, "\n") .. "\n"
|
|
||||||
local hunks = vim.text.diff(old_str, new_str, {
|
|
||||||
result_type = "indices",
|
|
||||||
algorithm = "histogram",
|
|
||||||
})
|
|
||||||
---@cast hunks [integer, integer, integer, integer][]
|
|
||||||
if #hunks == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
for i = #hunks, 1, -1 do
|
|
||||||
local sa, ca, sb, cb = unpack(hunks[i])
|
|
||||||
local start = ca == 0 and sa or sa - 1
|
|
||||||
util.set_buf_lines(
|
|
||||||
buf,
|
|
||||||
start,
|
|
||||||
start + ca,
|
|
||||||
vim.list_slice(new_lines, sb, sb + cb - 1)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param buf integer
|
|
||||||
function M.read_uri(buf)
|
|
||||||
local name = vim.api.nvim_buf_get_name(buf)
|
|
||||||
local worktree = name:sub(#M.URI_PREFIX + 1)
|
|
||||||
if worktree == "" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local r = repo.resolve(worktree)
|
|
||||||
if not r then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
repo.bind(buf, r)
|
|
||||||
|
|
||||||
util.setup_scratch(buf, { bufhidden = "hide" })
|
|
||||||
vim.bo[buf].filetype = "gitlog"
|
|
||||||
|
|
||||||
attach_dispatch(buf)
|
|
||||||
populate(buf, r)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class ow.Git.Log.OpenOpts
|
---@class ow.Git.Log.OpenOpts
|
||||||
@@ -120,19 +75,25 @@ function M.open(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
max_counts[r.worktree] = opts.max_count
|
max_counts[r.worktree] = opts.max_count
|
||||||
local buf = vim.fn.bufadd(M.URI_PREFIX .. r.worktree)
|
local buf = vim.fn.bufadd(r.worktree .. "/GitLog")
|
||||||
local was_loaded = vim.api.nvim_buf_is_loaded(buf)
|
|
||||||
|
|
||||||
local win = vim.fn.bufwinid(buf)
|
local visible = vim.fn.bufwinid(buf)
|
||||||
if win == -1 then
|
if visible ~= -1 then
|
||||||
util.place_buf(buf, nil)
|
vim.api.nvim_set_current_win(visible)
|
||||||
else
|
|
||||||
vim.api.nvim_set_current_win(win)
|
|
||||||
end
|
|
||||||
|
|
||||||
if was_loaded then
|
|
||||||
populate(buf, r)
|
populate(buf, r)
|
||||||
|
vim.api.nvim_win_set_cursor(visible, { 1, 0 })
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
vim.fn.bufload(buf)
|
||||||
|
repo.bind(buf, r)
|
||||||
|
util.setup_scratch(buf, { bufhidden = "hide" })
|
||||||
|
vim.bo[buf].filetype = "gitlog"
|
||||||
|
attach_dispatch(buf)
|
||||||
|
|
||||||
|
local win = util.place_buf(buf, nil)
|
||||||
|
vim.api.nvim_win_set_cursor(win, { 1, 0 })
|
||||||
|
populate(buf, r)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param cmd_opts table
|
---@param cmd_opts table
|
||||||
@@ -170,6 +131,4 @@ function M.complete_glog(arg_lead)
|
|||||||
return matches
|
return matches
|
||||||
end
|
end
|
||||||
|
|
||||||
repo.on_uri_change(M.URI_PREFIX, populate)
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ local WINDOW_WIDTH = 50
|
|||||||
---@param r ow.Git.Repo
|
---@param r ow.Git.Repo
|
||||||
---@return string
|
---@return string
|
||||||
local function buf_name_for(r)
|
local function buf_name_for(r)
|
||||||
return r.worktree .. "/Git Status"
|
return r.worktree .. "/GitStatus"
|
||||||
end
|
end
|
||||||
|
|
||||||
---@alias ow.Git.StatusView.Placement "sidebar"|"split"|"current"
|
---@alias ow.Git.StatusView.Placement "sidebar"|"split"|"current"
|
||||||
|
|||||||
@@ -96,13 +96,6 @@ vim.api.nvim_create_autocmd("BufReadCmd", {
|
|||||||
require("git.object").read_uri(args.buf)
|
require("git.object").read_uri(args.buf)
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
vim.api.nvim_create_autocmd("BufReadCmd", {
|
|
||||||
pattern = "gitlog://*",
|
|
||||||
group = group,
|
|
||||||
callback = function(args)
|
|
||||||
require("git.log_view").read_uri(args.buf)
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
vim.api.nvim_create_user_command("G", function(opts)
|
vim.api.nvim_create_user_command("G", function(opts)
|
||||||
local cmd = require("git.cmd")
|
local cmd = require("git.cmd")
|
||||||
cmd.run(cmd.parse_args(opts.args), { bang = opts.bang })
|
cmd.run(cmd.parse_args(opts.args), { bang = opts.bang })
|
||||||
|
|||||||
@@ -338,10 +338,10 @@ t.test("<leader>gl log buffer refills after jumping back", function()
|
|||||||
h.git(dir, "commit", "-q", "-m", "second")
|
h.git(dir, "commit", "-q", "-m", "second")
|
||||||
|
|
||||||
require("git.log_view").open({ max_count = 1000 })
|
require("git.log_view").open({ max_count = 1000 })
|
||||||
wait_buf_populated("^gitlog://")
|
wait_buf_populated("/GitLog$")
|
||||||
local log_buf = vim.api.nvim_get_current_buf()
|
local log_buf = vim.api.nvim_get_current_buf()
|
||||||
local log_win = vim.api.nvim_get_current_win()
|
local log_win = vim.api.nvim_get_current_win()
|
||||||
t.truthy(vim.api.nvim_buf_get_name(log_buf):match("^gitlog://"))
|
t.truthy(vim.api.nvim_buf_get_name(log_buf):match("/GitLog$"))
|
||||||
local initial_lines = #vim.api.nvim_buf_get_lines(log_buf, 0, -1, false)
|
local initial_lines = #vim.api.nvim_buf_get_lines(log_buf, 0, -1, false)
|
||||||
t.truthy(initial_lines >= 2)
|
t.truthy(initial_lines >= 2)
|
||||||
|
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ t.test(
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
|
||||||
t.test("sidebar buffer is named <worktree>/Git Status", function()
|
t.test("sidebar buffer is named <worktree>/GitStatus", function()
|
||||||
local repo = h.make_repo({ ["foo.txt"] = "x\n" })
|
local repo = h.make_repo({ ["foo.txt"] = "x\n" })
|
||||||
vim.cmd("cd " .. repo)
|
vim.cmd("cd " .. repo)
|
||||||
require("git.status_view").open({ placement = "sidebar" })
|
require("git.status_view").open({ placement = "sidebar" })
|
||||||
@@ -325,8 +325,8 @@ t.test("sidebar buffer is named <worktree>/Git Status", function()
|
|||||||
assert(buf, "sidebar buffer should exist")
|
assert(buf, "sidebar buffer should exist")
|
||||||
t.eq(
|
t.eq(
|
||||||
vim.api.nvim_buf_get_name(buf),
|
vim.api.nvim_buf_get_name(buf),
|
||||||
r.worktree .. "/Git Status",
|
r.worktree .. "/GitStatus",
|
||||||
"buffer name should be <worktree>/Git Status"
|
"buffer name should be <worktree>/GitStatus"
|
||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user