diff options
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/inlay_hints.rs | 74 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 10 |
2 files changed, 67 insertions, 17 deletions
diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 0cd959848..24a7ca5e7 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs | |||
@@ -19,10 +19,15 @@ pub struct InlayHint { | |||
19 | pub label: SmolStr, | 19 | pub label: SmolStr, |
20 | } | 20 | } |
21 | 21 | ||
22 | pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec<InlayHint> { | 22 | pub(crate) fn inlay_hints( |
23 | db: &RootDatabase, | ||
24 | file_id: FileId, | ||
25 | file: &SourceFile, | ||
26 | max_inlay_hint_length: Option<usize>, | ||
27 | ) -> Vec<InlayHint> { | ||
23 | file.syntax() | 28 | file.syntax() |
24 | .descendants() | 29 | .descendants() |
25 | .map(|node| get_inlay_hints(db, file_id, &node).unwrap_or_default()) | 30 | .map(|node| get_inlay_hints(db, file_id, &node, max_inlay_hint_length).unwrap_or_default()) |
26 | .flatten() | 31 | .flatten() |
27 | .collect() | 32 | .collect() |
28 | } | 33 | } |
@@ -31,6 +36,7 @@ fn get_inlay_hints( | |||
31 | db: &RootDatabase, | 36 | db: &RootDatabase, |
32 | file_id: FileId, | 37 | file_id: FileId, |
33 | node: &SyntaxNode, | 38 | node: &SyntaxNode, |
39 | max_inlay_hint_length: Option<usize>, | ||
34 | ) -> Option<Vec<InlayHint>> { | 40 | ) -> Option<Vec<InlayHint>> { |
35 | let analyzer = SourceAnalyzer::new(db, hir::Source::new(file_id.into(), node), None); | 41 | let analyzer = SourceAnalyzer::new(db, hir::Source::new(file_id.into(), node), None); |
36 | match_ast! { | 42 | match_ast! { |
@@ -40,7 +46,7 @@ fn get_inlay_hints( | |||
40 | return None; | 46 | return None; |
41 | } | 47 | } |
42 | let pat = it.pat()?; | 48 | let pat = it.pat()?; |
43 | Some(get_pat_type_hints(db, &analyzer, pat, false)) | 49 | Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) |
44 | }, | 50 | }, |
45 | ast::LambdaExpr(it) => { | 51 | ast::LambdaExpr(it) => { |
46 | it.param_list().map(|param_list| { | 52 | it.param_list().map(|param_list| { |
@@ -48,22 +54,22 @@ fn get_inlay_hints( | |||
48 | .params() | 54 | .params() |
49 | .filter(|closure_param| closure_param.ascribed_type().is_none()) | 55 | .filter(|closure_param| closure_param.ascribed_type().is_none()) |
50 | .filter_map(|closure_param| closure_param.pat()) | 56 | .filter_map(|closure_param| closure_param.pat()) |
51 | .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false)) | 57 | .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, false, max_inlay_hint_length)) |
52 | .flatten() | 58 | .flatten() |
53 | .collect() | 59 | .collect() |
54 | }) | 60 | }) |
55 | }, | 61 | }, |
56 | ast::ForExpr(it) => { | 62 | ast::ForExpr(it) => { |
57 | let pat = it.pat()?; | 63 | let pat = it.pat()?; |
58 | Some(get_pat_type_hints(db, &analyzer, pat, false)) | 64 | Some(get_pat_type_hints(db, &analyzer, pat, false, max_inlay_hint_length)) |
59 | }, | 65 | }, |
60 | ast::IfExpr(it) => { | 66 | ast::IfExpr(it) => { |
61 | let pat = it.condition()?.pat()?; | 67 | let pat = it.condition()?.pat()?; |
62 | Some(get_pat_type_hints(db, &analyzer, pat, true)) | 68 | Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) |
63 | }, | 69 | }, |
64 | ast::WhileExpr(it) => { | 70 | ast::WhileExpr(it) => { |
65 | let pat = it.condition()?.pat()?; | 71 | let pat = it.condition()?.pat()?; |
66 | Some(get_pat_type_hints(db, &analyzer, pat, true)) | 72 | Some(get_pat_type_hints(db, &analyzer, pat, true, max_inlay_hint_length)) |
67 | }, | 73 | }, |
68 | ast::MatchArmList(it) => { | 74 | ast::MatchArmList(it) => { |
69 | Some( | 75 | Some( |
@@ -71,7 +77,7 @@ fn get_inlay_hints( | |||
71 | .arms() | 77 | .arms() |
72 | .map(|match_arm| match_arm.pats()) | 78 | .map(|match_arm| match_arm.pats()) |
73 | .flatten() | 79 | .flatten() |
74 | .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true)) | 80 | .map(|root_pat| get_pat_type_hints(db, &analyzer, root_pat, true, max_inlay_hint_length)) |
75 | .flatten() | 81 | .flatten() |
76 | .collect(), | 82 | .collect(), |
77 | ) | 83 | ) |
@@ -86,6 +92,7 @@ fn get_pat_type_hints( | |||
86 | analyzer: &SourceAnalyzer, | 92 | analyzer: &SourceAnalyzer, |
87 | root_pat: ast::Pat, | 93 | root_pat: ast::Pat, |
88 | skip_root_pat_hint: bool, | 94 | skip_root_pat_hint: bool, |
95 | max_inlay_hint_length: Option<usize>, | ||
89 | ) -> Vec<InlayHint> { | 96 | ) -> Vec<InlayHint> { |
90 | let original_pat = &root_pat.clone(); | 97 | let original_pat = &root_pat.clone(); |
91 | 98 | ||
@@ -99,7 +106,7 @@ fn get_pat_type_hints( | |||
99 | .map(|(range, pat_type)| InlayHint { | 106 | .map(|(range, pat_type)| InlayHint { |
100 | range, | 107 | range, |
101 | kind: InlayKind::TypeHint, | 108 | kind: InlayKind::TypeHint, |
102 | label: pat_type.display(db).to_string().into(), | 109 | label: pat_type.display_truncated(db, max_inlay_hint_length).to_string().into(), |
103 | }) | 110 | }) |
104 | .collect() | 111 | .collect() |
105 | } | 112 | } |
@@ -209,7 +216,7 @@ fn main() { | |||
209 | }"#, | 216 | }"#, |
210 | ); | 217 | ); |
211 | 218 | ||
212 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 219 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
213 | [ | 220 | [ |
214 | InlayHint { | 221 | InlayHint { |
215 | range: [193; 197), | 222 | range: [193; 197), |
@@ -278,7 +285,7 @@ fn main() { | |||
278 | }"#, | 285 | }"#, |
279 | ); | 286 | ); |
280 | 287 | ||
281 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 288 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
282 | [ | 289 | [ |
283 | InlayHint { | 290 | InlayHint { |
284 | range: [21; 30), | 291 | range: [21; 30), |
@@ -307,7 +314,7 @@ fn main() { | |||
307 | }"#, | 314 | }"#, |
308 | ); | 315 | ); |
309 | 316 | ||
310 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 317 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
311 | [ | 318 | [ |
312 | InlayHint { | 319 | InlayHint { |
313 | range: [21; 30), | 320 | range: [21; 30), |
@@ -355,7 +362,7 @@ fn main() { | |||
355 | }"#, | 362 | }"#, |
356 | ); | 363 | ); |
357 | 364 | ||
358 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 365 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
359 | [ | 366 | [ |
360 | InlayHint { | 367 | InlayHint { |
361 | range: [166; 170), | 368 | range: [166; 170), |
@@ -418,7 +425,7 @@ fn main() { | |||
418 | }"#, | 425 | }"#, |
419 | ); | 426 | ); |
420 | 427 | ||
421 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 428 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
422 | [ | 429 | [ |
423 | InlayHint { | 430 | InlayHint { |
424 | range: [166; 170), | 431 | range: [166; 170), |
@@ -481,7 +488,7 @@ fn main() { | |||
481 | }"#, | 488 | }"#, |
482 | ); | 489 | ); |
483 | 490 | ||
484 | assert_debug_snapshot!(analysis.inlay_hints(file_id).unwrap(), @r###" | 491 | assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" |
485 | [ | 492 | [ |
486 | InlayHint { | 493 | InlayHint { |
487 | range: [311; 315), | 494 | range: [311; 315), |
@@ -507,4 +514,41 @@ fn main() { | |||
507 | "### | 514 | "### |
508 | ); | 515 | ); |
509 | } | 516 | } |
517 | |||
518 | #[test] | ||
519 | fn hint_truncation() { | ||
520 | let (analysis, file_id) = single_file( | ||
521 | r#" | ||
522 | struct Smol<T>(T); | ||
523 | |||
524 | struct VeryLongOuterName<T>(T); | ||
525 | |||
526 | fn main() { | ||
527 | let a = Smol(0u32); | ||
528 | let b = VeryLongOuterName(0usize); | ||
529 | let c = Smol(Smol(0u32)) | ||
530 | }"#, | ||
531 | ); | ||
532 | |||
533 | assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" | ||
534 | [ | ||
535 | InlayHint { | ||
536 | range: [74; 75), | ||
537 | kind: TypeHint, | ||
538 | label: "Smol<u32>", | ||
539 | }, | ||
540 | InlayHint { | ||
541 | range: [98; 99), | ||
542 | kind: TypeHint, | ||
543 | label: "VeryLongOuterName<…>", | ||
544 | }, | ||
545 | InlayHint { | ||
546 | range: [137; 138), | ||
547 | kind: TypeHint, | ||
548 | label: "Smol<Smol<…>>", | ||
549 | }, | ||
550 | ] | ||
551 | "### | ||
552 | ); | ||
553 | } | ||
510 | } | 554 | } |
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 57ed97147..62ad996bc 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -344,8 +344,14 @@ impl Analysis { | |||
344 | } | 344 | } |
345 | 345 | ||
346 | /// Returns a list of the places in the file where type hints can be displayed. | 346 | /// Returns a list of the places in the file where type hints can be displayed. |
347 | pub fn inlay_hints(&self, file_id: FileId) -> Cancelable<Vec<InlayHint>> { | 347 | pub fn inlay_hints( |
348 | self.with_db(|db| inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree())) | 348 | &self, |
349 | file_id: FileId, | ||
350 | max_inlay_hint_length: Option<usize>, | ||
351 | ) -> Cancelable<Vec<InlayHint>> { | ||
352 | self.with_db(|db| { | ||
353 | inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) | ||
354 | }) | ||
349 | } | 355 | } |
350 | 356 | ||
351 | /// Returns the set of folding ranges. | 357 | /// Returns the set of folding ranges. |