refactor(git): scratch buffers wipe by default, consolidate factories in util
This commit is contained in:
+41
-12
@@ -44,6 +44,44 @@ function M.parse_revspec(revspec)
|
||||
return { stage = nil, path = path }
|
||||
end
|
||||
|
||||
---@class ow.Git.ScratchOpts
|
||||
---@field name string?
|
||||
---@field bufhidden ("hide"|"wipe")? defaults to "wipe"
|
||||
|
||||
---Configure a fresh buffer as a read-only scratch and optionally name
|
||||
---it. Shared by `empty_buf` and `new_scratch`; the public functions
|
||||
---differ in whether they dedup-by-name or place the buffer in a window.
|
||||
---@param buf integer
|
||||
---@param opts ow.Git.ScratchOpts
|
||||
local function setup_scratch(buf, opts)
|
||||
vim.bo[buf].buftype = "nofile"
|
||||
vim.bo[buf].bufhidden = opts.bufhidden or "wipe"
|
||||
vim.bo[buf].swapfile = false
|
||||
vim.bo[buf].modifiable = false
|
||||
vim.bo[buf].modified = false
|
||||
if opts.name then
|
||||
pcall(vim.api.nvim_buf_set_name, buf, opts.name)
|
||||
end
|
||||
end
|
||||
|
||||
---Build a read-only scratch buffer, optionally naming it. When `opts.name`
|
||||
---is set and a loaded buffer with that name already exists, returns it
|
||||
---instead of creating a duplicate.
|
||||
---@param opts ow.Git.ScratchOpts?
|
||||
---@return integer
|
||||
function M.empty_buf(opts)
|
||||
opts = opts or {}
|
||||
if opts.name then
|
||||
local existing = vim.fn.bufnr(opts.name)
|
||||
if existing ~= -1 and vim.api.nvim_buf_is_loaded(existing) then
|
||||
return existing
|
||||
end
|
||||
end
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
setup_scratch(buf, opts)
|
||||
return buf
|
||||
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
|
||||
@@ -62,12 +100,10 @@ function M.place_buf(buf, split)
|
||||
})
|
||||
end
|
||||
|
||||
---@class ow.Git.NewScratchOpts
|
||||
---@field name string?
|
||||
---@field bufhidden ("hide"|"wipe")? defaults to "hide"
|
||||
---@class ow.Git.NewScratchOpts : ow.Git.ScratchOpts
|
||||
---@field split (false|"above"|"below"|"left"|"right")? defaults to splitbelow-aware horizontal. `false` places the buffer in the current window (drops a `'` mark first so the user can jump back).
|
||||
|
||||
---Create a fresh non-modifiable scratch buffer and place it. Default split
|
||||
---Create a fresh read-only scratch buffer and place it. Default split
|
||||
---direction is horizontal, honouring `splitbelow`. Caller flips
|
||||
---`modifiable`, fills the buffer, and sets `filetype` once content lands.
|
||||
---@param opts ow.Git.NewScratchOpts?
|
||||
@@ -76,14 +112,7 @@ end
|
||||
function M.new_scratch(opts)
|
||||
opts = opts or {}
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
vim.bo[buf].buftype = "nofile"
|
||||
vim.bo[buf].bufhidden = opts.bufhidden or "hide"
|
||||
vim.bo[buf].swapfile = false
|
||||
vim.bo[buf].modifiable = false
|
||||
vim.bo[buf].modified = false
|
||||
if opts.name then
|
||||
pcall(vim.api.nvim_buf_set_name, buf, opts.name)
|
||||
end
|
||||
setup_scratch(buf, opts)
|
||||
return buf, M.place_buf(buf, opts.split)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user