1
+ use std:: fmt;
2
+
1
3
use either:: Either ;
2
4
use hir:: { known, Callable , HasVisibility , HirDisplay , Mutability , Semantics , TypeInfo } ;
3
5
use ide_db:: {
@@ -69,7 +71,7 @@ pub enum InlayKind {
69
71
pub struct InlayHint {
70
72
pub range : TextRange ,
71
73
pub kind : InlayKind ,
72
- pub label : String ,
74
+ pub label : InlayHintLabel ,
73
75
pub tooltip : Option < InlayTooltip > ,
74
76
}
75
77
@@ -80,6 +82,78 @@ pub enum InlayTooltip {
80
82
HoverOffset ( FileId , TextSize ) ,
81
83
}
82
84
85
+ pub struct InlayHintLabel {
86
+ pub parts : Vec < InlayHintLabelPart > ,
87
+ }
88
+
89
+ impl InlayHintLabel {
90
+ pub fn as_simple_str ( & self ) -> Option < & str > {
91
+ match & * self . parts {
92
+ [ part] => part. as_simple_str ( ) ,
93
+ _ => None ,
94
+ }
95
+ }
96
+
97
+ pub fn prepend_str ( & mut self , s : & str ) {
98
+ match & mut * self . parts {
99
+ [ part, ..] if part. as_simple_str ( ) . is_some ( ) => part. text = format ! ( "{s}{}" , part. text) ,
100
+ _ => self . parts . insert ( 0 , InlayHintLabelPart { text : s. into ( ) , linked_location : None } ) ,
101
+ }
102
+ }
103
+
104
+ pub fn append_str ( & mut self , s : & str ) {
105
+ match & mut * self . parts {
106
+ [ .., part] if part. as_simple_str ( ) . is_some ( ) => part. text . push_str ( s) ,
107
+ _ => self . parts . push ( InlayHintLabelPart { text : s. into ( ) , linked_location : None } ) ,
108
+ }
109
+ }
110
+ }
111
+
112
+ impl From < String > for InlayHintLabel {
113
+ fn from ( s : String ) -> Self {
114
+ Self { parts : vec ! [ InlayHintLabelPart { text: s, linked_location: None } ] }
115
+ }
116
+ }
117
+
118
+ impl fmt:: Display for InlayHintLabel {
119
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
120
+ write ! ( f, "{}" , self . parts. iter( ) . map( |part| & part. text) . format( "" ) )
121
+ }
122
+ }
123
+
124
+ impl fmt:: Debug for InlayHintLabel {
125
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
126
+ f. debug_list ( ) . entries ( & self . parts ) . finish ( )
127
+ }
128
+ }
129
+
130
+ pub struct InlayHintLabelPart {
131
+ pub text : String ,
132
+ pub linked_location : Option < FileRange > ,
133
+ }
134
+
135
+ impl InlayHintLabelPart {
136
+ pub fn as_simple_str ( & self ) -> Option < & str > {
137
+ match self {
138
+ Self { text, linked_location : None } => Some ( text) ,
139
+ _ => None ,
140
+ }
141
+ }
142
+ }
143
+
144
+ impl fmt:: Debug for InlayHintLabelPart {
145
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
146
+ match self . as_simple_str ( ) {
147
+ Some ( string) => string. fmt ( f) ,
148
+ None => f
149
+ . debug_struct ( "InlayHintLabelPart" )
150
+ . field ( "text" , & self . text )
151
+ . field ( "linked_location" , & self . linked_location )
152
+ . finish ( ) ,
153
+ }
154
+ }
155
+ }
156
+
83
157
// Feature: Inlay Hints
84
158
//
85
159
// rust-analyzer shows additional information inline with the source code.
@@ -281,7 +355,7 @@ fn closing_brace_hints(
281
355
acc. push ( InlayHint {
282
356
range : closing_token. text_range ( ) ,
283
357
kind : InlayKind :: ClosingBraceHint ,
284
- label,
358
+ label : label . into ( ) ,
285
359
tooltip : name_offset. map ( |it| InlayTooltip :: HoverOffset ( file_id, it) ) ,
286
360
} ) ;
287
361
@@ -311,7 +385,7 @@ fn implicit_static_hints(
311
385
acc. push ( InlayHint {
312
386
range : t. text_range ( ) ,
313
387
kind : InlayKind :: LifetimeHint ,
314
- label : "'static" . to_owned ( ) ,
388
+ label : "'static" . to_owned ( ) . into ( ) ,
315
389
tooltip : Some ( InlayTooltip :: String ( "Elided static lifetime" . into ( ) ) ) ,
316
390
} ) ;
317
391
}
@@ -329,10 +403,10 @@ fn fn_lifetime_fn_hints(
329
403
return None ;
330
404
}
331
405
332
- let mk_lt_hint = |t : SyntaxToken , label| InlayHint {
406
+ let mk_lt_hint = |t : SyntaxToken , label : String | InlayHint {
333
407
range : t. text_range ( ) ,
334
408
kind : InlayKind :: LifetimeHint ,
335
- label,
409
+ label : label . into ( ) ,
336
410
tooltip : Some ( InlayTooltip :: String ( "Elided lifetime" . into ( ) ) ) ,
337
411
} ;
338
412
@@ -486,7 +560,8 @@ fn fn_lifetime_fn_hints(
486
560
"{}{}" ,
487
561
allocated_lifetimes. iter( ) . format( ", " ) ,
488
562
if is_empty { "" } else { ", " }
489
- ) ,
563
+ )
564
+ . into ( ) ,
490
565
tooltip : Some ( InlayTooltip :: String ( "Elided lifetimes" . into ( ) ) ) ,
491
566
} ) ;
492
567
}
@@ -535,7 +610,8 @@ fn closure_ret_hints(
535
610
range : param_list. syntax ( ) . text_range ( ) ,
536
611
kind : InlayKind :: ClosureReturnTypeHint ,
537
612
label : hint_iterator ( sema, & famous_defs, config, & ty)
538
- . unwrap_or_else ( || ty. display_truncated ( sema. db , config. max_length ) . to_string ( ) ) ,
613
+ . unwrap_or_else ( || ty. display_truncated ( sema. db , config. max_length ) . to_string ( ) )
614
+ . into ( ) ,
539
615
tooltip : Some ( InlayTooltip :: HoverRanged ( file_id, param_list. syntax ( ) . text_range ( ) ) ) ,
540
616
} ) ;
541
617
Some ( ( ) )
@@ -562,7 +638,7 @@ fn reborrow_hints(
562
638
acc. push ( InlayHint {
563
639
range : expr. syntax ( ) . text_range ( ) ,
564
640
kind : InlayKind :: ImplicitReborrowHint ,
565
- label : label. to_string ( ) ,
641
+ label : label. to_string ( ) . into ( ) ,
566
642
tooltip : Some ( InlayTooltip :: String ( "Compiler inserted reborrow" . into ( ) ) ) ,
567
643
} ) ;
568
644
Some ( ( ) )
@@ -620,9 +696,9 @@ fn chaining_hints(
620
696
acc. push ( InlayHint {
621
697
range : expr. syntax ( ) . text_range ( ) ,
622
698
kind : InlayKind :: ChainingHint ,
623
- label : hint_iterator ( sema, & famous_defs, config, & ty) . unwrap_or_else ( || {
624
- ty. display_truncated ( sema. db , config. max_length ) . to_string ( )
625
- } ) ,
699
+ label : hint_iterator ( sema, & famous_defs, config, & ty)
700
+ . unwrap_or_else ( || ty. display_truncated ( sema. db , config. max_length ) . to_string ( ) )
701
+ . into ( ) ,
626
702
tooltip : Some ( InlayTooltip :: HoverRanged ( file_id, expr. syntax ( ) . text_range ( ) ) ) ,
627
703
} ) ;
628
704
}
@@ -674,7 +750,7 @@ fn param_name_hints(
674
750
InlayHint {
675
751
range,
676
752
kind : InlayKind :: ParameterHint ,
677
- label : param_name,
753
+ label : param_name. into ( ) ,
678
754
tooltip : tooltip. map ( |it| InlayTooltip :: HoverOffset ( it. file_id , it. range . start ( ) ) ) ,
679
755
}
680
756
} ) ;
@@ -705,7 +781,7 @@ fn binding_mode_hints(
705
781
acc. push ( InlayHint {
706
782
range,
707
783
kind : InlayKind :: BindingModeHint ,
708
- label : r. to_string ( ) ,
784
+ label : r. to_string ( ) . into ( ) ,
709
785
tooltip : Some ( InlayTooltip :: String ( "Inferred binding mode" . into ( ) ) ) ,
710
786
} ) ;
711
787
} ) ;
@@ -720,7 +796,7 @@ fn binding_mode_hints(
720
796
acc. push ( InlayHint {
721
797
range,
722
798
kind : InlayKind :: BindingModeHint ,
723
- label : bm. to_string ( ) ,
799
+ label : bm. to_string ( ) . into ( ) ,
724
800
tooltip : Some ( InlayTooltip :: String ( "Inferred binding mode" . into ( ) ) ) ,
725
801
} ) ;
726
802
}
@@ -772,7 +848,7 @@ fn bind_pat_hints(
772
848
None => pat. syntax ( ) . text_range ( ) ,
773
849
} ,
774
850
kind : InlayKind :: TypeHint ,
775
- label,
851
+ label : label . into ( ) ,
776
852
tooltip : pat
777
853
. name ( )
778
854
. map ( |it| it. syntax ( ) . text_range ( ) )
@@ -2223,7 +2299,9 @@ fn main() {
2223
2299
InlayHint {
2224
2300
range: 147..172,
2225
2301
kind: ChainingHint,
2226
- label: "B",
2302
+ label: [
2303
+ "B",
2304
+ ],
2227
2305
tooltip: Some(
2228
2306
HoverRanged(
2229
2307
FileId(
@@ -2236,7 +2314,9 @@ fn main() {
2236
2314
InlayHint {
2237
2315
range: 147..154,
2238
2316
kind: ChainingHint,
2239
- label: "A",
2317
+ label: [
2318
+ "A",
2319
+ ],
2240
2320
tooltip: Some(
2241
2321
HoverRanged(
2242
2322
FileId(
@@ -2294,7 +2374,9 @@ fn main() {
2294
2374
InlayHint {
2295
2375
range: 143..190,
2296
2376
kind: ChainingHint,
2297
- label: "C",
2377
+ label: [
2378
+ "C",
2379
+ ],
2298
2380
tooltip: Some(
2299
2381
HoverRanged(
2300
2382
FileId(
@@ -2307,7 +2389,9 @@ fn main() {
2307
2389
InlayHint {
2308
2390
range: 143..179,
2309
2391
kind: ChainingHint,
2310
- label: "B",
2392
+ label: [
2393
+ "B",
2394
+ ],
2311
2395
tooltip: Some(
2312
2396
HoverRanged(
2313
2397
FileId(
@@ -2350,7 +2434,9 @@ fn main() {
2350
2434
InlayHint {
2351
2435
range: 246..283,
2352
2436
kind: ChainingHint,
2353
- label: "B<X<i32, bool>>",
2437
+ label: [
2438
+ "B<X<i32, bool>>",
2439
+ ],
2354
2440
tooltip: Some(
2355
2441
HoverRanged(
2356
2442
FileId(
@@ -2363,7 +2449,9 @@ fn main() {
2363
2449
InlayHint {
2364
2450
range: 246..265,
2365
2451
kind: ChainingHint,
2366
- label: "A<X<i32, bool>>",
2452
+ label: [
2453
+ "A<X<i32, bool>>",
2454
+ ],
2367
2455
tooltip: Some(
2368
2456
HoverRanged(
2369
2457
FileId(
@@ -2408,7 +2496,9 @@ fn main() {
2408
2496
InlayHint {
2409
2497
range: 174..241,
2410
2498
kind: ChainingHint,
2411
- label: "impl Iterator<Item = ()>",
2499
+ label: [
2500
+ "impl Iterator<Item = ()>",
2501
+ ],
2412
2502
tooltip: Some(
2413
2503
HoverRanged(
2414
2504
FileId(
@@ -2421,7 +2511,9 @@ fn main() {
2421
2511
InlayHint {
2422
2512
range: 174..224,
2423
2513
kind: ChainingHint,
2424
- label: "impl Iterator<Item = ()>",
2514
+ label: [
2515
+ "impl Iterator<Item = ()>",
2516
+ ],
2425
2517
tooltip: Some(
2426
2518
HoverRanged(
2427
2519
FileId(
@@ -2434,7 +2526,9 @@ fn main() {
2434
2526
InlayHint {
2435
2527
range: 174..206,
2436
2528
kind: ChainingHint,
2437
- label: "impl Iterator<Item = ()>",
2529
+ label: [
2530
+ "impl Iterator<Item = ()>",
2531
+ ],
2438
2532
tooltip: Some(
2439
2533
HoverRanged(
2440
2534
FileId(
@@ -2447,7 +2541,9 @@ fn main() {
2447
2541
InlayHint {
2448
2542
range: 174..189,
2449
2543
kind: ChainingHint,
2450
- label: "&mut MyIter",
2544
+ label: [
2545
+ "&mut MyIter",
2546
+ ],
2451
2547
tooltip: Some(
2452
2548
HoverRanged(
2453
2549
FileId(
@@ -2489,7 +2585,9 @@ fn main() {
2489
2585
InlayHint {
2490
2586
range: 124..130,
2491
2587
kind: TypeHint,
2492
- label: "Struct",
2588
+ label: [
2589
+ "Struct",
2590
+ ],
2493
2591
tooltip: Some(
2494
2592
HoverRanged(
2495
2593
FileId(
@@ -2502,7 +2600,9 @@ fn main() {
2502
2600
InlayHint {
2503
2601
range: 145..185,
2504
2602
kind: ChainingHint,
2505
- label: "Struct",
2603
+ label: [
2604
+ "Struct",
2605
+ ],
2506
2606
tooltip: Some(
2507
2607
HoverRanged(
2508
2608
FileId(
@@ -2515,7 +2615,9 @@ fn main() {
2515
2615
InlayHint {
2516
2616
range: 145..168,
2517
2617
kind: ChainingHint,
2518
- label: "Struct",
2618
+ label: [
2619
+ "Struct",
2620
+ ],
2519
2621
tooltip: Some(
2520
2622
HoverRanged(
2521
2623
FileId(
@@ -2528,7 +2630,9 @@ fn main() {
2528
2630
InlayHint {
2529
2631
range: 222..228,
2530
2632
kind: ParameterHint,
2531
- label: "self",
2633
+ label: [
2634
+ "self",
2635
+ ],
2532
2636
tooltip: Some(
2533
2637
HoverOffset(
2534
2638
FileId(
0 commit comments