]> granicus.if.org Git - vim/commitdiff
patch 8.2.4172: filetype detection for BASIC is not optimal v8.2.4172
authorBram Moolenaar <Bram@vim.org>
Fri, 21 Jan 2022 14:55:13 +0000 (14:55 +0000)
committerBram Moolenaar <Bram@vim.org>
Fri, 21 Jan 2022 14:55:13 +0000 (14:55 +0000)
Problem:    Filetype detection for BASIC is not optimal.
Solution:   Improve BASIC filetype detection. (Doug Kearns)

runtime/autoload/dist/ft.vim
runtime/filetype.vim
src/testdir/test_filetype.vim
src/version.c

index 0d6841a35bd06ab0295601761c199a923f896fb0..69712046a5445d7da4cbb89408de2606be53d34d 100644 (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
 
index 3b6129585929426268b6fc8b06ac5cf80a7f4c40..acb87ff93415ce9051e4a96972c6a63efae16cc3 100644 (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
index 755eafcd224b20e955626230e011e1f3cf228df5..ff2af5fb3848b4c3cc87822e32e34e35d0e2ebcc 100644 (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
index 07c28aee203b6a128bdb5b9e206f206e6a903308..45ae6d58f36a6c4b6b22c812821ad24bc7b411af 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4172,
 /**/
     4171,
 /**/