remove path_ident from CompletionContext

We really shouldn't be looking at the identifier at point. Instead,
all filtering and sorting should be implemented at the layer above.

This layer should probably be home for auto-import completions as
well, but, since that is not yet implemented, let's just stick this
into complete_scope.
This commit is contained in:
Aleksey Kladov 2019-04-22 16:04:56 +03:00
parent e01052d1f0
commit c27a3da5e8
2 changed files with 38 additions and 39 deletions

View file

@ -1,6 +1,6 @@
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use ra_text_edit::TextEditBuilder; use ra_text_edit::TextEditBuilder;
use ra_syntax::SmolStr; use ra_syntax::{SmolStr, ast, AstNode};
use ra_assists::auto_import; use ra_assists::auto_import;
use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext};
@ -9,41 +9,43 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
if ctx.is_trivial_path { if ctx.is_trivial_path {
let names = ctx.analyzer.all_names(ctx.db); let names = ctx.analyzer.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));
}
if let Some(name) = ctx.path_ident.as_ref() { // auto-import
let import_resolver = ImportResolver::new(); // We fetch ident from the original file, because we need to pre-filter auto-imports
let import_names = import_resolver.all_names(&name.to_string()); if ast::NameRef::cast(ctx.token.parent()).is_some() {
import_names.into_iter().for_each(|(name, path)| { let import_resolver = ImportResolver::new();
let edit = { let import_names = import_resolver.all_names(ctx.token.text());
let mut builder = TextEditBuilder::default(); import_names.into_iter().for_each(|(name, path)| {
builder.replace(ctx.source_range(), name.to_string()); let edit = {
auto_import::auto_import_text_edit( let mut builder = TextEditBuilder::default();
ctx.token.parent(), builder.replace(ctx.source_range(), name.to_string());
ctx.token.parent(), auto_import::auto_import_text_edit(
&path, ctx.token.parent(),
&mut builder, ctx.token.parent(),
); &path,
builder.finish() &mut builder,
}; );
builder.finish()
};
// Hack: copied this check form conv.rs beacause auto import can produce edits // Hack: copied this check form conv.rs beacause auto import can produce edits
// that invalidate assert in conv_with. // that invalidate assert in conv_with.
if edit if edit
.as_atoms() .as_atoms()
.iter() .iter()
.filter(|atom| !ctx.source_range().is_subrange(&atom.delete)) .filter(|atom| !ctx.source_range().is_subrange(&atom.delete))
.all(|atom| ctx.source_range().intersection(&atom.delete).is_none()) .all(|atom| ctx.source_range().intersection(&atom.delete).is_none())
{ {
CompletionItem::new( CompletionItem::new(
CompletionKind::Reference, CompletionKind::Reference,
ctx.source_range(), ctx.source_range(),
build_import_label(&name, &path), build_import_label(&name, &path),
) )
.text_edit(edit) .text_edit(edit)
.add_to(acc); .add_to(acc);
} }
}); });
}
} }
} }

View file

@ -5,10 +5,10 @@ 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;
use hir::{ source_binder, Name };
use crate::{db, FilePosition}; use crate::{db, FilePosition};
/// `CompletionContext` is created early during completion to figure out, where /// `CompletionContext` is created early during completion to figure out, where
/// exactly is the cursor, syntax-wise. /// exactly is the cursor, syntax-wise.
#[derive(Debug)] #[derive(Debug)]
@ -29,8 +29,6 @@ pub(crate) struct CompletionContext<'a> {
pub(super) is_trivial_path: bool, pub(super) is_trivial_path: bool,
/// If not a trivial path, the prefix (qualifier). /// If not a trivial path, the prefix (qualifier).
pub(super) path_prefix: Option<hir::Path>, pub(super) path_prefix: Option<hir::Path>,
/// If a trivial path, the ident.
pub(super) path_ident: Option<Name>,
pub(super) after_if: bool, pub(super) after_if: bool,
/// `true` if we are a statement or a last expr in the block. /// `true` if we are a statement or a last expr in the block.
pub(super) can_be_stmt: bool, pub(super) can_be_stmt: bool,
@ -65,7 +63,6 @@ impl<'a> CompletionContext<'a> {
is_pat_binding: false, is_pat_binding: false,
is_trivial_path: false, is_trivial_path: false,
path_prefix: None, path_prefix: None,
path_ident: None,
after_if: false, after_if: false,
can_be_stmt: false, can_be_stmt: false,
is_new_item: false, is_new_item: false,