This commit is contained in:
Z. Jonny Kong 2024-09-16 23:26:29 +01:00 committed by GitHub
commit f4003f6832
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 12 deletions

View File

@ -2791,6 +2791,7 @@ vim.filetype.match({args}) *vim.filetype.match()*
(`function?`) A function that modifies buffer state when called (for
example, to set some filetype specific buffer variables). The function
accepts a buffer number as its only argument.
(`boolean?`) True if a match was found via fallback
==============================================================================

View File

@ -11,21 +11,13 @@ vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
if not vim.api.nvim_buf_is_valid(args.buf) then
return
end
local ft, on_detect = vim.filetype.match({
local ft, on_detect, is_fallback = vim.filetype.match({
-- The unexpanded file name is needed here. #27914
-- Neither args.file nor args.match are guaranteed to be unexpanded.
filename = vim.fn.bufname(args.buf),
buf = args.buf,
})
if not ft then
-- Generic configuration file used as fallback
ft = require('vim.filetype.detect').conf(args.file, args.buf)
if ft then
vim._with({ buf = args.buf }, function()
vim.api.nvim_cmd({ cmd = 'setf', args = { 'FALLBACK', ft } }, {})
end)
end
else
if ft then
-- on_detect is called before setting the filetype so that it can set any buffer local
-- variables that may be used the filetype's ftplugin
if on_detect then
@ -33,7 +25,10 @@ vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
end
vim._with({ buf = args.buf }, function()
vim.api.nvim_cmd({ cmd = 'setf', args = { ft } }, {})
vim.api.nvim_cmd({
cmd = 'setf',
args = (is_fallback and { 'FALLBACK', ft } or { ft }),
}, {})
end)
end
end,

View File

@ -2706,6 +2706,7 @@ end
---@return function|nil # A function that modifies buffer state when called (for example, to set some
--- filetype specific buffer variables). The function accepts a buffer number as
--- its only argument.
---@return boolean|nil # True if a match was found via fallback
function M.match(args)
vim.validate({
arg = { args, 't' },
@ -2796,11 +2797,19 @@ function M.match(args)
return dispatch(extension[ext], name, bufnr)
end
)
if ok then
if ok and ft then
return ft, on_detect
end
end
end
-- Generic configuration file used as fallback
if name and bufnr then
ft = detect.conf(name, bufnr)
if ft then
return ft, nil, true
end
end
end
--- Get the default option value for a {filetype}.