From fc3652502a82bcb95a3d3b7fae84a5a9f520abfa Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 29 Apr 2026 11:27:50 +0200 Subject: [PATCH] fix(git): set filetype on URI buffers from the inner path --- lua/git/init.lua | 7 ------- lua/git/object.lua | 24 +++++++++++++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lua/git/init.lua b/lua/git/init.lua index 164045a..259f039 100644 --- a/lua/git/init.lua +++ b/lua/git/init.lua @@ -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://*", diff --git a/lua/git/object.lua b/lua/git/object.lua index 68d38ce..8256b23 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -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