Skip to content

Commit

Permalink
patch 8.2.4172: filetype detection for BASIC is not optimal
Browse files Browse the repository at this point in the history
Problem:    Filetype detection for BASIC is not optimal.
Solution:   Improve BASIC filetype detection. (Doug Kearns)
  • Loading branch information
brammool committed Jan 21, 2022
1 parent f0e7e63 commit 6517f14
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
26 changes: 21 additions & 5 deletions runtime/autoload/dist/ft.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions runtime/filetype.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
64 changes: 63 additions & 1 deletion src/testdir/test_filetype.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
4172,
/**/
4171,
/**/
Expand Down

0 comments on commit 6517f14

Please sign in to comment.