aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2022-01-05 09:50:54 -0700
committerGitHub <noreply@github.com>2022-01-05 09:50:54 -0700
commitf40ce3456313138f2e0f822da9676ed3bc51f608 (patch)
treefe255279c898902a601e182e97092dc540f80736 /runtime
parent55a59e56eda98f17448a1c318a346ae12d30fc05 (diff)
downloadrneovim-f40ce3456313138f2e0f822da9676ed3bc51f608.tar.gz
rneovim-f40ce3456313138f2e0f822da9676ed3bc51f608.tar.bz2
rneovim-f40ce3456313138f2e0f822da9676ed3bc51f608.zip
fix(filetype): match on <afile> rather than <abuf> (#16943)
Filetype detection runs on BufRead and BufNewFile autocommands, both of which can fire without an underlying buffer, so it's incorrect to use <abuf> to determine the file path. Instead, match on <afile> and assume that the buffer we're operating on is the current buffer. This is the same assumption that filetype.vim makes, so it should be safe.
Diffstat (limited to 'runtime')
-rw-r--r--runtime/filetype.lua2
-rw-r--r--runtime/lua/vim/filetype.lua13
2 files changed, 10 insertions, 5 deletions
diff --git a/runtime/filetype.lua b/runtime/filetype.lua
index 855baf7dfa..fcfc5701f0 100644
--- a/runtime/filetype.lua
+++ b/runtime/filetype.lua
@@ -9,7 +9,7 @@ end
vim.cmd [[
augroup filetypedetect
-au BufRead,BufNewFile * call v:lua.vim.filetype.match(str2nr(expand('<abuf>')))
+au BufRead,BufNewFile * call v:lua.vim.filetype.match(expand('<afile>'))
" These *must* be sourced after the autocommand above is created
runtime! ftdetect/*.vim
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua
index 1496975365..863e460065 100644
--- a/runtime/lua/vim/filetype.lua
+++ b/runtime/lua/vim/filetype.lua
@@ -1449,15 +1449,20 @@ local function dispatch(ft, path, bufnr, ...)
end
---@private
-function M.match(bufnr)
- local path = api.nvim_buf_get_name(bufnr)
+function M.match(name, bufnr)
+ -- When fired from the main filetypedetect autocommand the {bufnr} argument is omitted, so we use
+ -- the current buffer. The {bufnr} argument is provided to allow extensibility in case callers
+ -- wish to perform filetype detection on buffers other than the current one.
+ bufnr = bufnr or api.nvim_get_current_buf()
+
-- First check for the simple case where the full path exists as a key
+ local path = vim.fn.fnamemodify(name, ":p")
if dispatch(filename[path], path, bufnr) then
return
end
-- Next check against just the file name
- local tail = vim.fn.fnamemodify(path, ":t")
+ local tail = vim.fn.fnamemodify(name, ":t")
if dispatch(filename[tail], path, bufnr) then
return
end
@@ -1477,7 +1482,7 @@ function M.match(bufnr)
end
-- Finally, check file extension
- local ext = vim.fn.fnamemodify(path, ":e")
+ local ext = vim.fn.fnamemodify(name, ":e")
if dispatch(extension[ext], path, bufnr) then
return
end