diff options
Diffstat (limited to 'crates/ide/src/inlay_hints.rs')
-rw-r--r-- | crates/ide/src/inlay_hints.rs | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index a2039fcc7..4ceb20742 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs | |||
@@ -109,26 +109,31 @@ fn get_chaining_hints( | |||
109 | // Chaining can be defined as an expression whose next sibling tokens are newline and dot | 109 | // Chaining can be defined as an expression whose next sibling tokens are newline and dot |
110 | // Ignoring extra whitespace and comments | 110 | // Ignoring extra whitespace and comments |
111 | let next = tokens.next()?.kind(); | 111 | let next = tokens.next()?.kind(); |
112 | let next_next = tokens.next()?.kind(); | 112 | if next == SyntaxKind::WHITESPACE { |
113 | if next == SyntaxKind::WHITESPACE && next_next == T![.] { | 113 | let mut next_next = tokens.next()?.kind(); |
114 | let ty = sema.type_of_expr(&expr)?; | 114 | while next_next == SyntaxKind::WHITESPACE { |
115 | if ty.is_unknown() { | 115 | next_next = tokens.next()?.kind(); |
116 | return None; | ||
117 | } | 116 | } |
118 | if matches!(expr, ast::Expr::PathExpr(_)) { | 117 | if next_next == T![.] { |
119 | if let Some(hir::Adt::Struct(st)) = ty.as_adt() { | 118 | let ty = sema.type_of_expr(&expr)?; |
120 | if st.fields(sema.db).is_empty() { | 119 | if ty.is_unknown() { |
121 | return None; | 120 | return None; |
121 | } | ||
122 | if matches!(expr, ast::Expr::PathExpr(_)) { | ||
123 | if let Some(hir::Adt::Struct(st)) = ty.as_adt() { | ||
124 | if st.fields(sema.db).is_empty() { | ||
125 | return None; | ||
126 | } | ||
122 | } | 127 | } |
123 | } | 128 | } |
129 | acc.push(InlayHint { | ||
130 | range: expr.syntax().text_range(), | ||
131 | kind: InlayKind::ChainingHint, | ||
132 | label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| { | ||
133 | ty.display_truncated(sema.db, config.max_length).to_string().into() | ||
134 | }), | ||
135 | }); | ||
124 | } | 136 | } |
125 | acc.push(InlayHint { | ||
126 | range: expr.syntax().text_range(), | ||
127 | kind: InlayKind::ChainingHint, | ||
128 | label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| { | ||
129 | ty.display_truncated(sema.db, config.max_length).to_string().into() | ||
130 | }), | ||
131 | }); | ||
132 | } | 137 | } |
133 | Some(()) | 138 | Some(()) |
134 | } | 139 | } |
@@ -411,13 +416,16 @@ fn get_string_representation(expr: &ast::Expr) -> Option<String> { | |||
411 | match expr { | 416 | match expr { |
412 | ast::Expr::MethodCallExpr(method_call_expr) => { | 417 | ast::Expr::MethodCallExpr(method_call_expr) => { |
413 | let name_ref = method_call_expr.name_ref()?; | 418 | let name_ref = method_call_expr.name_ref()?; |
414 | match name_ref.text().as_str() { | 419 | match name_ref.text() { |
415 | "clone" => method_call_expr.receiver().map(|rec| rec.to_string()), | 420 | "clone" => method_call_expr.receiver().map(|rec| rec.to_string()), |
416 | name_ref => Some(name_ref.to_owned()), | 421 | name_ref => Some(name_ref.to_owned()), |
417 | } | 422 | } |
418 | } | 423 | } |
424 | ast::Expr::FieldExpr(field_expr) => Some(field_expr.name_ref()?.to_string()), | ||
425 | ast::Expr::PathExpr(path_expr) => Some(path_expr.to_string()), | ||
426 | ast::Expr::PrefixExpr(prefix_expr) => get_string_representation(&prefix_expr.expr()?), | ||
419 | ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?), | 427 | ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?), |
420 | _ => Some(expr.to_string()), | 428 | _ => None, |
421 | } | 429 | } |
422 | } | 430 | } |
423 | 431 | ||
@@ -651,6 +659,7 @@ fn main() { | |||
651 | let test = "test"; | 659 | let test = "test"; |
652 | //^^^^ &str | 660 | //^^^^ &str |
653 | let test = InnerStruct {}; | 661 | let test = InnerStruct {}; |
662 | //^^^^ InnerStruct | ||
654 | 663 | ||
655 | let test = unresolved(); | 664 | let test = unresolved(); |
656 | 665 | ||
@@ -979,6 +988,7 @@ struct C; | |||
979 | fn main() { | 988 | fn main() { |
980 | let c = A(B(C)) | 989 | let c = A(B(C)) |
981 | .into_b() // This is a comment | 990 | .into_b() // This is a comment |
991 | // This is another comment | ||
982 | .into_c(); | 992 | .into_c(); |
983 | } | 993 | } |
984 | "#, | 994 | "#, |
@@ -1438,4 +1448,19 @@ fn main() { | |||
1438 | "#, | 1448 | "#, |
1439 | ) | 1449 | ) |
1440 | } | 1450 | } |
1451 | |||
1452 | #[test] | ||
1453 | fn param_name_hints_show_for_literals() { | ||
1454 | check( | ||
1455 | r#"pub fn test(a: i32, b: i32) -> [i32; 2] { [a, b] } | ||
1456 | fn main() { | ||
1457 | test( | ||
1458 | 0x0fab272b, | ||
1459 | //^^^^^^^^^^ a | ||
1460 | 0x0fab272b | ||
1461 | //^^^^^^^^^^ b | ||
1462 | ); | ||
1463 | }"#, | ||
1464 | ) | ||
1465 | } | ||
1441 | } | 1466 | } |