Skip to content

Commit 35443fa

Browse files
committed
insert braces for closure
1 parent 3fc655b commit 35443fa

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,11 @@ fn test() {
11401140

11411141
#[test]
11421142
fn no_edit_for_closure_return_without_body_block() {
1143-
// We can lift this limitation; see FIXME in closure_ret module.
11441143
let config = InlayHintsConfig {
11451144
closure_return_type_hints: ClosureReturnTypeHints::Always,
11461145
..TEST_CONFIG
11471146
};
1148-
check_no_edit(
1147+
check_edit(
11491148
config,
11501149
r#"
11511150
struct S<T>(T);
@@ -1154,6 +1153,13 @@ fn test() {
11541153
let f = |a: S<usize>| S(a);
11551154
}
11561155
"#,
1156+
expect![[r#"
1157+
struct S<T>(T);
1158+
fn test() {
1159+
let f = || -> i32 { 3 };
1160+
let f = |a: S<usize>| -> S<S<usize>> { S(a) };
1161+
}
1162+
"#]],
11571163
);
11581164
}
11591165

crates/ide/src/inlay_hints/closure_ret.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Implementation of "closure return type" inlay hints.
22
//!
33
//! Tests live in [`bind_pat`][super::bind_pat] module.
4-
use hir::DisplayTarget;
5-
use ide_db::famous_defs::FamousDefs;
4+
use hir::{DisplayTarget, HirDisplay};
5+
use ide_db::{famous_defs::FamousDefs, text_edit::TextEdit};
66
use syntax::ast::{self, AstNode};
77

88
use crate::{
@@ -62,7 +62,28 @@ pub(super) fn hints(
6262
if arrow.is_none() { " -> " } else { "" },
6363
)
6464
} else {
65-
None
65+
let body = closure.body()?;
66+
let body_range = body.syntax().text_range();
67+
68+
Some(config.lazy_text_edit(|| {
69+
let mut builder = TextEdit::builder();
70+
let insert_pos = param_list.syntax().text_range().end();
71+
72+
let rendered = match sema.scope(closure.syntax()).and_then(|scope| {
73+
ty.display_source_code(scope.db, scope.module().into(), false).ok()
74+
}) {
75+
Some(rendered) => rendered,
76+
None => return TextEdit::builder().finish(),
77+
};
78+
79+
let arrow_text = if arrow.is_none() { " -> ".to_owned() } else { "".to_owned() };
80+
builder.insert(insert_pos, arrow_text);
81+
builder.insert(insert_pos, rendered);
82+
builder.insert(body_range.start(), "{ ".to_owned());
83+
builder.insert(body_range.end(), " }".to_owned());
84+
85+
builder.finish()
86+
}))
6687
};
6788

6889
acc.push(InlayHint {

0 commit comments

Comments
 (0)