Merge pull request #18367 from neovim/backport-18353-to-release-0.7

This commit is contained in:
James McCoy 2022-05-02 11:23:25 -04:00 committed by GitHub
commit f3587babfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -1463,8 +1463,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.
@ -1533,7 +1543,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

View File

@ -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',