From f3ce06cfa139ca3fb142cf5adf96a2ecc4d8f551 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Sun, 26 Jun 2022 10:41:20 -0600 Subject: refactor(filetype)!: allow vim.filetype.match to use different strategies (#18895) This enables vim.filetype.match to match based on a buffer (most accurate) or simply a filename or file contents, which are less accurate but may still be useful for some scenarios. When matching based on a buffer, the buffer's name and contents are both used to do full filetype matching. When using a filename, if the file exists the file is loaded into a buffer and full filetype detection is performed. If the file does not exist then filetype matching is only performed against the filename itself. Content-based matching does the equivalent of scripts.vim, and matches solely based on file contents without any information from the name of the file itself (e.g. for shebangs). BREAKING CHANGE: use `vim.filetype.match({buf = bufnr})` instead of `vim.filetype.match(name, bufnr)` --- test/functional/lua/filetype_spec.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index d0cef53c4b..be57b2db31 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -3,6 +3,7 @@ local exec_lua = helpers.exec_lua local eq = helpers.eq local clear = helpers.clear local pathroot = helpers.pathroot +local command = helpers.command local root = pathroot() @@ -23,7 +24,7 @@ describe('vim.filetype', function() rs = 'radicalscript', }, }) - return vim.filetype.match('main.rs') + return vim.filetype.match({ filename = 'main.rs' }) ]]) end) @@ -37,7 +38,7 @@ describe('vim.filetype', function() ['main.rs'] = 'somethingelse', }, }) - return vim.filetype.match('main.rs') + return vim.filetype.match({ filename = 'main.rs' }) ]]) end) @@ -48,7 +49,7 @@ describe('vim.filetype', function() ['s_O_m_e_F_i_l_e'] = 'nim', }, }) - return vim.filetype.match('s_O_m_e_F_i_l_e') + return vim.filetype.match({ filename = 's_O_m_e_F_i_l_e' }) ]]) eq('dosini', exec_lua([[ @@ -59,7 +60,7 @@ describe('vim.filetype', function() [root .. '/.config/fun/config'] = 'dosini', }, }) - return vim.filetype.match(root .. '/.config/fun/config') + return vim.filetype.match({ filename = root .. '/.config/fun/config' }) ]], root)) end) @@ -72,11 +73,13 @@ describe('vim.filetype', function() ['~/blog/.*%.txt'] = 'markdown', } }) - return vim.filetype.match('~/blog/why_neovim_is_awesome.txt') + return vim.filetype.match({ filename = '~/blog/why_neovim_is_awesome.txt' }) ]], root)) end) it('works with functions', function() + command('new') + command('file relevant_to_me') eq('foss', exec_lua [[ vim.filetype.add({ pattern = { @@ -87,7 +90,7 @@ describe('vim.filetype', function() end, } }) - return vim.filetype.match('relevant_to_me') + return vim.filetype.match({ buf = 0 }) ]]) end) end) -- cgit