diff options
Diffstat (limited to 'crates/ide/src/inlay_hints.rs')
-rw-r--r-- | crates/ide/src/inlay_hints.rs | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index e2079bbcf..cccea129a 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs | |||
@@ -1,15 +1,14 @@ | |||
1 | use assists::utils::FamousDefs; | 1 | use assists::utils::FamousDefs; |
2 | use either::Either; | ||
2 | use hir::{known, HirDisplay, Semantics}; | 3 | use hir::{known, HirDisplay, Semantics}; |
3 | use ide_db::RootDatabase; | 4 | use ide_db::RootDatabase; |
4 | use stdx::to_lower_snake_case; | 5 | use stdx::to_lower_snake_case; |
5 | use syntax::{ | 6 | use syntax::{ |
6 | ast::{self, ArgListOwner, AstNode}, | 7 | ast::{self, ArgListOwner, AstNode, NameOwner}, |
7 | match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T, | 8 | match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T, |
8 | }; | 9 | }; |
9 | 10 | ||
10 | use crate::FileId; | 11 | use crate::FileId; |
11 | use ast::NameOwner; | ||
12 | use either::Either; | ||
13 | 12 | ||
14 | #[derive(Clone, Debug, PartialEq, Eq)] | 13 | #[derive(Clone, Debug, PartialEq, Eq)] |
15 | pub struct InlayHintsConfig { | 14 | pub struct InlayHintsConfig { |
@@ -100,6 +99,9 @@ fn get_chaining_hints( | |||
100 | return None; | 99 | return None; |
101 | } | 100 | } |
102 | 101 | ||
102 | let krate = sema.scope(expr.syntax()).module().map(|it| it.krate()); | ||
103 | let famous_defs = FamousDefs(&sema, krate); | ||
104 | |||
103 | let mut tokens = expr | 105 | let mut tokens = expr |
104 | .syntax() | 106 | .syntax() |
105 | .siblings_with_tokens(Direction::Next) | 107 | .siblings_with_tokens(Direction::Next) |
@@ -129,7 +131,7 @@ fn get_chaining_hints( | |||
129 | acc.push(InlayHint { | 131 | acc.push(InlayHint { |
130 | range: expr.syntax().text_range(), | 132 | range: expr.syntax().text_range(), |
131 | kind: InlayKind::ChainingHint, | 133 | kind: InlayKind::ChainingHint, |
132 | label: hint_iterator(sema, config, &ty).unwrap_or_else(|| { | 134 | label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| { |
133 | ty.display_truncated(sema.db, config.max_length).to_string().into() | 135 | ty.display_truncated(sema.db, config.max_length).to_string().into() |
134 | }), | 136 | }), |
135 | }); | 137 | }); |
@@ -189,6 +191,9 @@ fn get_bind_pat_hints( | |||
189 | return None; | 191 | return None; |
190 | } | 192 | } |
191 | 193 | ||
194 | let krate = sema.scope(pat.syntax()).module().map(|it| it.krate()); | ||
195 | let famous_defs = FamousDefs(&sema, krate); | ||
196 | |||
192 | let ty = sema.type_of_pat(&pat.clone().into())?; | 197 | let ty = sema.type_of_pat(&pat.clone().into())?; |
193 | 198 | ||
194 | if should_not_display_type_hint(sema, &pat, &ty) { | 199 | if should_not_display_type_hint(sema, &pat, &ty) { |
@@ -197,7 +202,7 @@ fn get_bind_pat_hints( | |||
197 | acc.push(InlayHint { | 202 | acc.push(InlayHint { |
198 | range: pat.syntax().text_range(), | 203 | range: pat.syntax().text_range(), |
199 | kind: InlayKind::TypeHint, | 204 | kind: InlayKind::TypeHint, |
200 | label: hint_iterator(sema, config, &ty) | 205 | label: hint_iterator(sema, &famous_defs, config, &ty) |
201 | .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()), |
202 | }); | 207 | }); |
203 | 208 | ||
@@ -207,6 +212,7 @@ fn get_bind_pat_hints( | |||
207 | /// 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>`. |
208 | fn hint_iterator( | 213 | fn hint_iterator( |
209 | sema: &Semantics<RootDatabase>, | 214 | sema: &Semantics<RootDatabase>, |
215 | famous_defs: &FamousDefs, | ||
210 | config: &InlayHintsConfig, | 216 | config: &InlayHintsConfig, |
211 | ty: &hir::Type, | 217 | ty: &hir::Type, |
212 | ) -> Option<SmolStr> { | 218 | ) -> Option<SmolStr> { |
@@ -215,11 +221,11 @@ fn hint_iterator( | |||
215 | .last() | 221 | .last() |
216 | .and_then(|strukt| strukt.as_adt())?; | 222 | .and_then(|strukt| strukt.as_adt())?; |
217 | let krate = strukt.krate(db)?; | 223 | let krate = strukt.krate(db)?; |
218 | if krate.declaration_name(db).as_deref() != Some("core") { | 224 | if krate != famous_defs.core()? { |
219 | return None; | 225 | return None; |
220 | } | 226 | } |
221 | let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?; | 227 | let iter_trait = famous_defs.core_iter_Iterator()?; |
222 | let iter_mod = FamousDefs(sema, krate).core_iter()?; | 228 | let iter_mod = famous_defs.core_iter()?; |
223 | // assert this struct comes from `core::iter` | 229 | // assert this struct comes from `core::iter` |
224 | 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)?; |
225 | if ty.impls_trait(db, iter_trait, &[]) { | 231 | if ty.impls_trait(db, iter_trait, &[]) { |
@@ -231,7 +237,7 @@ fn hint_iterator( | |||
231 | const LABEL_START: &str = "impl Iterator<Item = "; | 237 | const LABEL_START: &str = "impl Iterator<Item = "; |
232 | const LABEL_END: &str = ">"; | 238 | const LABEL_END: &str = ">"; |
233 | 239 | ||
234 | let ty_display = hint_iterator(sema, config, &ty) | 240 | let ty_display = hint_iterator(sema, famous_defs, config, &ty) |
235 | .map(|assoc_type_impl| assoc_type_impl.to_string()) | 241 | .map(|assoc_type_impl| assoc_type_impl.to_string()) |
236 | .unwrap_or_else(|| { | 242 | .unwrap_or_else(|| { |
237 | ty.display_truncated( | 243 | ty.display_truncated( |