mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
fix(filetype): make vim.filetype.match() work with contents only (#22181)
Co-authored-by: Gregory Anders <greg@gpanders.com>
This commit is contained in:
parent
24ec0aaa7a
commit
9668c166e8
@ -2449,6 +2449,7 @@ local function match_pattern(name, path, tail, pat)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- If the pattern contains a / match against the full path, otherwise just the tail
|
-- If the pattern contains a / match against the full path, otherwise just the tail
|
||||||
local fullpat = '^' .. pat .. '$'
|
local fullpat = '^' .. pat .. '$'
|
||||||
local matches
|
local matches
|
||||||
@ -2526,64 +2527,64 @@ function M.match(args)
|
|||||||
name = api.nvim_buf_get_name(bufnr)
|
name = api.nvim_buf_get_name(bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
if name then
|
|
||||||
name = normalize_path(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
local ft, on_detect
|
local ft, on_detect
|
||||||
|
|
||||||
-- First check for the simple case where the full path exists as a key
|
if name then
|
||||||
local path = vim.fn.fnamemodify(name, ':p')
|
name = normalize_path(name)
|
||||||
ft, on_detect = dispatch(filename[path], path, bufnr)
|
|
||||||
if ft then
|
|
||||||
return ft, on_detect
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Next check against just the file name
|
-- First check for the simple case where the full path exists as a key
|
||||||
local tail = vim.fn.fnamemodify(name, ':t')
|
local path = vim.fn.fnamemodify(name, ':p')
|
||||||
ft, on_detect = dispatch(filename[tail], path, bufnr)
|
ft, on_detect = dispatch(filename[path], path, bufnr)
|
||||||
if ft then
|
if ft then
|
||||||
return ft, on_detect
|
return ft, on_detect
|
||||||
end
|
|
||||||
|
|
||||||
-- Next, check the file path against available patterns with non-negative priority
|
|
||||||
local j = 1
|
|
||||||
for i, v in ipairs(pattern_sorted) do
|
|
||||||
local k = next(v)
|
|
||||||
local opts = v[k][2]
|
|
||||||
if opts.priority < 0 then
|
|
||||||
j = i
|
|
||||||
break
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local filetype = v[k][1]
|
-- Next check against just the file name
|
||||||
local matches = match_pattern(name, path, tail, k)
|
local tail = vim.fn.fnamemodify(name, ':t')
|
||||||
if matches then
|
ft, on_detect = dispatch(filename[tail], path, bufnr)
|
||||||
ft, on_detect = dispatch(filetype, path, bufnr, matches)
|
if ft then
|
||||||
if ft then
|
return ft, on_detect
|
||||||
return ft, on_detect
|
end
|
||||||
|
|
||||||
|
-- Next, check the file path against available patterns with non-negative priority
|
||||||
|
local j = 1
|
||||||
|
for i, v in ipairs(pattern_sorted) do
|
||||||
|
local k = next(v)
|
||||||
|
local opts = v[k][2]
|
||||||
|
if opts.priority < 0 then
|
||||||
|
j = i
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
local filetype = v[k][1]
|
||||||
|
local matches = match_pattern(name, path, tail, k)
|
||||||
|
if matches then
|
||||||
|
ft, on_detect = dispatch(filetype, path, bufnr, matches)
|
||||||
|
if ft then
|
||||||
|
return ft, on_detect
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- Next, check file extension
|
-- Next, check file extension
|
||||||
local ext = vim.fn.fnamemodify(name, ':e')
|
local ext = vim.fn.fnamemodify(name, ':e')
|
||||||
ft, on_detect = dispatch(extension[ext], path, bufnr)
|
ft, on_detect = dispatch(extension[ext], path, bufnr)
|
||||||
if ft then
|
if ft then
|
||||||
return ft, on_detect
|
return ft, on_detect
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Next, check patterns with negative priority
|
-- Next, check patterns with negative priority
|
||||||
for i = j, #pattern_sorted do
|
for i = j, #pattern_sorted do
|
||||||
local v = pattern_sorted[i]
|
local v = pattern_sorted[i]
|
||||||
local k = next(v)
|
local k = next(v)
|
||||||
|
|
||||||
local filetype = v[k][1]
|
local filetype = v[k][1]
|
||||||
local matches = match_pattern(name, path, tail, k)
|
local matches = match_pattern(name, path, tail, k)
|
||||||
if matches then
|
if matches then
|
||||||
ft, on_detect = dispatch(filetype, path, bufnr, matches)
|
ft, on_detect = dispatch(filetype, path, bufnr, matches)
|
||||||
if ft then
|
if ft then
|
||||||
return ft, on_detect
|
return ft, on_detect
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -94,6 +94,14 @@ describe('vim.filetype', function()
|
|||||||
return vim.filetype.match({ buf = 0 })
|
return vim.filetype.match({ buf = 0 })
|
||||||
]])
|
]])
|
||||||
end)
|
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)
|
end)
|
||||||
|
|
||||||
describe('filetype.lua', function()
|
describe('filetype.lua', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user