refactor(git): route status_view.open through BufReadCmd
This commit is contained in:
+40
-60
@@ -602,10 +602,6 @@ local function action_help(placement)
|
||||
print(table.concat(lines, "\n"))
|
||||
end
|
||||
|
||||
---Place an existing or new gitstatus buffer in a window per `placement`.
|
||||
---Sidebar uses a left split with fixed width and winfixwidth; split uses
|
||||
---the default split direction (above/below per `splitbelow`); current
|
||||
---swaps into the current window.
|
||||
---@param bufnr integer
|
||||
---@param placement ow.Git.StatusView.Placement
|
||||
---@return integer win
|
||||
@@ -651,11 +647,6 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
||||
{ buffer = bufnr, silent = true, desc = desc }
|
||||
)
|
||||
end
|
||||
k("<Tab>", function()
|
||||
if state[bufnr].placement == "sidebar" then
|
||||
preview_or_open(false)
|
||||
end
|
||||
end, "Preview diff (sidebar)")
|
||||
k("<CR>", function()
|
||||
preview_or_open(true)
|
||||
end, "Open")
|
||||
@@ -685,56 +676,16 @@ local function setup_buffer(bufnr, r, placement, win, invocation_win)
|
||||
})
|
||||
end
|
||||
|
||||
---Update an existing status view's state and re-place its buffer.
|
||||
---@param bufnr integer
|
||||
---@param placement ow.Git.StatusView.Placement
|
||||
---@param invocation_win integer
|
||||
local function reuse(bufnr, placement, invocation_win)
|
||||
local win = place(bufnr, placement)
|
||||
local s = state[bufnr]
|
||||
if s then
|
||||
s.win = win
|
||||
s.invocation_win = invocation_win
|
||||
s.placement = placement
|
||||
end
|
||||
local function set_keymaps(bufnr, placement)
|
||||
if placement == "sidebar" then
|
||||
vim.api.nvim_set_current_win(invocation_win)
|
||||
vim.keymap.set("n", "<Tab>", function()
|
||||
preview_or_open(false)
|
||||
end, { buffer = bufnr, silent = true, desc = "Preview diff" })
|
||||
else
|
||||
pcall(vim.keymap.del, "n", "<Tab>", { buffer = bufnr })
|
||||
end
|
||||
refresh(bufnr)
|
||||
end
|
||||
|
||||
---@param r ow.Git.Repo
|
||||
---@param placement ow.Git.StatusView.Placement
|
||||
local function open(r, placement)
|
||||
local previous_win = vim.api.nvim_get_current_win()
|
||||
local name = M.URI_PREFIX .. r.worktree
|
||||
local existing_buf = find_buf(name)
|
||||
|
||||
if existing_buf then
|
||||
local visible = vim.fn.bufwinid(existing_buf)
|
||||
if visible ~= -1 then
|
||||
vim.api.nvim_set_current_win(visible)
|
||||
return
|
||||
end
|
||||
reuse(existing_buf, placement, previous_win)
|
||||
return
|
||||
end
|
||||
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.bo[bufnr].buftype = "nofile"
|
||||
vim.bo[bufnr].swapfile = false
|
||||
vim.bo[bufnr].modifiable = false
|
||||
vim.bo[bufnr].bufhidden = placement == "sidebar" and "wipe" or "hide"
|
||||
vim.api.nvim_buf_set_name(bufnr, name)
|
||||
vim.bo[bufnr].filetype = "gitstatus"
|
||||
|
||||
local win = place(bufnr, placement)
|
||||
setup_buffer(bufnr, r, placement, win, previous_win)
|
||||
|
||||
if placement == "sidebar" then
|
||||
vim.api.nvim_set_current_win(previous_win)
|
||||
end
|
||||
refresh(bufnr)
|
||||
end
|
||||
|
||||
---@param opts? { placement: ow.Git.StatusView.Placement? }
|
||||
@@ -746,12 +697,36 @@ function M.open(opts)
|
||||
util.error("not in a git repository")
|
||||
return
|
||||
end
|
||||
open(r, placement)
|
||||
local previous_win = vim.api.nvim_get_current_win()
|
||||
local buf = vim.fn.bufadd(M.URI_PREFIX .. r.worktree)
|
||||
|
||||
local visible = vim.fn.bufwinid(buf)
|
||||
if visible ~= -1 then
|
||||
vim.api.nvim_set_current_win(visible)
|
||||
return
|
||||
end
|
||||
|
||||
local was_loaded = vim.api.nvim_buf_is_loaded(buf)
|
||||
local win = place(buf, placement)
|
||||
|
||||
vim.bo[buf].bufhidden = placement == "sidebar" and "wipe" or "hide"
|
||||
local s = state[buf]
|
||||
if s then
|
||||
s.win = win
|
||||
s.invocation_win = previous_win
|
||||
s.placement = placement
|
||||
end
|
||||
set_keymaps(buf, placement)
|
||||
|
||||
if placement == "sidebar" then
|
||||
vim.api.nvim_set_current_win(previous_win)
|
||||
end
|
||||
|
||||
if was_loaded then
|
||||
refresh(buf)
|
||||
end
|
||||
end
|
||||
|
||||
---Set up an existing buffer (created by `:e gitstatus://<worktree>`) as a
|
||||
---status view. Treated as `current` placement. Relative URIs are made
|
||||
---absolute against the cwd.
|
||||
---@param buf integer
|
||||
function M.read_uri(buf)
|
||||
local name = vim.api.nvim_buf_get_name(buf)
|
||||
@@ -777,7 +752,12 @@ function M.read_uri(buf)
|
||||
vim.api.nvim_win_set_buf(win, existing)
|
||||
end
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
reuse(existing, "current", win)
|
||||
local s = state[existing]
|
||||
if s then
|
||||
s.win = win
|
||||
s.placement = "current"
|
||||
end
|
||||
refresh(existing)
|
||||
return
|
||||
end
|
||||
pcall(vim.api.nvim_buf_set_name, buf, canonical)
|
||||
|
||||
Reference in New Issue
Block a user