From 783af171f74d95b498662e5168c3ba320cca8553 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Oct 2020 13:14:12 +0200 Subject: Clean up inlay_hints --- crates/assists/src/utils.rs | 13 ++++++++++++- crates/ide/src/inlay_hints.rs | 32 ++++++++++++++------------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index c074b8d02..c1847f601 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -3,7 +3,7 @@ pub(crate) mod insert_use; use std::{iter, ops}; -use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; +use hir::{Adt, Crate, Enum, Module, ScopeDef, Semantics, Trait, Type}; use ide_db::RootDatabase; use itertools::Itertools; use rustc_hash::FxHashSet; @@ -373,6 +373,10 @@ pub use prelude::*; self.find_trait("core:iter:traits:iterator:Iterator") } + pub fn core_iter(&self) -> Option { + self.find_module("core:iter") + } + fn find_trait(&self, path: &str) -> Option { match self.find_def(path)? { hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it), @@ -387,6 +391,13 @@ pub use prelude::*; } } + fn find_module(&self, path: &str) -> Option { + match self.find_def(path)? { + hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it), + _ => None, + } + } + fn find_def(&self, path: &str) -> Option { let db = self.0.db; let mut path = path.split(':'); diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 7540f56a4..7d716577e 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -1,5 +1,5 @@ use assists::utils::FamousDefs; -use hir::{known, Adt, AssocItem, Callable, HirDisplay, Semantics, Type}; +use hir::{known, HirDisplay, Semantics}; use ide_db::RootDatabase; use stdx::to_lower_snake_case; use syntax::{ @@ -120,7 +120,7 @@ fn get_chaining_hints( return None; } if matches!(expr, ast::Expr::PathExpr(_)) { - if let Some(Adt::Struct(st)) = ty.as_adt() { + if let Some(hir::Adt::Struct(st)) = ty.as_adt() { if st.fields(sema.db).is_empty() { return None; } @@ -208,7 +208,7 @@ fn get_bind_pat_hints( fn hint_iterator( sema: &Semantics, config: &InlayHintsConfig, - ty: &Type, + ty: &hir::Type, ) -> Option { let db = sema.db; let strukt = std::iter::successors(Some(ty.clone()), |ty| ty.remove_ref()) @@ -218,17 +218,13 @@ fn hint_iterator( if krate.declaration_name(db).as_deref() != Some("core") { return None; } - // assert this type comes from `core::iter` - strukt - .module(db) - .path_to_root(db) - .into_iter() - .rev() - .find(|module| module.name(db) == Some(known::iter))?; let iter_trait = FamousDefs(sema, krate).core_iter_Iterator()?; + let iter_mod = FamousDefs(sema, krate).core_iter()?; + // assert this type comes from `core::iter` + iter_mod.visibility_of(db, &iter_trait.into()).filter(|&vis| vis == hir::Visibility::Public)?; if ty.impls_trait(db, iter_trait, &[]) { let assoc_type_item = iter_trait.items(db).into_iter().find_map(|item| match item { - AssocItem::TypeAlias(alias) if alias.name(db) == known::Item => Some(alias), + hir::AssocItem::TypeAlias(alias) if alias.name(db) == known::Item => Some(alias), _ => None, })?; if let Some(ty) = ty.normalize_trait_assoc_type(db, iter_trait, &[], assoc_type_item) { @@ -248,8 +244,8 @@ fn hint_iterator( None } -fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &Type) -> bool { - if let Some(Adt::Enum(enum_data)) = pat_ty.as_adt() { +fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &hir::Type) -> bool { + if let Some(hir::Adt::Enum(enum_data)) = pat_ty.as_adt() { let pat_text = bind_pat.to_string(); enum_data .variants(db) @@ -264,7 +260,7 @@ fn pat_is_enum_variant(db: &RootDatabase, bind_pat: &ast::IdentPat, pat_ty: &Typ fn should_not_display_type_hint( sema: &Semantics, bind_pat: &ast::IdentPat, - pat_ty: &Type, + pat_ty: &hir::Type, ) -> bool { let db = sema.db; @@ -272,7 +268,7 @@ fn should_not_display_type_hint( return true; } - if let Some(Adt::Struct(s)) = pat_ty.as_adt() { + if let Some(hir::Adt::Struct(s)) = pat_ty.as_adt() { if s.fields(db).is_empty() && s.name(db).to_string() == bind_pat.to_string() { return true; } @@ -316,7 +312,7 @@ fn should_not_display_type_hint( fn should_show_param_name_hint( sema: &Semantics, - callable: &Callable, + callable: &hir::Callable, param_name: &str, argument: &ast::Expr, ) -> bool { @@ -363,7 +359,7 @@ fn is_enum_name_similar_to_param_name( param_name: &str, ) -> bool { match sema.type_of_expr(argument).and_then(|t| t.as_adt()) { - Some(Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name, + Some(hir::Adt::Enum(e)) => to_lower_snake_case(&e.name(sema.db).to_string()) == param_name, _ => false, } } @@ -384,7 +380,7 @@ fn is_obvious_param(param_name: &str) -> bool { param_name.len() == 1 || is_obvious_param_name } -fn get_callable(sema: &Semantics, expr: &ast::Expr) -> Option { +fn get_callable(sema: &Semantics, expr: &ast::Expr) -> Option { match expr { ast::Expr::CallExpr(expr) => sema.type_of_expr(&expr.expr()?)?.as_callable(sema.db), ast::Expr::MethodCallExpr(expr) => sema.resolve_method_call_as_callable(expr), -- cgit v1.2.3