From 7348f7883fa2bd571fff036c82e98c102d05c362 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 12:05:54 +0100 Subject: Add testing infrastructure for type inference - move dir_tests to test_utils for that. --- crates/ra_analysis/src/imp.rs | 14 +++++++++++++- crates/ra_analysis/src/lib.rs | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'crates/ra_analysis/src') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index b01382808..4e0631679 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -5,7 +5,8 @@ use std::{ use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; use ra_syntax::{ - ast::{self, ArgListOwner, Expr, NameOwner}, + ast::{self, ArgListOwner, Expr, NameOwner, FnDef}, + algo::find_covering_node, AstNode, SourceFileNode, SyntaxKind::*, SyntaxNodeRef, TextRange, TextUnit, @@ -510,6 +511,17 @@ impl AnalysisImpl { Ok(None) } + pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable> { + let file = self.db.source_file(file_id); + let syntax = file.syntax(); + let node = find_covering_node(syntax, range); + let parent_fn = node.ancestors().filter_map(FnDef::cast).next(); + let parent_fn = if let Some(p) = parent_fn { p } else { return Ok(None) }; + let function = ctry!(source_binder::function_from_source(&*self.db, file_id, parent_fn)?); + let infer = function.infer(&*self.db); + Ok(infer.type_of_node(node).map(|t| t.to_string())) + } + fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 85df9c089..830898140 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -366,6 +366,9 @@ impl Analysis { ) -> Cancelable)>> { self.imp.resolve_callable(position) } + pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable> { + self.imp.type_of(file_id, range) + } } pub struct LibraryData { -- cgit v1.2.3