Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(filetype): fix filetype patterns #19218

Merged
merged 2 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions runtime/filetype.lua
Expand Up @@ -12,8 +12,14 @@ vim.api.nvim_create_augroup('filetypedetect', { clear = false })
vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
group = 'filetypedetect',
callback = function(args)
local ft, on_detect = vim.filetype.match({ buf = args.buf })
if ft then
local ft, on_detect = vim.filetype.match({ filename = args.match, buf = args.buf })
if not ft then
-- Generic configuration file used as fallback
ft = require('vim.filetype.detect').conf(args.file, args.buf)
if ft then
vim.api.nvim_cmd({ cmd = 'setf', args = { 'FALLBACK', ft } }, {})
end
else
vim.api.nvim_buf_set_option(args.buf, 'filetype', ft)
if on_detect then
on_detect(args.buf)
Expand Down
138 changes: 112 additions & 26 deletions runtime/lua/vim/filetype.lua
Expand Up @@ -17,7 +17,7 @@ local function starsetf(ft, opts)
end
end,
{
-- Starset matches should always have lowest priority
-- Starset matches should have lowest priority by default
priority = (opts and opts.priority) or -math.huge,
},
}
Expand Down Expand Up @@ -310,7 +310,7 @@ local extension = {
end,
ecd = 'ecd',
edf = 'edif',
edfi = 'edif',
edif = 'edif',
edo = 'edif',
edn = function(path, bufnr)
return require('vim.filetype.detect').edn(bufnr)
Expand Down Expand Up @@ -425,7 +425,6 @@ local extension = {
gleam = 'gleam',
glsl = 'glsl',
gpi = 'gnuplot',
gnuplot = 'gnuplot',
go = 'go',
gp = 'gp',
gs = 'grads',
Expand Down Expand Up @@ -1175,9 +1174,8 @@ local extension = {
return require('vim.filetype.detect').sgml(bufnr)
end,
t = function(path, bufnr)
if not require('vim.filetype.detect').nroff(bufnr) and not require('vim.filetype.detect').perl(path, bufnr) then
return 'tads'
end
local nroff = require('vim.filetype.detect').nroff(bufnr)
return nroff or require('vim.filetype.detect').perl(path, bufnr) or 'tads'
end,
-- Ignored extensions
bak = function(path, bufnr)
Expand Down Expand Up @@ -1253,13 +1251,6 @@ local filename = {
['.arch-inventory'] = 'arch',
['GNUmakefile.am'] = 'automake',
['named.root'] = 'bindzone',
['.*/bind/db%..*'] = starsetf('bindzone'),
['.*/named/db%..*'] = starsetf('bindzone'),
['cabal%.project%..*'] = starsetf('cabalproject'),
['sgml%.catalog.*'] = starsetf('catalog'),
['.*/etc/hostname%..*'] = starsetf('config'),
['.*/etc/cron%.d/.*'] = starsetf('crontab'),
['crontab%..*'] = starsetf('crontab'),
WORKSPACE = 'bzl',
BUILD = 'bzl',
['cabal.project'] = 'cabalproject',
Expand Down Expand Up @@ -1305,9 +1296,6 @@ local filename = {
['/debian/copyright'] = 'debcopyright',
['/etc/apt/sources.list'] = 'debsources',
['denyhosts.conf'] = 'denyhosts',
['.*/debian/patches/.*'] = function(path, bufnr)
return require('vim.filetype.detect').dep3patch(path, bufnr)
end,
['dict.conf'] = 'dictconf',
['.dictrc'] = 'dictconf',
['/etc/DIR_COLORS'] = 'dircolors',
Expand Down Expand Up @@ -1366,6 +1354,7 @@ local filename = {
['.gnashpluginrc'] = 'gnash',
gnashpluginrc = 'gnash',
gnashrc = 'gnash',
['.gnuplot'] = 'gnuplot',
['go.work'] = 'gowork',
['.gprc'] = 'gp',
['/.gnupg/gpg.conf'] = 'gpg',
Expand Down Expand Up @@ -1514,7 +1503,6 @@ local filename = {
['.screenrc'] = 'screen',
['/etc/sensors3.conf'] = 'sensors',
['/etc/sensors.conf'] = 'sensors',
['.*/etc/sensors%.d/[^.].*'] = starsetf('sensors'),
['/etc/services'] = 'services',
['/etc/serial.conf'] = 'setserial',
['/etc/udev/cdsymlinks.conf'] = 'sh',
Expand Down Expand Up @@ -1560,13 +1548,15 @@ local filename = {
['.slrnrc'] = 'slrnrc',
['sendmail.cf'] = 'sm',
['squid.conf'] = 'squid',
['/.ssh/config'] = 'sshconfig',
['ssh_config'] = 'sshconfig',
['sshd_config'] = 'sshdconfig',
['/etc/sudoers'] = 'sudoers',
['sudoers.tmp'] = 'sudoers',
['/etc/sysctl.conf'] = 'sysctl',
tags = 'tags',
['pending.data'] = 'taskdata',
['completed.data'] = 'taskdata',
['undo.data'] = 'taskdata',
['.tclshrc'] = 'tcl',
['.wishrc'] = 'tcl',
['tclsh.rc'] = 'tcl',
Expand Down Expand Up @@ -1667,12 +1657,16 @@ local pattern = {
return require('vim.filetype.detect').asm(bufnr)
end,
['[mM]akefile%.am'] = 'automake',
['.*/bind/db%..*'] = starsetf('bindzone'),
['.*/named/db%..*'] = starsetf('bindzone'),
['.*bsd'] = 'bsdl',
['bzr_log%..*'] = 'bzr',
['.*enlightenment/.*%.cfg'] = 'c',
['cabal%.project%..*'] = starsetf('cabalproject'),
['.*/%.calendar/.*'] = starsetf('calendar'),
['.*/share/calendar/.*/calendar%..*'] = starsetf('calendar'),
['.*/share/calendar/calendar%..*'] = starsetf('calendar'),
['sgml%.catalog.*'] = starsetf('catalog'),
['.*/etc/defaults/cdrdao'] = 'cdrdaoconf',
['.*/etc/cdrdao%.conf'] = 'cdrdaoconf',
['.*/etc/default/cdrdao'] = 'cdrdaoconf',
Expand All @@ -1681,10 +1675,13 @@ local pattern = {
function(path, bufnr)
return require('vim.filetype.detect').cfg(bufnr)
end,
-- Decrease the priority to avoid conflicts with more specific patterns
-- Decrease priority to avoid conflicts with more specific patterns
-- such as '.*/etc/a2ps/.*%.cfg', '.*enlightenment/.*%.cfg', etc.
{ priority = -1 },
},
['[cC]hange[lL]og.*'] = starsetf(function(path, bufnr)
require('vim.filetype.detect').changelog(bufnr)
end),
['.*%.%.ch'] = 'chill',
['.*%.cmake%.in'] = 'cmake',
-- */cmus/rc and */.cmus/rc
Expand All @@ -1693,6 +1690,9 @@ local pattern = {
['.*/%.?cmus/.*%.theme'] = 'cmusrc',
['.*/%.cmus/autosave'] = 'cmusrc',
['.*/%.cmus/command%-history'] = 'cmusrc',
['.*/etc/hostname%..*'] = starsetf('config'),
['crontab%..*'] = starsetf('crontab'),
['.*/etc/cron%.d/.*'] = starsetf('crontab'),
['%.cshrc.*'] = function(path, bufnr)
return require('vim.filetype.detect').csh(path, bufnr)
end,
Expand All @@ -1703,9 +1703,9 @@ local pattern = {
['.*%.[Dd][Aa][Tt]'] = function(path, bufnr)
return require('vim.filetype.detect').dat(path, bufnr)
end,
['[cC]hange[lL]og.*'] = starsetf(function(path, bufnr)
require('vim.filetype.detect').changelog(bufnr)
end),
['.*/debian/patches/.*'] = function(path, bufnr)
return require('vim.filetype.detect').dep3patch(path, bufnr)
end,
['.*/etc/dnsmasq%.d/.*'] = starsetf('dnsmasq'),
['Containerfile%..*'] = starsetf('dockerfile'),
['Dockerfile%..*'] = starsetf('dockerfile'),
Expand All @@ -1716,6 +1716,8 @@ local pattern = {
['.*/debian/copyright'] = 'debcopyright',
['.*/etc/apt/sources%.list%.d/.*%.list'] = 'debsources',
['.*/etc/apt/sources%.list'] = 'debsources',
['.*%.directory'] = 'desktop',
['.*%.desktop'] = 'desktop',
['dictd.*%.conf'] = 'dictdconf',
['.*/etc/DIR_COLORS'] = 'dircolors',
['.*/etc/dnsmasq%.conf'] = 'dnsmasq',
Expand Down Expand Up @@ -1770,10 +1772,15 @@ local pattern = {
['.*/gitolite%-admin/conf/.*'] = starsetf('gitolite'),
['tmac%..*'] = starsetf('nroff'),
['.*/%.gitconfig%.d/.*'] = starsetf('gitconfig'),
['.*%.git/.*'] = function(path, bufnr)
require('vim.filetype.detect').git(bufnr)
end,
['.*%.git/.*'] = {
function(path, bufnr)
return require('vim.filetype.detect').git(bufnr)
end,
-- Decrease priority to run after simple pattern checks
{ priority = -1 },
},
['.*%.git/modules/.*/config'] = 'gitconfig',
['.*%.git/modules/config'] = 'gitconfig',
['.*%.git/config'] = 'gitconfig',
['.*/etc/gitconfig'] = 'gitconfig',
['.*/%.config/git/config'] = 'gitconfig',
Expand Down Expand Up @@ -1859,6 +1866,83 @@ local pattern = {
['.*[mM]akefile'] = 'make',
['[mM]akefile.*'] = starsetf('make'),
['.*/etc/man%.conf'] = 'manconf',
['.*/log/auth'] = 'messages',
['.*/log/cron'] = 'messages',
['.*/log/daemon'] = 'messages',
['.*/log/debug'] = 'messages',
['.*/log/kern'] = 'messages',
['.*/log/lpr'] = 'messages',
['.*/log/mail'] = 'messages',
['.*/log/messages'] = 'messages',
['.*/log/news/news'] = 'messages',
['.*/log/syslog'] = 'messages',
['.*/log/user'] = 'messages',
['.*/log/auth%.log'] = 'messages',
['.*/log/cron%.log'] = 'messages',
['.*/log/daemon%.log'] = 'messages',
['.*/log/debug%.log'] = 'messages',
['.*/log/kern%.log'] = 'messages',
['.*/log/lpr%.log'] = 'messages',
['.*/log/mail%.log'] = 'messages',
['.*/log/messages%.log'] = 'messages',
['.*/log/news/news%.log'] = 'messages',
['.*/log/syslog%.log'] = 'messages',
['.*/log/user%.log'] = 'messages',
['.*/log/auth%.err'] = 'messages',
['.*/log/cron%.err'] = 'messages',
['.*/log/daemon%.err'] = 'messages',
['.*/log/debug%.err'] = 'messages',
['.*/log/kern%.err'] = 'messages',
['.*/log/lpr%.err'] = 'messages',
['.*/log/mail%.err'] = 'messages',
['.*/log/messages%.err'] = 'messages',
['.*/log/news/news%.err'] = 'messages',
['.*/log/syslog%.err'] = 'messages',
['.*/log/user%.err'] = 'messages',
['.*/log/auth%.info'] = 'messages',
['.*/log/cron%.info'] = 'messages',
['.*/log/daemon%.info'] = 'messages',
['.*/log/debug%.info'] = 'messages',
['.*/log/kern%.info'] = 'messages',
['.*/log/lpr%.info'] = 'messages',
['.*/log/mail%.info'] = 'messages',
['.*/log/messages%.info'] = 'messages',
['.*/log/news/news%.info'] = 'messages',
['.*/log/syslog%.info'] = 'messages',
['.*/log/user%.info'] = 'messages',
['.*/log/auth%.warn'] = 'messages',
['.*/log/cron%.warn'] = 'messages',
['.*/log/daemon%.warn'] = 'messages',
['.*/log/debug%.warn'] = 'messages',
['.*/log/kern%.warn'] = 'messages',
['.*/log/lpr%.warn'] = 'messages',
['.*/log/mail%.warn'] = 'messages',
['.*/log/messages%.warn'] = 'messages',
['.*/log/news/news%.warn'] = 'messages',
['.*/log/syslog%.warn'] = 'messages',
['.*/log/user%.warn'] = 'messages',
['.*/log/auth%.crit'] = 'messages',
['.*/log/cron%.crit'] = 'messages',
['.*/log/daemon%.crit'] = 'messages',
['.*/log/debug%.crit'] = 'messages',
['.*/log/kern%.crit'] = 'messages',
['.*/log/lpr%.crit'] = 'messages',
['.*/log/mail%.crit'] = 'messages',
['.*/log/messages%.crit'] = 'messages',
['.*/log/news/news%.crit'] = 'messages',
['.*/log/syslog%.crit'] = 'messages',
['.*/log/user%.crit'] = 'messages',
['.*/log/auth%.notice'] = 'messages',
['.*/log/cron%.notice'] = 'messages',
['.*/log/daemon%.notice'] = 'messages',
['.*/log/debug%.notice'] = 'messages',
['.*/log/kern%.notice'] = 'messages',
['.*/log/lpr%.notice'] = 'messages',
['.*/log/mail%.notice'] = 'messages',
['.*/log/messages%.notice'] = 'messages',
['.*/log/news/news%.notice'] = 'messages',
['.*/log/syslog%.notice'] = 'messages',
['.*/log/user%.notice'] = 'messages',
['.*%.[Mm][Oo][Dd]'] = function(path, bufnr)
return require('vim.filetype.detect').mod(path, bufnr)
end,
Expand Down Expand Up @@ -1937,6 +2021,7 @@ local pattern = {
['[rR]akefile.*'] = starsetf('ruby'),
['[rR]antfile'] = 'ruby',
['[rR]akefile'] = 'ruby',
['.*/etc/sensors%.d/[^.].*'] = starsetf('sensors'),
['.*/etc/sensors%.conf'] = 'sensors',
['.*/etc/sensors3%.conf'] = 'sensors',
['.*/etc/services'] = 'services',
Expand Down Expand Up @@ -1975,6 +2060,7 @@ local pattern = {
['.*/etc/slp%.spi'] = 'slpspi',
['.*/etc/ssh/ssh_config%.d/.*%.conf'] = 'sshconfig',
['.*/%.ssh/config'] = 'sshconfig',
['.*/%.ssh/.*%.conf'] = 'sshconfig',
['.*/etc/ssh/sshd_config%.d/.*%.conf'] = 'sshdconfig',
['.*%.[Ss][Rr][Cc]'] = function(path, bufnr)
return require('vim.filetype.detect').src(bufnr)
Expand Down Expand Up @@ -2322,7 +2408,7 @@ function M.match(args)
local ft, on_detect

-- First check for the simple case where the full path exists as a key
local path = vim.fn.resolve(vim.fn.fnamemodify(name, ':p'))
local path = vim.fn.fnamemodify(name, ':p')
ft, on_detect = dispatch(filename[path], path, bufnr)
if ft then
return ft, on_detect
Expand Down
22 changes: 17 additions & 5 deletions runtime/lua/vim/filetype/detect.lua
Expand Up @@ -187,6 +187,17 @@ function M.cls(bufnr)
end
end

function M.conf(path, bufnr)
if vim.fn.did_filetype() ~= 0 or path:find(vim.g.ft_ignore_pat) then
return
end
for _, line in ipairs(getlines(bufnr, 1, 5)) do
if line:find('^#') then
return 'conf'
end
end
end

-- Debian Control
function M.control(bufnr)
if getlines(bufnr, 1):find('^Source:') then
Expand Down Expand Up @@ -256,8 +267,9 @@ local function cvs_diff(path, contents)
end

function M.dat(path, bufnr)
local file_name = vim.fn.fnamemodify(path, ':t'):lower()
-- Innovation data processing
if findany(path:lower(), { '^upstream%.dat$', '^upstream%..*%.dat$', '^.*%.upstream%.dat$' }) then
if findany(file_name, { '^upstream%.dat$', '^upstream%..*%.dat$', '^.*%.upstream%.dat$' }) then
return 'upstreamdat'
end
if vim.g.filetype_dat then
Expand Down Expand Up @@ -458,7 +470,7 @@ end

function M.git(bufnr)
local line = getlines(bufnr, 1)
if line:find('^' .. string.rep('%x', 40) .. '+ ') or line:sub(1, 5) == 'ref: ' then
if matchregex(line, [[^\x\{40,\}\>\|^ref: ]]) then
return 'git'
end
end
Expand Down Expand Up @@ -568,7 +580,7 @@ function M.log(path)
return 'upstreaminstalllog'
elseif findany(path, { 'usserver%.log', 'usserver%..*%.log', '.*%.usserver%.log' }) then
return 'usserverlog'
elseif findany(path, { 'usw2kagt%.log', 'usws2kagt%..*%.log', '.*%.usws2kagt%.log' }) then
elseif findany(path, { 'usw2kagt%.log', 'usw2kagt%..*%.log', '.*%.usw2kagt%.log' }) then
return 'usw2kagtlog'
end
end
Expand Down Expand Up @@ -759,8 +771,8 @@ end
-- If the first line starts with '#' and contains 'perl' it's probably a Perl file.
-- (Slow test) If a file contains a 'use' statement then it is almost certainly a Perl file.
function M.perl(path, bufnr)
local dirname = vim.fn.expand(path, '%:p:h:t')
if vim.fn.expand(dirname, '%:e') == 't' and (dirname == 't' or dirname == 'xt') then
local dir_name = vim.fs.dirname(path)
if vim.fn.expand(path, '%:e') == 't' and (dir_name == 't' or dir_name == 'xt') then
return 'perl'
end
local first_line = getlines(bufnr, 1)
Expand Down