Skip to content

Commit 3edfbcd

Browse files
authored
Merge pull request #19348 from jnyfah/some-branch
Add text edit support for return type hints on non-block body closures
2 parents d305b0b + a0638e5 commit 3edfbcd

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
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: 26 additions & 4 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::{
@@ -48,7 +48,6 @@ pub(super) fn hints(
4848
if arrow.is_none() {
4949
label.prepend_str(" -> ");
5050
}
51-
// FIXME?: We could provide text edit to insert braces for closures with non-block body.
5251
let text_edit = if has_block_body {
5352
ty_to_text_edit(
5453
sema,
@@ -62,7 +61,30 @@ pub(super) fn hints(
6261
if arrow.is_none() { " -> " } else { "" },
6362
)
6463
} else {
65-
None
64+
Some(config.lazy_text_edit(|| {
65+
let body = closure.body();
66+
let body_range = match body {
67+
Some(body) => body.syntax().text_range(),
68+
None => return TextEdit::builder().finish(),
69+
};
70+
let mut builder = TextEdit::builder();
71+
let insert_pos = param_list.syntax().text_range().end();
72+
73+
let rendered = match sema.scope(closure.syntax()).and_then(|scope| {
74+
ty.display_source_code(scope.db, scope.module().into(), false).ok()
75+
}) {
76+
Some(rendered) => rendered,
77+
None => return TextEdit::builder().finish(),
78+
};
79+
80+
let arrow_text = if arrow.is_none() { " -> ".to_owned() } else { "".to_owned() };
81+
builder.insert(insert_pos, arrow_text);
82+
builder.insert(insert_pos, rendered);
83+
builder.insert(body_range.start(), "{ ".to_owned());
84+
builder.insert(body_range.end(), " }".to_owned());
85+
86+
builder.finish()
87+
}))
6688
};
6789

6890
acc.push(InlayHint {

0 commit comments

Comments
 (0)