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

View File

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