insert braces for closure

This commit is contained in:
jnyfah 2025-03-13 09:04:47 +01:00
parent 8148fa9324
commit 7bcf2506f7
2 changed files with 32 additions and 5 deletions

View file

@ -1140,12 +1140,11 @@ fn test() {
#[test]
fn no_edit_for_closure_return_without_body_block() {
// We can lift this limitation; see FIXME in closure_ret module.
let config = InlayHintsConfig {
closure_return_type_hints: ClosureReturnTypeHints::Always,
..TEST_CONFIG
};
check_no_edit(
check_edit(
config,
r#"
struct S<T>(T);
@ -1154,6 +1153,13 @@ fn test() {
let f = |a: S<usize>| S(a);
}
"#,
expect![[r#"
struct S<T>(T);
fn test() {
let f = || -> i32 { 3 };
let f = |a: S<usize>| -> S<S<usize>> { S(a) };
}
"#]],
);
}

View file

@ -1,8 +1,8 @@
//! Implementation of "closure return type" inlay hints.
//!
//! Tests live in [`bind_pat`][super::bind_pat] module.
use hir::DisplayTarget;
use ide_db::famous_defs::FamousDefs;
use hir::{DisplayTarget, HirDisplay};
use ide_db::{famous_defs::FamousDefs, text_edit::TextEdit};
use syntax::ast::{self, AstNode};
use crate::{
@ -62,7 +62,28 @@ pub(super) fn hints(
if arrow.is_none() { " -> " } else { "" },
)
} else {
None
let body = closure.body()?;
let body_range = body.syntax().text_range();
Some(config.lazy_text_edit(|| {
let mut builder = TextEdit::builder();
let insert_pos = param_list.syntax().text_range().end();
let rendered = match sema.scope(closure.syntax()).and_then(|scope| {
ty.display_source_code(scope.db, scope.module().into(), false).ok()
}) {
Some(rendered) => rendered,
None => return TextEdit::builder().finish(),
};
let arrow_text = if arrow.is_none() { " -> ".to_owned() } else { "".to_owned() };
builder.insert(insert_pos, arrow_text);
builder.insert(insert_pos, rendered);
builder.insert(body_range.start(), "{ ".to_owned());
builder.insert(body_range.end(), " }".to_owned());
builder.finish()
}))
};
acc.push(InlayHint {