aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-19 15:35:42 +0000
committerGitHub <[email protected]>2019-12-19 15:35:42 +0000
commita766f63650aa08697f68e749d274c2c84d1e3eee (patch)
treeb57a54e65ee71ac29d932728fc25591402c3d524 /crates
parent1e32412e28d6668af5c6fe85b4fc921fb1de9a98 (diff)
parent6419d49f1d88600d4a480701285d4e33be5cd3f1 (diff)
Merge #2594
2594: Omit default parameter types r=matklad a=SomeoneToIgnore Part of https://github.com/rust-analyzer/rust-analyzer/issues/1946 Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_ty/src/display.rs12
-rw-r--r--crates/ra_hir_ty/src/lib.rs33
-rw-r--r--crates/ra_ide/src/hover.rs19
-rw-r--r--crates/ra_ide/src/inlay_hints.rs26
4 files changed, 85 insertions, 5 deletions
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs
index 9bb3ece6c..dcca1bace 100644
--- a/crates/ra_hir_ty/src/display.rs
+++ b/crates/ra_hir_ty/src/display.rs
@@ -10,6 +10,7 @@ pub struct HirFormatter<'a, 'b, DB> {
10 buf: String, 10 buf: String,
11 curr_size: usize, 11 curr_size: usize,
12 max_size: Option<usize>, 12 max_size: Option<usize>,
13 should_display_default_types: bool,
13} 14}
14 15
15pub trait HirDisplay { 16pub trait HirDisplay {
@@ -19,7 +20,7 @@ pub trait HirDisplay {
19 where 20 where
20 Self: Sized, 21 Self: Sized,
21 { 22 {
22 HirDisplayWrapper(db, self, None) 23 HirDisplayWrapper(db, self, None, true)
23 } 24 }
24 25
25 fn display_truncated<'a, DB>( 26 fn display_truncated<'a, DB>(
@@ -30,7 +31,7 @@ pub trait HirDisplay {
30 where 31 where
31 Self: Sized, 32 Self: Sized,
32 { 33 {
33 HirDisplayWrapper(db, self, max_size) 34 HirDisplayWrapper(db, self, max_size, false)
34 } 35 }
35} 36}
36 37
@@ -72,9 +73,13 @@ where
72 false 73 false
73 } 74 }
74 } 75 }
76
77 pub fn should_display_default_types(&self) -> bool {
78 self.should_display_default_types
79 }
75} 80}
76 81
77pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>); 82pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool);
78 83
79impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> 84impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
80where 85where
@@ -88,6 +93,7 @@ where
88 buf: String::with_capacity(20), 93 buf: String::with_capacity(20),
89 curr_size: 0, 94 curr_size: 0,
90 max_size: self.2, 95 max_size: self.2,
96 should_display_default_types: self.3,
91 }) 97 })
92 } 98 }
93} 99}
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index 3ad913e55..7ca9e6b8a 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -906,7 +906,38 @@ impl HirDisplay for ApplicationTy {
906 write!(f, "{}", name)?; 906 write!(f, "{}", name)?;
907 if self.parameters.len() > 0 { 907 if self.parameters.len() > 0 {
908 write!(f, "<")?; 908 write!(f, "<")?;
909 f.write_joined(&*self.parameters.0, ", ")?; 909
910 let mut non_default_parameters = Vec::with_capacity(self.parameters.len());
911 let parameters_to_write = if f.should_display_default_types() {
912 self.parameters.0.as_ref()
913 } else {
914 match self
915 .ctor
916 .as_generic_def()
917 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
918 .filter(|defaults| !defaults.is_empty())
919 {
920 Option::None => self.parameters.0.as_ref(),
921 Option::Some(default_parameters) => {
922 for (i, parameter) in self.parameters.into_iter().enumerate() {
923 match (parameter, default_parameters.get(i)) {
924 (&Ty::Unknown, _) | (_, None) => {
925 non_default_parameters.push(parameter.clone())
926 }
927 (_, Some(default_parameter))
928 if parameter != default_parameter =>
929 {
930 non_default_parameters.push(parameter.clone())
931 }
932 _ => (),
933 }
934 }
935 &non_default_parameters
936 }
937 }
938 };
939
940 f.write_joined(parameters_to_write, ", ")?;
910 write!(f, ">")?; 941 write!(f, ">")?;
911 } 942 }
912 } 943 }
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 51e320128..a227bf546 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -250,7 +250,7 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
250 } else { 250 } else {
251 return None; 251 return None;
252 }; 252 };
253 Some(ty.display(db).to_string()) 253 Some(ty.display_truncated(db, None).to_string())
254} 254}
255 255
256#[cfg(test)] 256#[cfg(test)]
@@ -425,6 +425,23 @@ mod tests {
425 } 425 }
426 426
427 #[test] 427 #[test]
428 fn hover_omits_default_generic_types() {
429 check_hover_result(
430 r#"
431//- /main.rs
432struct Test<K, T = u8> {
433 k: K,
434 t: T,
435}
436
437fn main() {
438 let zz<|> = Test { t: 23, k: 33 };
439}"#,
440 &["Test<i32>"],
441 );
442 }
443
444 #[test]
428 fn hover_some() { 445 fn hover_some() {
429 let (analysis, position) = single_file_with_position( 446 let (analysis, position) = single_file_with_position(
430 " 447 "
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 3730121af..3154df457 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -160,6 +160,32 @@ mod tests {
160 use crate::mock_analysis::single_file; 160 use crate::mock_analysis::single_file;
161 161
162 #[test] 162 #[test]
163 fn default_generic_types_should_not_be_displayed() {
164 let (analysis, file_id) = single_file(
165 r#"
166struct Test<K, T = u8> {
167 k: K,
168 t: T,
169}
170
171fn main() {
172 let zz = Test { t: 23, k: 33 };
173}"#,
174 );
175
176 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###"
177 [
178 InlayHint {
179 range: [69; 71),
180 kind: TypeHint,
181 label: "Test<i32>",
182 },
183 ]
184 "###
185 );
186 }
187
188 #[test]
163 fn let_statement() { 189 fn let_statement() {
164 let (analysis, file_id) = single_file( 190 let (analysis, file_id) = single_file(
165 r#" 191 r#"