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