From 14c167a9f6da07024a5101ffa04bc2f79ce64353 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 8 Dec 2019 00:54:18 +0200 Subject: Omit default parameter types --- crates/ra_hir_ty/src/display.rs | 21 +++++++++++++++------ crates/ra_hir_ty/src/lib.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir_ty/src') 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> { fmt: &'a mut fmt::Formatter<'b>, buf: String, curr_size: usize, - max_size: Option, + truncate_options: Option<&'a TruncateOptions>, } pub trait HirDisplay { @@ -25,12 +25,12 @@ pub trait HirDisplay { fn display_truncated<'a, DB>( &'a self, db: &'a DB, - max_size: Option, + truncate_options: &'a TruncateOptions, ) -> HirDisplayWrapper<'a, DB, Self> where Self: Sized, { - HirDisplayWrapper(db, self, max_size) + HirDisplayWrapper(db, self, Some(truncate_options)) } } @@ -66,15 +66,24 @@ where } pub fn should_truncate(&self) -> bool { - if let Some(max_size) = self.max_size { + if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) { self.curr_size >= max_size } else { false } } + + pub fn should_display_default_types(&self) -> bool { + self.truncate_options.map(|options| options.show_default_types).unwrap_or(true) + } +} + +pub struct TruncateOptions { + pub max_length: Option, + pub show_default_types: bool, } -pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option); +pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>); impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> where @@ -87,7 +96,7 @@ where fmt: f, buf: String::with_capacity(20), curr_size: 0, - max_size: self.2, + truncate_options: self.2, }) } } 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 { write!(f, "{}", name)?; if self.parameters.len() > 0 { write!(f, "<")?; - f.write_joined(&*self.parameters.0, ", ")?; + + let mut non_default_parameters = Vec::with_capacity(self.parameters.len()); + let parameters_to_write = if f.should_display_default_types() { + self.parameters.0.as_ref() + } else { + match self + .ctor + .as_generic_def() + .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) + .filter(|defaults| !defaults.is_empty()) + { + Option::None => self.parameters.0.as_ref(), + Option::Some(default_parameters) => { + for (i, parameter) in self.parameters.into_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 + } + } + }; + + f.write_joined(parameters_to_write, ", ")?; write!(f, ">")?; } } -- cgit v1.2.3 From 4ed78f80f4cc3cf32681fce6722293da6c8df76d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 19 Dec 2019 16:43:41 +0200 Subject: Remove TruncateOptions struct --- crates/ra_hir_ty/src/display.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index 9176c0629..dcca1bace 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs @@ -9,7 +9,8 @@ pub struct HirFormatter<'a, 'b, DB> { fmt: &'a mut fmt::Formatter<'b>, buf: String, curr_size: usize, - truncate_options: Option<&'a TruncateOptions>, + max_size: Option, + should_display_default_types: bool, } pub trait HirDisplay { @@ -19,18 +20,18 @@ pub trait HirDisplay { where Self: Sized, { - HirDisplayWrapper(db, self, None) + HirDisplayWrapper(db, self, None, true) } fn display_truncated<'a, DB>( &'a self, db: &'a DB, - truncate_options: &'a TruncateOptions, + max_size: Option, ) -> HirDisplayWrapper<'a, DB, Self> where Self: Sized, { - HirDisplayWrapper(db, self, Some(truncate_options)) + HirDisplayWrapper(db, self, max_size, false) } } @@ -66,7 +67,7 @@ where } pub fn should_truncate(&self) -> bool { - if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) { + if let Some(max_size) = self.max_size { self.curr_size >= max_size } else { false @@ -74,16 +75,11 @@ where } pub fn should_display_default_types(&self) -> bool { - self.truncate_options.map(|options| options.show_default_types).unwrap_or(true) + self.should_display_default_types } } -pub struct TruncateOptions { - pub max_length: Option, - pub show_default_types: bool, -} - -pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>); +pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option, bool); impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> where @@ -96,7 +92,8 @@ where fmt: f, buf: String::with_capacity(20), curr_size: 0, - truncate_options: self.2, + max_size: self.2, + should_display_default_types: self.3, }) } } -- cgit v1.2.3