Skip to content

Commit

Permalink
util/vim: Fix indentation for interfaces and if guards
Browse files Browse the repository at this point in the history
This commit fixes two issues with the vim indentor.

 * Indentation of methods in interfaces:

```bsv
interface SomeInterface;
    method Action someMethod();
    // Previously this would indent the next method once more
    method Action someOtherMethod();
endinterface : SomeInterface
```

 * If guards on methods being treated as if statements, therefore only
   indenting the next statement

```bsv
method Action someMethod() if (ready);
    a <= 0;
    // Previously this next line would not be indented at all.
    b <= 0;
endmethod
```
  • Loading branch information
nea89o authored and quark17 committed May 28, 2023
1 parent f00bb4a commit 4dbb027
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions util/vim/indent/bsv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ function GetBSVIndent()
echo vverb_str "De-indent after a multiple-line comment."
endif

" Indent after if/else/for/case/always/initial/specify/fork blocks
elseif last_line =~ '`\@<!\<\(if\|else\)\>' ||
\ last_line =~ '^\s*\<\(for\|case\%[[zx]]\|do\|foreach\|randcase\)\>' ||
\ last_line =~ '^\s*\<\(always\|always_comb\|always_ff\|always_latch\)\>' ||
\ last_line =~ '^\s*\<\(initial\|specify\|fork\|final\)\>'
if last_line !~ '\(;\|\<end\>\)\s*' . vlog_comment . '*$' ||
\ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
let ind = ind + offset
if vverb | echo vverb_str "Indent after a block statement." | endif
endif
" Indent after function/task/class/package/sequence/clocking/
" rule/method/interface/covergroup/property/program blocks
elseif last_line =~ '^\s*\<\(function\|task\|class\|package\)\>' ||
Expand All @@ -100,12 +90,48 @@ function GetBSVIndent()
\ last_line =~ '^\s*\<\(property\|program\)\>'
if last_line !~ '\<end\>\s*' . vlog_comment . '*$' ||
\ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
let ind = ind + offset
if vverb
echo vverb_str "Indent after function/task/class block statement."

let is_method = last_line =~ '^\s*\<method\>'
let is_interface = last_line =~ '^\s*\<interface\>'
let scopenum = lnum - 1
let in_module = 0
let interface_depth = 0
let found_end_interface = 0
let found_interface = 0
while scopenum >= 0 && (is_method || is_interface)
let scopeline = getline(scopenum)
if scopeline =~ '^\s*interface\>' && !found_end_interface
let found_interface = 1
elseif scopeline =~ '^\s*endinterface\>'
let found_end_interface = 1
elseif scopeline =~ '^\s*module\>'
let in_module = 1
break
elseif scopeline =~ '^\s*endmodule\>'
break
endif
let scopenum = scopenum - 1
endwhile

if (is_method && in_module) || (is_interface && !found_interface) || (!is_method && !is_interface)
let ind = ind + offset
if vverb
echo vverb_str "Indent after function/task/class block statement."
endif
endif
endif

" Indent after if/else/for/case/always/initial/specify/fork blocks
elseif last_line =~ '`\@<!\<\(if\|else\)\>' ||
\ last_line =~ '^\s*\<\(for\|case\%[[zx]]\|do\|foreach\|randcase\)\>' ||
\ last_line =~ '^\s*\<\(always\|always_comb\|always_ff\|always_latch\)\>' ||
\ last_line =~ '^\s*\<\(initial\|specify\|fork\|final\)\>'
if last_line !~ '\(;\|\<end\>\)\s*' . vlog_comment . '*$' ||
\ last_line =~ '\(//\|/\*\).*\(;\|\<end\>\)\s*' . vlog_comment . '*$'
let ind = ind + offset
if vverb | echo vverb_str "Indent after a block statement." | endif
endif

" Indent after module/function/task/specify/fork blocks
elseif last_line =~ '^\s*\(\<extern\>\s*\)\=\<module\>'
let ind = ind + indent_modules
Expand Down Expand Up @@ -139,11 +165,9 @@ function GetBSVIndent()
" De-indent for the end of one-line block
elseif ( last_line !~ '\<begin\>' ||
\ last_line =~ '\(//\|/\*\).*\<begin\>' ) &&
\ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|final\)\>.*' .
\ vlog_comment . '*$' &&
\ last_line2 !~
\
'\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|final\)\>' &&
\ last_line2 =~ '\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|final\)\>[^;]*' .
\ vlog_comment . '*$' &&
\ last_line2 !~ '\(//\|/\*\).*\<\(`\@<!if\|`\@<!else\|for\|always\|initial\|do\|foreach\|final\)\>' &&
\ last_line2 !~ vlog_openstat . '\s*' . vlog_comment . '*$' &&
\ ( last_line2 !~ '\<begin\>' ||
\ last_line2 =~ '\(//\|/\*\).*\<begin\>' )
Expand Down

0 comments on commit 4dbb027

Please sign in to comment.