]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1478: filetypes for *.v files not detected properly v9.0.1478
authorTuriiya <34311583+tobealive@users.noreply.github.com>
Sat, 22 Apr 2023 20:38:47 +0000 (21:38 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 22 Apr 2023 20:38:47 +0000 (21:38 +0100)
Problem:    Filetypes for *.v files not detected properly.
Solution:   Use the file contents to detect the filetype. (Turiiya,
            closes #12281)

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

index 5d2053d70a4249ccfc90ac556e190d58b2ab7ae4..4e0906d8b4747a0bc8691d9444a45e91c62736c8 100644 (file)
@@ -1106,5 +1106,40 @@ export def FTlsl()
   endif
 enddef
 
+# Set the filetype of a *.v file to Verilog, V or Cog based on the first 200
+# lines.
+export def FTv()
+  if did_filetype()
+    # ":setf" will do nothing, bail out early
+    return
+  endif
+
+  for line in getline(1, 200)
+    if line[0] =~ '^\s*/'
+      # skip comment line
+      continue
+    endif
+
+    # Verilog: line ends with ';' followed by an optional variable number of
+    # spaces and an optional start of a comment.
+    # Example: " b <= a + 1; // Add 1".
+    if line =~ ';\(\s*\)\?\(/.*\)\?$'
+      setf verilog
+      return
+    endif
+
+    # Coq: line ends with a '.' followed by an optional variable number of
+    # spaces and an optional start of a comment.
+    # Example: "Definition x := 10. (*".
+    if line =~ '\.\(\s*\)\?\((\*.*\)\?$'
+      setf coq
+      return
+    endif
+  endfor
+
+  # No line matched, fall back to "v".
+  setf v
+enddef
+
 # Uncomment this line to check for compilation errors early
 # defcompile
index 50187f6db0ccfc0bf57cce2a2d47146874e9d2c5..538ddd8d644dbf8e889e7c80a7a87bade5d25cdb 100644 (file)
@@ -2303,8 +2303,8 @@ au BufNewFile,BufRead *.vr,*.vri,*.vrh            setf vera
 " Vagrant (uses Ruby syntax)
 au BufNewFile,BufRead Vagrantfile              setf ruby
 
-" Verilog HDL
-au BufNewFile,BufRead *.v                      setf verilog
+" Verilog HDL, V or Coq
+au BufNewFile,BufRead *.v                      call dist#ft#FTv()
 
 " Verilog-AMS HDL
 au BufNewFile,BufRead *.va,*.vams              setf verilogams
index f399f1b2424456c92d9a7cfc0299913ef5762728..85187707dcf8893ef78534422348fe4bea3700b4 100644 (file)
@@ -646,7 +646,6 @@ let s:filename_checks = {
     \ 'vdmrt': ['file.vdmrt'],
     \ 'vdmsl': ['file.vdm', 'file.vdmsl'],
     \ 'vera': ['file.vr', 'file.vri', 'file.vrh'],
-    \ 'verilog': ['file.v'],
     \ 'verilogams': ['file.va', 'file.vams'],
     \ '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'],
@@ -1771,6 +1770,27 @@ func Test_ttl_file()
   filetype off
 endfunc
 
+func Test_v_file()
+  filetype on
+
+  call writefile(['module tb; // Looks like a Verilog'], 'Xfile.v', 'D')
+  split Xfile.v
+  call assert_equal('verilog', &filetype)
+  bwipe!
+
+  call writefile(['module main'], 'Xfile.v')
+  split Xfile.v
+  call assert_equal('v', &filetype)
+  bwipe!
+
+  call writefile(['Definition x := 10.  (*'], 'Xfile.v')
+  split Xfile.v
+  call assert_equal('coq', &filetype)
+  bwipe!
+
+  filetype off
+endfunc
+
 func Test_xpm_file()
   filetype on
 
index c94a503c57cfe2998c5b1896bbe7c4ede8215046..0df443603f1251a22f4c6f6bb9caa142be42f641 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1478,
 /**/
     1477,
 /**/