From 6c61a7b22f70a7359d7bad9509b93a00d73c53bf Mon Sep 17 00:00:00 2001 From: nathanwhit Date: Tue, 21 Apr 2020 14:28:49 -0400 Subject: 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. --- crates/ra_ide/src/completion/completion_context.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 8b3401595..cfc5c34df 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -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 { + 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, -- cgit v1.2.3