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 } ;
66use syntax:: ast:: { self , AstNode } ;
77
88use 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