aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/inlay_hints.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/inlay_hints.rs')
-rw-r--r--crates/ra_ide_api/src/inlay_hints.rs470
1 files changed, 394 insertions, 76 deletions
diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs
index 174662beb..a524e014f 100644
--- a/crates/ra_ide_api/src/inlay_hints.rs
+++ b/crates/ra_ide_api/src/inlay_hints.rs
@@ -1,16 +1,22 @@
1use crate::{db::RootDatabase, FileId}; 1use crate::{db::RootDatabase, FileId};
2use hir::{HirDisplay, Ty}; 2use hir::{HirDisplay, SourceAnalyzer, Ty};
3use ra_syntax::ast::Pat;
4use ra_syntax::{ 3use ra_syntax::{
5 algo::visit::{visitor, Visitor}, 4 algo::visit::{visitor, Visitor},
6 ast::{self, PatKind, TypeAscriptionOwner}, 5 ast::{
7 AstNode, SmolStr, SourceFile, SyntaxNode, TextRange, 6 AstNode, ForExpr, IfExpr, LambdaExpr, LetStmt, MatchArmList, Pat, PatKind, SourceFile,
7 TypeAscriptionOwner, WhileExpr,
8 },
9 SmolStr, SyntaxKind, SyntaxNode, TextRange,
8}; 10};
9 11
10#[derive(Debug, PartialEq, Eq)] 12#[derive(Debug, PartialEq, Eq, Clone)]
11pub enum InlayKind { 13pub enum InlayKind {
12 LetBindingType, 14 LetBindingType,
13 ClosureParameterType, 15 ClosureParameterType,
16 ForExpressionBindingType,
17 IfExpressionType,
18 WhileLetExpressionType,
19 MatchArmType,
14} 20}
15 21
16#[derive(Debug)] 22#[derive(Debug)]
@@ -34,68 +40,142 @@ fn get_inlay_hints(
34 node: &SyntaxNode, 40 node: &SyntaxNode,
35) -> Option<Vec<InlayHint>> { 41) -> Option<Vec<InlayHint>> {
36 visitor() 42 visitor()
37 .visit(|let_statement: ast::LetStmt| { 43 .visit(|let_statement: LetStmt| {
38 let let_syntax = let_statement.syntax();
39
40 if let_statement.ascribed_type().is_some() { 44 if let_statement.ascribed_type().is_some() {
41 return None; 45 return None;
42 } 46 }
43 47 let pat = let_statement.pat()?;
44 let let_pat = let_statement.pat()?; 48 let analyzer = SourceAnalyzer::new(db, file_id, let_statement.syntax(), None);
45 let inlay_type_string = get_node_displayable_type(db, file_id, let_syntax, &let_pat)? 49 Some(get_pat_hints(db, &analyzer, pat, InlayKind::LetBindingType, false))
46 .display(db)
47 .to_string()
48 .into();
49
50 let pat_range = match let_pat.kind() {
51 PatKind::BindPat(bind_pat) => bind_pat.syntax().text_range(),
52 PatKind::TuplePat(tuple_pat) => tuple_pat.syntax().text_range(),
53 _ => return None,
54 };
55
56 Some(vec![InlayHint {
57 range: pat_range,
58 kind: InlayKind::LetBindingType,
59 label: inlay_type_string,
60 }])
61 }) 50 })
62 .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { 51 .visit(|closure_parameter: LambdaExpr| {
63 Some(param_list) => Some( 52 let analyzer = SourceAnalyzer::new(db, file_id, closure_parameter.syntax(), None);
53 closure_parameter.param_list().map(|param_list| {
64 param_list 54 param_list
65 .params() 55 .params()
66 .filter(|closure_param| closure_param.ascribed_type().is_none()) 56 .filter(|closure_param| closure_param.ascribed_type().is_none())
67 .filter_map(|closure_param| { 57 .filter_map(|closure_param| closure_param.pat())
68 let closure_param_syntax = closure_param.syntax(); 58 .map(|root_pat| {
69 let inlay_type_string = get_node_displayable_type( 59 get_pat_hints(
70 db, 60 db,
71 file_id, 61 &analyzer,
72 closure_param_syntax, 62 root_pat,
73 &closure_param.pat()?, 63 InlayKind::ClosureParameterType,
74 )? 64 false,
75 .display(db) 65 )
76 .to_string()
77 .into();
78
79 Some(InlayHint {
80 range: closure_param_syntax.text_range(),
81 kind: InlayKind::ClosureParameterType,
82 label: inlay_type_string,
83 })
84 }) 66 })
67 .flatten()
68 .collect()
69 })
70 })
71 .visit(|for_expression: ForExpr| {
72 let pat = for_expression.pat()?;
73 let analyzer = SourceAnalyzer::new(db, file_id, for_expression.syntax(), None);
74 Some(get_pat_hints(db, &analyzer, pat, InlayKind::ForExpressionBindingType, false))
75 })
76 .visit(|if_expr: IfExpr| {
77 let pat = if_expr.condition()?.pat()?;
78 let analyzer = SourceAnalyzer::new(db, file_id, if_expr.syntax(), None);
79 Some(get_pat_hints(db, &analyzer, pat, InlayKind::IfExpressionType, true))
80 })
81 .visit(|while_expr: WhileExpr| {
82 let pat = while_expr.condition()?.pat()?;
83 let analyzer = SourceAnalyzer::new(db, file_id, while_expr.syntax(), None);
84 Some(get_pat_hints(db, &analyzer, pat, InlayKind::WhileLetExpressionType, true))
85 })
86 .visit(|match_arm_list: MatchArmList| {
87 let analyzer = SourceAnalyzer::new(db, file_id, match_arm_list.syntax(), None);
88 Some(
89 match_arm_list
90 .arms()
91 .map(|match_arm| match_arm.pats())
92 .flatten()
93 .map(|root_pat| {
94 get_pat_hints(db, &analyzer, root_pat, InlayKind::MatchArmType, true)
95 })
96 .flatten()
85 .collect(), 97 .collect(),
86 ), 98 )
87 None => None,
88 }) 99 })
89 .accept(&node)? 100 .accept(&node)?
90} 101}
91 102
103fn get_pat_hints(
104 db: &RootDatabase,
105 analyzer: &SourceAnalyzer,
106 root_pat: Pat,
107 kind: InlayKind,
108 skip_root_pat_hint: bool,
109) -> Vec<InlayHint> {
110 let original_pat = &root_pat.clone();
111
112 get_leaf_pats(root_pat)
113 .into_iter()
114 .filter(|pat| !skip_root_pat_hint || pat != original_pat)
115 .filter_map(|pat| {
116 get_node_displayable_type(db, &analyzer, &pat)
117 .map(|pat_type| (pat.syntax().text_range(), pat_type))
118 })
119 .map(|(range, pat_type)| InlayHint {
120 range,
121 kind: kind.clone(),
122 label: pat_type.display(db).to_string().into(),
123 })
124 .collect()
125}
126
127fn get_leaf_pats(root_pat: Pat) -> Vec<Pat> {
128 let mut pats_to_process = std::collections::VecDeque::<Pat>::new();
129 pats_to_process.push_back(root_pat);
130
131 let mut leaf_pats = Vec::new();
132
133 while let Some(maybe_leaf_pat) = pats_to_process.pop_front() {
134 match maybe_leaf_pat.kind() {
135 PatKind::BindPat(bind_pat) => {
136 if let Some(pat) = bind_pat.pat() {
137 pats_to_process.push_back(pat);
138 } else {
139 leaf_pats.push(maybe_leaf_pat);
140 }
141 }
142 PatKind::TuplePat(tuple_pat) => {
143 for arg_pat in tuple_pat.args() {
144 pats_to_process.push_back(arg_pat);
145 }
146 }
147 PatKind::StructPat(struct_pat) => {
148 if let Some(pat_list) = struct_pat.field_pat_list() {
149 pats_to_process.extend(
150 pat_list
151 .field_pats()
152 .filter_map(|field_pat| {
153 field_pat
154 .pat()
155 .filter(|pat| pat.syntax().kind() != SyntaxKind::BIND_PAT)
156 })
157 .chain(pat_list.bind_pats().map(|bind_pat| {
158 bind_pat.pat().unwrap_or_else(|| Pat::from(bind_pat))
159 })),
160 );
161 }
162 }
163 PatKind::TupleStructPat(tuple_struct_pat) => {
164 for arg_pat in tuple_struct_pat.args() {
165 pats_to_process.push_back(arg_pat);
166 }
167 }
168 _ => (),
169 }
170 }
171 leaf_pats
172}
173
92fn get_node_displayable_type( 174fn get_node_displayable_type(
93 db: &RootDatabase, 175 db: &RootDatabase,
94 file_id: FileId, 176 analyzer: &SourceAnalyzer,
95 node_syntax: &SyntaxNode,
96 node_pat: &Pat, 177 node_pat: &Pat,
97) -> Option<Ty> { 178) -> Option<Ty> {
98 let analyzer = hir::SourceAnalyzer::new(db, file_id, node_syntax, None);
99 analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| { 179 analyzer.type_of_pat(db, node_pat).and_then(|resolved_type| {
100 if let Ty::Apply(_) = resolved_type { 180 if let Ty::Apply(_) = resolved_type {
101 Some(resolved_type) 181 Some(resolved_type)
@@ -111,68 +191,306 @@ mod tests {
111 use insta::assert_debug_snapshot_matches; 191 use insta::assert_debug_snapshot_matches;
112 192
113 #[test] 193 #[test]
114 fn test_inlay_hints() { 194 fn let_statement() {
115 let (analysis, file_id) = single_file( 195 let (analysis, file_id) = single_file(
116 r#" 196 r#"
117struct OuterStruct {} 197#[derive(PartialEq)]
198enum CustomOption<T> {
199 None,
200 Some(T),
201}
202
203#[derive(PartialEq)]
204struct Test {
205 a: CustomOption<u32>,
206 b: u8,
207}
118 208
119fn main() { 209fn main() {
120 struct InnerStruct {} 210 struct InnerStruct {}
121 211
122 let test = 54; 212 let test = 54;
123 let test = InnerStruct {}; 213 let test: i32 = 33;
124 let test = OuterStruct {};
125 let test = vec![222];
126 let mut test = Vec::new();
127 test.push(333);
128 let test = test.into_iter().map(|i| i * i).collect::<Vec<_>>();
129 let mut test = 33; 214 let mut test = 33;
130 let _ = 22; 215 let _ = 22;
131 let test: Vec<_> = (0..3).collect(); 216 let test = "test";
217 let test = InnerStruct {};
132 218
133 let _ = (0..23).map(|i: u32| { 219 let test = vec![222];
134 let i_squared = i * i; 220 let test: Vec<_> = (0..3).collect();
135 i_squared 221 let test = (0..3).collect::<Vec<i128>>();
136 }); 222 let test = (0..3).collect::<Vec<_>>();
137 223
138 let test: i32 = 33; 224 let mut test = Vec::new();
225 test.push(333);
139 226
140 let (x, c) = (42, 'a');
141 let test = (42, 'a'); 227 let test = (42, 'a');
142} 228 let (a, (b, c, (d, e), f)) = (2, (3, 4, (6.6, 7.7), 5));
143"#, 229}"#,
144 ); 230 );
145 231
146 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ 232 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
147 InlayHint { 233 InlayHint {
148 range: [71; 75), 234 range: [193; 197),
149 kind: LetBindingType, 235 kind: LetBindingType,
150 label: "i32", 236 label: "i32",
151 }, 237 },
152 InlayHint { 238 InlayHint {
153 range: [121; 125), 239 range: [236; 244),
154 kind: LetBindingType, 240 kind: LetBindingType,
155 label: "OuterStruct", 241 label: "i32",
156 }, 242 },
157 InlayHint { 243 InlayHint {
158 range: [297; 305), 244 range: [275; 279),
245 kind: LetBindingType,
246 label: "&str",
247 },
248 InlayHint {
249 range: [539; 543),
250 kind: LetBindingType,
251 label: "(i32, char)",
252 },
253 InlayHint {
254 range: [566; 567),
159 kind: LetBindingType, 255 kind: LetBindingType,
160 label: "i32", 256 label: "i32",
161 }, 257 },
162 InlayHint { 258 InlayHint {
163 range: [417; 426), 259 range: [570; 571),
164 kind: LetBindingType, 260 kind: LetBindingType,
165 label: "u32", 261 label: "i32",
166 }, 262 },
167 InlayHint { 263 InlayHint {
168 range: [496; 502), 264 range: [573; 574),
169 kind: LetBindingType, 265 kind: LetBindingType,
170 label: "(i32, char)", 266 label: "i32",
171 }, 267 },
172 InlayHint { 268 InlayHint {
173 range: [524; 528), 269 range: [584; 585),
174 kind: LetBindingType, 270 kind: LetBindingType,
175 label: "(i32, char)", 271 label: "i32",
272 },
273 InlayHint {
274 range: [577; 578),
275 kind: LetBindingType,
276 label: "f64",
277 },
278 InlayHint {
279 range: [580; 581),
280 kind: LetBindingType,
281 label: "f64",
282 },
283]"#
284 );
285 }
286
287 #[test]
288 fn closure_parameter() {
289 let (analysis, file_id) = single_file(
290 r#"
291fn main() {
292 let mut start = 0;
293 (0..2).for_each(|increment| {
294 start += increment;
295 })
296}"#,
297 );
298
299 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
300 InlayHint {
301 range: [21; 30),
302 kind: LetBindingType,
303 label: "i32",
304 },
305 InlayHint {
306 range: [57; 66),
307 kind: ClosureParameterType,
308 label: "i32",
309 },
310]"#
311 );
312 }
313
314 #[test]
315 fn for_expression() {
316 let (analysis, file_id) = single_file(
317 r#"
318fn main() {
319 let mut start = 0;
320 for increment in 0..2 {
321 start += increment;
322 }
323}"#,
324 );
325
326 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
327 InlayHint {
328 range: [21; 30),
329 kind: LetBindingType,
330 label: "i32",
331 },
332 InlayHint {
333 range: [44; 53),
334 kind: ForExpressionBindingType,
335 label: "i32",
336 },
337]"#
338 );
339 }
340
341 #[test]
342 fn if_expr() {
343 let (analysis, file_id) = single_file(
344 r#"
345#[derive(PartialEq)]
346enum CustomOption<T> {
347 None,
348 Some(T),
349}
350
351#[derive(PartialEq)]
352struct Test {
353 a: CustomOption<u32>,
354 b: u8,
355}
356
357fn main() {
358 let test = CustomOption::Some(Test { a: CustomOption::Some(3), b: 1 });
359 if let CustomOption::None = &test {};
360 if let test = &test {};
361 if let CustomOption::Some(test) = &test {};
362 if let CustomOption::Some(Test { a, b }) = &test {};
363 if let CustomOption::Some(Test { a: x, b: y }) = &test {};
364 if let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {};
365 if let CustomOption::Some(Test { a: CustomOption::None, b: y }) = &test {};
366 if let CustomOption::Some(Test { b: y, .. }) = &test {};
367
368 if test == CustomOption::None {}
369}"#,
370 );
371
372 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
373 InlayHint {
374 range: [166; 170),
375 kind: LetBindingType,
376 label: "CustomOption<Test>",
377 },
378 InlayHint {
379 range: [334; 338),
380 kind: IfExpressionType,
381 label: "&Test",
382 },
383 InlayHint {
384 range: [389; 390),
385 kind: IfExpressionType,
386 label: "&CustomOption<u32>",
387 },
388 InlayHint {
389 range: [392; 393),
390 kind: IfExpressionType,
391 label: "&u8",
392 },
393 InlayHint {
394 range: [531; 532),
395 kind: IfExpressionType,
396 label: "&u32",
397 },
398]"#
399 );
400 }
401
402 #[test]
403 fn while_expr() {
404 let (analysis, file_id) = single_file(
405 r#"
406#[derive(PartialEq)]
407enum CustomOption<T> {
408 None,
409 Some(T),
410}
411
412#[derive(PartialEq)]
413struct Test {
414 a: CustomOption<u32>,
415 b: u8,
416}
417
418fn main() {
419 let test = CustomOption::Some(Test { a: CustomOption::Some(3), b: 1 });
420 while let CustomOption::None = &test {};
421 while let test = &test {};
422 while let CustomOption::Some(test) = &test {};
423 while let CustomOption::Some(Test { a, b }) = &test {};
424 while let CustomOption::Some(Test { a: x, b: y }) = &test {};
425 while let CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) = &test {};
426 while let CustomOption::Some(Test { a: CustomOption::None, b: y }) = &test {};
427 while let CustomOption::Some(Test { b: y, .. }) = &test {};
428
429 while test == CustomOption::None {}
430}"#,
431 );
432
433 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
434 InlayHint {
435 range: [166; 170),
436 kind: LetBindingType,
437 label: "CustomOption<Test>",
438 },
439]"#
440 );
441 }
442
443 #[test]
444 fn match_arm_list() {
445 let (analysis, file_id) = single_file(
446 r#"
447#[derive(PartialEq)]
448enum CustomOption<T> {
449 None,
450 Some(T),
451}
452
453#[derive(PartialEq)]
454struct Test {
455 a: CustomOption<u32>,
456 b: u8,
457}
458
459fn main() {
460 match CustomOption::Some(Test { a: CustomOption::Some(3), b: 1 }) {
461 CustomOption::None => (),
462 test => (),
463 CustomOption::Some(test) => (),
464 CustomOption::Some(Test { a, b }) => (),
465 CustomOption::Some(Test { a: x, b: y }) => (),
466 CustomOption::Some(Test { a: CustomOption::Some(x), b: y }) => (),
467 CustomOption::Some(Test { a: CustomOption::None, b: y }) => (),
468 CustomOption::Some(Test { b: y, .. }) => (),
469 _ => {}
470 }
471}"#,
472 );
473
474 assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[
475 InlayHint {
476 range: [312; 316),
477 kind: MatchArmType,
478 label: "Test",
479 },
480 InlayHint {
481 range: [359; 360),
482 kind: MatchArmType,
483 label: "CustomOption<u32>",
484 },
485 InlayHint {
486 range: [362; 363),
487 kind: MatchArmType,
488 label: "u8",
489 },
490 InlayHint {
491 range: [485; 486),
492 kind: MatchArmType,
493 label: "u32",
176 }, 494 },
177]"# 495]"#
178 ); 496 );