aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_hir_ty/src/display.rs23
-rw-r--r--crates/ra_ide/src/inlay_hints.rs24
-rw-r--r--crates/ra_ide/src/lib.rs4
4 files changed, 24 insertions, 32 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index e9ca548ca..2e52a1f5c 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -60,7 +60,4 @@ pub use hir_def::{
60pub use hir_expand::{ 60pub use hir_expand::{
61 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin, 61 name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin,
62}; 62};
63pub use hir_ty::{ 63pub use hir_ty::{display::HirDisplay, CallableDef};
64 display::{HirDisplay, TruncateOptions},
65 CallableDef,
66};
diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs
index 9176c0629..dcca1bace 100644
--- a/crates/ra_hir_ty/src/display.rs
+++ b/crates/ra_hir_ty/src/display.rs
@@ -9,7 +9,8 @@ pub struct HirFormatter<'a, 'b, DB> {
9 fmt: &'a mut fmt::Formatter<'b>, 9 fmt: &'a mut fmt::Formatter<'b>,
10 buf: String, 10 buf: String,
11 curr_size: usize, 11 curr_size: usize,
12 truncate_options: Option<&'a TruncateOptions>, 12 max_size: Option<usize>,
13 should_display_default_types: bool,
13} 14}
14 15
15pub trait HirDisplay { 16pub trait HirDisplay {
@@ -19,18 +20,18 @@ pub trait HirDisplay {
19 where 20 where
20 Self: Sized, 21 Self: Sized,
21 { 22 {
22 HirDisplayWrapper(db, self, None) 23 HirDisplayWrapper(db, self, None, true)
23 } 24 }
24 25
25 fn display_truncated<'a, DB>( 26 fn display_truncated<'a, DB>(
26 &'a self, 27 &'a self,
27 db: &'a DB, 28 db: &'a DB,
28 truncate_options: &'a TruncateOptions, 29 max_size: Option<usize>,
29 ) -> HirDisplayWrapper<'a, DB, Self> 30 ) -> HirDisplayWrapper<'a, DB, Self>
30 where 31 where
31 Self: Sized, 32 Self: Sized,
32 { 33 {
33 HirDisplayWrapper(db, self, Some(truncate_options)) 34 HirDisplayWrapper(db, self, max_size, false)
34 } 35 }
35} 36}
36 37
@@ -66,7 +67,7 @@ where
66 } 67 }
67 68
68 pub fn should_truncate(&self) -> bool { 69 pub fn should_truncate(&self) -> bool {
69 if let Some(max_size) = self.truncate_options.and_then(|options| options.max_length) { 70 if let Some(max_size) = self.max_size {
70 self.curr_size >= max_size 71 self.curr_size >= max_size
71 } else { 72 } else {
72 false 73 false
@@ -74,16 +75,11 @@ where
74 } 75 }
75 76
76 pub fn should_display_default_types(&self) -> bool { 77 pub fn should_display_default_types(&self) -> bool {
77 self.truncate_options.map(|options| options.show_default_types).unwrap_or(true) 78 self.should_display_default_types
78 } 79 }
79} 80}
80 81
81pub struct TruncateOptions { 82pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<usize>, bool);
82 pub max_length: Option<usize>,
83 pub show_default_types: bool,
84}
85
86pub struct HirDisplayWrapper<'a, DB, T>(&'a DB, &'a T, Option<&'a TruncateOptions>);
87 83
88impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T> 84impl<'a, DB, T> fmt::Display for HirDisplayWrapper<'a, DB, T>
89where 85where
@@ -96,7 +92,8 @@ where
96 fmt: f, 92 fmt: f,
97 buf: String::with_capacity(20), 93 buf: String::with_capacity(20),
98 curr_size: 0, 94 curr_size: 0,
99 truncate_options: self.2, 95 max_size: self.2,
96 should_display_default_types: self.3,
100 }) 97 })
101 } 98 }
102} 99}
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 4c5004f67..3154df457 100644
--- a/crates/ra_ide/src/inlay_hints.rs
+++ b/crates/ra_ide/src/inlay_hints.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use crate::{db::RootDatabase, FileId}; 3use crate::{db::RootDatabase, FileId};
4use hir::{HirDisplay, SourceAnalyzer, TruncateOptions}; 4use hir::{HirDisplay, SourceAnalyzer};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, TypeAscriptionOwner}, 6 ast::{self, AstNode, TypeAscriptionOwner},
7 match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange, 7 match_ast, SmolStr, SourceFile, SyntaxKind, SyntaxNode, TextRange,
@@ -23,11 +23,11 @@ pub(crate) fn inlay_hints(
23 db: &RootDatabase, 23 db: &RootDatabase,
24 file_id: FileId, 24 file_id: FileId,
25 file: &SourceFile, 25 file: &SourceFile,
26 truncate_options: &TruncateOptions, 26 max_inlay_hint_length: Option<usize>,
27) -> Vec<InlayHint> { 27) -> Vec<InlayHint> {
28 file.syntax() 28 file.syntax()
29 .descendants() 29 .descendants()
30 .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).unwrap_or_default()) 30 .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default())
31 .flatten() 31 .flatten()
32 .collect() 32 .collect()
33} 33}
@@ -36,7 +36,7 @@ fn get_inlay_hints(
36 db: &RootDatabase, 36 db: &RootDatabase,
37 file_id: FileId, 37 file_id: FileId,
38 node: &SyntaxNode, 38 node: &SyntaxNode,
39 truncate_options: &TruncateOptions, 39 max_inlay_hint_length: Option<usize>,
40) -> Option<Vec<InlayHint>> { 40) -> Option<Vec<InlayHint>> {
41 let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None); 41 let analyzer = SourceAnalyzer::new(db, hir::InFile::new(file_id.into(), node), None);
42 match_ast! { 42 match_ast! {
@@ -46,7 +46,7 @@ fn get_inlay_hints(
46 return None; 46 return None;
47 } 47 }
48 let pat = it.pat()?; 48 let pat = it.pat()?;
49 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) 49 Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length))
50 }, 50 },
51 ast::LambdaExpr(it) => { 51 ast::LambdaExpr(it) => {
52 it.param_list().map(|param_list| { 52 it.param_list().map(|param_list| {
@@ -54,22 +54,22 @@ fn get_inlay_hints(
54 .params() 54 .params()
55 .filter(|closure_param| closure_param.ascribed_type().is_none()) 55 .filter(|closure_param| closure_param.ascribed_type().is_none())
56 .filter_map(|closure_param| closure_param.pat()) 56 .filter_map(|closure_param| closure_param.pat())
57 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options)) 57 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length))
58 .flatten() 58 .flatten()
59 .collect() 59 .collect()
60 }) 60 })
61 }, 61 },
62 ast::ForExpr(it) => { 62 ast::ForExpr(it) => {
63 let pat = it.pat()?; 63 let pat = it.pat()?;
64 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options)) 64 Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length))
65 }, 65 },
66 ast::IfExpr(it) => { 66 ast::IfExpr(it) => {
67 let pat = it.condition()?.pat()?; 67 let pat = it.condition()?.pat()?;
68 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) 68 Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length))
69 }, 69 },
70 ast::WhileExpr(it) => { 70 ast::WhileExpr(it) => {
71 let pat = it.condition()?.pat()?; 71 let pat = it.condition()?.pat()?;
72 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options)) 72 Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length))
73 }, 73 },
74 ast::MatchArmList(it) => { 74 ast::MatchArmList(it) => {
75 Some( 75 Some(
@@ -77,7 +77,7 @@ fn get_inlay_hints(
77 .arms() 77 .arms()
78 .map(|match_arm| match_arm.pats()) 78 .map(|match_arm| match_arm.pats())
79 .flatten() 79 .flatten()
80 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options)) 80 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length))
81 .flatten() 81 .flatten()
82 .collect(), 82 .collect(),
83 ) 83 )
@@ -92,7 +92,7 @@ fn get_pat_type_hints(
92 analyzer: &SourceAnalyzer, 92 analyzer: &SourceAnalyzer,
93 root_pat: ast::Pat, 93 root_pat: ast::Pat,
94 skip_root_pat_hint: bool, 94 skip_root_pat_hint: bool,
95 truncate_options: &TruncateOptions, 95 max_inlay_hint_length: Option<usize>,
96) -> Vec<InlayHint> { 96) -> Vec<InlayHint> {
97 let original_pat = &root_pat.clone(); 97 let original_pat = &root_pat.clone();
98 98
@@ -109,7 +109,7 @@ fn get_pat_type_hints(
109 .map(|(range, pat_type)| InlayHint { 109 .map(|(range, pat_type)| InlayHint {
110 range, 110 range,
111 kind: InlayKind::TypeHint, 111 kind: InlayKind::TypeHint,
112 label: pat_type.display_truncated(db, truncate_options).to_string().into(), 112 label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(),
113 }) 113 })
114 .collect() 114 .collect()
115} 115}
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 875919e60..779a81b2c 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -349,10 +349,8 @@ impl Analysis {
349 file_id: FileId, 349 file_id: FileId,
350 max_inlay_hint_length: Option<usize>, 350 max_inlay_hint_length: Option<usize>,
351 ) -> Cancelable<Vec<InlayHint>> { 351 ) -> Cancelable<Vec<InlayHint>> {
352 let truncate_options =
353 hir::TruncateOptions { max_length: max_inlay_hint_length, show_default_types: false };
354 self.with_db(|db| { 352 self.with_db(|db| {
355 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options) 353 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length)
356 }) 354 })
357 } 355 }
358 356