diff options
author | Florian Diebold <[email protected]> | 2018-12-23 11:05:54 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2018-12-23 12:48:04 +0000 |
commit | 7348f7883fa2bd571fff036c82e98c102d05c362 (patch) | |
tree | e7882097498b6d85e631d570dac0d8a89cd24875 /crates/ra_analysis/src/imp.rs | |
parent | 3899898d75176ce3cd87f9e2acecd7e3a987dda5 (diff) |
Add testing infrastructure for type inference
- move dir_tests to test_utils for that.
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 14 |
1 files changed, 13 insertions, 1 deletions
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::{ | |||
5 | 5 | ||
6 | use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; | 6 | use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | ast::{self, ArgListOwner, Expr, NameOwner}, | 8 | ast::{self, ArgListOwner, Expr, NameOwner, FnDef}, |
9 | algo::find_covering_node, | ||
9 | AstNode, SourceFileNode, | 10 | AstNode, SourceFileNode, |
10 | SyntaxKind::*, | 11 | SyntaxKind::*, |
11 | SyntaxNodeRef, TextRange, TextUnit, | 12 | SyntaxNodeRef, TextRange, TextUnit, |
@@ -510,6 +511,17 @@ impl AnalysisImpl { | |||
510 | Ok(None) | 511 | Ok(None) |
511 | } | 512 | } |
512 | 513 | ||
514 | pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable<Option<String>> { | ||
515 | let file = self.db.source_file(file_id); | ||
516 | let syntax = file.syntax(); | ||
517 | let node = find_covering_node(syntax, range); | ||
518 | let parent_fn = node.ancestors().filter_map(FnDef::cast).next(); | ||
519 | let parent_fn = if let Some(p) = parent_fn { p } else { return Ok(None) }; | ||
520 | let function = ctry!(source_binder::function_from_source(&*self.db, file_id, parent_fn)?); | ||
521 | let infer = function.infer(&*self.db); | ||
522 | Ok(infer.type_of_node(node).map(|t| t.to_string())) | ||
523 | } | ||
524 | |||
513 | fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> { | 525 | fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> { |
514 | let name = name_ref.text(); | 526 | let name = name_ref.text(); |
515 | let mut query = Query::new(name.to_string()); | 527 | let mut query = Query::new(name.to_string()); |