From 58323b1fe2e494cf6a75f108780e21c08996c08e Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 9 Jun 2022 13:12:36 -0600 Subject: 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. --- runtime/filetype.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'runtime/filetype.lua') diff --git a/runtime/filetype.lua b/runtime/filetype.lua index d2510c5494..b002b8971b 100644 --- a/runtime/filetype.lua +++ b/runtime/filetype.lua @@ -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('')) + 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, }) -- cgit