refactor(git): simplify status view refresh and add R/BufEnter
This commit is contained in:
+17
-39
@@ -37,7 +37,6 @@ end
|
||||
---@field diff_right_win integer?
|
||||
---@field unsubscribe fun()?
|
||||
---@field last_shown_key string?
|
||||
---@field last_render_key string?
|
||||
|
||||
---@type table<integer, ow.Git.StatusView.State>
|
||||
local state = {}
|
||||
@@ -139,37 +138,10 @@ local function render(bufnr, status)
|
||||
state[bufnr].lines = meta
|
||||
end
|
||||
|
||||
---@param status ow.Git.Status
|
||||
---@return string
|
||||
local function fingerprint(status)
|
||||
local branch = status.branch
|
||||
local parts = {
|
||||
branch.head or "",
|
||||
branch.upstream or "",
|
||||
tostring(branch.ahead),
|
||||
tostring(branch.behind),
|
||||
}
|
||||
for _, kind in ipairs(KINDS) do
|
||||
for _, e in ipairs(status:by_kind(kind)) do
|
||||
table.insert(
|
||||
parts,
|
||||
e.kind
|
||||
.. ":"
|
||||
.. e.path
|
||||
.. ":"
|
||||
.. (e.orig or "")
|
||||
.. ":"
|
||||
.. e.char
|
||||
)
|
||||
end
|
||||
end
|
||||
return table.concat(parts, "\0")
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
local function refresh(bufnr)
|
||||
local s = state[bufnr]
|
||||
if not s then
|
||||
if not s or not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
end
|
||||
|
||||
@@ -183,17 +155,8 @@ local function refresh(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
if not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
end
|
||||
local status = s.repo.status
|
||||
local fp = fingerprint(status)
|
||||
if fp == s.last_render_key then
|
||||
return
|
||||
end
|
||||
s.last_render_key = fp
|
||||
s.last_shown_key = nil
|
||||
render(bufnr, status)
|
||||
render(bufnr, s.repo.status)
|
||||
if not saved_path then
|
||||
return
|
||||
end
|
||||
@@ -598,6 +561,7 @@ local function action_help(placement)
|
||||
lines,
|
||||
" X discard worktree changes (untracked: delete file)"
|
||||
)
|
||||
table.insert(lines, " R refresh")
|
||||
table.insert(lines, " g? show this help")
|
||||
print(table.concat(lines, "\n"))
|
||||
end
|
||||
@@ -653,6 +617,9 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
||||
k("s", action_stage, "Stage file")
|
||||
k("u", action_unstage, "Unstage file")
|
||||
k("X", action_discard, "Discard worktree changes")
|
||||
k("R", function()
|
||||
r:refresh()
|
||||
end, "Refresh")
|
||||
k("g?", function()
|
||||
action_help(state[bufnr].placement)
|
||||
end, "Help")
|
||||
@@ -660,6 +627,13 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
||||
state[bufnr].unsubscribe = r:on("refresh", function()
|
||||
refresh(bufnr)
|
||||
end)
|
||||
vim.api.nvim_create_autocmd("BufEnter", {
|
||||
buffer = bufnr,
|
||||
group = group,
|
||||
callback = function()
|
||||
r:refresh()
|
||||
end,
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ "BufWipeout", "BufDelete" }, {
|
||||
buffer = bufnr,
|
||||
group = group,
|
||||
@@ -703,6 +677,7 @@ function M.open(opts)
|
||||
local visible = vim.fn.bufwinid(buf)
|
||||
if visible ~= -1 then
|
||||
vim.api.nvim_set_current_win(visible)
|
||||
r:refresh()
|
||||
return
|
||||
end
|
||||
|
||||
@@ -725,6 +700,7 @@ function M.open(opts)
|
||||
if was_loaded then
|
||||
refresh(buf)
|
||||
end
|
||||
r:refresh()
|
||||
end
|
||||
|
||||
---@param buf integer
|
||||
@@ -758,6 +734,7 @@ function M.read_uri(buf)
|
||||
s.placement = "current"
|
||||
end
|
||||
refresh(existing)
|
||||
r:refresh()
|
||||
return
|
||||
end
|
||||
pcall(vim.api.nvim_buf_set_name, buf, canonical)
|
||||
@@ -781,6 +758,7 @@ function M.read_uri(buf)
|
||||
state[buf].win = win
|
||||
end
|
||||
refresh(buf)
|
||||
r:refresh()
|
||||
end
|
||||
|
||||
function M.toggle()
|
||||
|
||||
Reference in New Issue
Block a user