aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/inlay_hints.rs90
-rw-r--r--crates/ra_ide/src/lib.rs7
2 files changed, 77 insertions, 20 deletions
diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs
index 3730121af..8674912a6 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}; 4use hir::{HirDisplay, SourceAnalyzer, TruncateOptions};
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 max_inlay_hint_length: Option<usize>, 26 truncate_options: &TruncateOptions,
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, max_inlay_hint_length).unwrap_or_default()) 30 .map(|node| get_inlay_hints(db, file_id, &node, truncate_options).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 max_inlay_hint_length: Option<usize>, 39 truncate_options: &TruncateOptions,
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, max_inlay_hint_length)) 49 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
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, max_inlay_hint_length)) 57 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, truncate_options))
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, max_inlay_hint_length)) 64 Some(get_pat_type_hints(db, &analyzer, pat, false, truncate_options))
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, max_inlay_hint_length)) 68 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
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, max_inlay_hint_length)) 72 Some(get_pat_type_hints(db, &analyzer, pat, true, truncate_options))
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, max_inlay_hint_length)) 80 .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, truncate_options))
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 max_inlay_hint_length: Option<usize>, 95 truncate_options: &TruncateOptions,
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, max_inlay_hint_length).to_string().into(), 112 label: pat_type.display_truncated(db, truncate_options).to_string().into(),
113 }) 113 })
114 .collect() 114 .collect()
115} 115}
@@ -160,6 +160,58 @@ mod tests {
160 use crate::mock_analysis::single_file; 160 use crate::mock_analysis::single_file;
161 161
162 #[test] 162 #[test]
163 fn default_generic_types_disabled() {
164 let (analysis, file_id) = single_file(
165 r#"
166struct Test<K, T = u8> {
167k: K,
168 t: T,
169}
170
171fn main() {
172 let zz = Test { t: 23, k: 33 };
173}"#,
174 );
175
176 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, false).unwrap(), @r###"
177 [
178 InlayHint {
179 range: [65; 67),
180 kind: TypeHint,
181 label: "Test<i32>",
182 },
183 ]
184 "###
185 );
186 }
187
188 #[test]
189 fn default_generic_types_enabled() {
190 let (analysis, file_id) = single_file(
191 r#"
192struct Test<K, T = u8> {
193 k: K,
194 t: T,
195}
196
197fn main() {
198 let zz = Test { t: 23, k: 33 };
199}"#,
200 );
201
202 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
203 [
204 InlayHint {
205 range: [69; 71),
206 kind: TypeHint,
207 label: "Test<i32, u8>",
208 },
209 ]
210 "###
211 );
212 }
213
214 #[test]
163 fn let_statement() { 215 fn let_statement() {
164 let (analysis, file_id) = single_file( 216 let (analysis, file_id) = single_file(
165 r#" 217 r#"
@@ -199,7 +251,7 @@ fn main() {
199}"#, 251}"#,
200 ); 252 );
201 253
202 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 254 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
203 [ 255 [
204 InlayHint { 256 InlayHint {
205 range: [193; 197), 257 range: [193; 197),
@@ -273,7 +325,7 @@ fn main() {
273}"#, 325}"#,
274 ); 326 );
275 327
276 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 328 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
277 [ 329 [
278 InlayHint { 330 InlayHint {
279 range: [21; 30), 331 range: [21; 30),
@@ -302,7 +354,7 @@ fn main() {
302}"#, 354}"#,
303 ); 355 );
304 356
305 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 357 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
306 [ 358 [
307 InlayHint { 359 InlayHint {
308 range: [21; 30), 360 range: [21; 30),
@@ -350,7 +402,7 @@ fn main() {
350}"#, 402}"#,
351 ); 403 );
352 404
353 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 405 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
354 [ 406 [
355 InlayHint { 407 InlayHint {
356 range: [166; 170), 408 range: [166; 170),
@@ -413,7 +465,7 @@ fn main() {
413}"#, 465}"#,
414 ); 466 );
415 467
416 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 468 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
417 [ 469 [
418 InlayHint { 470 InlayHint {
419 range: [166; 170), 471 range: [166; 170),
@@ -476,7 +528,7 @@ fn main() {
476}"#, 528}"#,
477 ); 529 );
478 530
479 assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" 531 assert_debug_snapshot!(analysis.inlay_hints(file_id, None, true).unwrap(), @r###"
480 [ 532 [
481 InlayHint { 533 InlayHint {
482 range: [311; 315), 534 range: [311; 315),
@@ -518,7 +570,7 @@ fn main() {
518}"#, 570}"#,
519 ); 571 );
520 572
521 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" 573 assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8), true).unwrap(), @r###"
522 [ 574 [
523 InlayHint { 575 InlayHint {
524 range: [74; 75), 576 range: [74; 75),
diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs
index 779a81b2c..c3244a8dd 100644
--- a/crates/ra_ide/src/lib.rs
+++ b/crates/ra_ide/src/lib.rs
@@ -348,9 +348,14 @@ impl Analysis {
348 &self, 348 &self,
349 file_id: FileId, 349 file_id: FileId,
350 max_inlay_hint_length: Option<usize>, 350 max_inlay_hint_length: Option<usize>,
351 show_default_types_in_inlay_hints: bool,
351 ) -> Cancelable<Vec<InlayHint>> { 352 ) -> Cancelable<Vec<InlayHint>> {
353 let truncate_options = hir::TruncateOptions {
354 max_length: max_inlay_hint_length,
355 show_default_types: show_default_types_in_inlay_hints,
356 };
352 self.with_db(|db| { 357 self.with_db(|db| {
353 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) 358 inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), &truncate_options)
354 }) 359 })
355 } 360 }
356 361