diff options
-rw-r--r-- | runtime/lua/vim/filetype.lua | 11 | ||||
-rw-r--r-- | test/functional/lua/filetype_spec.lua | 17 |
2 files changed, 27 insertions, 1 deletions
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index cd8dabe2ca..30dde915ed 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -2565,6 +2565,15 @@ local function normalize_path(path, as_pattern) return normal end +local abspath = function(x) + return fn.fnamemodify(x, ':p') +end +if fn.has('win32') == 1 then + abspath = function(x) + return (fn.fnamemodify(x, ':p'):gsub('\\', '/')) + end +end + --- @class vim.filetype.add.filetypes --- @inlinedoc --- @field pattern? vim.filetype.mapping @@ -2873,7 +2882,7 @@ function M.match(args) name = normalize_path(name) -- First check for the simple case where the full path exists as a key - local path = fn.fnamemodify(name, ':p') + local path = abspath(name) ft, on_detect = dispatch(filename[path], path, bufnr) if ft then return ft, on_detect diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index b75ff75b05..e52f64eab3 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -173,6 +173,23 @@ describe('vim.filetype', function() eq(buf, api.nvim_get_current_buf()) end) + + it('matches full paths', function() + mkdir('Xfiletype') + command('lcd Xfiletype') + eq( + 'Xfiletype', + exec_lua(function() + vim.filetype.add({ + pattern = { + ['.*/Xfiletype/Xfilename'] = 'Xfiletype', + }, + }) + return vim.filetype.match({ filename = 'Xfilename' }) + end) + ) + rmdir('Xfiletype') + end) end) describe('filetype.lua', function() |