From 66a74535d40c6b68d38cb92088315fa67dc1304b Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Thu, 1 Aug 2024 12:43:42 +0300 Subject: [PATCH] refactor(filetype): use file name match instead of pattern if possible Problem: some patterns are used as a replacement for one-two explicit file matches (like '^[mM]akefile$'). As matching file name directly is faster and more explicit, it should be preferred. Solution: move those patterns to direct file name match. NOTE: this is not strictly backwards compatible, because exact file name matching is done *before* pattern matching. If user has conflicting `vim.filetype.add()` call with high priority (like with `pattern='file$'` and `priority=100`), after this change it will be ignored (i.e. 'makefile' will match exactly). Judging by converted cases, it seems reasonable to prefer exact matches there. --- runtime/lua/vim/filetype.lua | 37 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 3dea4e1046..c7f8041172 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -1332,7 +1332,17 @@ local filename = { ['/.aptitude/config'] = 'aptconf', ['=tagging-method'] = 'arch', ['.arch-inventory'] = 'arch', + ['makefile.am'] = 'automake', + ['Makefile.am'] = 'automake', ['GNUmakefile.am'] = 'automake', + ['.bash_aliases'] = detect.bash, + ['.bash-aliases'] = detect.bash, + ['.bash_history'] = detect.bash, + ['.bash-history'] = detect.bash, + ['.bash_logout'] = detect.bash, + ['.bash-logout'] = detect.bash, + ['.bash_profile'] = detect.bash, + ['.bash-profile'] = detect.bash, ['named.root'] = 'bindzone', WORKSPACE = 'bzl', ['WORKSPACE.bzlmod'] = 'bzl', @@ -1507,6 +1517,8 @@ local filename = { ['.swrc'] = 'jsonc', ['.vsconfig'] = 'jsonc', ['.justfile'] = 'just', + ['justfile'] = 'just', + ['Justfile'] = 'just', Kconfig = 'kconfig', ['Kconfig.debug'] = 'kconfig', ['Config.in'] = 'kconfig', @@ -1558,6 +1570,8 @@ local filename = { mrxvtrc = 'mrxvtrc', ['.mrxvtrc'] = 'mrxvtrc', ['.msmtprc'] = 'msmtp', + ['Muttngrc'] = 'muttrc', + ['Muttrc'] = 'muttrc', ['.mysql_history'] = 'mysql', ['/etc/nanorc'] = 'nanorc', Neomuttrc = 'neomuttrc', @@ -1582,6 +1596,9 @@ local filename = { ['/etc/shadow-'] = 'passwd', ['/etc/shadow'] = 'passwd', ['/etc/passwd.edit'] = 'passwd', + ['.gitolite.rc'] = 'perl', + ['gitolite.rc'] = 'perl', + ['example.gitolite.rc'] = 'perl', ['latexmkrc'] = 'perl', ['.latexmkrc'] = 'perl', ['pf.conf'] = 'pf', @@ -1637,6 +1654,10 @@ local filename = { irbrc = 'ruby', ['.irb_history'] = 'ruby', irb_history = 'ruby', + ['rakefile'] = 'ruby', + ['Rakefile'] = 'ruby', + ['rantfile'] = 'ruby', + ['Rantfile'] = 'ruby', Vagrantfile = 'ruby', ['smb.conf'] = 'samba', screenrc = 'screen', @@ -2074,13 +2095,6 @@ local pattern = { ['%.git/info/exclude$'] = 'gitignore', ['/%.config/git/ignore$'] = 'gitignore', }, - ['bash'] = { - ['^%.bash[_%-]aliases$'] = detect.bash, - ['^%.bash[_%-]history$'] = detect.bash, - ['^%.bash[_%-]logout$'] = detect.bash, - ['^%.bash[_%-]profile$'] = detect.bash, - ['^bash%-fc[%-%.]'] = detect.bash, - }, ['%.cfg'] = { ['enlightenment/.*%.cfg$'] = 'c', ['Eterm/.*%.cfg$'] = 'eterm', @@ -2121,15 +2135,11 @@ local pattern = { ['%.sst%.meta$'] = 'sisu', }, ['file'] = { - ['^[mM]akefile%.am$'] = 'automake', ['^Containerfile%.'] = starsetf('dockerfile'), ['^Dockerfile%.'] = starsetf('dockerfile'), - ['^[jJ]ustfile$'] = 'just', ['[mM]akefile$'] = detect.make, ['^[mM]akefile'] = starsetf('make'), - ['^[rR]akefile$'] = 'ruby', ['^[rR]akefile'] = starsetf('ruby'), - ['^[rR]antfile$'] = 'ruby', ['^%.profile'] = detect.sh, }, ['fvwm'] = { @@ -2169,9 +2179,7 @@ local pattern = { ['/%.mutt/muttrc'] = detect_muttrc, ['/%.muttng/muttngrc'] = detect_muttrc, ['/%.muttng/muttrc'] = detect_muttrc, - ['^Muttngrc$'] = 'muttrc', ['^Muttngrc'] = detect_muttrc, - ['^Muttrc$'] = 'muttrc', ['^Muttrc'] = detect_muttrc, -- neomuttrc* and .neomuttrc* ['^%.?neomuttrc'] = detect_neomuttrc, @@ -2195,6 +2203,7 @@ local pattern = { ['%.vbproj%.user$'] = 'xml', }, [''] = { + ['^bash%-fc[%-%.]'] = detect.bash, ['/bind/db%.'] = starsetf('bindzone'), ['/named/db%.'] = starsetf('bindzone'), ['%.blade%.php$'] = 'blade', @@ -2265,8 +2274,6 @@ local pattern = { ['%.opam%.locked$'] = 'opam', ['%.opam%.template$'] = 'opam', ['%.[Oo][Pp][Ll]$'] = 'opl', - ['^%.?gitolite%.rc$'] = 'perl', - ['^example%.gitolite%.rc$'] = 'perl', ['%.php%d$'] = 'php', ['%.[Pp][Rr][Gg]$'] = detect.prg, ['printcap'] = starsetf(function(path, bufnr)