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 diff_right_win integer?
|
||||||
---@field unsubscribe fun()?
|
---@field unsubscribe fun()?
|
||||||
---@field last_shown_key string?
|
---@field last_shown_key string?
|
||||||
---@field last_render_key string?
|
|
||||||
|
|
||||||
---@type table<integer, ow.Git.StatusView.State>
|
---@type table<integer, ow.Git.StatusView.State>
|
||||||
local state = {}
|
local state = {}
|
||||||
@@ -139,37 +138,10 @@ local function render(bufnr, status)
|
|||||||
state[bufnr].lines = meta
|
state[bufnr].lines = meta
|
||||||
end
|
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
|
---@param bufnr integer
|
||||||
local function refresh(bufnr)
|
local function refresh(bufnr)
|
||||||
local s = state[bufnr]
|
local s = state[bufnr]
|
||||||
if not s then
|
if not s or not vim.api.nvim_buf_is_valid(bufnr) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -183,17 +155,8 @@ local function refresh(bufnr)
|
|||||||
end
|
end
|
||||||
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
|
s.last_shown_key = nil
|
||||||
render(bufnr, status)
|
render(bufnr, s.repo.status)
|
||||||
if not saved_path then
|
if not saved_path then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -598,6 +561,7 @@ local function action_help(placement)
|
|||||||
lines,
|
lines,
|
||||||
" X discard worktree changes (untracked: delete file)"
|
" X discard worktree changes (untracked: delete file)"
|
||||||
)
|
)
|
||||||
|
table.insert(lines, " R refresh")
|
||||||
table.insert(lines, " g? show this help")
|
table.insert(lines, " g? show this help")
|
||||||
print(table.concat(lines, "\n"))
|
print(table.concat(lines, "\n"))
|
||||||
end
|
end
|
||||||
@@ -653,6 +617,9 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
|||||||
k("s", action_stage, "Stage file")
|
k("s", action_stage, "Stage file")
|
||||||
k("u", action_unstage, "Unstage file")
|
k("u", action_unstage, "Unstage file")
|
||||||
k("X", action_discard, "Discard worktree changes")
|
k("X", action_discard, "Discard worktree changes")
|
||||||
|
k("R", function()
|
||||||
|
r:refresh()
|
||||||
|
end, "Refresh")
|
||||||
k("g?", function()
|
k("g?", function()
|
||||||
action_help(state[bufnr].placement)
|
action_help(state[bufnr].placement)
|
||||||
end, "Help")
|
end, "Help")
|
||||||
@@ -660,6 +627,13 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
|||||||
state[bufnr].unsubscribe = r:on("refresh", function()
|
state[bufnr].unsubscribe = r:on("refresh", function()
|
||||||
refresh(bufnr)
|
refresh(bufnr)
|
||||||
end)
|
end)
|
||||||
|
vim.api.nvim_create_autocmd("BufEnter", {
|
||||||
|
buffer = bufnr,
|
||||||
|
group = group,
|
||||||
|
callback = function()
|
||||||
|
r:refresh()
|
||||||
|
end,
|
||||||
|
})
|
||||||
vim.api.nvim_create_autocmd({ "BufWipeout", "BufDelete" }, {
|
vim.api.nvim_create_autocmd({ "BufWipeout", "BufDelete" }, {
|
||||||
buffer = bufnr,
|
buffer = bufnr,
|
||||||
group = group,
|
group = group,
|
||||||
@@ -703,6 +677,7 @@ function M.open(opts)
|
|||||||
local visible = vim.fn.bufwinid(buf)
|
local visible = vim.fn.bufwinid(buf)
|
||||||
if visible ~= -1 then
|
if visible ~= -1 then
|
||||||
vim.api.nvim_set_current_win(visible)
|
vim.api.nvim_set_current_win(visible)
|
||||||
|
r:refresh()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -725,6 +700,7 @@ function M.open(opts)
|
|||||||
if was_loaded then
|
if was_loaded then
|
||||||
refresh(buf)
|
refresh(buf)
|
||||||
end
|
end
|
||||||
|
r:refresh()
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param buf integer
|
---@param buf integer
|
||||||
@@ -758,6 +734,7 @@ function M.read_uri(buf)
|
|||||||
s.placement = "current"
|
s.placement = "current"
|
||||||
end
|
end
|
||||||
refresh(existing)
|
refresh(existing)
|
||||||
|
r:refresh()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
pcall(vim.api.nvim_buf_set_name, buf, canonical)
|
pcall(vim.api.nvim_buf_set_name, buf, canonical)
|
||||||
@@ -781,6 +758,7 @@ function M.read_uri(buf)
|
|||||||
state[buf].win = win
|
state[buf].win = win
|
||||||
end
|
end
|
||||||
refresh(buf)
|
refresh(buf)
|
||||||
|
r:refresh()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.toggle()
|
function M.toggle()
|
||||||
|
|||||||
Reference in New Issue
Block a user