From 7bcf2506f7d5560d81cc3998953b1cd0f8019efd Mon Sep 17 00:00:00 2001 From: jnyfah Date: Thu, 13 Mar 2025 09:04:47 +0100 Subject: [PATCH 1/4] insert braces for closure --- .../crates/ide/src/inlay_hints/bind_pat.rs | 10 +++++-- .../crates/ide/src/inlay_hints/closure_ret.rs | 27 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs index aab8a3f87399..60abb7a44e0c 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/bind_pat.rs @@ -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); @@ -1154,6 +1153,13 @@ fn test() { let f = |a: S| S(a); } "#, + expect![[r#" + struct S(T); + fn test() { + let f = || -> i32 { 3 }; + let f = |a: S| -> S> { S(a) }; + } + "#]], ); } diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs index 61c9c25fe739..b9bbff333f59 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs @@ -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 { From 21ee875b4c619141af7347cff9853c7a71131bcf Mon Sep 17 00:00:00 2001 From: jnyfah Date: Thu, 13 Mar 2025 10:53:34 +0100 Subject: [PATCH 2/4] remove fixme --- .../rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs index b9bbff333f59..03835ec1f4cf 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs @@ -48,7 +48,6 @@ pub(super) fn hints( if arrow.is_none() { label.prepend_str(" -> "); } - // FIXME?: We could provide text edit to insert braces for closures with non-block body. let text_edit = if has_block_body { ty_to_text_edit( sema, From 78a6572616c9a041b955abe18d438f85efc0e5e5 Mon Sep 17 00:00:00 2001 From: jnyfah Date: Mon, 17 Mar 2025 16:48:46 +0100 Subject: [PATCH 3/4] some changes --- .../rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs index 03835ec1f4cf..22b8c722984c 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs @@ -61,10 +61,9 @@ pub(super) fn hints( if arrow.is_none() { " -> " } else { "" }, ) } else { - let body = closure.body()?; - let body_range = body.syntax().text_range(); - Some(config.lazy_text_edit(|| { + let body = closure.body(); + let body_range = body.expect("Closure must have a body").syntax().text_range(); let mut builder = TextEdit::builder(); let insert_pos = param_list.syntax().text_range().end(); From caff951a1cc90130b07d3a0a4caaae24989d36e6 Mon Sep 17 00:00:00 2001 From: jnyfah Date: Mon, 17 Mar 2025 18:03:21 +0100 Subject: [PATCH 4/4] No edit if no body --- .../rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs index 22b8c722984c..ac40a19a40a8 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_ret.rs @@ -63,7 +63,10 @@ pub(super) fn hints( } else { Some(config.lazy_text_edit(|| { let body = closure.body(); - let body_range = body.expect("Closure must have a body").syntax().text_range(); + let body_range = match body { + Some(body) => body.syntax().text_range(), + None => return TextEdit::builder().finish(), + }; let mut builder = TextEdit::builder(); let insert_pos = param_list.syntax().text_range().end();