diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/display.rs | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/crates/ra_hir/src/ty/display.rs b/crates/ra_hir/src/ty/display.rs index 7910429d7..9bb3ece6c 100644 --- a/crates/ra_hir/src/ty/display.rs +++ b/crates/ra_hir/src/ty/display.rs | |||
@@ -7,15 +7,30 @@ use crate::db::HirDatabase; | |||
7 | pub struct HirFormatter<'a, 'b, DB> { | 7 | pub struct HirFormatter<'a, 'b, DB> { |
8 | pub db: &'a DB, | 8 | pub db: &'a DB, |
9 | fmt: &'a mut fmt::Formatter<'b>, | 9 | fmt: &'a mut fmt::Formatter<'b>, |
10 | buf: String, | ||
11 | curr_size: usize, | ||
12 | max_size: Option<usize>, | ||
10 | } | 13 | } |
11 | 14 | ||
12 | pub trait HirDisplay { | 15 | pub trait HirDisplay { |
13 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result; | 16 | fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result; |
17 | |||
14 | fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self> | 18 | fn display<'a, DB>(&'a self, db: &'a DB) -> HirDisplayWrapper<'a, DB, Self> |
15 | where | 19 | where |
16 | Self: Sized, | 20 | Self: Sized, |
17 | { | 21 | { |
18 | HirDisplayWrapper(db, self) | 22 | HirDisplayWrapper(db, self, None) |
23 | } | ||
24 | |||
25 | fn display_truncated<'a, DB>( | ||
26 | &'a self, | ||
27 | db: &'a DB, | ||
28 | max_size: Option<usize>, | ||
29 | ) -> HirDisplayWrapper<'a, DB, Self> | ||
30 | where | ||
31 | Self: Sized, | ||
32 | { | ||
33 | HirDisplayWrapper(db, self, max_size) | ||
19 | } | 34 | } |
20 | } | 35 | } |
21 | 36 | ||
@@ -41,11 +56,25 @@ where | |||
41 | 56 | ||
42 | /// This allows using the `write!` macro directly with a `HirFormatter`. | 57 | /// This allows using the `write!` macro directly with a `HirFormatter`. |
43 | pub fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result { | 58 | pub fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result { |
44 | fmt::write(self.fmt, args) | 59 | // We write to a buffer first to track output size |
60 | self.buf.clear(); | ||
61 | fmt::write(&mut self.buf, args)?; | ||
62 | self.curr_size += self.buf.len(); | ||
63 | |||
64 | // Then we write to the internal formatter from the buffer | ||
65 | self.fmt.write_str(&self.buf) | ||
66 | } | ||
67 | |||
68 | pub fn should_truncate(&self) -> bool { | ||
69 | if let Some(max_size) = self.max_size { | ||
70 | self.curr_size >= max_size | ||
71 | } else { | ||
72 | false | ||
73 | } | ||
45 | } | 74 | } |
46 | } | 75 | } |
47 | 76 | ||
48 | pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T); | 77 | pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>); |
49 | 78 | ||
50 | impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> | 79 | impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> |
51 | where | 80 | where |
@@ -53,6 +82,12 @@ where | |||
53 | T: HirDisplay, | 82 | T: HirDisplay, |
54 | { | 83 | { |
55 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 84 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
56 | self.1.hir_fmt(&mut HirFormatter { db: self.0, fmt: f }) | 85 | self.1.hir_fmt(&mut HirFormatter { |
86 | db: self.0, | ||
87 | fmt: f, | ||
88 | buf: String::with_capacity(20), | ||
89 | curr_size: 0, | ||
90 | max_size: self.2, | ||
91 | }) | ||
57 | } | 92 | } |
58 | } | 93 | } |