fix(filetype): move fallback logic to vim.filetype.match() #30141

Problem:
Previously, the fallback logic to ".conf" was located outside of
`vim.filetype.match()` and directly within the AutoCmd definition. As a
result, `vim.filetype.match()` would return nil instead of ".conf" for
fallback cases (#30100).

Solution:
Added a boolean return value to `vim.filetype.match()` that indicates
whether the match was the result of fallback. If true, the filetype will
be set using `setf FALLBACK <ft>` instead of `setf <ft>`.
This commit is contained in:
Jonny Kong
2025-10-28 18:45:50 -07:00
committed by GitHub
parent c1b1c8c2e0
commit e2cb675705
4 changed files with 43 additions and 12 deletions

View File

@@ -210,6 +210,25 @@ describe('vim.filetype', function()
)
rmdir('Xfiletype')
end)
it('fallback to conf if any of the first five lines start with a #', function()
eq(
{ 'conf', true },
exec_lua(function()
local bufnr = vim.api.nvim_create_buf(true, false)
local lines = {
'# foo',
}
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)
)
end)
end)
describe('filetype.lua', function()