Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions crates/ide-assists/src/handlers/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Opt
acc.add_group(&group_label, assist_id, label, range, |builder| {
let editor = builder.make_editor(scope.as_syntax_node());
insert_use_with_editor(
&ctx.sema,
&scope,
mod_path_to_ast(&import_path, edition),
&ctx.config.insert_use,
Expand All @@ -147,6 +148,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Opt
acc.add_group(&group_label, assist_id, label, range, |builder| {
let editor = builder.make_editor(scope.as_syntax_node());
insert_use_as_alias_with_editor(
&ctx.sema,
&scope,
mod_path_to_ast(&import_path, edition),
&ctx.config.insert_use,
Expand Down Expand Up @@ -352,8 +354,8 @@ mod tests {
use test_fixture::WithFixture;

use crate::tests::{
TEST_CONFIG, check_assist, check_assist_by_label, check_assist_not_applicable,
check_assist_target,
TEST_CONFIG, check_assist, check_assist_by_label, check_assist_import_one,
check_assist_not_applicable, check_assist_target,
};

fn check_auto_import_order(before: &str, order: &[&str]) {
Expand Down Expand Up @@ -553,6 +555,80 @@ mod baz {
);
}

#[test]
fn preserves_value_namespace_when_merging() {
check_assist(
auto_import,
r#"
use foo::bar;

mod foo {
pub mod bar {
pub struct Baz;
}
pub fn bar() {}
}

fn main() {
let _: Baz$0;
bar();
}
"#,
r#"
use foo::{bar, bar::Baz};

mod foo {
pub mod bar {
pub struct Baz;
}
pub fn bar() {}
}

fn main() {
let _: Baz;
bar();
}
"#,
);
}

#[test]
fn preserves_value_namespace_when_merging_all_imports() {
check_assist_import_one(
auto_import,
r#"
use foo::bar;

mod foo {
pub mod bar {
pub struct Baz;
}
pub fn bar() {}
}

fn main() {
let _: Baz$0;
bar();
}
"#,
r#"
use {foo::bar, foo::bar::Baz};

mod foo {
pub mod bar {
pub struct Baz;
}
pub fn bar() {}
}

fn main() {
let _: Baz;
bar();
}
"#,
);
}

#[test]
fn applicable_when_found_an_import() {
check_assist(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/convert_bool_to_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(crate) fn convert_bool_to_enum(acc: &mut Assists, ctx: &AssistContext<'_, '_
);
for (file_id, scope, path) in delayed_mutations {
let editor = edit.make_editor(scope.as_syntax_node());
insert_use_with_editor(&scope, path, &ctx.config.insert_use, &editor);
insert_use_with_editor(&ctx.sema, &scope, path, &ctx.config.insert_use, &editor);
edit.add_file_edits(file_id, editor);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ fn replace_usages(
}
}
if let Some((import_scope, path)) = import_data {
insert_use_with_editor(&import_scope, path, &ctx.config.insert_use, &editor);
insert_use_with_editor(
&ctx.sema,
&import_scope,
path,
&ctx.config.insert_use,
&editor,
);
}
});
edit.add_file_edits(file_id.file_id(ctx.db()), editor);
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -

if let Some(mod_path) = mod_path {
insert_use_with_editor(
&ctx.sema,
&scope,
mod_path_to_ast_with_factory(make, &mod_path, edition),
&ctx.config.insert_use,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::iter;

use either::Either;
use hir::{EnumVariant, HasCrate, Module, ModuleDef, Name};
use hir::{EnumVariant, HasCrate, Module, ModuleDef, Name, Semantics};
use ide_db::{
FxHashSet, RootDatabase,
defs::Definition,
Expand Down Expand Up @@ -90,6 +90,7 @@ pub(crate) fn extract_struct_from_enum_variant(
let file_editor = builder.make_editor(processed[0].0.syntax());
processed.into_iter().for_each(|(path, node, import)| {
apply_references(
&ctx.sema,
ctx.config.insert_use,
path,
node,
Expand All @@ -110,7 +111,15 @@ pub(crate) fn extract_struct_from_enum_variant(
references,
);
processed.into_iter().for_each(|(path, node, import)| {
apply_references(ctx.config.insert_use, path, node, import, edition, &editor)
apply_references(
&ctx.sema,
ctx.config.insert_use,
path,
node,
import,
edition,
&editor,
)
});
}

Expand Down Expand Up @@ -385,6 +394,7 @@ fn collect_variant_comments(
}

fn apply_references(
sema: &Semantics<'_, RootDatabase>,
insert_use_cfg: InsertUseConfig,
segment: ast::PathSegment,
node: SyntaxNode,
Expand All @@ -395,6 +405,7 @@ fn apply_references(
let make = editor.make();
if let Some((scope, path)) = import {
insert_use_with_editor(
sema,
&scope,
mod_path_to_ast_with_factory(make, &path, edition),
&insert_use_cfg,
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/merge_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn merge_uses(
};
let mut merged = first.clone();
for item in &rest {
merged = try_merge_imports(editor.make(), &merged, item, mb)?;
merged = try_merge_imports(editor.make(), &merged, item, mb, None)?;
}
for item in rest {
item.remove(editor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub(crate) fn replace_qualified_name_with_use(
Some(qualifier) => make.path_concat(qualifier, path),
None => path,
};
insert_use_with_editor(&scope, path, &ctx.config.insert_use, &editor);
insert_use_with_editor(&ctx.sema, &scope, path, &ctx.config.insert_use, &editor);
builder.add_file_edits(ctx.vfs_file_id(), editor);
},
)
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/unqualify_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn add_import(

if let Some(scope) = scope {
ide_db::imports::insert_use::insert_use_with_editor(
&ctx.sema,
&scope,
import,
&ctx.config.insert_use,
Expand Down
9 changes: 8 additions & 1 deletion crates/ide-completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,21 @@ pub fn resolve_completion_edits(
let full_path = make.path_from_text_with_edition(&import.path, current_edition);
if import.as_underscore {
insert_use::insert_use_as_alias_with_editor(
&sema,
&scope,
full_path,
&config.insert_use,
current_edition,
&editor,
);
} else {
insert_use::insert_use_with_editor(&scope, full_path, &config.insert_use, &editor);
insert_use::insert_use_with_editor(
&sema,
&scope,
full_path,
&config.insert_use,
&editor,
);
}
});

Expand Down
92 changes: 92 additions & 0 deletions crates/ide-completion/src/tests/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,98 @@ fn main() {
);
}

#[test]
fn flyimport_preserves_value_namespace_when_merging() {
check_edit(
"Baz",
r#"
//- /lib.rs crate:dep
pub mod bar {
pub struct Baz;
}
pub fn bar() {}

//- /main.rs crate:main deps:dep
use dep::bar;

fn main() {
Ba$0;
bar();
}
"#,
r#"
use dep::{bar, bar::Baz};

fn main() {
Baz;
bar();
}
"#,
);
}

#[test]
fn flyimport_preserves_macro_namespace_when_merging() {
check_edit(
"vec!",
r#"
//- /lib.rs crate:dep
pub mod vec {
pub struct Vec;
}
#[macro_export]
macro_rules! vec {
() => {};
}

//- /main.rs crate:main deps:dep
use dep::vec::Vec;

fn main() {
ve$0
}
"#,
r#"
use dep::{vec, vec::Vec};

fn main() {
vec!($0)
}
"#,
);
}

#[test]
fn flyimport_preserves_macro_namespace_with_existing_self() {
check_edit(
"vec!",
r#"
//- /lib.rs crate:dep
pub mod vec {
pub struct Vec;
}
#[macro_export]
macro_rules! vec {
() => {};
}

//- /main.rs crate:main deps:dep
use dep::vec::{self, Vec};

fn main() {
ve$0
}
"#,
r#"
use dep::{vec, vec::Vec};

fn main() {
vec!($0)
}
"#,
);
}

#[test]
fn struct_fuzzy_completion() {
check_edit(
Expand Down
Loading