Skip to content

Commit ba3a5c5

Browse files
Merge #7724
7724: Consider import prefix config settings during flyimports r=SomeoneToIgnore a=SomeoneToIgnore Fixes #7666 Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents 23f0d4b + 4fe5786 commit ba3a5c5

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

crates/hir/src/code_model.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ impl Callable {
20762076
}
20772077

20782078
/// For IDE only
2079-
#[derive(Debug)]
2079+
#[derive(Debug, PartialEq, Eq, Hash)]
20802080
pub enum ScopeDef {
20812081
ModuleDef(ModuleDef),
20822082
MacroDef(MacroDef),

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use ide_db::helpers::{
5353
import_assets::{ImportAssets, ImportCandidate},
5454
insert_use::ImportScope,
5555
};
56+
use rustc_hash::FxHashSet;
5657
use syntax::{AstNode, SyntaxNode, T};
5758
use test_utils::mark;
5859

@@ -91,8 +92,10 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
9192
position_for_import(ctx, Some(import_assets.import_candidate()))?,
9293
&ctx.sema,
9394
)?;
95+
96+
let scope_definitions = scope_definitions(ctx);
9497
let mut all_mod_paths = import_assets
95-
.search_for_relative_paths(&ctx.sema)
98+
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
9699
.into_iter()
97100
.map(|(mod_path, item_in_ns)| {
98101
let scope_item = match item_in_ns {
@@ -102,6 +105,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
102105
};
103106
(mod_path, scope_item)
104107
})
108+
.filter(|(_, proposed_def)| !scope_definitions.contains(proposed_def))
105109
.collect::<Vec<_>>();
106110
all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
107111
compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
@@ -125,6 +129,14 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
125129
Some(())
126130
}
127131

132+
fn scope_definitions(ctx: &CompletionContext) -> FxHashSet<ScopeDef> {
133+
let mut scope_definitions = FxHashSet::default();
134+
ctx.scope.process_all_names(&mut |_, scope_def| {
135+
scope_definitions.insert(scope_def);
136+
});
137+
scope_definitions
138+
}
139+
128140
pub(crate) fn position_for_import<'a>(
129141
ctx: &'a CompletionContext,
130142
import_candidate: Option<&ImportCandidate>,
@@ -192,7 +204,7 @@ mod tests {
192204

193205
use crate::{
194206
item::CompletionKind,
195-
test_utils::{check_edit, completion_list},
207+
test_utils::{check_edit, check_edit_with_config, completion_list, TEST_CONFIG},
196208
};
197209

198210
fn check(ra_fixture: &str, expect: Expect) {
@@ -685,4 +697,82 @@ fn main() {}
685697
expect![[]],
686698
);
687699
}
700+
701+
#[test]
702+
fn prefix_config_usage() {
703+
let fixture = r#"
704+
mod foo {
705+
pub mod bar {
706+
pub struct Item;
707+
}
708+
}
709+
710+
use crate::foo::bar;
711+
712+
fn main() {
713+
Ite$0
714+
}"#;
715+
let mut config = TEST_CONFIG;
716+
717+
config.insert_use.prefix_kind = hir::PrefixKind::ByCrate;
718+
check_edit_with_config(
719+
config.clone(),
720+
"Item",
721+
fixture,
722+
r#"
723+
mod foo {
724+
pub mod bar {
725+
pub struct Item;
726+
}
727+
}
728+
729+
use crate::foo::bar::{self, Item};
730+
731+
fn main() {
732+
Item
733+
}"#,
734+
);
735+
736+
config.insert_use.prefix_kind = hir::PrefixKind::BySelf;
737+
check_edit_with_config(
738+
config.clone(),
739+
"Item",
740+
fixture,
741+
r#"
742+
mod foo {
743+
pub mod bar {
744+
pub struct Item;
745+
}
746+
}
747+
748+
use crate::foo::bar;
749+
750+
use self::foo::bar::Item;
751+
752+
fn main() {
753+
Item
754+
}"#,
755+
);
756+
757+
config.insert_use.prefix_kind = hir::PrefixKind::Plain;
758+
check_edit_with_config(
759+
config,
760+
"Item",
761+
fixture,
762+
r#"
763+
mod foo {
764+
pub mod bar {
765+
pub struct Item;
766+
}
767+
}
768+
769+
use foo::bar::Item;
770+
771+
use crate::foo::bar;
772+
773+
fn main() {
774+
Item
775+
}"#,
776+
);
777+
}
688778
}

crates/ide_completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn resolve_completion_edits(
151151
let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)
152152
.filter_map(|candidate| {
153153
let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
154-
current_module.find_use_path(db, item)
154+
current_module.find_use_path_prefixed(db, item, config.insert_use.prefix_kind)
155155
})
156156
.find(|mod_path| mod_path.to_string() == full_import_path)?;
157157

0 commit comments

Comments
 (0)