fix(vim.filetype): match() fails if g:ft_ignore_pat is not defined #39158

Problem: Calling `vim.filetype.match({ filename = '...', buf = ... })`
  during startup results in an error due to not yet defined
  `g:ft_ignore_pat`.

Solution: Add a guard to check `g:ft_ignore_pat` related properties only
  if the variable is defined. This also allows to simplify other tests
  which did not depend on `g:ft_ignore_pat` but required it explicitly
  set to work.
(cherry picked from commit 6b9b4a1377)
This commit is contained in:
Evgeni Chasnovski
2026-04-18 13:50:28 +03:00
committed by github-actions[bot]
parent 55d3d1bbeb
commit c6578ea28b
2 changed files with 14 additions and 10 deletions
+3 -3
View File
@@ -331,7 +331,7 @@ end
--- @type vim.filetype.mapfn
function M.conf(path, bufnr)
if fn.did_filetype() ~= 0 or path:find(vim.g.ft_ignore_pat) then
if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and path:find(vim.g.ft_ignore_pat)) then
return
end
if path:find('%.conf$') then
@@ -1679,7 +1679,7 @@ end
--- @return string?, fun(b: integer)?
local function sh(path, contents, name)
-- Path may be nil, do not fail in that case
if fn.did_filetype() ~= 0 or (path or ''):find(vim.g.ft_ignore_pat) then
if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and (path or ''):find(vim.g.ft_ignore_pat)) then
-- Filetype was already detected or detection should be skipped
return
end
@@ -1744,7 +1744,7 @@ M.tcsh = sh_with('tcsh')
--- @param name? string
--- @return string?
function M.shell(path, contents, name)
if fn.did_filetype() ~= 0 or matchregex(path, vim.g.ft_ignore_pat) then
if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and matchregex(path, vim.g.ft_ignore_pat)) then
-- Filetype was already detected or detection should be skipped
return
end
+11 -7
View File
@@ -59,7 +59,6 @@ describe('vim.filetype', function()
eq(
'sh',
exec_lua(function()
vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
return vim.filetype.match({ filename = 'main.sh' })
end)
)
@@ -69,12 +68,22 @@ describe('vim.filetype', function()
eq(
'text',
exec_lua(function()
vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
return vim.filetype.match({ filename = 'main.txt' })
end)
)
end)
it('works without defined g:ft_ignore_pat', function()
local match_opts = { filename = 'unknown-ft', buf = api.nvim_create_buf(false, true) }
eq(
nil,
exec_lua(function()
vim.g.ft_ignore_pat = nil
return vim.filetype.match(match_opts)
end)
)
end)
it('works with filenames', function()
eq(
'nim',
@@ -141,8 +150,6 @@ describe('vim.filetype', function()
eq(
'sh',
exec_lua(function()
-- Needs to be set so detect#sh doesn't fail
vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
return (vim.filetype.match({ contents = { '#!/usr/bin/env bash' } }))
end)
)
@@ -221,9 +228,6 @@ describe('vim.filetype', function()
}
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
-- Needs to be set so detect.conf() doesn't fail
vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$'
local ft, _, fallback = vim.filetype.match({ buf = bufnr })
return { ft, fallback }
end)