Skip to content

Commit

Permalink
fix(filetype): fix filetype patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
smjonas committed Jul 4, 2022
1 parent acb7a90 commit 8a405ac
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 28 deletions.
8 changes: 7 additions & 1 deletion runtime/filetype.lua
Expand Up @@ -13,7 +13,13 @@ 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
if not ft then
-- Generic configuration file used as fallback
ft = require('vim.filetype.detect').conf(args.amatch, args.buf)
if ft then
vim.cmd('setf FALLBACK ' .. ft)
end
else
vim.api.nvim_buf_set_option(args.buf, 'filetype', ft)
if on_detect then
on_detect(args.buf)
Expand Down
125 changes: 103 additions & 22 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 @@ -1685,6 +1679,9 @@ local pattern = {
-- 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 @@ -1771,7 +1773,7 @@ local pattern = {
['tmac%..*'] = starsetf('nroff'),
['.*/%.gitconfig%.d/.*'] = starsetf('gitconfig'),
['.*%.git/.*'] = function(path, bufnr)
require('vim.filetype.detect').git(bufnr)
return require('vim.filetype.detect').git(bufnr)
end,
['.*%.git/modules/.*/config'] = 'gitconfig',
['.*%.git/config'] = 'gitconfig',
Expand Down Expand Up @@ -1859,6 +1861,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 +2016,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 +2055,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
23 changes: 18 additions & 5 deletions runtime/lua/vim/filetype/detect.lua
Expand Up @@ -187,6 +187,18 @@ function M.cls(bufnr)
end
end

function M.conf(path, bufnr)
-- Path may be nil, do not fail in that case
if vim.fn.did_filetype() ~= 0 or (path or ''):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 +268,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 +471,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 +581,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 +772,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

0 comments on commit 8a405ac

Please sign in to comment.