remove resolver from CompletonContext

This commit is contained in:
Aleksey Kladov 2019-04-11 16:49:35 +03:00
parent 3c9f2d0e37
commit ebb0c377f0
5 changed files with 9 additions and 31 deletions

View file

@ -11,7 +11,7 @@ use ra_db::{FileId, FilePosition};
use ra_syntax::{ use ra_syntax::{
SyntaxNode, AstPtr, SyntaxNode, AstPtr,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
algo::{find_node_at_offset, find_token_at_offset}, algo::find_node_at_offset,
}; };
use crate::{ use crate::{
@ -196,29 +196,6 @@ pub fn trait_from_module(
Trait { id: ctx.to_def(trait_def) } Trait { id: ctx.to_def(trait_def) }
} }
pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver {
let file_id = position.file_id;
let file = db.parse(file_id);
find_token_at_offset(file.syntax(), position.offset)
.find_map(|token| {
token.parent().ancestors().find_map(|node| {
if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() {
if let Some(func) = function_from_child_node(db, file_id, node) {
let scopes = func.scopes(db);
let scope = scopes.scope_for_offset(position.offset);
Some(expr::resolver_for_scope(func.body(db), db, scope))
} else {
// FIXME const/static/array length
None
}
} else {
try_get_resolver_for_node(db, file_id, node)
}
})
})
.unwrap_or_default()
}
fn resolver_for_node(db: &impl HirDatabase, file_id: FileId, node: &SyntaxNode) -> Resolver { fn resolver_for_node(db: &impl HirDatabase, file_id: FileId, node: &SyntaxNode) -> Resolver {
node.ancestors() node.ancestors()
.find_map(|node| { .find_map(|node| {
@ -305,6 +282,10 @@ impl SourceAnalyzer {
} }
} }
pub fn resolver(&self) -> &Resolver {
&self.resolver
}
pub fn type_of(&self, _db: &impl HirDatabase, expr: &ast::Expr) -> Option<crate::Ty> { pub fn type_of(&self, _db: &impl HirDatabase, expr: &ast::Expr) -> Option<crate::Ty> {
let expr_id = self.body_source_map.as_ref()?.node_expr(expr)?; let expr_id = self.body_source_map.as_ref()?.node_expr(expr)?;
Some(self.infer.as_ref()?[expr_id].clone()) Some(self.infer.as_ref()?[expr_id].clone())

View file

@ -9,7 +9,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
Some(path) => path.clone(), Some(path) => path.clone(),
_ => return, _ => return,
}; };
let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() { let def = match ctx.analyzer.resolver().resolve_path(ctx.db, &path).take_types() {
Some(Resolution::Def(def)) => def, Some(Resolution::Def(def)) => def,
_ => return, _ => return,
}; };

View file

@ -7,7 +7,7 @@ pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
} }
// FIXME: ideally, we should look at the type we are matching against and // FIXME: ideally, we should look at the type we are matching against and
// suggest variants + auto-imports // suggest variants + auto-imports
let names = ctx.resolver.all_names(ctx.db); let names = ctx.analyzer.resolver().all_names(ctx.db);
for (name, res) in names.into_iter() { for (name, res) in names.into_iter() {
let r = res.as_ref(); let r = res.as_ref();
let def = match r.take_types().or(r.take_values()) { let def = match r.take_types().or(r.take_values()) {

View file

@ -4,7 +4,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path { if !ctx.is_trivial_path {
return; return;
} }
let names = ctx.resolver.all_names(ctx.db); let names = ctx.analyzer.resolver().all_names(ctx.db);
names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res));
} }

View file

@ -5,7 +5,7 @@ use ra_syntax::{
algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, algo::{find_token_at_offset, find_covering_element, find_node_at_offset},
SyntaxKind::*, SyntaxKind::*,
}; };
use hir::{source_binder, Resolver}; use hir::source_binder;
use crate::{db, FilePosition}; use crate::{db, FilePosition};
@ -17,7 +17,6 @@ pub(crate) struct CompletionContext<'a> {
pub(super) analyzer: hir::SourceAnalyzer, pub(super) analyzer: hir::SourceAnalyzer,
pub(super) offset: TextUnit, pub(super) offset: TextUnit,
pub(super) token: SyntaxToken<'a>, pub(super) token: SyntaxToken<'a>,
pub(super) resolver: Resolver,
pub(super) module: Option<hir::Module>, pub(super) module: Option<hir::Module>,
pub(super) function_syntax: Option<&'a ast::FnDef>, pub(super) function_syntax: Option<&'a ast::FnDef>,
pub(super) use_item_syntax: Option<&'a ast::UseItem>, pub(super) use_item_syntax: Option<&'a ast::UseItem>,
@ -47,7 +46,6 @@ impl<'a> CompletionContext<'a> {
original_file: &'a SourceFile, original_file: &'a SourceFile,
position: FilePosition, position: FilePosition,
) -> Option<CompletionContext<'a>> { ) -> Option<CompletionContext<'a>> {
let resolver = source_binder::resolver_for_position(db, position);
let module = source_binder::module_from_position(db, position); let module = source_binder::module_from_position(db, position);
let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?; let token = find_token_at_offset(original_file.syntax(), position.offset).left_biased()?;
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, token.parent()); let analyzer = hir::SourceAnalyzer::new(db, position.file_id, token.parent());
@ -56,7 +54,6 @@ impl<'a> CompletionContext<'a> {
analyzer, analyzer,
token, token,
offset: position.offset, offset: position.offset,
resolver,
module, module,
function_syntax: None, function_syntax: None,
use_item_syntax: None, use_item_syntax: None,