1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
//! FIXME: write short doc here
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<impl HirDatabase>) -> 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<T: HirDisplay>(
&mut self,
iter: impl IntoIterator<Item = T>,
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 })
}
}
|