Skip to content

Commit 289c402

Browse files
committed
feat: add tests for truncated inlay hints
1 parent 9dc4fe4 commit 289c402

File tree

4 files changed

+319
-5
lines changed

4 files changed

+319
-5
lines changed

crates/hir-ty/src/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ pub enum ClosureStyle {
556556
Hide,
557557
}
558558

559+
const TYPE_HINT_TRUNCATION: &str = "…";
560+
559561
impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
560562
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
561563
self.t.hir_fmt(&mut HirFormatter {
@@ -604,8 +606,6 @@ where
604606
}
605607
}
606608

607-
const TYPE_HINT_TRUNCATION: &str = "…";
608-
609609
impl<T: HirDisplay> HirDisplay for &T {
610610
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
611611
HirDisplay::hir_fmt(*self, f)

crates/ide/src/inlay_hints.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,12 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
726726
fn end_truncated(&mut self) {
727727
always!(self.in_truncated_part, "truncated is not started");
728728

729+
const HINT_TRUNCATION: &str = "…";
729730
if self.resolve {
730-
self.last_part = "…".to_owned();
731+
self.last_part = HINT_TRUNCATION.to_owned();
731732
self.tooltip = Some(LazyProperty::Lazy);
732733
} else {
733-
let mut tooltip = mem::replace(&mut self.last_part, "…".to_owned());
734+
let mut tooltip = mem::replace(&mut self.last_part, HINT_TRUNCATION.to_owned());
734735
tooltip.push_str("\n```");
735736
self.tooltip = Some(LazyProperty::Computed(InlayTooltip::Markdown(tooltip)));
736737
}
@@ -944,6 +945,15 @@ mod tests {
944945
lifetime_elision_hints: LifetimeElisionHints::Always,
945946
..DISABLED_CONFIG
946947
};
948+
pub(super) const TEST_CONFIG_WITH_TRUNCATION: InlayHintsConfig = InlayHintsConfig {
949+
type_hints: true,
950+
parameter_hints: true,
951+
chaining_hints: true,
952+
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
953+
binding_mode_hints: true,
954+
max_length: Some(10),
955+
..DISABLED_CONFIG
956+
};
947957

948958
#[track_caller]
949959
pub(super) fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ mod tests {
182182
use syntax::{TextRange, TextSize};
183183
use test_utils::extract_annotations;
184184

185-
use crate::{ClosureReturnTypeHints, fixture, inlay_hints::InlayHintsConfig};
185+
use crate::{
186+
ClosureReturnTypeHints, fixture,
187+
inlay_hints::{
188+
InlayHintsConfig,
189+
tests::{TEST_CONFIG_WITH_TRUNCATION, check_expect},
190+
},
191+
};
186192

187193
use crate::inlay_hints::tests::{
188194
DISABLED_CONFIG, TEST_CONFIG, check, check_edit, check_no_edit, check_with_config,
@@ -1257,4 +1263,124 @@ where
12571263
"#,
12581264
);
12591265
}
1266+
1267+
#[test]
1268+
fn hint_truncation_in_type_hint() {
1269+
check_expect(
1270+
TEST_CONFIG_WITH_TRUNCATION,
1271+
r#"
1272+
#[derive(Copy)]
1273+
struct SSSSSSSSS<T: Copy, T2: Copy> {
1274+
a: T,
1275+
b: T2,
1276+
}
1277+
1278+
fn main() {
1279+
let s1 = SSSSSSSSS { a: 1, b: 2.0 };
1280+
let s2 = SSSSSSSSS { a: s1, b: s1 };
1281+
let s3 = SSSSSSSSS { a: s2, b: s2 };
1282+
}
1283+
"#,
1284+
expect![[r#"
1285+
[
1286+
(
1287+
98..100,
1288+
[
1289+
InlayHintLabelPart {
1290+
text: "SSSSSSSSS",
1291+
linked_location: Some(
1292+
Computed(
1293+
FileRangeWrapper {
1294+
file_id: FileId(
1295+
0,
1296+
),
1297+
range: 23..32,
1298+
},
1299+
),
1300+
),
1301+
tooltip: "",
1302+
},
1303+
"<",
1304+
InlayHintLabelPart {
1305+
text: "…",
1306+
linked_location: None,
1307+
tooltip: "```rust\ni32\n```",
1308+
},
1309+
", ",
1310+
InlayHintLabelPart {
1311+
text: "…",
1312+
linked_location: None,
1313+
tooltip: "```rust\nf64\n```",
1314+
},
1315+
">",
1316+
],
1317+
),
1318+
(
1319+
139..141,
1320+
[
1321+
InlayHintLabelPart {
1322+
text: "SSSSSSSSS",
1323+
linked_location: Some(
1324+
Computed(
1325+
FileRangeWrapper {
1326+
file_id: FileId(
1327+
0,
1328+
),
1329+
range: 23..32,
1330+
},
1331+
),
1332+
),
1333+
tooltip: "",
1334+
},
1335+
"<",
1336+
InlayHintLabelPart {
1337+
text: "…",
1338+
linked_location: None,
1339+
tooltip: "```rust\nSSSSSSSSS<i32, f64>\n```",
1340+
},
1341+
", ",
1342+
InlayHintLabelPart {
1343+
text: "…",
1344+
linked_location: None,
1345+
tooltip: "```rust\nSSSSSSSSS<i32, f64>\n```",
1346+
},
1347+
">",
1348+
],
1349+
),
1350+
(
1351+
180..182,
1352+
[
1353+
InlayHintLabelPart {
1354+
text: "SSSSSSSSS",
1355+
linked_location: Some(
1356+
Computed(
1357+
FileRangeWrapper {
1358+
file_id: FileId(
1359+
0,
1360+
),
1361+
range: 23..32,
1362+
},
1363+
),
1364+
),
1365+
tooltip: "",
1366+
},
1367+
"<",
1368+
InlayHintLabelPart {
1369+
text: "…",
1370+
linked_location: None,
1371+
tooltip: "```rust\nSSSSSSSSS<SSSSSSSSS<i32, f64>, SSSSSSSSS<i32, f64>>\n```",
1372+
},
1373+
", ",
1374+
InlayHintLabelPart {
1375+
text: "…",
1376+
linked_location: None,
1377+
tooltip: "```rust\nSSSSSSSSS<SSSSSSSSS<i32, f64>, SSSSSSSSS<i32, f64>>\n```",
1378+
},
1379+
">",
1380+
],
1381+
),
1382+
]
1383+
"#]],
1384+
)
1385+
}
12601386
}

crates/ide/src/inlay_hints/chaining.rs

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,184 @@ fn main() {
583583
);
584584
}
585585

586+
#[test]
587+
fn shorten_iterator_chaining_hints_truncated() {
588+
check_expect_clear_loc(
589+
InlayHintsConfig { chaining_hints: true, max_length: Some(10), ..DISABLED_CONFIG },
590+
r#"
591+
//- minicore: iterators
592+
use core::iter;
593+
594+
struct MyIter;
595+
596+
impl Iterator for MyIter {
597+
type Item = i32;
598+
fn next(&mut self) -> Option<Self::Item> {
599+
Some(42)
600+
}
601+
}
602+
603+
fn main() {
604+
let _x = MyIter.by_ref()
605+
.take(5)
606+
.by_ref()
607+
.take(5)
608+
.by_ref();
609+
}
610+
"#,
611+
expect![[r#"
612+
[
613+
(
614+
179..246,
615+
[
616+
"impl ",
617+
InlayHintLabelPart {
618+
text: "Iterator",
619+
linked_location: Some(
620+
Computed(
621+
FileRangeWrapper {
622+
file_id: FileId(
623+
1,
624+
),
625+
range: 0..0,
626+
},
627+
),
628+
),
629+
tooltip: "",
630+
},
631+
"<",
632+
InlayHintLabelPart {
633+
text: "Item",
634+
linked_location: Some(
635+
Computed(
636+
FileRangeWrapper {
637+
file_id: FileId(
638+
1,
639+
),
640+
range: 0..0,
641+
},
642+
),
643+
),
644+
tooltip: "",
645+
},
646+
" = ",
647+
InlayHintLabelPart {
648+
text: "…",
649+
linked_location: None,
650+
tooltip: "```rust\ni32\n```",
651+
},
652+
">",
653+
],
654+
),
655+
(
656+
179..229,
657+
[
658+
"impl ",
659+
InlayHintLabelPart {
660+
text: "Iterator",
661+
linked_location: Some(
662+
Computed(
663+
FileRangeWrapper {
664+
file_id: FileId(
665+
1,
666+
),
667+
range: 0..0,
668+
},
669+
),
670+
),
671+
tooltip: "",
672+
},
673+
"<",
674+
InlayHintLabelPart {
675+
text: "Item",
676+
linked_location: Some(
677+
Computed(
678+
FileRangeWrapper {
679+
file_id: FileId(
680+
1,
681+
),
682+
range: 0..0,
683+
},
684+
),
685+
),
686+
tooltip: "",
687+
},
688+
" = ",
689+
InlayHintLabelPart {
690+
text: "…",
691+
linked_location: None,
692+
tooltip: "```rust\ni32\n```",
693+
},
694+
">",
695+
],
696+
),
697+
(
698+
179..211,
699+
[
700+
"impl ",
701+
InlayHintLabelPart {
702+
text: "Iterator",
703+
linked_location: Some(
704+
Computed(
705+
FileRangeWrapper {
706+
file_id: FileId(
707+
1,
708+
),
709+
range: 0..0,
710+
},
711+
),
712+
),
713+
tooltip: "",
714+
},
715+
"<",
716+
InlayHintLabelPart {
717+
text: "Item",
718+
linked_location: Some(
719+
Computed(
720+
FileRangeWrapper {
721+
file_id: FileId(
722+
1,
723+
),
724+
range: 0..0,
725+
},
726+
),
727+
),
728+
tooltip: "",
729+
},
730+
" = ",
731+
InlayHintLabelPart {
732+
text: "…",
733+
linked_location: None,
734+
tooltip: "```rust\ni32\n```",
735+
},
736+
">",
737+
],
738+
),
739+
(
740+
179..194,
741+
[
742+
"&mut ",
743+
InlayHintLabelPart {
744+
text: "MyIter",
745+
linked_location: Some(
746+
Computed(
747+
FileRangeWrapper {
748+
file_id: FileId(
749+
0,
750+
),
751+
range: 0..0,
752+
},
753+
),
754+
),
755+
tooltip: "",
756+
},
757+
],
758+
),
759+
]
760+
"#]],
761+
);
762+
}
763+
586764
#[test]
587765
fn hints_in_attr_call() {
588766
check_expect(

0 commit comments

Comments
 (0)