mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Add testing infrastructure for type inference
- move dir_tests to test_utils for that.
This commit is contained in:
parent
3899898d75
commit
7348f7883f
9 changed files with 223 additions and 125 deletions
|
@ -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<Option<String>> {
|
||||
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<Vec<(FileId, FileSymbol)>> {
|
||||
let name = name_ref.text();
|
||||
let mut query = Query::new(name.to_string());
|
||||
|
|
|
@ -366,6 +366,9 @@ impl Analysis {
|
|||
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
|
||||
self.imp.resolve_callable(position)
|
||||
}
|
||||
pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable<Option<String>> {
|
||||
self.imp.type_of(file_id, range)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LibraryData {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue