diff --git a/lua/git/diff.lua b/lua/git/diff.lua index a0134e3..dde60da 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -145,9 +145,13 @@ function M.load_file_buf(abs_path) return buf 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 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) local ft = vim.filetype.match({ buf = buf }) if ft then @@ -181,7 +185,7 @@ function M.split(opts) local is_index = opts.ref == "" local other = M.git_show_buf(worktree, opts.ref, rel, is_index) 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 " or "leftabove sbuffer " diff --git a/lua/git/show.lua b/lua/git/show.lua index 23625a4..331c5d5 100644 --- a/lua/git/show.lua +++ b/lua/git/show.lua @@ -94,11 +94,7 @@ local function blob_buf(worktree, blob, path, ref) end ---@cast blob string local buf = diff.git_show_blob(worktree, blob) - pcall(vim.api.nvim_buf_set_name, buf, "git://" .. ref .. "/" .. path) - local ft = vim.filetype.match({ buf = buf }) - if ft then - vim.bo[buf].filetype = ft - end + diff.set_buf_name_and_filetype(buf, "git://" .. ref .. "/" .. path) return buf end diff --git a/lua/git/status_win.lua b/lua/git/status_win.lua index fc20181..641661b 100644 --- a/lua/git/status_win.lua +++ b/lua/git/status_win.lua @@ -620,11 +620,7 @@ local function show_diff(s, entry, focus_left) vim.api.nvim_win_set_buf(right_win, pair.right.buf) for _, side in ipairs({ pair.left, pair.right }) do if side.name then - pcall(vim.api.nvim_buf_set_name, side.buf, side.name) - local ft = vim.filetype.match({ buf = side.buf }) - if ft then - vim.bo[side.buf].filetype = ft - end + diff.set_buf_name_and_filetype(side.buf, side.name) end end set_diff(left_win, true)