Skip to content

Commit

Permalink
runtime(dist/ft): improve filetype detection for *.v (V/Verilog/Coq)
Browse files Browse the repository at this point in the history
Patch provided by Dan Alt

closes: #13793

Signed-off-by: Christian Brabandt <cb@256bit.org>
  • Loading branch information
chrisbra committed Jan 1, 2024
1 parent b16fc98 commit 10b4f75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
42 changes: 31 additions & 11 deletions runtime/autoload/dist/ft.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1186,26 +1186,46 @@ export def FTv()
# ":setf" will do nothing, bail out early
return
endif
if exists("g:filetype_v")
exe "setf " .. g:filetype_v
return
endif

for line in getline(1, 200)
if line[0] =~ '^\s*/'
var in_comment = 0
for lnum in range(1, min([line("$"), 200]))
var line = getline(lnum)
# Skip Verilog and V comments (lines and blocks).
if line =~ '^\s*/\*'
# start comment block
in_comment = 1
endif
if in_comment == 1
if line =~ '\*/'
# end comment block
in_comment = 0
endif
# skip comment-block line
continue
endif
if line =~ '^\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
# Coq: line ends with a '.' followed by an optional variable number of
# spaces or contains the start of a comment, but not inside a Verilog or V
# comment.
# Example: "Definition x := 10. (*".
if (line =~ '\.\s*$' && line !~ '/[/*]') || (line =~ '(\*' && line !~ '/[/*].*(\*')
setf coq
return
endif

# Coq: line ends with a '.' followed by an optional variable number of
# Verilog: line ends with ';' followed by an optional variable number of
# spaces and an optional start of a comment.
# Example: "Definition x := 10. (*".
if line =~ '\.\(\s*\)\?\((\*.*\)\?$'
setf coq
# Example: " b <= a + 1; // Add 1".
if line =~ ';\s*\(/[/*].*\)\?$'
setf verilog
return
endif
endfor
Expand Down
3 changes: 2 additions & 1 deletion runtime/doc/filetype.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*filetype.txt* For Vim version 9.0. Last change: 2023 Dec 23
*filetype.txt* For Vim version 9.0. Last change: 2024 Jan 01


VIM REFERENCE MANUAL by Bram Moolenaar
Expand Down Expand Up @@ -169,6 +169,7 @@ variables can be used to overrule the filetype used for certain extensions:
*.sh g:bash_is_sh |ft-sh-syntax|
*.tex g:tex_flavor |ft-tex-plugin|
*.typ g:filetype_typ
*.v g:filetype_v
*.w g:filetype_w |ft-cweb-syntax|

For a few filetypes the global variable is used only when the filetype could
Expand Down

0 comments on commit 10b4f75

Please sign in to comment.