diff options
author | James McCoy <jamessan@jamessan.com> | 2022-05-02 00:15:16 -0400 |
---|---|---|
committer | James McCoy <jamessan@jamessan.com> | 2022-05-02 08:38:35 -0400 |
commit | 88595fbb212c0b770ed6aa08b09561af608c73e0 (patch) | |
tree | 3d3adb943fee03b7a3970f7fe501559264835791 | |
parent | e5f6f20968e546ebabd68c1800e915a0503f44cb (diff) | |
download | rneovim-88595fbb212c0b770ed6aa08b09561af608c73e0.tar.gz rneovim-88595fbb212c0b770ed6aa08b09561af608c73e0.tar.bz2 rneovim-88595fbb212c0b770ed6aa08b09561af608c73e0.zip |
fix(filetype.lua): escape expansion of ~ when used as a pattern
-rw-r--r-- | runtime/lua/vim/filetype.lua | 16 | ||||
-rw-r--r-- | test/functional/lua/filetype_spec.lua | 1 |
2 files changed, 14 insertions, 3 deletions
diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 2a34fec7f2..279fcf92a4 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -1480,8 +1480,18 @@ end local pattern_sorted = sort_by_priority(pattern) ---@private -local function normalize_path(path) - return (path:gsub("\\", "/"):gsub("^~", vim.env.HOME)) +local function normalize_path(path, as_pattern) + local normal = path:gsub("\\", '/') + if normal:find('^~') then + if as_pattern then + -- Escape Lua's metacharacters when $HOME is used in a pattern. + -- The rest of path should already be properly escaped. + normal = vim.env.HOME:gsub('[-^$()%%.%[%]+?]', '%%%0') .. normal:sub(2) + else + normal = vim.env.HOME .. normal:sub(2) + end + end + return normal end --- Add new filetype mappings. @@ -1550,7 +1560,7 @@ function M.add(filetypes) end for k, v in pairs(filetypes.pattern or {}) do - pattern[normalize_path(k)] = v + pattern[normalize_path(k, true)] = v end if filetypes.pattern then diff --git a/test/functional/lua/filetype_spec.lua b/test/functional/lua/filetype_spec.lua index 729ebc601b..e83dd2eb24 100644 --- a/test/functional/lua/filetype_spec.lua +++ b/test/functional/lua/filetype_spec.lua @@ -70,6 +70,7 @@ describe('vim.filetype', function() it('works with patterns', function() eq('markdown', exec_lua([[ local root = ... + vim.env.HOME = '/a-funky+home%dir' vim.filetype.add({ pattern = { ['~/blog/.*%.txt'] = 'markdown', |