diff options
author | Emil Lauridsen <[email protected]> | 2019-11-18 17:02:28 +0000 |
---|---|---|
committer | Emil Lauridsen <[email protected]> | 2019-11-19 16:23:50 +0000 |
commit | dadad36bb9770f9b13ed84bc219ea0168a7a5bf1 (patch) | |
tree | 00051540da204b4294501f3c56960975177ae502 /crates/ra_ide_api | |
parent | c24ee0990486b04723534f072d7a58e829bbd1bd (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_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/inlay_hints.rs | 37 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 10 |
2 files changed, 30 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..6cd492b2a 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), |
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 110ddcd62..fcb3da90e 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -338,8 +338,14 @@ impl Analysis { | |||
338 | } | 338 | } |
339 | 339 | ||
340 | /// Returns a list of the places in the file where type hints can be displayed. | 340 | /// Returns a list of the places in the file where type hints can be displayed. |
341 | pub fn inlay_hints(&self, file_id: FileId) -> Cancelable<Vec<InlayHint>> { | 341 | pub fn inlay_hints( |
342 | self.with_db(|db| inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree())) | 342 | &self, |
343 | file_id: FileId, | ||
344 | max_inlay_hint_length: Option<usize>, | ||
345 | ) -> Cancelable<Vec<InlayHint>> { | ||
346 | self.with_db(|db| { | ||
347 | inlay_hints::inlay_hints(db, file_id, &db.parse(file_id).tree(), max_inlay_hint_length) | ||
348 | }) | ||
343 | } | 349 | } |
344 | 350 | ||
345 | /// Returns the set of folding ranges. | 351 | /// Returns the set of folding ranges. |