introduce SourceAnalyzer

This commit is contained in:
Aleksey Kladov 2019-04-10 11:15:55 +03:00
parent e6e2571bdf
commit 10d66d63d7
15 changed files with 205 additions and 186 deletions

View file

@ -132,17 +132,15 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
.ancestors()
.take_while(|it| it.range() == leaf_node.range())
.find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())?;
let parent_fn = node.ancestors().find_map(ast::FnDef::cast)?;
let function = hir::source_binder::function_from_source(db, frange.file_id, parent_fn)?;
let infer = function.infer(db);
let source_map = function.body_source_map(db);
if let Some(expr) = ast::Expr::cast(node).and_then(|e| source_map.node_expr(e)) {
Some(infer[expr].display(db).to_string())
} else if let Some(pat) = ast::Pat::cast(node).and_then(|p| source_map.node_pat(p)) {
Some(infer[pat].display(db).to_string())
let analyzer = hir::SourceAnalyser::new(db, frange.file_id, node);
let ty = if let Some(ty) = ast::Expr::cast(node).and_then(|e| analyzer.type_of(db, e)) {
ty
} else if let Some(ty) = ast::Pat::cast(node).and_then(|p| analyzer.type_of_pat(db, p)) {
ty
} else {
None
}
return None;
};
Some(ty.display(db).to_string())
}
#[cfg(test)]