Only complete modules in empty use-statements

This commit is contained in:
Lukas Wirth 2021-05-28 02:40:40 +02:00
parent 01bfc5f5c0
commit 9e71dd9799
3 changed files with 27 additions and 14 deletions

View File

@ -1,7 +1,6 @@
//! Completion of names from the current scope, e.g. locals and imported items.
use hir::ScopeDef;
use syntax::AstNode;
use crate::{CompletionContext, Completions};
@ -24,6 +23,15 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
return;
}
if ctx.expects_use_tree() {
cov_mark::hit!(only_completes_modules_in_import);
ctx.scope.process_all_names(&mut |name, res| {
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
acc.add_resolution(ctx, name.to_string(), &res);
}
});
return;
}
if let Some(hir::Adt::Enum(e)) =
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
{
@ -37,14 +45,6 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
cov_mark::hit!(skip_lifetime_completion);
return;
}
if ctx.use_item_syntax.is_some() {
if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
if name_ref.syntax().text() == name.to_string().as_str() {
cov_mark::hit!(self_fulfilling_completion);
return;
}
}
}
acc.add_resolution(ctx, name.to_string(), &res);
});
}
@ -68,15 +68,17 @@ mod tests {
}
#[test]
fn self_fulfilling_completion() {
cov_mark::check!(self_fulfilling_completion);
fn only_completes_modules_in_import() {
cov_mark::check!(only_completes_modules_in_import);
check(
r#"
use foo$0
use std::collections;
use f$0
struct Foo;
mod foo {}
"#,
expect![[r#"
?? collections
md foo
"#]],
);
}

View File

@ -276,6 +276,10 @@ impl<'a> CompletionContext<'a> {
)
}
pub(crate) fn expects_use_tree(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Use))
}
pub(crate) fn expects_non_trait_assoc_item(&self) -> bool {
matches!(self.completion_location, Some(ImmediateLocation::Impl))
}

View File

@ -14,6 +14,7 @@ use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applic
/// Direct parent container of the cursor position
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum ImmediateLocation {
Use,
Impl,
Trait,
RecordField,
@ -58,6 +59,7 @@ pub(crate) fn determine_location(tok: SyntaxToken) -> Option<ImmediateLocation>
let res = match_ast! {
match parent {
ast::IdentPat(_it) => ImmediateLocation::IdentPat,
ast::Use(_it) => ImmediateLocation::Use,
ast::BlockExpr(_it) => ImmediateLocation::BlockExpr,
ast::SourceFile(_it) => ImmediateLocation::ItemList,
ast::ItemList(_it) => ImmediateLocation::ItemList,
@ -87,6 +89,11 @@ fn test_has_trait_parent() {
check_location(r"trait A { f$0 }", ImmediateLocation::Trait);
}
#[test]
fn test_has_use_parent() {
check_location(r"use f$0", ImmediateLocation::Use);
}
#[test]
fn test_has_impl_parent() {
check_location(r"impl A { f$0 }", ImmediateLocation::Impl);