diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index e214dee07a..8b4f9b0867 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -331,7 +331,7 @@ end --- @type vim.filetype.mapfn function M.conf(path, bufnr) - if fn.did_filetype() ~= 0 or path:find(vim.g.ft_ignore_pat) then + if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and path:find(vim.g.ft_ignore_pat)) then return end if path:find('%.conf$') then @@ -1679,7 +1679,7 @@ end --- @return string?, fun(b: integer)? local function sh(path, contents, name) -- Path may be nil, do not fail in that case - if fn.did_filetype() ~= 0 or (path or ''):find(vim.g.ft_ignore_pat) then + if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and (path or ''):find(vim.g.ft_ignore_pat)) then -- Filetype was already detected or detection should be skipped return end @@ -1744,7 +1744,7 @@ M.tcsh = sh_with('tcsh') --- @param name? string --- @return string? function M.shell(path, contents, name) - if fn.did_filetype() ~= 0 or matchregex(path, vim.g.ft_ignore_pat) then + if fn.did_filetype() ~= 0 or (vim.g.ft_ignore_pat and matchregex(path, vim.g.ft_ignore_pat)) then -- Filetype was already detected or detection should be skipped return end diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index 9b3d6f6e19..ab124028a6 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -59,7 +59,6 @@ describe('vim.filetype', function() eq( 'sh', exec_lua(function() - vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' return vim.filetype.match({ filename = 'main.sh' }) end) ) @@ -69,12 +68,22 @@ describe('vim.filetype', function() eq( 'text', exec_lua(function() - vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' return vim.filetype.match({ filename = 'main.txt' }) end) ) end) + it('works without defined g:ft_ignore_pat', function() + local match_opts = { filename = 'unknown-ft', buf = api.nvim_create_buf(false, true) } + eq( + nil, + exec_lua(function() + vim.g.ft_ignore_pat = nil + return vim.filetype.match(match_opts) + end) + ) + end) + it('works with filenames', function() eq( 'nim', @@ -141,8 +150,6 @@ describe('vim.filetype', function() eq( 'sh', exec_lua(function() - -- Needs to be set so detect#sh doesn't fail - vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' return (vim.filetype.match({ contents = { '#!/usr/bin/env bash' } })) end) ) @@ -221,9 +228,6 @@ describe('vim.filetype', function() } vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) - -- Needs to be set so detect.conf() doesn't fail - vim.g.ft_ignore_pat = '\\.\\(Z\\|gz\\|bz2\\|zip\\|tgz\\)$' - local ft, _, fallback = vim.filetype.match({ buf = bufnr }) return { ft, fallback } end)