Repository: rust-analyzer/rust-analyzer (rust)

Issue:
`HirDisplay` should hide default type paramters in source code mode
Here

https://github.com/rust-analyzer/rust-analyzer/blob/77d2eaba1679832f4c00241645712460351eddb4/crates/ra_hir_ty/src/display.rs#L306

I think we shouldn't do a check for `f.omit_verbose_types()`

See https://github.com/rust-analyzer/rust-analyzer/pull/4389/files#diff-c431a99e4457d62dceb56a4114987d00L192

cc @TimoFreiberg 

Pull request description:
Omit default types in HirDisplay SourceCode mode
Closes #4390

Patch:
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs
index 146cc75df2..7ced006266 100644
--- a/crates/ra_assists/src/handlers/add_explicit_type.rs
+++ b/crates/ra_assists/src/handlers/add_explicit_type.rs
@@ -209,7 +209,7 @@ struct Test<K, T = u8> {
 }
 
 fn main() {
-    let test<|>: Test<i32, u8> = Test { t: 23, k: 33 };
+    let test<|>: Test<i32> = Test { t: 23, k: 33 };
 }"#,
         );
     }
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs
index f5edaea8c8..b9c4d2e897 100644
--- a/crates/ra_hir_ty/src/display.rs
+++ b/crates/ra_hir_ty/src/display.rs
@@ -136,6 +136,12 @@ enum DisplayTarget {
     SourceCode { module_id: ModuleId },
 }
 
+impl DisplayTarget {
+    fn is_source_code(&self) -> bool {
+        matches!(self, Self::SourceCode {..})
+    }
+}
+
 #[derive(Debug)]
 pub enum DisplaySourceCodeError {
     PathNotFound,
@@ -303,37 +309,40 @@ impl HirDisplay for ApplicationTy {
 
                 if self.parameters.len() > 0 {
                     let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
-                    let parameters_to_write = if f.omit_verbose_types() {
-                        match self
-                            .ctor
-                            .as_generic_def()
-                            .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
-                            .filter(|defaults| !defaults.is_empty())
-                        {
-                            None => self.parameters.0.as_ref(),
-                            Some(default_parameters) => {
-                                for (i, parameter) in self.parameters.iter().enumerate() {
-                                    match (parameter, default_parameters.get(i)) {
-                                        (&Ty::Unknown, _) | (_, None) => {
-                                            non_default_parameters.push(parameter.clone())
-                                        }
-                                        (_, Some(default_parameter))
-                                            if parameter != default_parameter =>
-                                        {
-                                            non_default_parameters.push(parameter.clone())
+                    let parameters_to_write =
+                        if f.display_target.is_source_code() || f.omit_verbose_types() {
+                            match self
+                                .ctor
+                                .as_generic_def()
+                                .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
+                                .filter(|defaults| !defaults.is_empty())
+                            {
+                                None => self.parameters.0.as_ref(),
+                                Some(default_parameters) => {
+                                    for (i, parameter) in self.parameters.iter().enumerate() {
+                                        match (parameter, default_parameters.get(i)) {
+                                            (&Ty::Unknown, _) | (_, None) => {
+                                                non_default_parameters.push(parameter.clone())
+                                            }
+                                            (_, Some(default_parameter))
+                                                if parameter != default_parameter =>
+                                            {
+                                                non_default_parameters.push(parameter.clone())
+                                            }
+                                            _ => (),
                                         }
-                                        _ => (),
                                     }
+                                    &non_default_parameters
                                 }
-                                &non_default_parameters
                             }
-                        }
-                    } else {
-                        self.parameters.0.as_ref()
-                    };
-                    write!(f, "<")?;
-                    f.write_joined(parameters_to_write, ", ")?;
-                    write!(f, ">")?;
+                        } else {
+                            self.parameters.0.as_ref()
+                        };
+                    if !parameters_to_write.is_empty() {
+                        write!(f, "<")?;
+                        f.write_joined(parameters_to_write, ", ")?;
+                        write!(f, ">")?;
+                    }
                 }
             }
             TypeCtor::AssociatedType(type_alias) => {


Test patch:
diff --git a/crates/ra_hir_ty/src/tests/display_source_code.rs b/crates/ra_hir_ty/src/tests/display_source_code.rs
index ca17486152..4088b1d22d 100644
--- a/crates/ra_hir_ty/src/tests/display_source_code.rs
+++ b/crates/ra_hir_ty/src/tests/display_source_code.rs
@@ -21,3 +21,30 @@ fn bar() {
     );
     assert_eq!("foo::Foo", displayed_source_at_pos(&db, pos));
 }
+
+#[test]
+fn omit_default_type_parameters() {
+    let (db, pos) = TestDB::with_position(
+        r"
+        //- /main.rs
+        struct Foo<T = u8> { t: T }
+        fn main() {
+            let foo = Foo { t: 5 };
+            foo<|>;
+        }
+        ",
+    );
+    assert_eq!("Foo", displayed_source_at_pos(&db, pos));
+
+    let (db, pos) = TestDB::with_position(
+        r"
+        //- /main.rs
+        struct Foo<K, T = u8> { k: K, t: T }
+        fn main() {
+            let foo = Foo { k: 400, t: 5 };
+            foo<|>;
+        }
+        ",
+    );
+    assert_eq!("Foo<i32>", displayed_source_at_pos(&db, pos));
+}