Skip to content

Commit 939b2a3

Browse files
authored
Merge pull request #2474 from taichi-ishitani/remove_trailing_comma
Add multi-line formatting support for generic parameter and argument lists
2 parents 4f4026e + 897044e commit 939b2a3

6 files changed

Lines changed: 177 additions & 17 deletions

File tree

crates/formatter/src/formatter.rs

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,30 +2105,105 @@ impl VerylWalker for Formatter {
21052105
}
21062106
}
21072107

2108+
/// Semantic action for non-terminal 'WithGenericParameter'
2109+
fn with_generic_parameter(&mut self, arg: &WithGenericParameter) {
2110+
if arg.colon_colon_l_angle.line() != arg.r_angle.line() {
2111+
self.multi_line_start();
2112+
}
2113+
self.colon_colon_l_angle(&arg.colon_colon_l_angle);
2114+
if self.multi_line() {
2115+
self.newline_push();
2116+
}
2117+
2118+
self.with_generic_parameter_list(&arg.with_generic_parameter_list);
2119+
2120+
if self.multi_line() {
2121+
self.newline_pop();
2122+
self.align_reset();
2123+
}
2124+
self.r_angle(&arg.r_angle);
2125+
if arg.colon_colon_l_angle.line() != arg.r_angle.line() {
2126+
self.multi_line_finish();
2127+
}
2128+
}
2129+
21082130
/// Semantic action for non-terminal 'WithGenericParameterList'
21092131
fn with_generic_parameter_list(&mut self, arg: &WithGenericParameterList) {
21102132
self.with_generic_parameter_item(&arg.with_generic_parameter_item);
21112133
for x in &arg.with_generic_parameter_list_list {
21122134
self.comma(&x.comma);
2113-
self.space(1);
2135+
if self.multi_line() {
2136+
self.newline();
2137+
} else {
2138+
self.space(1);
2139+
}
21142140
self.with_generic_parameter_item(&x.with_generic_parameter_item);
21152141
}
2116-
if let Some(ref x) = arg.with_generic_parameter_list_opt {
2117-
self.comma(&x.comma);
2142+
if self.multi_line() {
2143+
if let Some(ref x) = arg.with_generic_parameter_list_opt {
2144+
self.comma(&x.comma);
2145+
} else {
2146+
self.str(",");
2147+
}
21182148
}
21192149
}
21202150

21212151
/// Semantic action for non-terminal 'WithGenericParameterItem'
21222152
fn with_generic_parameter_item(&mut self, arg: &WithGenericParameterItem) {
2153+
if self.multi_line() {
2154+
self.align_start(align_kind::IDENTIFIER);
2155+
}
21232156
self.identifier(&arg.identifier);
2157+
if self.multi_line() {
2158+
self.align_finish(align_kind::IDENTIFIER);
2159+
}
21242160
self.colon(&arg.colon);
21252161
self.space(1);
2162+
if self.multi_line() {
2163+
self.align_start(align_kind::TYPE);
2164+
}
21262165
self.generic_bound(&arg.generic_bound);
2166+
if self.multi_line() {
2167+
self.align_finish(align_kind::TYPE);
2168+
}
21272169
if let Some(ref x) = arg.with_generic_parameter_item_opt {
21282170
self.space(1);
21292171
self.equ(&x.equ);
21302172
self.space(1);
2173+
if self.multi_line() {
2174+
self.align_start(align_kind::EXPRESSION);
2175+
}
21312176
self.with_generic_argument_item(&x.with_generic_argument_item);
2177+
if self.multi_line() {
2178+
self.align_finish(align_kind::EXPRESSION);
2179+
}
2180+
}
2181+
}
2182+
2183+
/// Semantic action for non-terminal 'WithGenericArgument'
2184+
fn with_generic_argument(&mut self, arg: &WithGenericArgument) {
2185+
let multi_line = arg.with_generic_argument_opt.is_some()
2186+
&& arg.colon_colon_l_angle.line() != arg.r_angle.line();
2187+
2188+
if multi_line {
2189+
self.multi_line_start();
2190+
}
2191+
self.colon_colon_l_angle(&arg.colon_colon_l_angle);
2192+
if self.multi_line() {
2193+
self.newline_push();
2194+
}
2195+
2196+
if let Some(x) = &arg.with_generic_argument_opt {
2197+
self.with_generic_argument_list(&x.with_generic_argument_list);
2198+
}
2199+
2200+
if self.multi_line() {
2201+
self.newline_pop();
2202+
self.align_reset();
2203+
}
2204+
self.r_angle(&arg.r_angle);
2205+
if multi_line {
2206+
self.multi_line_finish();
21322207
}
21332208
}
21342209

@@ -2137,11 +2212,43 @@ impl VerylWalker for Formatter {
21372212
self.with_generic_argument_item(&arg.with_generic_argument_item);
21382213
for x in &arg.with_generic_argument_list_list {
21392214
self.comma(&x.comma);
2140-
self.space(1);
2215+
if self.multi_line() {
2216+
self.newline();
2217+
} else {
2218+
self.space(1);
2219+
}
21412220
self.with_generic_argument_item(&x.with_generic_argument_item);
21422221
}
2143-
if let Some(ref x) = arg.with_generic_argument_list_opt {
2144-
self.comma(&x.comma);
2222+
if self.multi_line() {
2223+
if let Some(ref x) = arg.with_generic_argument_list_opt {
2224+
self.comma(&x.comma);
2225+
} else {
2226+
self.str(",");
2227+
}
2228+
}
2229+
}
2230+
2231+
/// Semantic action for non-terminal 'WithGenericArgumentItem'
2232+
fn with_generic_argument_item(&mut self, arg: &WithGenericArgumentItem) {
2233+
if self.multi_line() {
2234+
self.align_start(align_kind::EXPRESSION);
2235+
}
2236+
match arg {
2237+
WithGenericArgumentItem::GenericArgIdentifier(x) => {
2238+
self.generic_arg_identifier(&x.generic_arg_identifier);
2239+
}
2240+
WithGenericArgumentItem::FixedType(x) => {
2241+
self.fixed_type(&x.fixed_type);
2242+
}
2243+
WithGenericArgumentItem::Number(x) => {
2244+
self.number(&x.number);
2245+
}
2246+
WithGenericArgumentItem::BooleanLiteral(x) => {
2247+
self.boolean_literal(&x.boolean_literal);
2248+
}
2249+
}
2250+
if self.multi_line() {
2251+
self.align_finish(align_kind::EXPRESSION);
21452252
}
21462253
}
21472254

crates/formatter/src/tests.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,56 @@ fn const_above_let_alignment() {
211211
let ret = format(&metadata, code);
212212
assert_eq!(ret, expect);
213213
}
214+
215+
#[test]
216+
fn format_generic_list() {
217+
let metadata = Metadata::create_default("prj").unwrap();
218+
219+
let code = r#"module ModuleA::<A : a_type, AA: u32,> {}
220+
"#;
221+
222+
let expect = r#"module ModuleA::<A: a_type, AA: u32> {}
223+
"#;
224+
225+
let ret = format(&metadata, &code);
226+
assert_eq!(ret, expect);
227+
228+
let code = r#"module ModuleA::<
229+
A: a_type,
230+
AA: u32
231+
> {}
232+
"#;
233+
234+
let expect = r#"module ModuleA::<
235+
A : a_type,
236+
AA: u32 ,
237+
> {}
238+
"#;
239+
240+
let ret = format(&metadata, &code);
241+
assert_eq!(ret, expect);
242+
243+
let code = r#"alias module ModuleA = ModuleB::<8, 16,>;
244+
"#;
245+
246+
let expect = r#"alias module ModuleA = ModuleB::<8, 16>;
247+
"#;
248+
249+
let ret = format(&metadata, &code);
250+
assert_eq!(ret, expect);
251+
252+
let code = r#"alias module ModuleB = ModuleC::<
253+
8,
254+
16
255+
>;
256+
"#;
257+
258+
let expect = r#"alias module ModuleB = ModuleC::<
259+
8 ,
260+
16,
261+
>;
262+
"#;
263+
264+
let ret = format(&metadata, &code);
265+
assert_eq!(ret, expect);
266+
}

crates/std/veryl/src/selector/demux.veryl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub function dispatch_binary::<N: p32, T: type,> (
1+
pub function dispatch_binary::<N: p32, T: type> (
22
sel : input logic<selector_pkg::calc_binary_select_width(N)>,
33
data : input T ,
44
default_data: input T ,
@@ -13,7 +13,7 @@ pub function dispatch_binary::<N: p32, T: type,> (
1313
return result;
1414
}
1515

16-
pub function dispatch_vector::<N: p32, T: type,> (
16+
pub function dispatch_vector::<N: p32, T: type> (
1717
sel : input logic<N>,
1818
data : input T ,
1919
default_data: input T ,
@@ -31,7 +31,7 @@ pub function dispatch_vector::<N: p32, T: type,> (
3131
return result;
3232
}
3333

34-
pub function dispatch::<KIND: selector_pkg::selector_kind, N: p32, T: type,> (
34+
pub function dispatch::<KIND: selector_pkg::selector_kind, N: p32, T: type> (
3535
sel : input logic<selector_pkg::calc_select_width(N, KIND)>,
3636
data : input T ,
3737
default_data: input T ,

crates/std/veryl/src/selector/mux.veryl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
pub function select_binary::<N: p32, T: type,> (
1+
pub function select_binary::<N: p32, T: type> (
22
sel : input logic<selector_pkg::calc_binary_select_width(N)>,
33
data: input T <N> ,
44
) -> T {
55
return data[sel];
66
}
77

8-
pub function select_vector::<N: p32, T: type,> (
8+
pub function select_vector::<N: p32, T: type> (
99
sel : input logic<N>,
1010
data: input T <N>,
1111
) -> T {
@@ -49,7 +49,7 @@ pub function select_vector::<N: p32, T: type,> (
4949
return next_d[0];
5050
}
5151

52-
pub function select_onehot::<N: p32, T: type,> (
52+
pub function select_onehot::<N: p32, T: type> (
5353
sel : input logic<N>,
5454
data: input T <N>,
5555
) -> T {
@@ -86,7 +86,7 @@ pub function select_onehot::<N: p32, T: type,> (
8686
return next_d[0];
8787
}
8888

89-
pub function select::<KIND: selector_pkg::selector_kind, N: p32, T: type,> (
89+
pub function select::<KIND: selector_pkg::selector_kind, N: p32, T: type> (
9090
sel : input logic<selector_pkg::calc_select_width(N, KIND)>,
9191
data: input T <N> ,
9292
) -> T {

crates/std/veryl/src/utility_functions/extend.veryl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub function zero_extend::<FROM_TYPE: type, TO_TYPE: type,> (
1+
pub function zero_extend::<FROM_TYPE: type, TO_TYPE: type> (
22
from: input FROM_TYPE,
33
) -> TO_TYPE {
44
const FROM_WIDTH : p32 = $bits(FROM_TYPE);
@@ -7,7 +7,7 @@ pub function zero_extend::<FROM_TYPE: type, TO_TYPE: type,> (
77
return {1'b0 repeat EXTEND_WIDTH, from} as TO_TYPE;
88
}
99

10-
pub function one_extend::<FROM_TYPE: type, TO_TYPE: type,> (
10+
pub function one_extend::<FROM_TYPE: type, TO_TYPE: type> (
1111
from: input FROM_TYPE,
1212
) -> TO_TYPE {
1313
const FROM_WIDTH : p32 = $bits(FROM_TYPE);
@@ -16,7 +16,7 @@ pub function one_extend::<FROM_TYPE: type, TO_TYPE: type,> (
1616
return {1'b1 repeat EXTEND_WIDTH, from} as TO_TYPE;
1717
}
1818

19-
pub function sign_extend::<FROM_TYPE: type, TO_TYPE: type,> (
19+
pub function sign_extend::<FROM_TYPE: type, TO_TYPE: type> (
2020
from: input FROM_TYPE,
2121
) -> TO_TYPE {
2222
const FROM_WIDTH : p32 = $bits(FROM_TYPE);

crates/std/veryl/src/utility_functions/truncate.veryl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub function truncate::<FROM_TYPE: type, TO_TYPE: type,> (
1+
pub function truncate::<FROM_TYPE: type, TO_TYPE: type> (
22
from: input FROM_TYPE,
33
) -> TO_TYPE {
44
return from as TO_TYPE;

0 commit comments

Comments
 (0)