aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Anders <greg@gpanders.com>2023-08-24 12:48:21 -0500
committerGregory Anders <greg@gpanders.com>2023-08-24 12:48:21 -0500
commit020d1f626a3fbda84b84b2f57e8a85662a792a1a (patch)
tree4631604fa63da09921175b1cff18f13394bcdf3e
parentdaf7abbc4238dc269e22dd431bc4b1627ef9b6a1 (diff)
downloadrneovim-020d1f626a3fbda84b84b2f57e8a85662a792a1a.tar.gz
rneovim-020d1f626a3fbda84b84b2f57e8a85662a792a1a.tar.bz2
rneovim-020d1f626a3fbda84b84b2f57e8a85662a792a1a.zip
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.
-rw-r--r--runtime/doc/lua.txt3
-rw-r--r--runtime/filetype.lua9
-rw-r--r--runtime/lua/vim/filetype.lua3
3 files changed, 10 insertions, 5 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index 08022c3ed6..b4ca88251e 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -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.
diff --git a/runtime/filetype.lua b/runtime/filetype.lua
index cf5fe39656..4a4f37a1c4 100644
--- a/runtime/filetype.lua
+++ b/runtime/filetype.lua
@@ -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,
})
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.