Skip to content

Commit 10b4f75

Browse files
committed
runtime(dist/ft): improve filetype detection for *.v (V/Verilog/Coq)
Patch provided by Dan Alt closes: #13793 Signed-off-by: Christian Brabandt <[email protected]>
1 parent b16fc98 commit 10b4f75

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

runtime/autoload/dist/ft.vim

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,26 +1186,46 @@ export def FTv()
11861186
# ":setf" will do nothing, bail out early
11871187
return
11881188
endif
1189+
if exists("g:filetype_v")
1190+
exe "setf " .. g:filetype_v
1191+
return
1192+
endif
11891193

1190-
for line in getline(1, 200)
1191-
if line[0] =~ '^\s*/'
1194+
var in_comment = 0
1195+
for lnum in range(1, min([line("$"), 200]))
1196+
var line = getline(lnum)
1197+
# Skip Verilog and V comments (lines and blocks).
1198+
if line =~ '^\s*/\*'
1199+
# start comment block
1200+
in_comment = 1
1201+
endif
1202+
if in_comment == 1
1203+
if line =~ '\*/'
1204+
# end comment block
1205+
in_comment = 0
1206+
endif
1207+
# skip comment-block line
1208+
continue
1209+
endif
1210+
if line =~ '^\s*//'
11921211
# skip comment line
11931212
continue
11941213
endif
11951214

1196-
# Verilog: line ends with ';' followed by an optional variable number of
1197-
# spaces and an optional start of a comment.
1198-
# Example: " b <= a + 1; // Add 1".
1199-
if line =~ ';\(\s*\)\?\(/.*\)\?$'
1200-
setf verilog
1215+
# Coq: line ends with a '.' followed by an optional variable number of
1216+
# spaces or contains the start of a comment, but not inside a Verilog or V
1217+
# comment.
1218+
# Example: "Definition x := 10. (*".
1219+
if (line =~ '\.\s*$' && line !~ '/[/*]') || (line =~ '(\*' && line !~ '/[/*].*(\*')
1220+
setf coq
12011221
return
12021222
endif
12031223

1204-
# Coq: line ends with a '.' followed by an optional variable number of
1224+
# Verilog: line ends with ';' followed by an optional variable number of
12051225
# spaces and an optional start of a comment.
1206-
# Example: "Definition x := 10. (*".
1207-
if line =~ '\.\(\s*\)\?\((\*.*\)\?$'
1208-
setf coq
1226+
# Example: " b <= a + 1; // Add 1".
1227+
if line =~ ';\s*\(/[/*].*\)\?$'
1228+
setf verilog
12091229
return
12101230
endif
12111231
endfor

runtime/doc/filetype.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*filetype.txt* For Vim version 9.0. Last change: 2023 Dec 23
1+
*filetype.txt* For Vim version 9.0. Last change: 2024 Jan 01
22

33

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

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

0 commit comments

Comments
 (0)