patch 8.2.4064: foam files are not detected v8.2.4064
authorElwardi <elwardifadeli@gmail.com>
Tue, 11 Jan 2022 18:14:23 +0000 (18:14 +0000)
committerBram Moolenaar <Bram@vim.org>
Tue, 11 Jan 2022 18:14:23 +0000 (18:14 +0000)
Problem:    Foam files are not detected.
Solution:   Detect the foam filetype by the path and file contents. (Mohammed
            Elwardi Fadeli, closes #9501)

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

index 9933ace8c24bdab09f813d5ff7a558c74fb4e85b..342731b272e1e5cad77fe5a0975591a855e50daf 100644 (file)
@@ -829,6 +829,23 @@ func dist#ft#Dep3patch()
   endfor
 endfunc
 
+" This function checks the first 15 lines for appearance of 'FoamFile'
+" and then 'object' in a following line.
+" In that case, it's probably an OpenFOAM file
+func dist#ft#FTfoam()
+    let ffile = 0
+    let lnum = 1
+    while lnum <= 15
+      if getline(lnum) =~# '^FoamFile'
+       let ffile = 1
+      elseif ffile == 1 && getline(lnum) =~# '^\s*object'
+       setf foam
+       return
+      endif
+      let lnum = lnum + 1
+    endwhile
+endfunc
+
 " Restore 'cpoptions'
 let &cpo = s:cpo_save
 unlet s:cpo_save
index 09abccf7da837ec01eb0ae4b6257b2e48b5b5135..2a09c679d8f21785d139afb8de4368526fe8139a 100644 (file)
@@ -1214,6 +1214,9 @@ au BufNewFile,BufRead *.xom,*.xin         setf omnimark
 " OPAM
 au BufNewFile,BufRead opam,*.opam,*.opam.template setf opam
 
+" OpenFOAM
+au BufNewFile,BufRead [a-zA-Z0-9]*Dict\(.*\)\=,[a-zA-Z]*Properties\(.*\)\=,*Transport\(.*\),fvSchemes,fvSolution,fvConstrains,fvModels,*/constant/g,*/0\(\.orig\)\=/* call dist#ft#FTfoam()
+
 " OpenROAD
 au BufNewFile,BufRead *.or                     setf openroad
 
index b86f33dfd553a20741ed5652c363b8f86a24a06b..e7c0645c0c34989dd8facd58978bd5cb3df69840 100644 (file)
@@ -1121,4 +1121,54 @@ func Test_git_file()
   filetype off
 endfunc
 
+func Test_foam_file()
+  filetype on
+  call assert_true(mkdir('0', 'p'))
+  call assert_true(mkdir('0.orig', 'p'))
+
+  call writefile(['FoamFile {', '    object something;'], 'Xfile1Dict')
+  split Xfile1Dict
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], 'Xfile1Dict.something')
+  split Xfile1Dict.something
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], 'XfileProperties')
+  split XfileProperties
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], 'XfileProperties.something')
+  split XfileProperties.something
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], 'XfileProperties')
+  split XfileProperties
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], 'XfileProperties.something')
+  split XfileProperties.something
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], '0/Xfile')
+  split 0/Xfile
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call writefile(['FoamFile {', '    object something;'], '0.orig/Xfile')
+  split 0.orig/Xfile
+  call assert_equal('foam', &filetype)
+  bwipe!
+
+  call delete('0', 'rf')
+  call delete('0.orig', 'rf')
+  filetype off
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index 731a6543f1841af55ae348fdd94da7cba3e99cf1..5f28da33d9c06f7c6c5f74e345c38f3f46099b78 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4064,
 /**/
     4063,
 /**/