patch 8.2.4172: filetype detection for BASIC is not optimal

Problem:    Filetype detection for BASIC is not optimal.
Solution:   Improve BASIC filetype detection. (Doug Kearns)
This commit is contained in:
Bram Moolenaar
2022-01-21 14:55:13 +00:00
parent f0e7e6365e
commit 6517f14165
4 changed files with 89 additions and 8 deletions

View File

@ -67,13 +67,29 @@ func dist#ft#FTasmsyntax()
endif
endfunc
" Check if one of the first five lines contains "VB_Name". In that case it is
" probably a Visual Basic file. Otherwise it's assumed to be "alt" filetype.
func dist#ft#FTVB(alt)
if getline(1).getline(2).getline(3).getline(4).getline(5) =~? 'VB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)'
func dist#ft#FTbas()
if exists("g:filetype_bas")
exe "setf " . g:filetype_bas
return
endif
" most frequent FreeBASIC-specific keywords in distro files
let fb_keywords = '\c^\s*\%(extern\|var\|enum\|private\|scope\|union\|byref\|operator\|constructor\|delete\|namespace\|public\|property\|with\|destructor\|using\)\>\%(\s*[:=(]\)\@!'
let fb_preproc = '\c^\s*\%(#\a\+\|option\s\+\%(byval\|dynamic\|escape\|\%(no\)\=gosub\|nokeyword\|private\|static\)\>\)'
let fb_comment = "^\\s*/'"
" OPTION EXPLICIT, without the leading underscore, is common to many dialects
let qb64_preproc = '\c^\s*\%($\a\+\|option\s\+\%(_explicit\|_\=explicitarray\)\>\)'
let lines = getline(1, min([line("$"), 100]))
if match(lines, fb_preproc) > -1 || match(lines, fb_comment) > -1 || match(lines, fb_keywords) > -1
setf freebasic
elseif match(lines, qb64_preproc) > -1
setf qb64
elseif match(lines, '\cVB_Name\|Begin VB\.\(Form\|MDIForm\|UserControl\)') > -1
setf vb
else
exe "setf " . a:alt
setf basic
endif
endfunc

View File

@ -193,7 +193,8 @@ au BufNewFile,BufRead *.awk,*.gawk setf awk
au BufNewFile,BufRead *.mch,*.ref,*.imp setf b
" BASIC or Visual Basic
au BufNewFile,BufRead *.bas call dist#ft#FTVB("basic")
au BufNewFile,BufRead *.bas call dist#ft#FTbas()
au BufNewFile,BufRead *.bi,*.bm call dist#ft#FTbas()
" Visual Basic Script (close to Visual Basic) or Visual Basic .NET
au BufNewFile,BufRead *.vb,*.vbs,*.dsm,*.ctl setf vb
@ -202,7 +203,7 @@ au BufNewFile,BufRead *.vb,*.vbs,*.dsm,*.ctl setf vb
au BufNewFile,BufRead *.iba,*.ibi setf ibasic
" FreeBasic file (similar to QBasic)
au BufNewFile,BufRead *.fb,*.bi setf freebasic
au BufNewFile,BufRead *.fb setf freebasic
" Batch file for MSDOS.
au BufNewFile,BufRead *.bat,*.sys setf dosbatch

View File

@ -76,6 +76,7 @@ let s:filename_checks = {
\ 'ave': ['file.ave'],
\ 'awk': ['file.awk', 'file.gawk'],
\ 'b': ['file.mch', 'file.ref', 'file.imp'],
\ 'basic': ['file.bas', 'file.bi', 'file.bm'],
\ 'bzl': ['file.bazel', 'file.bzl', 'WORKSPACE'],
\ 'bc': ['file.bc'],
\ 'bdf': ['file.bdf'],
@ -186,7 +187,7 @@ let s:filename_checks = {
\ 'fortran': ['file.f', 'file.for', 'file.fortran', 'file.fpp', 'file.ftn', 'file.f77', 'file.f90', 'file.f95', 'file.f03', 'file.f08'],
\ 'fpcmake': ['file.fpc'],
\ 'framescript': ['file.fsl'],
\ 'freebasic': ['file.fb', 'file.bi'],
\ 'freebasic': ['file.fb'],
\ 'fsharp': ['file.fs', 'file.fsi', 'file.fsx'],
\ 'fstab': ['fstab', 'mtab'],
\ 'fvwm': ['/.fvwm/file', 'any/.fvwm/file'],
@ -1171,4 +1172,65 @@ func Test_foam_file()
filetype off
endfunc
func Test_bas_file()
filetype on
call writefile(['looks like BASIC'], 'Xfile.bas')
split Xfile.bas
call assert_equal('basic', &filetype)
bwipe!
" Test dist#ft#FTbas()
let g:filetype_bas = 'freebasic'
split Xfile.bas
call assert_equal('freebasic', &filetype)
bwipe!
unlet g:filetype_bas
" FreeBASIC
call writefile(["/' FreeBASIC multiline comment '/"], 'Xfile.bas')
split Xfile.bas
call assert_equal('freebasic', &filetype)
bwipe!
call writefile(['#define TESTING'], 'Xfile.bas')
split Xfile.bas
call assert_equal('freebasic', &filetype)
bwipe!
call writefile(['option byval'], 'Xfile.bas')
split Xfile.bas
call assert_equal('freebasic', &filetype)
bwipe!
call writefile(['extern "C"'], 'Xfile.bas')
split Xfile.bas
call assert_equal('freebasic', &filetype)
bwipe!
" QB64
call writefile(['$LET TESTING = 1'], 'Xfile.bas')
split Xfile.bas
call assert_equal('qb64', &filetype)
bwipe!
call writefile(['OPTION _EXPLICIT'], 'Xfile.bas')
split Xfile.bas
call assert_equal('qb64', &filetype)
bwipe!
" Visual Basic
call writefile(['Attribute VB_NAME = "Testing"'], 'Xfile.bas')
split Xfile.bas
call assert_equal('vb', &filetype)
bwipe!
call delete('Xfile.bas')
filetype off
endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
4172,
/**/
4171,
/**/