mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
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:
parent
84e3304a9b
commit
6c61a7b22f
1 changed files with 12 additions and 1 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue