about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroife <roifewu@gmail.com>2024-09-09 23:14:31 +0800
committerroife <roifewu@gmail.com>2024-09-09 23:20:31 +0800
commit129acd2e08cbe614be11acb7a4f1f5169dba3a23 (patch)
treeaf747e44a9b5bd9222f656b0a780192dbd8f23a7
parentcbfa3578134e09a5a296cba3dc8b7951569ca603 (diff)
downloadrust-129acd2e08cbe614be11acb7a4f1f5169dba3a23.tar.gz
rust-129acd2e08cbe614be11acb7a4f1f5169dba3a23.zip
feat: use shorthand when pretty-print record pat
-rw-r--r--src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs33
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/hover/tests.rs52
2 files changed, 79 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs b/src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs
index 64d0c16f525..37167fcb815 100644
--- a/src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs
+++ b/src/tools/rust-analyzer/crates/hir-def/src/body/pretty.rs
@@ -616,12 +616,37 @@ impl Printer<'_> {
 
                 w!(self, " {{");
                 let edition = self.edition;
+                let oneline = matches!(self.line_format, LineFormat::Oneline);
                 self.indented(|p| {
-                    for arg in args.iter() {
-                        w!(p, "{}: ", arg.name.display(self.db.upcast(), edition));
-                        p.print_pat(arg.pat);
-                        wln!(p, ",");
+                    for (idx, arg) in args.iter().enumerate() {
+                        let field_name = arg.name.display(self.db.upcast(), edition).to_string();
+
+                        let mut same_name = false;
+                        if let Pat::Bind { id, subpat: None } = &self.body[arg.pat] {
+                            if let Binding { name, mode: BindingAnnotation::Unannotated, .. } =
+                                &self.body.bindings[*id]
+                            {
+                                if name.as_str() == field_name {
+                                    same_name = true;
+                                }
+                            }
+                        }
+
+                        w!(p, "{}", field_name);
+
+                        if !same_name {
+                            w!(p, ": ");
+                            p.print_pat(arg.pat);
+                        }
+
+                        // Do not print the extra comma if the line format is oneline
+                        if oneline && idx == args.len() - 1 {
+                            w!(p, " ");
+                        } else {
+                            wln!(p, ",");
+                        }
                     }
+
                     if *ellipsis {
                         wln!(p, "..");
                     }
diff --git a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
index 1a97d99f051..f2e5d24fcc6 100644
--- a/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/hover/tests.rs
@@ -8803,7 +8803,7 @@ fn test_hover_function_with_pat_param() {
             ```
 
             ```rust
-            fn test_4(Point { x: x, y: y, }: Point)
+            fn test_4(Point { x, y }: Point)
             ```
         "#]],
     );
@@ -8851,7 +8851,7 @@ fn test_hover_function_with_pat_param() {
             ```
 
             ```rust
-            fn test_7((x, Foo { a: a, b: b, }): (i32, Foo))
+            fn test_7((x, Foo { a, b }): (i32, Foo))
             ```
         "#]],
     );
@@ -8871,4 +8871,52 @@ fn test_hover_function_with_pat_param() {
             ```
         "#]],
     );
+
+    // Test case with a pattern as a function parameter
+    check(
+        r#"struct Foo { a: i32, b: i32 } fn test_9$0(Foo { a, b }: Foo) {}"#,
+        expect![[r#"
+            *test_9*
+
+            ```rust
+            test
+            ```
+
+            ```rust
+            fn test_9(Foo { a, b }: Foo)
+            ```
+        "#]],
+    );
+
+    // Test case with a pattern as a function parameter with a different name
+    check(
+        r#"struct Foo { a: i32, b: i32 } fn test_10$0(Foo { a, b: b1 }: Foo) {}"#,
+        expect![[r#"
+            *test_10*
+
+            ```rust
+            test
+            ```
+
+            ```rust
+            fn test_10(Foo { a, b: b1 }: Foo)
+            ```
+        "#]],
+    );
+
+    // Test case with a pattern as a function parameter with annotations
+    check(
+        r#"struct Foo { a: i32, b: i32 } fn test_10$0(Foo { a, b: mut b }: Foo) {}"#,
+        expect![[r#"
+            *test_10*
+
+            ```rust
+            test
+            ```
+
+            ```rust
+            fn test_10(Foo { a, b: mut b }: Foo)
+            ```
+        "#]],
+    );
 }