Auto merge of #12188 - Veykril:auto-import, r=Veykril

fix: Allow auto importing starting segments of use items
This commit is contained in:
bors 2022-05-07 14:16:00 +00:00
commit bfb241afa3
3 changed files with 84 additions and 29 deletions

View file

@ -373,19 +373,6 @@ mod baz {
);
}
#[test]
fn not_applicable_in_import_statements() {
check_assist_not_applicable(
auto_import,
r"
use PubStruct$0;
pub mod PubMod {
pub struct PubStruct;
}",
);
}
#[test]
fn function_import() {
check_assist(
@ -1121,4 +1108,43 @@ struct Foo;
"#,
);
}
#[test]
fn works_in_use_start() {
check_assist(
auto_import,
r#"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use foo$0::Foo;
"#,
r#"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use bar::foo;
use foo::Foo;
"#,
);
}
#[test]
fn not_applicable_in_non_start_use() {
check_assist_not_applicable(
auto_import,
r"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use foo::Foo$0;
",
);
}
}

View file

@ -381,20 +381,6 @@ pub mod PubMod {
check_assist_not_applicable(qualify_path, r#"PubStruct$0"#);
}
#[test]
fn not_applicable_in_import_statements() {
check_assist_not_applicable(
qualify_path,
r#"
use PubStruct$0;
pub mod PubMod {
pub struct PubStruct;
}
"#,
);
}
#[test]
fn qualify_function() {
check_assist(
@ -1270,4 +1256,42 @@ struct Foo;
"#,
);
}
#[test]
fn works_in_use_start() {
check_assist(
qualify_path,
r#"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use foo$0::Foo;
"#,
r#"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use bar::foo::Foo;
"#,
);
}
#[test]
fn not_applicable_in_non_start_use() {
check_assist_not_applicable(
qualify_path,
r"
mod bar {
pub mod foo {
pub struct Foo;
}
}
use foo::Foo$0;
",
);
}
}

View file

@ -114,8 +114,13 @@ impl ImportAssets {
sema: &Semantics<RootDatabase>,
) -> Option<Self> {
let candidate_node = fully_qualified_path.syntax().clone();
if candidate_node.ancestors().find_map(ast::Use::cast).is_some() {
return None;
if let Some(use_tree) = candidate_node.ancestors().find_map(ast::UseTree::cast) {
// Path is inside a use tree, then only continue if it is the first segment of a use statement.
if use_tree.syntax().parent().and_then(ast::Use::cast).is_none()
|| fully_qualified_path.qualifier().is_some()
{
return None;
}
}
Some(Self {
import_candidate: ImportCandidate::for_regular_path(sema, fully_qualified_path)?,