Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion crates/ide/src/inlay_hints/closing_brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(super) fn hints(
sema: &Semantics<'_, RootDatabase>,
config: &InlayHintsConfig,
file_id: EditionedFileId,
node: SyntaxNode,
mut node: SyntaxNode,
) -> Option<()> {
let min_lines = config.closing_brace_hints_min_lines?;

Expand Down Expand Up @@ -52,6 +52,14 @@ pub(super) fn hints(

let module = ast::Module::cast(list.syntax().parent()?)?;
(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()) {
closing_token = block.stmt_list()?.r_curly_token()?;

Expand Down Expand Up @@ -189,6 +197,29 @@ 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
"#,
);
}
Expand Down