From 7bf15a246a8e5592b510b7e4a4d678166451e4ac Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 29 Apr 2026 10:06:08 +0200 Subject: [PATCH] refactor(git): pull buffer-placement tail into util.place_buf --- lua/git/object.lua | 19 ++----------------- lua/git/util.lua | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lua/git/object.lua b/lua/git/object.lua index 7656a4c..aa48de7 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -286,15 +286,7 @@ local function open_uri(worktree, uri, sha, opts, default_ft) if default_ft and vim.bo[buf].filetype == "" then vim.bo[buf].filetype = default_ft end - local split = 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"), - }) + util.place_buf(buf, opts and opts.split) end ---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 existing = vim.fn.bufnr(name) if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then - if split == false then - 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 + util.place_buf(existing, split) return end diff --git a/lua/git/util.lua b/lua/git/util.lua index 15fa10e..dade6b5 100644 --- a/lua/git/util.lua +++ b/lua/git/util.lua @@ -41,6 +41,24 @@ function M.parse_revspec(revspec) return { stage = nil, path = path } 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 ---@field name string? ---@field bufhidden ("hide"|"wipe")? defaults to "hide" @@ -63,14 +81,7 @@ function M.new_scratch(opts) if opts.name then pcall(vim.api.nvim_buf_set_name, buf, opts.name) end - if opts.split == false then - 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 + return buf, M.place_buf(buf, opts.split) end ---@param fmt string