Skip to content

Commit d38bc28

Browse files
committed
feat: use shorthand when pretty-print record pat
1 parent 5c97361 commit d38bc28

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

crates/hir-def/src/body/pretty.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,37 @@ impl Printer<'_> {
616616

617617
w!(self, " {{");
618618
let edition = self.edition;
619+
let oneline = matches!(self.line_format, LineFormat::Oneline);
619620
self.indented(|p| {
620-
for arg in args.iter() {
621-
w!(p, "{}: ", arg.name.display(self.db.upcast(), edition));
622-
p.print_pat(arg.pat);
623-
wln!(p, ",");
621+
for (idx, arg) in args.iter().enumerate() {
622+
let field_name = arg.name.display(self.db.upcast(), edition).to_string();
623+
624+
let mut same_name = false;
625+
if let Pat::Bind { id, subpat: None } = &self.body[arg.pat] {
626+
if let Binding { name, mode: BindingAnnotation::Unannotated, .. } =
627+
&self.body.bindings[*id]
628+
{
629+
if name.as_str() == &field_name {
630+
same_name = true;
631+
}
632+
}
633+
}
634+
635+
w!(p, "{}", field_name);
636+
637+
if !same_name {
638+
w!(p, ": ");
639+
p.print_pat(arg.pat);
640+
}
641+
642+
// Do not print the extra comma if the line format is oneline
643+
if oneline && idx == args.len() - 1 {
644+
w!(p, " ");
645+
} else {
646+
wln!(p, ",");
647+
}
624648
}
649+
625650
if *ellipsis {
626651
wln!(p, "..");
627652
}

crates/ide/src/hover/tests.rs

+50-2
Original file line numberDiff line numberDiff line change
@@ -8803,7 +8803,7 @@ fn test_hover_function_with_pat_param() {
88038803
```
88048804
88058805
```rust
8806-
fn test_4(Point { x: x, y: y, }: Point)
8806+
fn test_4(Point { x, y }: Point)
88078807
```
88088808
"#]],
88098809
);
@@ -8851,7 +8851,7 @@ fn test_hover_function_with_pat_param() {
88518851
```
88528852
88538853
```rust
8854-
fn test_7((x, Foo { a: a, b: b, }): (i32, Foo))
8854+
fn test_7((x, Foo { a, b }): (i32, Foo))
88558855
```
88568856
"#]],
88578857
);
@@ -8871,4 +8871,52 @@ fn test_hover_function_with_pat_param() {
88718871
```
88728872
"#]],
88738873
);
8874+
8875+
// Test case with a pattern as a function parameter
8876+
check(
8877+
r#"struct Foo { a: i32, b: i32 } fn test_9$0(Foo { a, b }: Foo) {}"#,
8878+
expect![[r#"
8879+
*test_9*
8880+
8881+
```rust
8882+
test
8883+
```
8884+
8885+
```rust
8886+
fn test_9(Foo { a, b }: Foo)
8887+
```
8888+
"#]],
8889+
);
8890+
8891+
// Test case with a pattern as a function parameter with a different name
8892+
check(
8893+
r#"struct Foo { a: i32, b: i32 } fn test_10$0(Foo { a, b: b1 }: Foo) {}"#,
8894+
expect![[r#"
8895+
*test_10*
8896+
8897+
```rust
8898+
test
8899+
```
8900+
8901+
```rust
8902+
fn test_10(Foo { a, b: b1 }: Foo)
8903+
```
8904+
"#]],
8905+
);
8906+
8907+
// Test case with a pattern as a function parameter with annotations
8908+
check(
8909+
r#"struct Foo { a: i32, b: i32 } fn test_10$0(Foo { a, b: mut b }: Foo) {}"#,
8910+
expect![[r#"
8911+
*test_10*
8912+
8913+
```rust
8914+
test
8915+
```
8916+
8917+
```rust
8918+
fn test_10(Foo { a, b: mut b }: Foo)
8919+
```
8920+
"#]],
8921+
);
88748922
}

0 commit comments

Comments
 (0)