Auto merge of #17635 - Young-Flash:block_exp, r=lnicola

feat: add inlay hint support for block expr with lifetime label

![block_expr_with_label](https://github.com/user-attachments/assets/efede15b-d2ba-4aad-9775-a795b6cd473b)

close https://github.com/rust-lang/rust-analyzer/issues/17582
This commit is contained in:
bors 2024-07-20 05:11:10 +00:00
commit 6f84bdd06e

View file

@ -18,7 +18,7 @@ pub(super) fn hints(
sema: &Semantics<'_, RootDatabase>, sema: &Semantics<'_, RootDatabase>,
config: &InlayHintsConfig, config: &InlayHintsConfig,
file_id: EditionedFileId, file_id: EditionedFileId,
node: SyntaxNode, mut node: SyntaxNode,
) -> Option<()> { ) -> Option<()> {
let min_lines = config.closing_brace_hints_min_lines?; let min_lines = config.closing_brace_hints_min_lines?;
@ -52,6 +52,14 @@ pub(super) fn hints(
let module = ast::Module::cast(list.syntax().parent()?)?; let module = ast::Module::cast(list.syntax().parent()?)?;
(format!("mod {}", module.name()?), module.name().map(name)) (format!("mod {}", module.name()?), module.name().map(name))
} else if let Some(label) = ast::Label::cast(node.clone()) {
// in this case, `ast::Label` could be seen as a part of `ast::BlockExpr`
// the actual number of lines in this case should be the line count of the parent BlockExpr, which the `min_lines` config care about
node = node.parent()?;
let block = label.syntax().parent().and_then(ast::BlockExpr::cast)?;
closing_token = block.stmt_list()?.r_curly_token()?;
let lifetime = label.lifetime().map_or_else(String::new, |it| it.to_string());
(lifetime, Some(label.syntax().text_range()))
} else if let Some(block) = ast::BlockExpr::cast(node.clone()) { } else if let Some(block) = ast::BlockExpr::cast(node.clone()) {
closing_token = block.stmt_list()?.r_curly_token()?; closing_token = block.stmt_list()?.r_curly_token()?;
@ -189,6 +197,29 @@ fn f() {
]; ];
} }
//^ fn f //^ fn f
"#,
);
}
#[test]
fn hints_closing_brace_for_block_expr() {
check_with_config(
InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG },
r#"
fn test() {
'end: {
'do_a: {
'do_b: {
}
//^ 'do_b
break 'end;
}
//^ 'do_a
}
//^ 'end
}
//^ fn test
"#, "#,
); );
} }