systemverilog.vim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. " Vim indent file
  2. " Language: SystemVerilog
  3. " Maintainer: kocha <kocha.lsifrontend@gmail.com>
  4. " Last Change: 05-Feb-2017 by Bilal Wasim
  5. " 03-Aug-2022 Improved indent
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=SystemVerilogIndent()
  12. setlocal indentkeys=!^F,o,O,0),0},=begin,=end,=join,=endcase,=join_any,=join_none
  13. setlocal indentkeys+==endmodule,=endfunction,=endtask,=endspecify
  14. setlocal indentkeys+==endclass,=endpackage,=endsequence,=endclocking
  15. setlocal indentkeys+==endinterface,=endgroup,=endprogram,=endproperty,=endchecker
  16. setlocal indentkeys+==`else,=`elsif,=`endif
  17. let b:undo_indent = "setl inde< indk<"
  18. " Only define the function once.
  19. if exists("*SystemVerilogIndent")
  20. finish
  21. endif
  22. let s:cpo_save = &cpo
  23. set cpo&vim
  24. let s:multiple_comment = 0
  25. let s:open_statement = 0
  26. function SystemVerilogIndent()
  27. if exists('b:systemverilog_indent_width')
  28. let offset = b:systemverilog_indent_width
  29. else
  30. let offset = shiftwidth()
  31. endif
  32. if exists('b:systemverilog_indent_modules')
  33. let indent_modules = offset
  34. else
  35. let indent_modules = 0
  36. endif
  37. if exists('b:systemverilog_indent_ifdef_off')
  38. let indent_ifdef = 0
  39. else
  40. let indent_ifdef = 1
  41. endif
  42. " Find a non-blank line above the current line.
  43. let lnum = prevnonblank(v:lnum - 1)
  44. " At the start of the file use zero indent.
  45. if lnum == 0
  46. return 0
  47. endif
  48. let lnum2 = prevnonblank(lnum - 1)
  49. let curr_line = getline(v:lnum)
  50. let last_line = getline(lnum)
  51. let last_line2 = getline(lnum2)
  52. let ind = indent(lnum)
  53. let ind2 = indent(lnum - 1)
  54. " Define the condition of an open statement
  55. " Exclude the match of //, /* or */
  56. let sv_openstat = '\(\<or\>\|\([*/]\)\@<![*(,{><+-/%^&|!=?:]\([*/]\)\@!\)'
  57. " Define the condition when the statement ends with a one-line comment
  58. let sv_comment = '\(//.*\|/\*.*\*/\s*\)'
  59. if exists('b:systemverilog_indent_verbose')
  60. let vverb_str = 'INDENT VERBOSE: '. v:lnum .":"
  61. let vverb = 1
  62. else
  63. let vverb = 0
  64. endif
  65. " Multiple-line comment count
  66. if curr_line =~ '^\s*/\*' && curr_line !~ '/\*.\{-}\*/'
  67. let s:multiple_comment += 1
  68. if vverb | echom vverb_str "Start of multiple-line commnt" | endif
  69. elseif curr_line =~ '\*/\s*$' && curr_line !~ '/\*.\{-}\*/'
  70. let s:multiple_comment -= 1
  71. if vverb | echom vverb_str "End of multiple-line commnt" | endif
  72. return ind
  73. endif
  74. " Maintain indentation during commenting.
  75. if s:multiple_comment > 0
  76. return ind
  77. endif
  78. " Indent after if/else/for/case/always/initial/specify/fork blocks
  79. if last_line =~ '^\s*\(end\)\=\s*`\@<!\<\(if\|else\)\>' ||
  80. \ last_line =~ '^\s*\<\(for\|while\|repeat\|case\%[[zx]]\|do\|foreach\|forever\|randcase\)\>' ||
  81. \ last_line =~ '^\s*\<\(always\|always_comb\|always_ff\|always_latch\)\>' ||
  82. \ last_line =~ '^\s*\<\(initial\|specify\|fork\|final\)\>'
  83. if last_line !~ '\(;\|\<end\>\|\*/\)\s*' . sv_comment . '*$' ||
  84. \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . sv_comment . '*$'
  85. let ind = ind + offset
  86. if vverb | echom vverb_str "Indent after a block statement." | endif
  87. endif
  88. " Indent after function/task/class/package/sequence/clocking/
  89. " interface/covergroup/property/checkerprogram blocks
  90. elseif last_line =~ '^\s*\<\(function\|task\|class\|package\)\>' ||
  91. \ last_line =~ '^\s*\<\(sequence\|clocking\|interface\)\>' ||
  92. \ last_line =~ '^\s*\(\w\+\s*:\)\=\s*\<covergroup\>' ||
  93. \ last_line =~ '^\s*\<\(property\|checker\|program\)\>' ||
  94. \ ( last_line =~ '^\s*\<virtual\>' && last_line =~ '\<\(function\|task\|class\|interface\)\>' ) ||
  95. \ ( last_line =~ '^\s*\<pure\>' && last_line =~ '\<virtual\>' && last_line =~ '\<\(function\|task\)\>' )
  96. if last_line !~ '\<end\>\s*' . sv_comment . '*$' ||
  97. \ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . sv_comment . '*$'
  98. let ind = ind + offset
  99. if vverb
  100. echom vverb_str "Indent after function/task/class block statement."
  101. endif
  102. endif
  103. " Indent after module/function/task/specify/fork blocks
  104. elseif last_line =~ '^\s*\(\<extern\>\s*\)\=\<module\>'
  105. let ind = ind + indent_modules
  106. if vverb && indent_modules
  107. echom vverb_str "Indent after module statement."
  108. endif
  109. if last_line =~ '[(,]\s*' . sv_comment . '*$' &&
  110. \ last_line !~ '\(//\|/\*\).*[(,]\s*' . sv_comment . '*$'
  111. let ind = ind + offset
  112. if vverb
  113. echom vverb_str "Indent after a multiple-line module statement."
  114. endif
  115. endif
  116. " Indent after a 'begin' statement
  117. elseif last_line =~ '\(\<begin\>\)\(\s*:\s*\w\+\)*' . sv_comment . '*$' &&
  118. \ last_line !~ '\(//\|/\*\).*\(\<begin\>\)' &&
  119. \ ( last_line2 !~ sv_openstat . '\s*' . sv_comment . '*$' ||
  120. \ last_line2 =~ '^\s*[^=!]\+\s*:\s*' . sv_comment . '*$' )
  121. let ind = ind + offset
  122. if vverb | echom vverb_str "Indent after begin statement." | endif
  123. " Indent after a '{' or a '('
  124. elseif last_line =~ '[{(]' . sv_comment . '*$' &&
  125. \ last_line !~ '\(//\|/\*\).*[{(]' &&
  126. \ ( last_line2 !~ sv_openstat . '\s*' . sv_comment . '*$' ||
  127. \ last_line2 =~ '^\s*[^=!]\+\s*:\s*' . sv_comment . '*$' )
  128. let ind = ind + offset
  129. if vverb | echom vverb_str "Indent after begin statement." | endif
  130. " Ignore de-indent for the end of one-line block
  131. elseif ( last_line !~ '\<begin\>' ||
  132. \ last_line =~ '\(//\|/\*\).*\<begin\>' ) &&
  133. \ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|forever\|final\)\>.*' .
  134. \ sv_comment . '*$' &&
  135. \ last_line2 !~ '\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|forever\|final\)\>' &&
  136. \ last_line2 !~ sv_openstat . '\s*' . sv_comment . '*$' &&
  137. \ ( last_line2 !~ '\<begin\>' ||
  138. \ last_line2 =~ '\(//\|/\*\).*\<begin\>' ) &&
  139. \ last_line2 =~ ')*\s*;\s*' . sv_comment . '*$'
  140. if vverb
  141. echom vverb_str "Ignore de-indent after the end of one-line statement."
  142. endif
  143. " De-indent for the end of one-line block
  144. elseif ( last_line !~ '\<begin\>' ||
  145. \ last_line =~ '\(//\|/\*\).*\<begin\>' ) &&
  146. \ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|forever\|final\)\>.*' .
  147. \ sv_comment . '*$' &&
  148. \ last_line2 !~ '\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|forever\|final\)\>' &&
  149. \ last_line2 !~ sv_openstat . '\s*' . sv_comment . '*$' &&
  150. \ last_line2 !~ '\(;\|\<end\>\|\*/\)\s*' . sv_comment . '*$' &&
  151. \ ( last_line2 !~ '\<begin\>' ||
  152. \ last_line2 =~ '\(//\|/\*\).*\<begin\>' )
  153. let ind = ind - offset
  154. if vverb
  155. echom vverb_str "De-indent after the end of one-line statement."
  156. endif
  157. " Multiple-line statement (including case statement)
  158. " Open statement
  159. " Ident the first open line
  160. elseif last_line =~ sv_openstat . '\s*' . sv_comment . '*$' &&
  161. \ last_line !~ '\(//\|/\*\).*' . sv_openstat . '\s*$' &&
  162. \ last_line2 !~ sv_openstat . '\s*' . sv_comment . '*$'
  163. let ind = ind + offset
  164. let s:open_statement = 1
  165. if vverb | echom vverb_str "Indent after an open statement." | endif
  166. " `ifdef or `ifndef or `elsif or `else
  167. elseif last_line =~ '^\s*`\<\(ifn\?def\|elsif\|else\)\>' && indent_ifdef
  168. let ind = ind + offset
  169. if vverb
  170. echom vverb_str "Indent after a `ifdef or `ifndef or `elsif or `else statement."
  171. endif
  172. endif
  173. " Re-indent current line
  174. " De-indent on the end of the block
  175. " join/end/endcase/endfunction/endtask/endspecify
  176. if curr_line =~ '^\s*\<\(join\|join_any\|join_none\|\|end\|endcase\)\>' ||
  177. \ curr_line =~ '^\s*\<\(endfunction\|endtask\|endspecify\|endclass\)\>' ||
  178. \ curr_line =~ '^\s*\<\(endpackage\|endsequence\|endclocking\|endinterface\)\>' ||
  179. \ curr_line =~ '^\s*\<\(endgroup\|endproperty\|endchecker\|endprogram\)\>'
  180. let ind = ind - offset
  181. if vverb | echom vverb_str "De-indent the end of a block." | endif
  182. if s:open_statement == 1
  183. let ind = ind - offset
  184. let s:open_statement = 0
  185. if vverb | echom vverb_str "De-indent the close statement." | endif
  186. endif
  187. elseif curr_line =~ '^\s*\<endmodule\>'
  188. let ind = ind - indent_modules
  189. if vverb && indent_modules
  190. echom vverb_str "De-indent the end of a module."
  191. endif
  192. " De-indent on a stand-alone 'begin'
  193. elseif curr_line =~ '^\s*\<begin\>'
  194. if last_line !~ '^\s*\<\(function\|task\|specify\|module\|class\|package\)\>' ||
  195. \ last_line !~ '^\s*\<\(sequence\|clocking\|interface\|covergroup\)\>' ||
  196. \ last_line !~ '^\s*\<\(property\|checker\|program\)\>' &&
  197. \ last_line !~ '^\s*\()*\s*;\|)\+\)\s*' . sv_comment . '*$' &&
  198. \ ( last_line =~
  199. \ '\<\(`\@<!if\|`\@<!else\|for\|case\%[[zx]]\|always\|initial\|do\|foreach\|forever\|randcase\|final\)\>' ||
  200. \ last_line =~ ')\s*' . sv_comment . '*$' ||
  201. \ last_line =~ sv_openstat . '\s*' . sv_comment . '*$' )
  202. let ind = ind - offset
  203. if vverb
  204. echom vverb_str "De-indent a stand alone begin statement."
  205. endif
  206. if s:open_statement == 1
  207. let ind = ind - offset
  208. let s:open_statement = 0
  209. if vverb | echom vverb_str "De-indent the close statement." | endif
  210. endif
  211. endif
  212. " " Close statement
  213. " " De-indent for an optional close parenthesis and a semicolon, and only
  214. " " if there exists precedent non-whitespace char
  215. " elseif last_line =~ ')*\s*;\s*' . sv_comment . '*$' &&
  216. " \ last_line !~ '^\s*)*\s*;\s*' . sv_comment . '*$' &&
  217. " \ last_line !~ '\(//\|/\*\).*\S)*\s*;\s*' . sv_comment . '*$' &&
  218. " \ ( last_line2 =~ sv_openstat . '\s*' . sv_comment . '*$' &&
  219. " \ last_line2 !~ ';\s*//.*$') &&
  220. " \ last_line2 !~ '^\s*' . sv_comment . '$'
  221. " let ind = ind - offset
  222. " if vverb | echom vverb_str "De-indent after a close statement." | endif
  223. " " De-indent after the end of multiple-line statement
  224. " elseif curr_line =~ '^\s*)' &&
  225. " \ ( last_line =~ sv_openstat . '\s*' . sv_comment . '*$' ||
  226. " \ last_line !~ sv_openstat . '\s*' . sv_comment . '*$' &&
  227. " \ last_line2 =~ sv_openstat . '\s*' . sv_comment . '*$' )
  228. " let ind = ind - offset
  229. " if vverb
  230. " echom vverb_str "De-indent the end of a multiple statement."
  231. " endif
  232. " De-indent `elsif or `else or `endif
  233. elseif curr_line =~ '^\s*`\<\(elsif\|else\|endif\)\>' && indent_ifdef
  234. let ind = ind - offset
  235. if vverb | echom vverb_str "De-indent `elsif or `else or `endif statement." | endif
  236. if b:systemverilog_open_statement == 1
  237. let ind = ind - offset
  238. let b:systemverilog_open_statement = 0
  239. if vverb | echom vverb_str "De-indent the open statement." | endif
  240. endif
  241. endif
  242. " Return the indentation
  243. return ind
  244. endfunction
  245. let &cpo = s:cpo_save
  246. unlet s:cpo_save
  247. " vim:sw=2