From 79148813567909aae8bc733e585d7713f6069401 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 31 Aug 2023 07:44:33 +0800 Subject: [PATCH] vim-patch:9.0.1797: Vimball/Visual Basic filetype detection conflict (#24947) Problem: Vimball/Visual Basic filetype detection conflict Solution: runtime(vb): Improve Vimball and Visual Basic detection logic Only run Vimball Archiver's BufEnter autocommand on Vimball archives. Fixes vim/vim#2694. closes: vim/vim#12899 https://github.com/vim/vim/commit/f97f6bbf56408c0c97b4ddbe81fba858d7455b0d Co-authored-by: Doug Kearns --- runtime/lua/vim/filetype.lua | 12 +++++++---- runtime/lua/vim/filetype/detect.lua | 31 +++++++++++++++++++---------- test/old/testdir/test_filetype.vim | 30 ++++++++++++++++++++++++---- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 35a10b6bdc..0e8ecf1b07 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -351,6 +351,7 @@ local extension = { bat = 'dosbatch', wrap = 'dosini', ini = 'dosini', + vbp = 'dosini', dot = 'dot', gv = 'dot', drac = 'dracula', @@ -1025,13 +1026,18 @@ local extension = { url = 'urlshortcut', usd = 'usd', usda = 'usd', + v = detect.v, vsh = 'v', vv = 'v', + ctl = 'vb', + dob = 'vb', + dsm = 'vb', + dsr = 'vb', + pag = 'vb', sba = 'vb', vb = 'vb', - dsm = 'vb', - ctl = 'vb', vbs = 'vb', + vba = detect.vba, vdf = 'vdf', vdmpp = 'vdmpp', vpp = 'vdmpp', @@ -1041,7 +1047,6 @@ local extension = { vr = 'vera', vri = 'vera', vrh = 'vera', - v = detect.v, va = 'verilogams', vams = 'verilogams', vhdl = 'vhdl', @@ -1052,7 +1057,6 @@ local extension = { vbe = 'vhdl', tape = 'vhs', vim = 'vim', - vba = 'vim', mar = 'vmasm', cm = 'voscm', wrl = 'vrml', diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index af6d77ecbf..0609ac5513 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -77,7 +77,7 @@ function M.asm_syntax(_, bufnr) end local visual_basic_content = - { 'vb_name', 'begin vb%.form', 'begin vb%.mdiform', 'begin vb%.usercontrol' } + [[\c^\s*\%(Attribute\s\+VB_Name\|Begin\s\+\%(VB\.\|{\%(\x\+-\)\+\x\+}\)\)]] -- See frm() for Visual Basic form file detection --- @type vim.filetype.mapfn @@ -97,7 +97,7 @@ function M.bas(_, bufnr) local qb64_preproc = [[\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)]] for _, line in ipairs(getlines(bufnr, 1, 100)) do - if findany(line:lower(), visual_basic_content) then + if matchregex(line, visual_basic_content) then return 'vb' elseif line:find(fb_comment) @@ -193,12 +193,12 @@ function M.cls(_, bufnr) if vim.g.filetype_cls then return vim.g.filetype_cls end - local line = getline(bufnr, 1) - if line:find('^[%%\\]') then + local line1 = getline(bufnr, 1) + if line1:find('^[%%\\]') then return 'tex' - elseif line:find('^#') and line:lower():find('rexx') then + elseif line1:find('^#') and line1:lower():find('rexx') then return 'rexx' - elseif line == 'VERSION 1.0 CLASS' then + elseif line1 == 'VERSION 1.0 CLASS' then return 'vb' end return 'st' @@ -525,12 +525,15 @@ function M.frm(_, bufnr) if vim.g.filetype_frm then return vim.g.filetype_frm end - local lines = table.concat(getlines(bufnr, 1, 5)):lower() - if findany(lines, visual_basic_content) then + if getline(bufnr, 1) == 'VERSION 5.00' then return 'vb' - else - return 'form' end + for _, line in ipairs(getlines(bufnr, 1, 5)) do + if matchregex(line, visual_basic_content) then + return 'vb' + end + end + return 'form' end --- @type vim.filetype.mapfn @@ -1529,6 +1532,14 @@ function M.v(_, bufnr) return 'v' end +--- @type vim.filetype.mapfn +function M.vba(_, bufnr) + if getline(bufnr, 1):find('^["#] Vimball Archiver') then + return 'vim' + end + return 'vb' +end + -- WEB (*.web is also used for Winbatch: Guess, based on expecting "%" comment -- lines in a WEB file). --- @type vim.filetype.mapfn diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index dfeed25a8d..5d6b306d09 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -195,7 +195,7 @@ func s:GetFilenameChecks() abort \ 'dnsmasq': ['/etc/dnsmasq.conf', '/etc/dnsmasq.d/file', 'any/etc/dnsmasq.conf', 'any/etc/dnsmasq.d/file'], \ 'dockerfile': ['Containerfile', 'Dockerfile', 'dockerfile', 'file.Dockerfile', 'file.dockerfile', 'Dockerfile.debian', 'Containerfile.something'], \ 'dosbatch': ['file.bat'], - \ 'dosini': ['/etc/yum.conf', 'file.ini', 'npmrc', '.npmrc', 'php.ini', 'php.ini-5', 'php.ini-file', '/etc/yum.repos.d/file', 'any/etc/yum.conf', 'any/etc/yum.repos.d/file', 'file.wrap'], + \ 'dosini': ['/etc/yum.conf', 'file.ini', 'npmrc', '.npmrc', 'php.ini', 'php.ini-5', 'php.ini-file', '/etc/yum.repos.d/file', 'any/etc/yum.conf', 'any/etc/yum.repos.d/file', 'file.wrap', 'file.vbp'], \ 'dot': ['file.dot', 'file.gv'], \ 'dracula': ['file.drac', 'file.drc', 'filelvs', 'filelpe', 'drac.file', 'lpe', 'lvs', 'some-lpe', 'some-lvs'], \ 'dtd': ['file.dtd'], @@ -682,7 +682,7 @@ func s:GetFilenameChecks() abort \ 'usw2kagtlog': ['usw2kagt.log', 'USW2KAGT.LOG', 'usw2kagt.file.log', 'USW2KAGT.FILE.LOG', 'file.usw2kagt.log', 'FILE.USW2KAGT.LOG'], \ 'v': ['file.vsh', 'file.vv'], \ 'vala': ['file.vala'], - \ 'vb': ['file.sba', 'file.vb', 'file.vbs', 'file.dsm', 'file.ctl'], + \ 'vb': ['file.sba', 'file.vb', 'file.vbs', 'file.dsm', 'file.ctl', 'file.dob', 'file.dsr'], \ 'vdf': ['file.vdf'], \ 'vdmpp': ['file.vpp', 'file.vdmpp'], \ 'vdmrt': ['file.vdmrt'], @@ -692,7 +692,7 @@ func s:GetFilenameChecks() abort \ 'vgrindefs': ['vgrindefs'], \ 'vhdl': ['file.hdl', 'file.vhd', 'file.vhdl', 'file.vbe', 'file.vst', 'file.vhdl_123', 'file.vho', 'some.vhdl_1', 'some.vhdl_1-file'], \ 'vhs': ['file.tape'], - \ 'vim': ['file.vim', 'file.vba', '.exrc', '_exrc', 'some-vimrc', 'some-vimrc-file', 'vimrc', 'vimrc-file'], + \ 'vim': ['file.vim', '.exrc', '_exrc', 'some-vimrc', 'some-vimrc-file', 'vimrc', 'vimrc-file'], \ 'viminfo': ['.viminfo', '_viminfo'], \ 'vmasm': ['file.mar'], \ 'voscm': ['file.cm'], @@ -1289,7 +1289,7 @@ func Test_frm_file() " Visual Basic - call writefile(['Begin VB.Form Form1'], 'Xfile.frm') + call writefile(['VERSION 5.00', 'Begin VB.Form Form1'], 'Xfile.frm') split Xfile.frm call assert_equal('vb', &filetype) bwipe! @@ -2198,4 +2198,26 @@ func Test_typ_file() filetype off endfunc +func Test_vba_file() + filetype on + + " Test dist#ft#FTvba() + + " Visual Basic + + call writefile(['looks like Visual Basic'], 'Xfile.vba', 'D') + split Xfile.vba + call assert_equal('vb', &filetype) + bwipe! + + " Vimball Archiver (ft=vim) + + call writefile(['" Vimball Archiver by Charles E. Campbell, Ph.D.', 'UseVimball', 'finish'], 'Xfile.vba', 'D') + split Xfile.vba + call assert_equal('vim', &filetype) + bwipe! + + filetype off +endfunc + " vim: shiftwidth=2 sts=2 expandtab