From bdbdade036fe71c4438f931f450beb711d1379ed Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 5 Jan 2019 17:38:24 +0300 Subject: move typeof to hover --- crates/ra_analysis/src/hover.rs | 18 ++++++++++++++++-- crates/ra_analysis/src/imp.rs | 16 +--------------- crates/ra_analysis/src/lib.rs | 2 +- 3 files changed, 18 insertions(+), 18 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/hover.rs b/crates/ra_analysis/src/hover.rs index c99d87da6..766fa0547 100644 --- a/crates/ra_analysis/src/hover.rs +++ b/crates/ra_analysis/src/hover.rs @@ -2,7 +2,7 @@ use ra_db::{Cancelable, SyntaxDatabase}; use ra_syntax::{ AstNode, SyntaxNode, ast::{self, NameOwner}, - algo::visit::{visitor, Visitor}, + algo::{find_covering_node, visit::{visitor, Visitor}}, }; use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget}; @@ -27,7 +27,7 @@ pub(crate) fn hover( file_id: position.file_id, range: expr.syntax().range(), }; - res.extend(db.type_of(frange)?); + res.extend(type_of(db, frange)?); expr.syntax().range() }; if res.is_empty() { @@ -37,6 +37,20 @@ pub(crate) fn hover( Ok(Some(res)) } +pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable> { + let file = db.source_file(frange.file_id); + let syntax = file.syntax(); + let node = find_covering_node(syntax, frange.range); + let parent_fn = ctry!(node.ancestors().find_map(ast::FnDef::cast)); + let function = ctry!(hir::source_binder::function_from_source( + db, + frange.file_id, + parent_fn + )?); + let infer = function.infer(db)?; + Ok(infer.type_of_node(node).map(|t| t.to_string())) +} + // FIXME: this should not really use navigation target. Rather, approximatelly // resovled symbol should return a `DefId`. fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable> { diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 1e9129c4f..e2871451c 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -8,8 +8,7 @@ use hir::{ use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; use ra_syntax::{ - algo::find_covering_node, - ast::{self, ArgListOwner, Expr, FnDef, NameOwner}, + ast::{self, ArgListOwner, Expr, NameOwner}, AstNode, SourceFileNode, SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, @@ -398,19 +397,6 @@ impl db::RootDatabase { Ok(None) } - pub(crate) fn type_of(&self, frange: FileRange) -> Cancelable> { - let file = self.source_file(frange.file_id); - let syntax = file.syntax(); - let node = find_covering_node(syntax, frange.range); - let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast)); - let function = ctry!(source_binder::function_from_source( - self, - frange.file_id, - parent_fn - )?); - let infer = function.infer(self)?; - Ok(infer.type_of_node(node).map(|t| t.to_string())) - } pub(crate) fn rename( &self, position: FilePosition, diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 0e32a15f8..b068119d2 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -450,7 +450,7 @@ impl Analysis { } /// Computes the type of the expression at the given position. pub fn type_of(&self, frange: FileRange) -> Cancelable> { - self.db.type_of(frange) + hover::type_of(&*self.db, frange) } /// Returns the edit required to rename reference at the position to the new /// name. -- cgit v1.2.3