fix(filetype): make vim.filetype.match() work with contents only (#22181)

Co-authored-by: Gregory Anders <greg@gpanders.com>
This commit is contained in:
Jonas Strittmatter 2023-02-11 16:08:33 +01:00 committed by GitHub
parent 24ec0aaa7a
commit 9668c166e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 48 deletions

View File

@ -2449,6 +2449,7 @@ local function match_pattern(name, path, tail, pat)
return false
end
end
-- If the pattern contains a / match against the full path, otherwise just the tail
local fullpat = '^' .. pat .. '$'
local matches
@ -2526,11 +2527,10 @@ function M.match(args)
name = api.nvim_buf_get_name(bufnr)
end
local ft, on_detect
if name then
name = normalize_path(name)
end
local ft, on_detect
-- First check for the simple case where the full path exists as a key
local path = vim.fn.fnamemodify(name, ':p')
@ -2587,6 +2587,7 @@ function M.match(args)
end
end
end
end
-- Finally, check file contents
if contents or bufnr then

View File

@ -94,6 +94,14 @@ describe('vim.filetype', function()
return vim.filetype.match({ buf = 0 })
]])
end)
it('works with contents #22180', function()
eq('sh', exec_lua [[
-- 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)
end)
describe('filetype.lua', function()