diff options
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 00cdf7eff..a547c5a20 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, Severity}; | 6 | use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit, Severity}; |
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, |
@@ -513,6 +514,23 @@ impl AnalysisImpl { | |||
513 | Ok(None) | 514 | Ok(None) |
514 | } | 515 | } |
515 | 516 | ||
517 | pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable<Option<String>> { | ||
518 | let file = self.db.source_file(file_id); | ||
519 | let syntax = file.syntax(); | ||
520 | let node = find_covering_node(syntax, range); | ||
521 | let parent_fn = node.ancestors().filter_map(FnDef::cast).next(); | ||
522 | let parent_fn = if let Some(p) = parent_fn { | ||
523 | p | ||
524 | } else { | ||
525 | return Ok(None); | ||
526 | }; | ||
527 | let function = ctry!(source_binder::function_from_source( | ||
528 | &*self.db, file_id, parent_fn | ||
529 | )?); | ||
530 | let infer = function.infer(&*self.db)?; | ||
531 | Ok(infer.type_of_node(node).map(|t| t.to_string())) | ||
532 | } | ||
533 | |||
516 | fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> { | 534 | fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> { |
517 | let name = name_ref.text(); | 535 | let name = name_ref.text(); |
518 | let mut query = Query::new(name.to_string()); | 536 | let mut query = Query::new(name.to_string()); |