aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-10-20 16:38:21 +0100
committerAleksey Kladov <[email protected]>2020-10-20 17:14:14 +0100
commita3a8ad8bc45d87607493d3c07d7e89e54f7b8c11 (patch)
treebd5c5ececd7060b608aeaaab2eb7c16334b2f226 /crates/ide/src
parentf925735e64cb6aed85f28cacc0a91c1c1bc06bb4 (diff)
Don't rely on display names in inlay_hints
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/inlay_hints.rs19
1 files changed, 13 insertions, 6 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index 56b985e80..cccea129a 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -99,6 +99,9 @@ fn get_chaining_hints(
99 return None; 99 return None;
100 } 100 }
101 101
102 let krate = sema.scope(expr.syntax()).module().map(|it| it.krate());
103 let famous_defs = FamousDefs(&sema, krate);
104
102 let mut tokens = expr 105 let mut tokens = expr
103 .syntax() 106 .syntax()
104 .siblings_with_tokens(Direction::Next) 107 .siblings_with_tokens(Direction::Next)
@@ -128,7 +131,7 @@ fn get_chaining_hints(
128 acc.push(InlayHint { 131 acc.push(InlayHint {
129 range: expr.syntax().text_range(), 132 range: expr.syntax().text_range(),
130 kind: InlayKind::ChainingHint, 133 kind: InlayKind::ChainingHint,
131 label: hint_iterator(sema, config, &ty).unwrap_or_else(|| { 134 label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
132 ty.display_truncated(sema.db, config.max_length).to_string().into() 135 ty.display_truncated(sema.db, config.max_length).to_string().into()
133 }), 136 }),
134 }); 137 });
@@ -188,6 +191,9 @@ fn get_bind_pat_hints(
188 return None; 191 return None;
189 } 192 }
190 193
194 let krate = sema.scope(pat.syntax()).module().map(|it| it.krate());
195 let famous_defs = FamousDefs(&sema, krate);
196
191 let ty = sema.type_of_pat(&pat.clone().into())?; 197 let ty = sema.type_of_pat(&pat.clone().into())?;
192 198
193 if should_not_display_type_hint(sema, &pat, &ty) { 199 if should_not_display_type_hint(sema, &pat, &ty) {
@@ -196,7 +202,7 @@ fn get_bind_pat_hints(
196 acc.push(InlayHint { 202 acc.push(InlayHint {
197 range: pat.syntax().text_range(), 203 range: pat.syntax().text_range(),
198 kind: InlayKind::TypeHint, 204 kind: InlayKind::TypeHint,
199 label: hint_iterator(sema, config, &ty) 205 label: hint_iterator(sema, &famous_defs, config, &ty)
200 .unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()), 206 .unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()),
201 }); 207 });
202 208
@@ -206,6 +212,7 @@ fn get_bind_pat_hints(
206/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`. 212/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
207fn hint_iterator( 213fn hint_iterator(
208 sema: &Semantics<RootDatabase>, 214 sema: &Semantics<RootDatabase>,
215 famous_defs: &FamousDefs,
209 config: &InlayHintsConfig, 216 config: &InlayHintsConfig,
210 ty: &hir::Type, 217 ty: &hir::Type,
211) -> Option<SmolStr> { 218) -> Option<SmolStr> {
@@ -214,11 +221,11 @@ fn hint_iterator(
214 .last() 221 .last()
215 .and_then(|strukt| strukt.as_adt())?; 222 .and_then(|strukt| strukt.as_adt())?;
216 let krate = strukt.krate(db)?; 223 let krate = strukt.krate(db)?;
217 if krate.display_name(db).as_deref() != Some("core") { 224 if krate != famous_defs.core()? {
218 return None; 225 return None;
219 } 226 }
220 let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?; 227 let iter_trait = famous_defs.core_iter_Iterator()?;
221 let iter_mod = FamousDefs(sema, krate).core_iter()?; 228 let iter_mod = famous_defs.core_iter()?;
222 // assert this struct comes from `core::iter` 229 // assert this struct comes from `core::iter`
223 iter_mod.visibility_of(db, &strukt.into()).filter(|&vis| vis == hir::Visibility::Public)?; 230 iter_mod.visibility_of(db, &strukt.into()).filter(|&vis| vis == hir::Visibility::Public)?;
224 if ty.impls_trait(db, iter_trait, &[]) { 231 if ty.impls_trait(db, iter_trait, &[]) {
@@ -230,7 +237,7 @@ fn hint_iterator(
230 const LABEL_START: &str = "impl Iterator<Item = "; 237 const LABEL_START: &str = "impl Iterator<Item = ";
231 const LABEL_END: &str = ">"; 238 const LABEL_END: &str = ">";
232 239
233 let ty_display = hint_iterator(sema, config, &ty) 240 let ty_display = hint_iterator(sema, famous_defs, config, &ty)
234 .map(|assoc_type_impl| assoc_type_impl.to_string()) 241 .map(|assoc_type_impl| assoc_type_impl.to_string())
235 .unwrap_or_else(|| { 242 .unwrap_or_else(|| {
236 ty.display_truncated( 243 ty.display_truncated(