From b80fa14a850db78b1f45de95b0edde1a65da4625 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Nov 2019 12:00:36 +0300 Subject: Use Local more --- crates/ra_hir/src/expr.rs | 2 +- crates/ra_hir/src/resolve.rs | 17 ++++++++++------- crates/ra_hir/src/source_binder.rs | 8 -------- crates/ra_ide_api/src/completion/presentation.rs | 14 ++++++-------- 4 files changed, 17 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 9262325f2..899e0fa04 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -38,7 +38,7 @@ pub(crate) fn resolver_for_scope( let scopes = owner.expr_scopes(db); let scope_chain = scopes.scope_chain(scope_id).collect::>(); for scope in scope_chain.into_iter().rev() { - r = r.push_expr_scope(Arc::clone(&scopes), scope); + r = r.push_expr_scope(owner, Arc::clone(&scopes), scope); } r } diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index 2f3e12eb8..b922fe20f 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -16,8 +16,8 @@ use crate::{ expr::{ExprScopes, PatId, ScopeId}, generics::GenericParams, impl_block::ImplBlock, - Adt, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, PerNs, Static, Struct, Trait, - TypeAlias, + Adt, Const, DefWithBody, Enum, EnumVariant, Function, Local, MacroDef, ModuleDef, PerNs, + Static, Struct, Trait, TypeAlias, }; #[derive(Debug, Clone, Default)] @@ -34,6 +34,7 @@ pub(crate) struct ModuleItemMap { #[derive(Debug, Clone)] pub(crate) struct ExprScope { + owner: DefWithBody, expr_scopes: Arc, scope_id: ScopeId, } @@ -399,10 +400,11 @@ impl Resolver { pub(crate) fn push_expr_scope( self, + owner: DefWithBody, expr_scopes: Arc, scope_id: ScopeId, ) -> Resolver { - self.push_scope(Scope::ExprScope(ExprScope { expr_scopes, scope_id })) + self.push_scope(Scope::ExprScope(ExprScope { owner, expr_scopes, scope_id })) } } @@ -413,7 +415,7 @@ pub enum ScopeDef { GenericParam(u32), ImplSelfType(ImplBlock), AdtSelfType(Adt), - LocalBinding(PatId), + Local(Local), Unknown, } @@ -467,9 +469,10 @@ impl Scope { Scope::AdtScope(i) => { f(name::SELF_TYPE, ScopeDef::AdtSelfType(*i)); } - Scope::ExprScope(e) => { - e.expr_scopes.entries(e.scope_id).iter().for_each(|e| { - f(e.name().clone(), ScopeDef::LocalBinding(e.pat())); + Scope::ExprScope(scope) => { + scope.expr_scopes.entries(scope.scope_id).iter().for_each(|e| { + let local = Local { parent: scope.owner, pat_id: e.pat() }; + f(e.name().clone(), ScopeDef::Local(local)); }); } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index ca40e3b54..59046edcc 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -195,14 +195,6 @@ impl SourceAnalyzer { Some(self.infer.as_ref()?[pat_id].clone()) } - pub fn type_of_pat_by_id( - &self, - _db: &impl HirDatabase, - pat_id: expr::PatId, - ) -> Option { - Some(self.infer.as_ref()?[pat_id].clone()) - } - pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { let expr_id = self.expr_id(&call.clone().into())?; self.infer.as_ref()?.method_resolution(expr_id) diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index d861303b7..c343cece6 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -68,7 +68,7 @@ impl Completions { ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::TypeAlias, ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType, ScopeDef::GenericParam(..) => CompletionItemKind::TypeParam, - ScopeDef::LocalBinding(..) => CompletionItemKind::Binding, + ScopeDef::Local(..) => CompletionItemKind::Binding, // (does this need its own kind?) ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam, ScopeDef::MacroDef(mac) => { @@ -96,13 +96,11 @@ impl Completions { let mut completion_item = CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone()); - if let ScopeDef::LocalBinding(pat_id) = resolution { - let ty = ctx - .analyzer - .type_of_pat_by_id(ctx.db, pat_id.clone()) - .filter(|t| t != &Ty::Unknown) - .map(|t| t.display(ctx.db).to_string()); - completion_item = completion_item.set_detail(ty); + if let ScopeDef::Local(local) = resolution { + let ty = local.ty(ctx.db); + if ty != Ty::Unknown { + completion_item = completion_item.detail(ty.display(ctx.db).to_string()); + } }; // If not an import, add parenthesis automatically. -- cgit v1.2.3