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

f97f6bbf56

Co-authored-by: Doug Kearns <dougkearns@gmail.com>
This commit is contained in:
zeertzjq
2023-08-31 07:44:33 +08:00
committed by GitHub
parent 6463116818
commit 7914881356
3 changed files with 55 additions and 18 deletions

View File

@@ -351,6 +351,7 @@ local extension = {
bat = 'dosbatch', bat = 'dosbatch',
wrap = 'dosini', wrap = 'dosini',
ini = 'dosini', ini = 'dosini',
vbp = 'dosini',
dot = 'dot', dot = 'dot',
gv = 'dot', gv = 'dot',
drac = 'dracula', drac = 'dracula',
@@ -1025,13 +1026,18 @@ local extension = {
url = 'urlshortcut', url = 'urlshortcut',
usd = 'usd', usd = 'usd',
usda = 'usd', usda = 'usd',
v = detect.v,
vsh = 'v', vsh = 'v',
vv = 'v', vv = 'v',
ctl = 'vb',
dob = 'vb',
dsm = 'vb',
dsr = 'vb',
pag = 'vb',
sba = 'vb', sba = 'vb',
vb = 'vb', vb = 'vb',
dsm = 'vb',
ctl = 'vb',
vbs = 'vb', vbs = 'vb',
vba = detect.vba,
vdf = 'vdf', vdf = 'vdf',
vdmpp = 'vdmpp', vdmpp = 'vdmpp',
vpp = 'vdmpp', vpp = 'vdmpp',
@@ -1041,7 +1047,6 @@ local extension = {
vr = 'vera', vr = 'vera',
vri = 'vera', vri = 'vera',
vrh = 'vera', vrh = 'vera',
v = detect.v,
va = 'verilogams', va = 'verilogams',
vams = 'verilogams', vams = 'verilogams',
vhdl = 'vhdl', vhdl = 'vhdl',
@@ -1052,7 +1057,6 @@ local extension = {
vbe = 'vhdl', vbe = 'vhdl',
tape = 'vhs', tape = 'vhs',
vim = 'vim', vim = 'vim',
vba = 'vim',
mar = 'vmasm', mar = 'vmasm',
cm = 'voscm', cm = 'voscm',
wrl = 'vrml', wrl = 'vrml',

View File

@@ -77,7 +77,7 @@ function M.asm_syntax(_, bufnr)
end end
local visual_basic_content = 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 -- See frm() for Visual Basic form file detection
--- @type vim.filetype.mapfn --- @type vim.filetype.mapfn
@@ -97,7 +97,7 @@ function M.bas(_, bufnr)
local qb64_preproc = [[\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)]] local qb64_preproc = [[\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)]]
for _, line in ipairs(getlines(bufnr, 1, 100)) do 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' return 'vb'
elseif elseif
line:find(fb_comment) line:find(fb_comment)
@@ -193,12 +193,12 @@ function M.cls(_, bufnr)
if vim.g.filetype_cls then if vim.g.filetype_cls then
return vim.g.filetype_cls return vim.g.filetype_cls
end end
local line = getline(bufnr, 1) local line1 = getline(bufnr, 1)
if line:find('^[%%\\]') then if line1:find('^[%%\\]') then
return 'tex' return 'tex'
elseif line:find('^#') and line:lower():find('rexx') then elseif line1:find('^#') and line1:lower():find('rexx') then
return 'rexx' return 'rexx'
elseif line == 'VERSION 1.0 CLASS' then elseif line1 == 'VERSION 1.0 CLASS' then
return 'vb' return 'vb'
end end
return 'st' return 'st'
@@ -525,12 +525,15 @@ function M.frm(_, bufnr)
if vim.g.filetype_frm then if vim.g.filetype_frm then
return vim.g.filetype_frm return vim.g.filetype_frm
end end
local lines = table.concat(getlines(bufnr, 1, 5)):lower() if getline(bufnr, 1) == 'VERSION 5.00' then
if findany(lines, visual_basic_content) then
return 'vb' return 'vb'
else
return 'form'
end end
for _, line in ipairs(getlines(bufnr, 1, 5)) do
if matchregex(line, visual_basic_content) then
return 'vb'
end
end
return 'form'
end end
--- @type vim.filetype.mapfn --- @type vim.filetype.mapfn
@@ -1529,6 +1532,14 @@ function M.v(_, bufnr)
return 'v' return 'v'
end 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 -- WEB (*.web is also used for Winbatch: Guess, based on expecting "%" comment
-- lines in a WEB file). -- lines in a WEB file).
--- @type vim.filetype.mapfn --- @type vim.filetype.mapfn

View File

@@ -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'], \ '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'], \ 'dockerfile': ['Containerfile', 'Dockerfile', 'dockerfile', 'file.Dockerfile', 'file.dockerfile', 'Dockerfile.debian', 'Containerfile.something'],
\ 'dosbatch': ['file.bat'], \ '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'], \ 'dot': ['file.dot', 'file.gv'],
\ 'dracula': ['file.drac', 'file.drc', 'filelvs', 'filelpe', 'drac.file', 'lpe', 'lvs', 'some-lpe', 'some-lvs'], \ 'dracula': ['file.drac', 'file.drc', 'filelvs', 'filelpe', 'drac.file', 'lpe', 'lvs', 'some-lpe', 'some-lvs'],
\ 'dtd': ['file.dtd'], \ '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'], \ 'usw2kagtlog': ['usw2kagt.log', 'USW2KAGT.LOG', 'usw2kagt.file.log', 'USW2KAGT.FILE.LOG', 'file.usw2kagt.log', 'FILE.USW2KAGT.LOG'],
\ 'v': ['file.vsh', 'file.vv'], \ 'v': ['file.vsh', 'file.vv'],
\ 'vala': ['file.vala'], \ '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'], \ 'vdf': ['file.vdf'],
\ 'vdmpp': ['file.vpp', 'file.vdmpp'], \ 'vdmpp': ['file.vpp', 'file.vdmpp'],
\ 'vdmrt': ['file.vdmrt'], \ 'vdmrt': ['file.vdmrt'],
@@ -692,7 +692,7 @@ func s:GetFilenameChecks() abort
\ 'vgrindefs': ['vgrindefs'], \ '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'], \ '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'], \ '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'], \ 'viminfo': ['.viminfo', '_viminfo'],
\ 'vmasm': ['file.mar'], \ 'vmasm': ['file.mar'],
\ 'voscm': ['file.cm'], \ 'voscm': ['file.cm'],
@@ -1289,7 +1289,7 @@ func Test_frm_file()
" Visual Basic " Visual Basic
call writefile(['Begin VB.Form Form1'], 'Xfile.frm') call writefile(['VERSION 5.00', 'Begin VB.Form Form1'], 'Xfile.frm')
split Xfile.frm split Xfile.frm
call assert_equal('vb', &filetype) call assert_equal('vb', &filetype)
bwipe! bwipe!
@@ -2198,4 +2198,26 @@ func Test_typ_file()
filetype off filetype off
endfunc 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 " vim: shiftwidth=2 sts=2 expandtab