Skip to content

Commit bc9c952

Browse files
committed
Auto merge of #16028 - Young-Flash:fix-issue-16012, r=HKalbasi
fix: make drop inlay hint more readable ![drop_inlay_hint](https://github.com/rust-lang/rust-analyzer/assets/71162630/bb18707f-3278-435d-a938-ccff4c685586) follow up #16000, close #16012
2 parents 4196675 + afc4075 commit bc9c952

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

crates/ide/src/inlay_hints/implicit_drop.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ pub(super) fn hints(
6262
match_ast! {
6363
match expr {
6464
ast::BlockExpr(x) => x.stmt_list().and_then(|x| x.r_curly_token()).map(|x| x.text_range()).unwrap_or_else(|| expr.text_range()),
65-
_ => expr.text_range(),
65+
// make the inlay hint appear after the semicolon if there is
66+
_ => {
67+
let nearest_semicolon = nearest_token_after_node(expr, syntax::SyntaxKind::SEMICOLON);
68+
nearest_semicolon.map(|x| x.text_range()).unwrap_or_else(|| expr.text_range())
69+
},
6670
}
6771
}
6872
}
@@ -95,7 +99,7 @@ pub(super) fn hints(
9599
label.append_str(")");
96100
acc.push(InlayHint {
97101
range,
98-
position: InlayHintPosition::Before,
102+
position: InlayHintPosition::After,
99103
pad_left: true,
100104
pad_right: true,
101105
kind: InlayKind::Drop,
@@ -109,6 +113,16 @@ pub(super) fn hints(
109113
Some(())
110114
}
111115

116+
fn nearest_token_after_node(
117+
node: &syntax::SyntaxNode,
118+
token_type: syntax::SyntaxKind,
119+
) -> Option<syntax::SyntaxToken> {
120+
node.siblings_with_tokens(syntax::Direction::Next)
121+
.filter_map(|it| it.as_token().map(|it| it.clone()))
122+
.filter(|it| it.kind() == token_type)
123+
.next()
124+
}
125+
112126
#[cfg(test)]
113127
mod tests {
114128
use crate::{
@@ -129,7 +143,7 @@ mod tests {
129143
let x = X;
130144
if 2 == 5 {
131145
return;
132-
//^^^^^^ drop(x)
146+
//^ drop(x)
133147
}
134148
}
135149
//^ drop(x)
@@ -176,7 +190,7 @@ mod tests {
176190
let x = X;
177191
let t_opt = Some(2);
178192
let t = t_opt?;
179-
//^^^^^^ drop(x)
193+
//^ drop(x)
180194
Some(())
181195
}
182196
//^ drop(x)

0 commit comments

Comments
 (0)