aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorEmil Lauridsen <[email protected]>2019-11-18 17:02:28 +0000
committerEmil Lauridsen <[email protected]>2019-11-19 16:23:50 +0000
commitdadad36bb9770f9b13ed84bc219ea0168a7a5bf1 (patch)
tree00051540da204b4294501f3c56960975177ae502 /crates/ra_hir
parentc24ee0990486b04723534f072d7a58e829bbd1bd (diff)
Move type inlay hint truncation to language server
This commit implements a general truncation framework for HirFormatter that keeps track of how much has been output so far. This information can then be used to perform truncation inside the language server, instead of relying on the client. Initial support is implemented for truncating types hints using the maxInlayHintLength server config option. The existing solution in the VSCode extension has been removed in favor of letting the server truncate type hints.
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/ty.rs20
-rw-r--r--crates/ra_hir/src/ty/display.rs43
2 files changed, 59 insertions, 4 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index b7f50b714..776613c7c 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -800,6 +800,10 @@ impl HirDisplay for &Ty {
800 800
801impl HirDisplay for ApplicationTy { 801impl HirDisplay for ApplicationTy {
802 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 802 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
803 if f.should_truncate() {
804 return write!(f, "…");
805 }
806
803 match self.ctor { 807 match self.ctor {
804 TypeCtor::Bool => write!(f, "bool")?, 808 TypeCtor::Bool => write!(f, "bool")?,
805 TypeCtor::Char => write!(f, "char")?, 809 TypeCtor::Char => write!(f, "char")?,
@@ -901,6 +905,10 @@ impl HirDisplay for ApplicationTy {
901 905
902impl HirDisplay for ProjectionTy { 906impl HirDisplay for ProjectionTy {
903 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 907 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
908 if f.should_truncate() {
909 return write!(f, "…");
910 }
911
904 let trait_name = self 912 let trait_name = self
905 .associated_ty 913 .associated_ty
906 .parent_trait(f.db) 914 .parent_trait(f.db)
@@ -919,6 +927,10 @@ impl HirDisplay for ProjectionTy {
919 927
920impl HirDisplay for Ty { 928impl HirDisplay for Ty {
921 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 929 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
930 if f.should_truncate() {
931 return write!(f, "…");
932 }
933
922 match self { 934 match self {
923 Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, 935 Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
924 Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, 936 Ty::Projection(p_ty) => p_ty.hir_fmt(f)?,
@@ -1001,6 +1013,10 @@ impl HirDisplay for Ty {
1001 1013
1002impl TraitRef { 1014impl TraitRef {
1003 fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result { 1015 fn hir_fmt_ext(&self, f: &mut HirFormatter<impl HirDatabase>, use_as: bool) -> fmt::Result {
1016 if f.should_truncate() {
1017 return write!(f, "…");
1018 }
1019
1004 self.substs[0].hir_fmt(f)?; 1020 self.substs[0].hir_fmt(f)?;
1005 if use_as { 1021 if use_as {
1006 write!(f, " as ")?; 1022 write!(f, " as ")?;
@@ -1031,6 +1047,10 @@ impl HirDisplay for &GenericPredicate {
1031 1047
1032impl HirDisplay for GenericPredicate { 1048impl HirDisplay for GenericPredicate {
1033 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result { 1049 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
1050 if f.should_truncate() {
1051 return write!(f, "…");
1052 }
1053
1034 match self { 1054 match self {
1035 GenericPredicate::Implemented(trait_ref) => trait_ref.hir_fmt(f)?, 1055 GenericPredicate::Implemented(trait_ref) => trait_ref.hir_fmt(f)?,
1036 GenericPredicate::Projection(projection_pred) => { 1056 GenericPredicate::Projection(projection_pred) => {
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;
7pub struct HirFormatter<'a, 'b, DB> { 7pub 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
12pub trait HirDisplay { 15pub 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
48pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T); 77pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>);
49 78
50impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> 79impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
51where 80where
@@ -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}