Skip to content

Commit bfb241a

Browse files
committed
Auto merge of #12188 - Veykril:auto-import, r=Veykril
fix: Allow auto importing starting segments of use items
2 parents f221676 + 61e074f commit bfb241a

File tree

3 files changed

+84
-29
lines changed

3 files changed

+84
-29
lines changed

crates/ide-assists/src/handlers/auto_import.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,6 @@ mod baz {
373373
);
374374
}
375375

376-
#[test]
377-
fn not_applicable_in_import_statements() {
378-
check_assist_not_applicable(
379-
auto_import,
380-
r"
381-
use PubStruct$0;
382-
383-
pub mod PubMod {
384-
pub struct PubStruct;
385-
}",
386-
);
387-
}
388-
389376
#[test]
390377
fn function_import() {
391378
check_assist(
@@ -1121,4 +1108,43 @@ struct Foo;
11211108
"#,
11221109
);
11231110
}
1111+
1112+
#[test]
1113+
fn works_in_use_start() {
1114+
check_assist(
1115+
auto_import,
1116+
r#"
1117+
mod bar {
1118+
pub mod foo {
1119+
pub struct Foo;
1120+
}
1121+
}
1122+
use foo$0::Foo;
1123+
"#,
1124+
r#"
1125+
mod bar {
1126+
pub mod foo {
1127+
pub struct Foo;
1128+
}
1129+
}
1130+
use bar::foo;
1131+
use foo::Foo;
1132+
"#,
1133+
);
1134+
}
1135+
1136+
#[test]
1137+
fn not_applicable_in_non_start_use() {
1138+
check_assist_not_applicable(
1139+
auto_import,
1140+
r"
1141+
mod bar {
1142+
pub mod foo {
1143+
pub struct Foo;
1144+
}
1145+
}
1146+
use foo::Foo$0;
1147+
",
1148+
);
1149+
}
11241150
}

crates/ide-assists/src/handlers/qualify_path.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,6 @@ pub mod PubMod {
381381
check_assist_not_applicable(qualify_path, r#"PubStruct$0"#);
382382
}
383383

384-
#[test]
385-
fn not_applicable_in_import_statements() {
386-
check_assist_not_applicable(
387-
qualify_path,
388-
r#"
389-
use PubStruct$0;
390-
391-
pub mod PubMod {
392-
pub struct PubStruct;
393-
}
394-
"#,
395-
);
396-
}
397-
398384
#[test]
399385
fn qualify_function() {
400386
check_assist(
@@ -1270,4 +1256,42 @@ struct Foo;
12701256
"#,
12711257
);
12721258
}
1259+
1260+
#[test]
1261+
fn works_in_use_start() {
1262+
check_assist(
1263+
qualify_path,
1264+
r#"
1265+
mod bar {
1266+
pub mod foo {
1267+
pub struct Foo;
1268+
}
1269+
}
1270+
use foo$0::Foo;
1271+
"#,
1272+
r#"
1273+
mod bar {
1274+
pub mod foo {
1275+
pub struct Foo;
1276+
}
1277+
}
1278+
use bar::foo::Foo;
1279+
"#,
1280+
);
1281+
}
1282+
1283+
#[test]
1284+
fn not_applicable_in_non_start_use() {
1285+
check_assist_not_applicable(
1286+
qualify_path,
1287+
r"
1288+
mod bar {
1289+
pub mod foo {
1290+
pub struct Foo;
1291+
}
1292+
}
1293+
use foo::Foo$0;
1294+
",
1295+
);
1296+
}
12731297
}

crates/ide-db/src/imports/import_assets.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,13 @@ impl ImportAssets {
114114
sema: &Semantics<RootDatabase>,
115115
) -> Option<Self> {
116116
let candidate_node = fully_qualified_path.syntax().clone();
117-
if candidate_node.ancestors().find_map(ast::Use::cast).is_some() {
118-
return None;
117+
if let Some(use_tree) = candidate_node.ancestors().find_map(ast::UseTree::cast) {
118+
// Path is inside a use tree, then only continue if it is the first segment of a use statement.
119+
if use_tree.syntax().parent().and_then(ast::Use::cast).is_none()
120+
|| fully_qualified_path.qualifier().is_some()
121+
{
122+
return None;
123+
}
119124
}
120125
Some(Self {
121126
import_candidate: ImportCandidate::for_regular_path(sema, fully_qualified_path)?,

0 commit comments

Comments
 (0)