aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authornathanwhit <[email protected]>2020-04-21 19:28:49 +0100
committernathanwhit <[email protected]>2020-04-22 17:20:18 +0100
commit6c61a7b22f70a7359d7bad9509b93a00d73c53bf (patch)
tree2c77e8a126fa0d86786e3d11f130c95d90e24360 /crates/ra_ide/src
parent84e3304a9bf0d68e30d58b1e37a6db2e9ec97525 (diff)
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.
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs13
1 files changed, 12 insertions, 1 deletions
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 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{Semantics, SemanticsScope}; 3use hir::{Semantics, SemanticsScope, Type};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_ide_db::RootDatabase; 5use ra_ide_db::RootDatabase;
6use ra_syntax::{ 6use ra_syntax::{
@@ -168,6 +168,17 @@ impl<'a> CompletionContext<'a> {
168 self.sema.scope_at_offset(&self.token.parent(), self.offset) 168 self.sema.scope_at_offset(&self.token.parent(), self.offset)
169 } 169 }
170 170
171 pub(crate) fn expected_type_of(&self, node: &SyntaxNode) -> Option<Type> {
172 for ancestor in node.ancestors() {
173 if let Some(pat) = ast::Pat::cast(ancestor.clone()) {
174 return self.sema.type_of_pat(&pat);
175 } else if let Some(expr) = ast::Expr::cast(ancestor) {
176 return self.sema.type_of_expr(&expr);
177 }
178 }
179 None
180 }
181
171 fn fill( 182 fn fill(
172 &mut self, 183 &mut self,
173 original_file: &SyntaxNode, 184 original_file: &SyntaxNode,