Add utility fn for expected type of a node

Adds `expected_type_of` to `CompletionContext` to return the expected type of a
node, if it is known.
This commit is contained in:
nathanwhit 2020-04-21 14:28:49 -04:00
parent 84e3304a9b
commit 6c61a7b22f

View file

@ -1,6 +1,6 @@
//! FIXME: write short doc here
use hir::{Semantics, SemanticsScope};
use hir::{Semantics, SemanticsScope, Type};
use ra_db::SourceDatabase;
use ra_ide_db::RootDatabase;
use ra_syntax::{
@ -168,6 +168,17 @@ impl<'a> CompletionContext<'a> {
self.sema.scope_at_offset(&self.token.parent(), self.offset)
}
pub(crate) fn expected_type_of(&self, node: &SyntaxNode) -> Option<Type> {
for ancestor in node.ancestors() {
if let Some(pat) = ast::Pat::cast(ancestor.clone()) {
return self.sema.type_of_pat(&pat);
} else if let Some(expr) = ast::Expr::cast(ancestor) {
return self.sema.type_of_expr(&expr);
}
}
None
}
fn fill(
&mut self,
original_file: &SyntaxNode,