From c5ee60e05b1fafe20f56b21cfab30e7b80cb9d42 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 14 Mar 2019 22:03:39 +0100 Subject: Replace Display by a pretty printing trait for Ty This allows removing the names from Adt and FnDef (and more later), as a first step towards aligning more with chalk's Ty :) --- crates/ra_hir/src/ty/display.rs | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 crates/ra_hir/src/ty/display.rs (limited to 'crates/ra_hir/src/ty/display.rs') diff --git a/crates/ra_hir/src/ty/display.rs b/crates/ra_hir/src/ty/display.rs new file mode 100644 index 000000000..63ec9d7e1 --- /dev/null +++ b/crates/ra_hir/src/ty/display.rs @@ -0,0 +1,56 @@ +use std::fmt; + +use crate::db::HirDatabase; + +pub struct HirFormatter<'a, 'b, DB> { + pub db: &'a DB, + fmt: &'a mut fmt::Formatter<'b>, +} + +pub trait HirDisplay { + fn hir_fmt(&self, f: &mut HirFormatter) -> fmt::Result; + fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self> + where + Self: Sized, + { + HirDisplayWrapper(db, self) + } +} + +impl<'a, 'b, DB> HirFormatter<'a, 'b, DB> +where + DB: HirDatabase, +{ + pub fn write_joined( + &mut self, + iter: impl IntoIterator, + sep: &str, + ) -> fmt::Result { + let mut first = true; + for e in iter { + if !first { + write!(self, "{}", sep)?; + } + first = false; + e.hir_fmt(self)?; + } + Ok(()) + } + + /// This allows using the `write!` macro directly with a `HirFormatter`. + pub fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result { + fmt::write(self.fmt, args) + } +} + +pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T); + +impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> +where + DB: HirDatabase, + T: HirDisplay, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.1.hir_fmt(&mut HirFormatter { db: self.0, fmt: f }) + } +} -- cgit v1.2.3