Skip to content

Commit 209e9b9

Browse files
committed
Shorten iterator chain hints
1 parent c133651 commit 209e9b9

File tree

2 files changed

+115
-43
lines changed

2 files changed

+115
-43
lines changed

crates/assists/src/utils.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,43 +281,70 @@ impl FamousDefs<'_, '_> {
281281
pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core
282282
pub mod convert {
283283
pub trait From<T> {
284-
fn from(T) -> Self;
284+
fn from(t: T) -> Self;
285285
}
286286
}
287287
288288
pub mod iter {
289289
pub use self::traits::{collect::IntoIterator, iterator::Iterator};
290290
mod traits {
291-
mod iterator {
291+
pub(crate) mod iterator {
292292
use crate::option::Option;
293293
pub trait Iterator {
294294
type Item;
295295
fn next(&mut self) -> Option<Self::Item>;
296+
fn by_ref(&mut self) -> &mut Self {
297+
self
298+
}
299+
fn take(self, n: usize) -> crate::iter::Take<Self> {
300+
crate::iter::Take { inner: self }
301+
}
302+
}
303+
304+
impl<I: Iterator> Iterator for &mut I {
305+
type Item = I::Item;
306+
fn next(&mut self) -> Option<I::Item> {
307+
(**self).next()
308+
}
296309
}
297310
}
298-
mod collect {
311+
pub(crate) mod collect {
299312
pub trait IntoIterator {
300313
type Item;
301314
}
302315
}
303316
}
304317
305318
pub use self::sources::*;
306-
mod sources {
319+
pub(crate) mod sources {
307320
use super::Iterator;
321+
use crate::option::Option::{self, *};
308322
pub struct Repeat<A> {
309323
element: A,
310324
}
311325
312-
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
326+
pub fn repeat<T>(elt: T) -> Repeat<T> {
313327
Repeat { element: elt }
314328
}
315329
316-
impl<A: Clone> Iterator for Repeat<A> {
330+
impl<A> Iterator for Repeat<A> {
317331
type Item = A;
318332
319333
fn next(&mut self) -> Option<A> {
320-
Some(self.element.clone())
334+
None
335+
}
336+
}
337+
}
338+
339+
pub use self::adapters::*;
340+
pub(crate) mod adapters {
341+
use super::Iterator;
342+
use crate::option::Option::{self, *};
343+
pub struct Take<I> { pub(crate) inner: I }
344+
impl<I> Iterator for Take<I> where I: Iterator {
345+
type Item = <I as Iterator>::Item;
346+
fn next(&mut self) -> Option<<I as Iterator>::Item> {
347+
None
321348
}
322349
}
323350
}

crates/ide/src/inlay_hints.rs

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,12 @@ fn get_chaining_hints(
126126
}
127127
}
128128
}
129-
let label = ty.display_truncated(sema.db, config.max_length).to_string();
130129
acc.push(InlayHint {
131130
range: expr.syntax().text_range(),
132131
kind: InlayKind::ChainingHint,
133-
label: label.into(),
132+
label: hint_iterator(sema, config, &ty).unwrap_or_else(|| {
133+
ty.display_truncated(sema.db, config.max_length).to_string().into()
134+
}),
134135
});
135136
}
136137
Some(())
@@ -193,17 +194,12 @@ fn get_bind_pat_hints(
193194
if should_not_display_type_hint(sema, &pat, &ty) {
194195
return None;
195196
}
196-
197-
let db = sema.db;
198-
if let Some(hint) = hint_iterator(sema, config, &ty, pat.clone()) {
199-
acc.push(hint);
200-
} else {
201-
acc.push(InlayHint {
202-
range: pat.syntax().text_range(),
203-
kind: InlayKind::TypeHint,
204-
label: ty.display_truncated(db, config.max_length).to_string().into(),
205-
});
206-
}
197+
acc.push(InlayHint {
198+
range: pat.syntax().text_range(),
199+
kind: InlayKind::TypeHint,
200+
label: hint_iterator(sema, config, &ty)
201+
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()),
202+
});
207203

208204
Some(())
209205
}
@@ -213,8 +209,7 @@ fn hint_iterator(
213209
sema: &Semantics<RootDatabase>,
214210
config: &InlayHintsConfig,
215211
ty: &Type,
216-
pat: ast::IdentPat,
217-
) -> Option<InlayHint> {
212+
) -> Option<SmolStr> {
218213
let db = sema.db;
219214
let strukt = ty.as_adt()?;
220215
let krate = strukt.krate(db)?;
@@ -244,11 +239,7 @@ fn hint_iterator(
244239
.max_length
245240
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
246241
);
247-
return Some(InlayHint {
248-
range: pat.syntax().text_range(),
249-
kind: InlayKind::TypeHint,
250-
label: format!("{}{}{}", LABEL_START, ty_display, LABEL_END).into(),
251-
});
242+
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END).into());
252243
}
253244
}
254245

@@ -412,7 +403,8 @@ mod tests {
412403
}
413404

414405
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
415-
let ra_fixture = format!("{}\n{}", ra_fixture, FamousDefs::FIXTURE);
406+
let ra_fixture =
407+
format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
416408
let (analysis, file_id) = fixture::file(&ra_fixture);
417409
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
418410
let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap();
@@ -422,7 +414,9 @@ mod tests {
422414
}
423415

424416
fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) {
425-
let (analysis, file_id) = fixture::file(ra_fixture);
417+
let ra_fixture =
418+
format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
419+
let (analysis, file_id) = fixture::file(&ra_fixture);
426420
let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap();
427421
expect.assert_debug_eq(&inlay_hints)
428422
}
@@ -854,12 +848,12 @@ fn main() {
854848
expect![[r#"
855849
[
856850
InlayHint {
857-
range: 147..172,
851+
range: 148..173,
858852
kind: ChainingHint,
859853
label: "B",
860854
},
861855
InlayHint {
862-
range: 147..154,
856+
range: 148..155,
863857
kind: ChainingHint,
864858
label: "A",
865859
},
@@ -920,12 +914,12 @@ fn main() {
920914
expect![[r#"
921915
[
922916
InlayHint {
923-
range: 143..190,
917+
range: 144..191,
924918
kind: ChainingHint,
925919
label: "C",
926920
},
927921
InlayHint {
928-
range: 143..179,
922+
range: 144..180,
929923
kind: ChainingHint,
930924
label: "B",
931925
},
@@ -965,12 +959,12 @@ fn main() {
965959
expect![[r#"
966960
[
967961
InlayHint {
968-
range: 246..283,
962+
range: 247..284,
969963
kind: ChainingHint,
970964
label: "B<X<i32, bool>>",
971965
},
972966
InlayHint {
973-
range: 246..265,
967+
range: 247..266,
974968
kind: ChainingHint,
975969
label: "A<X<i32, bool>>",
976970
},
@@ -991,7 +985,6 @@ fn main() {
991985
);
992986
check(
993987
r#"
994-
//- /main.rs crate:main deps:core
995988
pub struct Vec<T> {}
996989
997990
impl<T> Vec<T> {
@@ -1031,7 +1024,6 @@ mod collections {
10311024
fn complete_for_hint() {
10321025
check(
10331026
r#"
1034-
//- /main.rs crate:main deps:core
10351027
pub struct Vec<T> {}
10361028
10371029
impl<T> Vec<T> {
@@ -1078,7 +1070,6 @@ mod collections {
10781070
max_length: None,
10791071
},
10801072
r#"
1081-
//- /main.rs crate:main
10821073
pub struct Vec<T> {}
10831074
10841075
impl<T> Vec<T> {
@@ -1108,12 +1099,11 @@ fn main() {
11081099
InlayHintsConfig {
11091100
parameter_hints: false,
11101101
type_hints: true,
1111-
chaining_hints: true,
1102+
chaining_hints: false,
11121103
max_length: None,
11131104
},
11141105
r#"
1115-
//- /main.rs crate:main deps:std
1116-
use std::iter;
1106+
use core::iter;
11171107
11181108
struct MyIter;
11191109
@@ -1132,12 +1122,67 @@ fn main() {
11321122
fn generic<T: Clone>(t: T) {
11331123
let _x = iter::repeat(t);
11341124
//^^ impl Iterator<Item = T>
1125+
let _chained = iter::repeat(t).take(10);
1126+
//^^^^^^^^ impl Iterator<Item = T>
1127+
}
1128+
}
1129+
"#,
1130+
);
1131+
}
1132+
1133+
#[test]
1134+
fn shorten_iterator_chaining_hints() {
1135+
check_expect(
1136+
InlayHintsConfig {
1137+
parameter_hints: false,
1138+
type_hints: false,
1139+
chaining_hints: true,
1140+
max_length: None,
1141+
},
1142+
r#"
1143+
use core::iter;
1144+
1145+
struct MyIter;
1146+
1147+
impl Iterator for MyIter {
1148+
type Item = ();
1149+
fn next(&mut self) -> Option<Self::Item> {
1150+
None
11351151
}
11361152
}
11371153
1138-
//- /std.rs crate:std deps:core
1139-
use core::*;
1154+
fn main() {
1155+
let _x = MyIter.by_ref()
1156+
.take(5)
1157+
.by_ref()
1158+
.take(5)
1159+
.by_ref();
1160+
}
11401161
"#,
1162+
expect![[r#"
1163+
[
1164+
InlayHint {
1165+
range: 175..242,
1166+
kind: ChainingHint,
1167+
label: "impl Iterator<Item = ()>",
1168+
},
1169+
InlayHint {
1170+
range: 175..225,
1171+
kind: ChainingHint,
1172+
label: "&mut Take<&mut MyIter>",
1173+
},
1174+
InlayHint {
1175+
range: 175..207,
1176+
kind: ChainingHint,
1177+
label: "impl Iterator<Item = ()>",
1178+
},
1179+
InlayHint {
1180+
range: 175..190,
1181+
kind: ChainingHint,
1182+
label: "&mut MyIter",
1183+
},
1184+
]
1185+
"#]],
11411186
);
11421187
}
11431188
}

0 commit comments

Comments
 (0)