Skip to content

Commit b3c848e

Browse files
bors[bot]unratito
andauthored
Merge #7703
7703: Allow comments between newlines in chaining hints r=Veykril a=unratito Currently, chaining hints are not generated if there are comments between newlines, which is a very common pattern: ```rust let vec = (0..10) // Multiply by 2 .map(|x| x * 2) // Add 3 .map(|x| x + 3) .collect::<Vec<i32>>(); ``` Besides, it seems a bit weird that this piece of code generates a chaining hint: ```rust let vec = (0..10) .collect::<Vec<i32>>(); ``` But this one doesn't: ```rust let vec = (0..10) // This is a comment .collect::<Vec<i32>>(); ``` Co-authored-by: Paco Soberón <[email protected]>
2 parents 94f56eb + e0eb80e commit b3c848e

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,31 @@ fn get_chaining_hints(
109109
// Chaining can be defined as an expression whose next sibling tokens are newline and dot
110110
// Ignoring extra whitespace and comments
111111
let next = tokens.next()?.kind();
112-
let next_next = tokens.next()?.kind();
113-
if next == SyntaxKind::WHITESPACE && next_next == T![.] {
114-
let ty = sema.type_of_expr(&expr)?;
115-
if ty.is_unknown() {
116-
return None;
112+
if next == SyntaxKind::WHITESPACE {
113+
let mut next_next = tokens.next()?.kind();
114+
while next_next == SyntaxKind::WHITESPACE {
115+
next_next = tokens.next()?.kind();
117116
}
118-
if matches!(expr, ast::Expr::PathExpr(_)) {
119-
if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
120-
if st.fields(sema.db).is_empty() {
121-
return None;
117+
if next_next == T![.] {
118+
let ty = sema.type_of_expr(&expr)?;
119+
if ty.is_unknown() {
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+
}
122127
}
123128
}
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+
});
124136
}
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-
});
132137
}
133138
Some(())
134139
}
@@ -983,6 +988,7 @@ struct C;
983988
fn main() {
984989
let c = A(B(C))
985990
.into_b() // This is a comment
991+
// This is another comment
986992
.into_c();
987993
}
988994
"#,

0 commit comments

Comments
 (0)