Skip to content

Commit 406534f

Browse files
committed
feat: add tests for truncated inlay hints
1 parent 58840c3 commit 406534f

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
@@ -519,6 +519,8 @@ pub enum ClosureStyle {
519519
Hide,
520520
}
521521

522+
const TYPE_HINT_TRUNCATION: &str = "…";
523+
522524
impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
523525
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
524526
self.t.hir_fmt(&mut HirFormatter {
@@ -561,8 +563,6 @@ where
561563
}
562564
}
563565

564-
const TYPE_HINT_TRUNCATION: &str = "…";
565-
566566
impl<T: HirDisplay> HirDisplay for &T {
567567
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
568568
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
@@ -716,11 +716,12 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
716716
fn end_truncated(&mut self) {
717717
always!(self.in_truncated_part, "truncated is not started");
718718

719+
const HINT_TRUNCATION: &str = "…";
719720
if self.resolve {
720-
self.last_part = "…".to_owned();
721+
self.last_part = HINT_TRUNCATION.to_owned();
721722
self.tooltip = Some(LazyProperty::Lazy);
722723
} else {
723-
let mut tooltip = mem::replace(&mut self.last_part, "…".to_owned());
724+
let mut tooltip = mem::replace(&mut self.last_part, HINT_TRUNCATION.to_owned());
724725
tooltip.push_str("\n```");
725726
self.tooltip = Some(LazyProperty::Computed(InlayTooltip::Markdown(tooltip)));
726727
}
@@ -930,6 +931,15 @@ mod tests {
930931
lifetime_elision_hints: LifetimeElisionHints::Always,
931932
..DISABLED_CONFIG
932933
};
934+
pub(super) const TEST_CONFIG_WITH_TRUNCATION: InlayHintsConfig = InlayHintsConfig {
935+
type_hints: true,
936+
parameter_hints: true,
937+
chaining_hints: true,
938+
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
939+
binding_mode_hints: true,
940+
max_length: Some(10),
941+
..DISABLED_CONFIG
942+
};
933943

934944
#[track_caller]
935945
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
@@ -181,7 +181,13 @@ mod tests {
181181
use syntax::{TextRange, TextSize};
182182
use test_utils::extract_annotations;
183183

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

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

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)