From 505acc973b3b865195d7d0aeb47c419c35f6bbbc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Apr 2019 15:34:13 +0300 Subject: Make call info to use real name resolution --- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/ty.rs | 14 ++++++-- crates/ra_ide_api/src/call_info.rs | 37 ++++++++++++---------- .../src/completion/completion_context.rs | 6 ---- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 59b402c57..8702c6222 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -60,7 +60,7 @@ pub use self::{ source_id::{AstIdMap, ErasedFileAstId}, ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc}, nameres::{PerNs, Namespace, ImportId}, - ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, + ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay, CallableDef}, impl_block::{ImplBlock, ImplItem}, docs::{Docs, Documentation}, adt::AdtDef, diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 20e55d92d..ecf13fbc3 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -15,10 +15,11 @@ use std::sync::Arc; use std::{fmt, mem}; use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase, Trait}; +use display::{HirDisplay, HirFormatter}; -pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field, callable_item_sig}; +pub(crate) use lower::{TypableDef, type_for_def, type_for_field, callable_item_sig}; pub(crate) use infer::{infer, InferenceResult, InferTy}; -use display::{HirDisplay, HirFormatter}; +pub use lower::CallableDef; /// A type constructor or type name: this might be something like the primitive /// type `bool`, a struct like `Vec`, or things like function pointers or @@ -288,6 +289,15 @@ impl Ty { } } + pub fn as_callable(&self) -> Option<(CallableDef, &Substs)> { + match self { + Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(callable_def), parameters }) => { + Some((*callable_def, parameters)) + } + _ => None, + } + } + fn builtin_deref(&self) -> Option { match self { Ty::Apply(a_ty) => match a_ty.ctor { diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index dbb3853d0..d06876777 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -2,7 +2,6 @@ use test_utils::tested_by; use ra_db::SourceDatabase; use ra_syntax::{ AstNode, SyntaxNode, TextUnit, - SyntaxKind::FN_DEF, ast::{self, ArgListOwner}, algo::find_node_at_offset, }; @@ -18,19 +17,26 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option { + //FIXME: apply subst + let (callable_def, _subst) = + analyser.type_of(db, expr.expr()?.into())?.as_callable()?; + match callable_def { + hir::CallableDef::Function(it) => it, + //FIXME: handle other callables + _ => return None, + } + } + FnCallNode::MethodCallExpr(expr) => analyser.resolve_method_call(expr)?, + }; let mut call_info = CallInfo::new(db, function); // If we have a calling expression let's find which argument we are on let num_params = call_info.parameters().len(); - let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some(); + let has_self = function.signature(db).has_self_param(); if num_params == 1 { if !has_self { @@ -142,7 +148,7 @@ mod tests { } #[test] - fn test_fn_signature_two_args_first() { + fn test_fn_signature_two_args_firstx() { let info = call_info( r#"fn foo(x: u32, y: u32) -> u32 {x + y} fn bar() { foo(<|>3, ); }"#, @@ -382,11 +388,9 @@ assert_eq!(6, my_crate::add_one(5)); fn test_fn_signature_with_docs_from_actix() { let info = call_info( r#" -pub trait WriteHandler -where - Self: Actor, - Self::Context: ActorContext, -{ +struct WriteHandler; + +impl WriteHandler { /// Method is called when writer emits error. /// /// If this method returns `ErrorAction::Continue` writer processing @@ -403,8 +407,7 @@ where } } -pub fn foo() { - WriteHandler r; +pub fn foo(mut r: WriteHandler<()>) { r.finished(<|>); } diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index ce21fca9b..ddcf46b4e 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -19,7 +19,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) token: SyntaxToken<'a>, pub(super) resolver: Resolver, pub(super) module: Option, - pub(super) function: Option, pub(super) function_syntax: Option<&'a ast::FnDef>, pub(super) use_item_syntax: Option<&'a ast::UseItem>, pub(super) struct_lit_syntax: Option<&'a ast::StructLit>, @@ -59,7 +58,6 @@ impl<'a> CompletionContext<'a> { offset: position.offset, resolver, module, - function: None, function_syntax: None, use_item_syntax: None, struct_lit_syntax: None, @@ -150,10 +148,6 @@ impl<'a> CompletionContext<'a> { .ancestors() .take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE) .find_map(ast::FnDef::cast); - if let (Some(module), Some(fn_def)) = (self.module, self.function_syntax) { - let function = source_binder::function_from_module(self.db, module, fn_def); - self.function = Some(function); - } let parent = match name_ref.syntax().parent() { Some(it) => it, -- cgit v1.2.3