refactor(git): replace status_view URI scheme with path-style name

This commit is contained in:
2026-05-19 16:29:09 +02:00
parent 4461a65b90
commit ffd5584a05
3 changed files with 110 additions and 84 deletions
+17 -76
View File
@@ -7,8 +7,6 @@ local util = require("git.core.util")
local M = {}
M.URI_PREFIX = "gitstatus://"
---@type ow.Git.StatusView.Placement[]
M.PLACEMENTS = { "sidebar", "split", "current" }
@@ -16,14 +14,10 @@ M.PLACEMENTS = { "sidebar", "split", "current" }
local SECTIONS = { "untracked", "unstaged", "staged", "unmerged" }
local WINDOW_WIDTH = 50
---@param name string
---@return integer? bufnr
local function find_buf(name)
for _, b in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_get_name(b) == name then
return b
end
end
---@param r ow.Git.Repo
---@return string
local function buf_name_for(r)
return r.worktree .. "/Git Status"
end
---@alias ow.Git.StatusView.Placement "sidebar"|"split"|"current"
@@ -697,8 +691,9 @@ function M.open(opts)
util.error("not in a git repository")
return
end
local previous_win = vim.api.nvim_get_current_win()
local buf = vim.fn.bufadd(M.URI_PREFIX .. r.worktree)
local buf = vim.fn.bufadd(buf_name_for(r))
local visible = vim.fn.bufwinid(buf)
if visible ~= -1 then
@@ -707,78 +702,24 @@ function M.open(opts)
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.placement = placement
if not state[buf] then
vim.fn.bufload(buf)
repo.bind(buf, r)
util.setup_scratch(buf, {})
vim.bo[buf].filetype = "gitstatus"
setup_buffer(buf, r, placement)
end
vim.bo[buf].bufhidden = placement == "sidebar" and "wipe" or "hide"
local win = place(buf, placement)
state[buf].win = win
state[buf].placement = placement
set_keymaps(buf, placement)
if placement == "sidebar" then
vim.api.nvim_set_current_win(previous_win)
end
if was_loaded then
refresh(buf)
end
r:refresh()
end
---@param buf integer
function M.read_uri(buf)
local name = vim.api.nvim_buf_get_name(buf)
local raw = name:sub(#M.URI_PREFIX + 1)
if raw == "" then
return
end
local worktree = vim.fs.abspath(raw)
local r = repo.resolve(worktree)
if not r then
util.error("not a git worktree: %s", raw)
return
end
if r.worktree ~= worktree then
util.warning("%s is not a worktree root, using %s", raw, r.worktree)
end
local canonical = M.URI_PREFIX .. r.worktree
if name ~= canonical then
local existing = find_buf(canonical)
if existing and existing ~= buf then
local win = vim.api.nvim_get_current_win()
if vim.api.nvim_win_get_buf(win) == buf then
vim.api.nvim_win_set_buf(win, existing)
end
vim.api.nvim_buf_delete(buf, { force = true })
local s = state[existing]
if s then
s.win = win
s.placement = "current"
end
refresh(existing)
r:refresh()
return
end
pcall(vim.api.nvim_buf_set_name, buf, canonical)
end
repo.bind(buf, r)
util.setup_scratch(buf, { bufhidden = "hide" })
vim.bo[buf].filetype = "gitstatus"
---@type integer?
local win = vim.fn.bufwinid(buf)
if win == -1 then
win = nil
end
if not state[buf] then
setup_buffer(buf, r, "current", win)
else
state[buf].win = win
end
refresh(buf)
r:refresh()
end