From 242be3631e6f2c581614b362ff1a572c7ffb8ff6 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 23 Dec 2019 17:53:35 +0200 Subject: [PATCH] Omit closure parameters --- crates/ra_hir_ty/src/display.rs | 12 +++++------ crates/ra_hir_ty/src/lib.rs | 31 ++++++++++++++++---------- crates/ra_ide/src/inlay_hints.rs | 37 +++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index dcca1bace6..37def7c035 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 48abf97c92..55b6dd836c 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 { diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index c5e4069773..977aafc51f 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -293,7 +293,7 @@ fn main() { } #[test] - fn closure_parameter() { + fn closure_parameters() { let (analysis, file_id) = single_file( r#" fn main() { @@ -301,6 +301,11 @@ fn main() { (0..2).for_each(|increment| { start += increment; }) + + let multiply = |a, b, c, d| a * b * c * d; + let _: i32 = multiply(1, 2, 3, 4); + + let return_42 = || 42; }"#, ); @@ -316,6 +321,36 @@ fn main() { kind: TypeHint, label: "i32", }, + InlayHint { + range: [114; 122), + kind: TypeHint, + label: "|…| -> i32", + }, + InlayHint { + range: [126; 127), + kind: TypeHint, + label: "i32", + }, + InlayHint { + range: [129; 130), + kind: TypeHint, + label: "i32", + }, + InlayHint { + range: [132; 133), + kind: TypeHint, + label: "i32", + }, + InlayHint { + range: [135; 136), + kind: TypeHint, + label: "i32", + }, + InlayHint { + range: [201; 210), + kind: TypeHint, + label: "|| -> i32", + }, ] "### );