From 0063d8f0d7e86651eacbfb58a480395916804743 Mon Sep 17 00:00:00 2001 From: smjonas Date: Sun, 3 Jul 2022 19:15:52 +0200 Subject: [PATCH] fix(filetype): fix filetype patterns --- runtime/filetype.lua | 8 +- runtime/lua/vim/filetype.lua | 125 +++++++++++++++++++++++----- runtime/lua/vim/filetype/detect.lua | 22 +++-- 3 files changed, 127 insertions(+), 28 deletions(-) diff --git a/runtime/filetype.lua b/runtime/filetype.lua index 35bb31edcedf1c..21e4e77bcaf460 100644 --- a/runtime/filetype.lua +++ b/runtime/filetype.lua @@ -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(vim.api.nvim_buf_get_name(args.buf), 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) diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 2874ea45e7c260..d7d14506693a90 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -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, }, } @@ -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) @@ -425,7 +425,6 @@ local extension = { gleam = 'gleam', glsl = 'glsl', gpi = 'gnuplot', - gnuplot = 'gnuplot', go = 'go', gp = 'gp', gs = 'grads', @@ -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) @@ -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', @@ -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', @@ -1366,6 +1354,7 @@ local filename = { ['.gnashpluginrc'] = 'gnash', gnashpluginrc = 'gnash', gnashrc = 'gnash', + ['.gnuplot'] = 'gnuplot', ['go.work'] = 'gowork', ['.gprc'] = 'gp', ['/.gnupg/gpg.conf'] = 'gpg', @@ -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', @@ -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', @@ -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', @@ -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 @@ -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, @@ -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'), @@ -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', @@ -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', @@ -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, @@ -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', @@ -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) diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index 37922b4ebf3e4d..b3c096c8774939 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)