refactor(git): promote set_buf_name_and_filetype to diff.M

This commit is contained in:
2026-04-27 13:53:12 +02:00
parent 49e0ac726e
commit 5833323814
3 changed files with 8 additions and 12 deletions
+6 -2
View File
@@ -145,9 +145,13 @@ function M.load_file_buf(abs_path)
return buf return buf
end end
---Name a scratch buffer with a `git://...` URI and apply the filetype
---inferred from the inner path segment. The `nvim_buf_set_name` call is
---wrapped in pcall because a buffer with that name may already exist
---(E95).
---@param buf integer ---@param buf integer
---@param name string ---@param name string
local function set_buf_name_and_filetype(buf, name) function M.set_buf_name_and_filetype(buf, name)
pcall(vim.api.nvim_buf_set_name, buf, name) pcall(vim.api.nvim_buf_set_name, buf, name)
local ft = vim.filetype.match({ buf = buf }) local ft = vim.filetype.match({ buf = buf })
if ft then if ft then
@@ -181,7 +185,7 @@ function M.split(opts)
local is_index = opts.ref == "" local is_index = opts.ref == ""
local other = M.git_show_buf(worktree, opts.ref, rel, is_index) local other = M.git_show_buf(worktree, opts.ref, rel, is_index)
local label = is_index and "index" or opts.ref local label = is_index and "index" or opts.ref
set_buf_name_and_filetype(other, "git://" .. label .. "/" .. rel) M.set_buf_name_and_filetype(other, "git://" .. label .. "/" .. rel)
local split_cmd = opts.vertical and "leftabove vertical sbuffer " local split_cmd = opts.vertical and "leftabove vertical sbuffer "
or "leftabove sbuffer " or "leftabove sbuffer "
+1 -5
View File
@@ -94,11 +94,7 @@ local function blob_buf(worktree, blob, path, ref)
end end
---@cast blob string ---@cast blob string
local buf = diff.git_show_blob(worktree, blob) local buf = diff.git_show_blob(worktree, blob)
pcall(vim.api.nvim_buf_set_name, buf, "git://" .. ref .. "/" .. path) diff.set_buf_name_and_filetype(buf, "git://" .. ref .. "/" .. path)
local ft = vim.filetype.match({ buf = buf })
if ft then
vim.bo[buf].filetype = ft
end
return buf return buf
end end
+1 -5
View File
@@ -620,11 +620,7 @@ local function show_diff(s, entry, focus_left)
vim.api.nvim_win_set_buf(right_win, pair.right.buf) vim.api.nvim_win_set_buf(right_win, pair.right.buf)
for _, side in ipairs({ pair.left, pair.right }) do for _, side in ipairs({ pair.left, pair.right }) do
if side.name then if side.name then
pcall(vim.api.nvim_buf_set_name, side.buf, side.name) diff.set_buf_name_and_filetype(side.buf, side.name)
local ft = vim.filetype.match({ buf = side.buf })
if ft then
vim.bo[side.buf].filetype = ft
end
end end
end end
set_diff(left_win, true) set_diff(left_win, true)