Skip to content

Commit 437daa4

Browse files
committed
Keep comments and empty lines
1 parent 36981bd commit 437daa4

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

plugin/verilog_instance.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
import sys
99

1010
skip_last_coma = 0
11+
keep_comment = 1
12+
keep_empty_line = 1
1113
if len(sys.argv) > 1:
1214
skip_last_coma = int(sys.argv[1])
1315

16+
if len(sys.argv) > 2:
17+
keep_comment = int(sys.argv[2])
18+
1419
keywords = []
1520
keywords.extend(["input", "output", "inout", "ref", "parameter", "localparam"])
1621
keywords.extend(["reg", "logic", "wire", "bit", "integer", "int", "string", "type"])
@@ -19,7 +24,7 @@
1924
patterns = []
2025
patterns.append(re.compile(r'\[[^\[\]]*\]')) # port size, array size
2126
patterns.append(re.compile(r'=.*')) # assignment
22-
patterns.append(re.compile(r'//.*')) # // comment
27+
patterns.append(re.compile(r'//.*') )
2328
patterns.append(re.compile(r'\w+\.\w+')) # interfaces with modport
2429
for kw in keywords: # match keywords
2530
patterns.append(re.compile("\\b%s\\b" % kw))
@@ -32,7 +37,12 @@
3237
pattern_two_words_no_coma = re.compile(r'^\s*(\w+)\s+(\w+.*)')
3338
pattern_spaces = re.compile(r'\s+')
3439

40+
pattern_inline_comment_kept = re.compile(r'.*\w+.*(//.*)') # comment in port define
41+
pattern_comment_kept = re.compile(r'\s*(//.*)') # one line comment
42+
3543
ports = []
44+
ports_comments = {} # save comment for every port
45+
contents = [] # save ports and single line comments
3646
wait_to_close_comment = 0
3747
indent_len = -1
3848

@@ -53,6 +63,14 @@
5363
else:
5464
wait_to_close_comment = 1
5565
continue
66+
# handle port comment
67+
port_comment = pattern_inline_comment_kept.match(line)
68+
if port_comment is not None:
69+
port_comment = port_comment.group(1)
70+
# handle single line comment
71+
line_comment = pattern_comment_kept.match(line)
72+
if line_comment is not None:
73+
line_comment = line_comment.group(1)
5674
# handle all other patterns
5775
for pattern in patterns:
5876
line = pattern.sub(' ', line)
@@ -63,16 +81,41 @@
6381
# finally, get port names
6482
line = line.strip()
6583
if line != "":
66-
ports.extend(line.split(' '))
84+
port_names = line.split(' ')
85+
ports.extend(port_names)
86+
contents.extend(port_names)
87+
for port in port_names:
88+
ports_comments[port] = port_comment
89+
else:
90+
# add single line comment to port
91+
if line_comment is not None:
92+
contents.append(line_comment)
6793

6894
ports_nb = len(ports)
6995
i = 0
7096
if ports_nb > 0:
7197
max_str_len = len(max(ports, key=len))
72-
for port in ports:
98+
indent_str = " " * indent_len
99+
for content in contents:
100+
if len(content) > 0:
101+
if content[:2] == "//":
102+
if keep_comment == 1:
103+
print(f'{indent_str}{content}')
104+
continue
105+
else:
106+
# empty line
107+
if keep_empty_line == 1:
108+
print('')
109+
continue
110+
port = content
73111
skip_coma = skip_last_coma and i == (ports_nb - 1)
74112
space_str = " " * (max_str_len - len(port))
75-
indent_str = " " * indent_len
76-
print("%s.%s%s (%s%s)%s" % (
77-
indent_str, port, space_str, port, space_str, (",", "")[skip_coma]))
113+
output_line_port = "%s.%s%s (%s%s)%s" % (
114+
indent_str, port, space_str, port, space_str, (",", "")[skip_coma])
115+
if ports_comments.get(port) is not None and keep_comment == 1:
116+
# add port comment
117+
output_line = f"{output_line_port} {ports_comments.get(port)}"
118+
else:
119+
output_line = output_line_port
120+
print(output_line)
78121
i = i + 1

plugin/verilog_instance.vim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ if !get(g:, 'verilog_instance_skip_last_coma')
1212
let g:verilog_instance_skip_last_coma = 0
1313
endif
1414

15+
if !get(g:, 'verilog_instance_keep_comments')
16+
let g:verilog_instance_keep_comments = 0
17+
endif
18+
19+
if !get(g:, 'verilog_instance_keep_empty_lines')
20+
let g:verilog_instance_keep_empty_lines = 0
21+
endif
22+
1523
function! s:VerilogInstance(type,...) abort
1624
if a:0
1725
let [lnum1, lnum2] = [a:type, a:1]
@@ -20,7 +28,7 @@ function! s:VerilogInstance(type,...) abort
2028
endif
2129
let cmd = lnum1 . "norm! =="
2230
execute cmd
23-
let cmd = lnum1 . "," . lnum2 . "!" . " " . s:plugin_dir_path . "/verilog_instance.py " . g:verilog_instance_skip_last_coma
31+
let cmd = lnum1 . "," . lnum2 . "!" . " " . s:plugin_dir_path . "/verilog_instance.py " . g:verilog_instance_skip_last_coma . g:verilog_instance_keep_comments . g:verilog_instance_keep_empty_lines
2432
execute cmd
2533
endfunction
2634

test.sv

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module sub_block(
2-
input clk,
2+
input clk, // 50 MHz clk
33
input /* foo */ rstn,
44
/* interface
55
* network_if.IN i0, i1,
66
* network_if.OUT o0, o1
77
*/
8-
fifo_if_.IN i0, i1,
8+
fifo_if_.IN i0, i1, // fifo in
99
fifo_if_.OUT o0, o1,
10+
1011
input custom_t data_in,
12+
// output
1113
output reg[31:0] /*comment*/ reg32_out,
1214
output custom_t data_out
1315
);

0 commit comments

Comments
 (0)