1
1
//! Implementation of "closure return type" inlay hints.
2
2
//!
3
3
//! 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 } ;
6
6
use syntax:: ast:: { self , AstNode } ;
7
7
8
8
use crate :: {
@@ -48,7 +48,6 @@ pub(super) fn hints(
48
48
if arrow. is_none ( ) {
49
49
label. prepend_str ( " -> " ) ;
50
50
}
51
- // FIXME?: We could provide text edit to insert braces for closures with non-block body.
52
51
let text_edit = if has_block_body {
53
52
ty_to_text_edit (
54
53
sema,
@@ -62,7 +61,30 @@ pub(super) fn hints(
62
61
if arrow. is_none ( ) { " -> " } else { "" } ,
63
62
)
64
63
} 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
+ } ) )
66
88
} ;
67
89
68
90
acc. push ( InlayHint {
0 commit comments