feat(filetype): remove side effects from vim.filetype.match (#18894)

Many filetypes from filetype.vim set buffer-local variables, meaning
vim.filetype.match cannot be used without side effects. Instead of
setting these buffer-local variables in the filetype detection functions
themselves, have vim.filetype.match return an optional function value
that, when called, sets these variables. This allows vim.filetype.match
to work without side effects.
This commit is contained in:
Gregory Anders
2022-06-09 13:12:36 -06:00
committed by GitHub
parent 28e43881b7
commit 58323b1fe2
5 changed files with 157 additions and 100 deletions

View File

@@ -11,8 +11,14 @@ vim.api.nvim_create_augroup('filetypedetect', { clear = false })
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
group = 'filetypedetect',
callback = function()
vim.filetype.match(vim.fn.expand('<afile>'))
callback = function(args)
local ft, on_detect = vim.filetype.match(args.file, args.buf)
if ft then
vim.api.nvim_buf_set_option(args.buf, 'filetype', ft)
if on_detect then
on_detect(args.buf)
end
end
end,
})