diff options
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/src/display.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 33 |
2 files changed, 47 insertions, 7 deletions
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index 9bb3ece6c..9176c0629 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs | |||
@@ -9,7 +9,7 @@ pub struct HirFormatter<'a, 'b, DB> { | |||
9 | fmt: &'a mut fmt::Formatter<'b>, | 9 | fmt: &'a mut fmt::Formatter<'b>, |
10 | buf: String, | 10 | buf: String, |
11 | curr_size: usize, | 11 | curr_size: usize, |
12 | max_size: Option<usize>, | 12 | truncate_options: Option<&'a TruncateOptions>, |
13 | } | 13 | } |
14 | 14 | ||
15 | pub trait HirDisplay { | 15 | pub trait HirDisplay { |
@@ -25,12 +25,12 @@ pub trait HirDisplay { | |||
25 | fn display_truncated<'a, DB>( | 25 | fn display_truncated<'a, DB>( |
26 | &'a self, | 26 | &'a self, |
27 | db: &'a DB, | 27 | db: &'a DB, |
28 | max_size: Option<usize>, | 28 | truncate_options: &'a TruncateOptions, |
29 | ) -> HirDisplayWrapper<'a, DB, Self> | 29 | ) -> HirDisplayWrapper<'a, DB, Self> |
30 | where | 30 | where |
31 | Self: Sized, | 31 | Self: Sized, |
32 | { | 32 | { |
33 | HirDisplayWrapper(db, self, max_size) | 33 | HirDisplayWrapper(db, self, Some(truncate_options)) |
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
@@ -66,15 +66,24 @@ where | |||
66 | } | 66 | } |
67 | 67 | ||
68 | pub fn should_truncate(&self) -> bool { | 68 | pub fn should_truncate(&self) -> bool { |
69 | if let Some(max_size) = self.max_size { | 69 | if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) { |
70 | self.curr_size >= max_size | 70 | self.curr_size >= max_size |
71 | } else { | 71 | } else { |
72 | false | 72 | false |
73 | } | 73 | } |
74 | } | 74 | } |
75 | |||
76 | pub fn should_display_default_types(&self) -> bool { | ||
77 | self.truncate_options.map(|options| options.show_default_types).unwrap_or(true) | ||
78 | } | ||
79 | } | ||
80 | |||
81 | pub struct TruncateOptions { | ||
82 | pub max_length: Option<usize>, | ||
83 | pub show_default_types: bool, | ||
75 | } | 84 | } |
76 | 85 | ||
77 | pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>); | 86 | pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>); |
78 | 87 | ||
79 | impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> | 88 | impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> |
80 | where | 89 | where |
@@ -87,7 +96,7 @@ where | |||
87 | fmt: f, | 96 | fmt: f, |
88 | buf: String::with_capacity(20), | 97 | buf: String::with_capacity(20), |
89 | curr_size: 0, | 98 | curr_size: 0, |
90 | max_size: self.2, | 99 | truncate_options: self.2, |
91 | }) | 100 | }) |
92 | } | 101 | } |
93 | } | 102 | } |
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 | } |