fix(git): set filetype on URI buffers from the inner path

This commit is contained in:
2026-04-29 11:27:50 +02:00
parent e5c049bdaa
commit fc3652502a
2 changed files with 19 additions and 12 deletions
-7
View File
@@ -35,13 +35,6 @@ function M.setup()
for name, link in pairs(HIGHLIGHTS) do
vim.api.nvim_set_hl(0, name, { link = link, default = true })
end
vim.filetype.add({
pattern = {
["git://.*:([^:]+)$"] = function(_, bufnr, inner)
return vim.filetype.match({ filename = inner, buf = bufnr })
end,
},
})
local group = vim.api.nvim_create_augroup("ow.git", { clear = true })
vim.api.nvim_create_autocmd("BufReadCmd", {
pattern = "git://*",
+19 -5
View File
@@ -198,14 +198,13 @@ function M.read_uri(buf)
end
local parsed = util.parse_revspec(revspec)
local index_path = parsed.stage == 0 and parsed.path or nil
if index_path then
if parsed.stage == 0 and parsed.path then
vim.bo[buf].buftype = "acwrite"
-- Re-running BufReadCmd (e.g. on `:edit`) would otherwise stack
-- another BufWriteCmd on the same buffer, so each `:w` runs
-- hash-object + update-index N times.
if not vim.b[buf].git_index_writer then
attach_index_writer(buf, worktree, index_path)
attach_index_writer(buf, worktree, parsed.path)
vim.b[buf].git_index_writer = true
end
else
@@ -214,8 +213,23 @@ function M.read_uri(buf)
end
vim.bo[buf].modified = false
-- BufReadCmd suppresses the normal BufReadPost dispatch, so filetype
-- detection and modeline parsing don't run unless we fire it ourselves.
-- Filetype from the inner path. We can't lean on `vim.filetype.add`
-- because Vim normalises `git://` filenames (cwd-prefix + collapses
-- `://` to `:/`) before matching, breaking any pattern keyed on the
-- raw scheme as well as any built-in pattern that doesn't catch a
-- recognisable extension on the mangled form (.Xresources is the
-- canonical example).
if parsed.path then
local ft = vim.filetype.match({ filename = parsed.path, buf = buf })
if ft then
vim.bo[buf].filetype = ft
end
end
-- BufReadCmd suppresses the normal BufReadPost dispatch, so
-- modeline parsing doesn't run unless we fire it ourselves. The
-- modeline can still override the filetype set above; standard Vim
-- precedence.
vim.api.nvim_exec_autocmds("BufReadPost", { buffer = buf })
end