aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/inlay_hints.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/inlay_hints.rs')
-rw-r--r--crates/ide/src/inlay_hints.rs53
1 files changed, 43 insertions, 10 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index e2079bbcf..49d8e4ae1 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -1,15 +1,14 @@
1use assists::utils::FamousDefs; 1use assists::utils::FamousDefs;
2use either::Either;
2use hir::{known, HirDisplay, Semantics}; 3use hir::{known, HirDisplay, Semantics};
3use ide_db::RootDatabase; 4use ide_db::RootDatabase;
4use stdx::to_lower_snake_case; 5use stdx::to_lower_snake_case;
5use syntax::{ 6use 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
10use crate::FileId; 11use crate::FileId;
11use ast::NameOwner;
12use either::Either;
13 12
14#[derive(Clone, Debug, PartialEq, Eq)] 13#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct InlayHintsConfig { 14pub 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>`.
208fn hint_iterator( 213fn 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(
@@ -372,7 +378,11 @@ fn is_enum_name_similar_to_param_name(
372fn get_string_representation(expr: &ast::Expr) -> Option<String> { 378fn get_string_representation(expr: &ast::Expr) -> Option<String> {
373 match expr { 379 match expr {
374 ast::Expr::MethodCallExpr(method_call_expr) => { 380 ast::Expr::MethodCallExpr(method_call_expr) => {
375 Some(method_call_expr.name_ref()?.to_string()) 381 let name_ref = method_call_expr.name_ref()?;
382 match name_ref.text().as_str() {
383 "clone" => method_call_expr.receiver().map(|rec| rec.to_string()),
384 name_ref => Some(name_ref.to_owned()),
385 }
376 } 386 }
377 ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?), 387 ast::Expr::RefExpr(ref_expr) => get_string_representation(&ref_expr.expr()?),
378 _ => Some(expr.to_string()), 388 _ => Some(expr.to_string()),
@@ -1202,4 +1212,27 @@ fn main() {
1202"#, 1212"#,
1203 ); 1213 );
1204 } 1214 }
1215
1216 #[test]
1217 fn hide_param_hints_for_clones() {
1218 check_with_config(
1219 InlayHintsConfig {
1220 parameter_hints: true,
1221 type_hints: false,
1222 chaining_hints: false,
1223 max_length: None,
1224 },
1225 r#"
1226fn foo(bar: i32, baz: String, qux: f32) {}
1227
1228fn main() {
1229 let bar = 3;
1230 let baz = &"baz";
1231 let fez = 1.0;
1232 foo(bar.clone(), baz.clone(), fez.clone());
1233 //^^^^^^^^^^^ qux
1234}
1235"#,
1236 );
1237 }
1205} 1238}