Merge pull request #24864 from gpanders/filetype-on-detect-order

This commit is contained in:
Gregory Anders 2023-08-24 21:39:57 -05:00 committed by GitHub
commit 0b0d912763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 10 deletions

View File

@ -2600,7 +2600,8 @@ vim.filetype.add({filetypes}) *vim.filetype.add()*
matched pattern, if any) and should return a string that will be used as
the buffer's filetype. Optionally, the function can return a second
function value which, when called, modifies the state of the buffer. This
can be used to, for example, set filetype-specific buffer variables.
can be used to, for example, set filetype-specific buffer variables. This
function will be called by Nvim before setting the buffer's filetype.
Filename patterns can specify an optional priority to resolve cases when a
file path matches multiple patterns. Higher priorities are matched first.

View File

@ -18,12 +18,15 @@ vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'StdinReadPost' }, {
end)
end
else
vim.api.nvim_buf_call(args.buf, function()
vim.api.nvim_cmd({ cmd = 'setf', args = { ft } }, {})
end)
-- 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
on_detect(args.buf)
end
vim.api.nvim_buf_call(args.buf, function()
vim.api.nvim_cmd({ cmd = 'setf', args = { ft } }, {})
end)
end
end,
})

View File

@ -2061,7 +2061,8 @@ end
--- pattern, if any) and should return a string that will be used as the
--- buffer's filetype. Optionally, the function can return a second function
--- value which, when called, modifies the state of the buffer. This can be used
--- to, for example, set filetype-specific buffer variables.
--- to, for example, set filetype-specific buffer variables. This function will
--- be called by Nvim before setting the buffer's filetype.
---
--- Filename patterns can specify an optional priority to resolve cases when a
--- file path matches multiple patterns. Higher priorities are matched first.
@ -2376,11 +2377,16 @@ function M.match(args)
-- If the function tries to use the filename that is nil then it will fail,
-- but this enables checks which do not need a filename to still work.
local ok
ok, ft = pcall(require('vim.filetype.detect').match_contents, contents, name, function(ext)
return dispatch(extension[ext], name, bufnr)
end)
if ok and ft then
return ft
ok, ft, on_detect = pcall(
require('vim.filetype.detect').match_contents,
contents,
name,
function(ext)
return dispatch(extension[ext], name, bufnr)
end
)
if ok then
return ft, on_detect
end
end
end