From 242be3631e6f2c581614b362ff1a572c7ffb8ff6 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 23 Dec 2019 17:53:35 +0200 Subject: Omit closure parameters --- crates/ra_hir_ty/src/display.rs | 12 ++++++------ crates/ra_hir_ty/src/lib.rs | 31 ++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 17 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 dcca1bace..37def7c03 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs @@ -10,7 +10,7 @@ pub struct HirFormatter<'a, 'b, DB> { buf: String, curr_size: usize, max_size: Option, - should_display_default_types: bool, + omit_verbose_types: bool, } pub trait HirDisplay { @@ -20,7 +20,7 @@ pub trait HirDisplay { where Self: Sized, { - HirDisplayWrapper(db, self, None, true) + HirDisplayWrapper(db, self, None, false) } fn display_truncated<'a, DB>( @@ -31,7 +31,7 @@ pub trait HirDisplay { where Self: Sized, { - HirDisplayWrapper(db, self, max_size, false) + HirDisplayWrapper(db, self, max_size, true) } } @@ -74,8 +74,8 @@ where } } - pub fn should_display_default_types(&self) -> bool { - self.should_display_default_types + pub fn omit_verbose_types(&self) -> bool { + self.omit_verbose_types } } @@ -93,7 +93,7 @@ where buf: String::with_capacity(20), curr_size: 0, max_size: self.2, - should_display_default_types: self.3, + omit_verbose_types: self.3, }) } } diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 48abf97c9..55b6dd836 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -821,6 +821,8 @@ impl TypeWalk for Ty { } } +const TYPE_HINT_TRUNCATION: &str = "…"; + impl HirDisplay for &Ty { fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { HirDisplay::hir_fmt(*self, f) @@ -830,7 +832,7 @@ impl HirDisplay for &Ty { impl HirDisplay for ApplicationTy { fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { if f.should_truncate() { - return write!(f, "…"); + return write!(f, "{}", TYPE_HINT_TRUNCATION); } match self.ctor { @@ -908,9 +910,7 @@ impl HirDisplay for ApplicationTy { write!(f, "<")?; 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 { + let parameters_to_write = if f.omit_verbose_types() { match self .ctor .as_generic_def() @@ -935,6 +935,8 @@ impl HirDisplay for ApplicationTy { &non_default_parameters } } + } else { + self.parameters.0.as_ref() }; f.write_joined(parameters_to_write, ", ")?; @@ -959,9 +961,16 @@ impl HirDisplay for ApplicationTy { let sig = self.parameters[0] .callable_sig(f.db) .expect("first closure parameter should contain signature"); - write!(f, "|")?; - f.write_joined(sig.params(), ", ")?; - write!(f, "| -> {}", sig.ret().display(f.db))?; + let return_type_hint = sig.ret().display(f.db); + if sig.params().is_empty() { + write!(f, "|| -> {}", return_type_hint)?; + } else if f.omit_verbose_types() { + write!(f, "|{}| -> {}", TYPE_HINT_TRUNCATION, return_type_hint)?; + } else { + write!(f, "|")?; + f.write_joined(sig.params(), ", ")?; + write!(f, "| -> {}", return_type_hint)?; + }; } } Ok(()) @@ -971,7 +980,7 @@ impl HirDisplay for ApplicationTy { impl HirDisplay for ProjectionTy { fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { if f.should_truncate() { - return write!(f, "…"); + return write!(f, "{}", TYPE_HINT_TRUNCATION); } let trait_name = f.db.trait_data(self.trait_(f.db)).name.clone(); @@ -989,7 +998,7 @@ impl HirDisplay for ProjectionTy { impl HirDisplay for Ty { fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { if f.should_truncate() { - return write!(f, "…"); + return write!(f, "{}", TYPE_HINT_TRUNCATION); } match self { @@ -1074,7 +1083,7 @@ impl HirDisplay for Ty { impl TraitRef { fn hir_fmt_ext(&self, f: &mut HirFormatter, use_as: bool) -> fmt::Result { if f.should_truncate() { - return write!(f, "…"); + return write!(f, "{}", TYPE_HINT_TRUNCATION); } self.substs[0].hir_fmt(f)?; @@ -1108,7 +1117,7 @@ impl HirDisplay for &GenericPredicate { impl HirDisplay for GenericPredicate { fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result { if f.should_truncate() { - return write!(f, "…"); + return write!(f, "{}", TYPE_HINT_TRUNCATION); } match self { -- cgit v1.2.3