From 020d1f626a3fbda84b84b2f57e8a85662a792a1a Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 24 Aug 2023 12:48:21 -0500 Subject: fix(filetype): call on_detect before setting buffer filetype The on_detect functions returned by filetype.lua set buffer local variables which are often used by filetype plugins. For example, the on_detect function for shell buffers sets variables such as b:is_bash or b:is_sh, which are used by the sh ftplugin. When called after setting the buffer's filetype, these variables cannot be used by the ftplugin (because they are not yet defined). Instead, call on_detect before setting the buffer filetype so that any buffer variables set by on_detect can be used in the ftplugin. --- runtime/lua/vim/filetype.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index c310eb3e42..a05c1075eb 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -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. -- cgit From af38b46a2574f59358bc3bf54dd7c34b5c0f396d Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 24 Aug 2023 12:56:24 -0500 Subject: fix(filetype): return on_detect function when matching by file contents --- runtime/lua/vim/filetype.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index a05c1075eb..193c9b0199 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -2377,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 -- cgit