refactor(git): pull buffer-placement tail into util.place_buf

This commit is contained in:
2026-04-29 10:06:08 +02:00
parent 9d938b228b
commit 7bf15a246a
2 changed files with 21 additions and 25 deletions
+2 -17
View File
@@ -286,15 +286,7 @@ local function open_uri(worktree, uri, sha, opts, default_ft)
if default_ft and vim.bo[buf].filetype == "" then if default_ft and vim.bo[buf].filetype == "" then
vim.bo[buf].filetype = default_ft vim.bo[buf].filetype = default_ft
end end
local split = opts and opts.split util.place_buf(buf, opts and opts.split)
if split == false then
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(buf)
return
end
vim.api.nvim_open_win(buf, true, {
split = split or (vim.o.splitbelow and "below" or "above"),
})
end end
---Open a commit's body via `git cat-file -p` for the header (raw object ---Open a commit's body via `git cat-file -p` for the header (raw object
@@ -313,14 +305,7 @@ function M.open_commit(worktree, ref, opts)
local name = util.uri(sha) local name = util.uri(sha)
local existing = vim.fn.bufnr(name) local existing = vim.fn.bufnr(name)
if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then
if split == false then util.place_buf(existing, split)
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(existing)
else
vim.api.nvim_open_win(existing, true, {
split = split or (vim.o.splitbelow and "below" or "above"),
})
end
return return
end end
+19 -8
View File
@@ -41,6 +41,24 @@ function M.parse_revspec(revspec)
return { stage = nil, path = path } return { stage = nil, path = path }
end end
---Place a buffer in the current window or a new split per `split`.
---`false` replaces the current buffer (drops a `'` mark first so `''`
---jumps back); a direction string opens a leftabove split; nil falls
---back to a `splitbelow`-aware horizontal split.
---@param buf integer
---@param split (false|"above"|"below"|"left"|"right")?
---@return integer win
function M.place_buf(buf, split)
if split == false then
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(buf)
return vim.api.nvim_get_current_win()
end
return vim.api.nvim_open_win(buf, true, {
split = split or (vim.o.splitbelow and "below" or "above"),
})
end
---@class ow.Git.NewScratchOpts ---@class ow.Git.NewScratchOpts
---@field name string? ---@field name string?
---@field bufhidden ("hide"|"wipe")? defaults to "hide" ---@field bufhidden ("hide"|"wipe")? defaults to "hide"
@@ -63,14 +81,7 @@ function M.new_scratch(opts)
if opts.name then if opts.name then
pcall(vim.api.nvim_buf_set_name, buf, opts.name) pcall(vim.api.nvim_buf_set_name, buf, opts.name)
end end
if opts.split == false then return buf, M.place_buf(buf, opts.split)
vim.cmd.normal({ "m'", bang = true })
vim.api.nvim_set_current_buf(buf)
return buf, vim.api.nvim_get_current_win()
end
local split = opts.split or (vim.o.splitbelow and "below" or "above")
local win = vim.api.nvim_open_win(buf, true, { split = split })
return buf, win
end end
---@param fmt string ---@param fmt string