Add config to unconditionally prefer core imports over std

Fixes https://github.com/rust-lang/rust-analyzer/issues/12979
This commit is contained in:
Lukas Wirth 2022-09-09 20:04:56 +02:00
parent 6909556435
commit 7d19971666
33 changed files with 156 additions and 43 deletions

View file

@ -551,7 +551,9 @@ fn enum_variants_with_paths(
}
for variant in variants {
if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
if let Some(path) =
ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant), ctx.config.prefer_core)
{
// Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice
if path.segments().len() > 1 {

View file

@ -165,7 +165,11 @@ pub(crate) fn complete_expr_path(
hir::Adt::Struct(strukt) => {
let path = ctx
.module
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
.find_use_path(
ctx.db,
hir::ModuleDef::from(strukt),
ctx.config.prefer_core,
)
.filter(|it| it.len() > 1);
acc.add_struct_literal(ctx, path_ctx, strukt, path, None);
@ -183,7 +187,7 @@ pub(crate) fn complete_expr_path(
hir::Adt::Union(un) => {
let path = ctx
.module
.find_use_path(ctx.db, hir::ModuleDef::from(un))
.find_use_path(ctx.db, hir::ModuleDef::from(un), ctx.config.prefer_core)
.filter(|it| it.len() > 1);
acc.add_union_literal(ctx, un, path, None);

View file

@ -262,7 +262,11 @@ fn import_on_the_fly(
acc.add_all(
import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)
.into_iter()
.filter(ns_filter)
.filter(|import| {
@ -306,7 +310,11 @@ fn import_on_the_fly_pat_(
acc.add_all(
import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)
.into_iter()
.filter(ns_filter)
.filter(|import| {
@ -344,7 +352,7 @@ fn import_on_the_fly_method(
let user_input_lowercased = potential_import_name.to_lowercase();
import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_core)
.into_iter()
.filter(|import| {
!ctx.is_item_hidden(&import.item_to_import)

View file

@ -17,6 +17,7 @@ pub struct CompletionConfig {
pub callable: Option<CallableSnippets>,
pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
pub snippets: Vec<Snippet>,
}

View file

@ -234,7 +234,12 @@ pub fn resolve_completion_edits(
);
let import = items_with_name
.filter_map(|candidate| {
current_module.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
current_module.find_use_path_prefixed(
db,
candidate,
config.insert_use.prefix_kind,
config.prefer_core,
)
})
.find(|mod_path| mod_path.to_string() == full_import_path);
if let Some(import_path) = import {

View file

@ -174,8 +174,12 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
hir::PathResolution::Def(def) => def.into(),
_ => return None,
};
let path =
ctx.module.find_use_path_prefixed(ctx.db, item, ctx.config.insert_use.prefix_kind)?;
let path = ctx.module.find_use_path_prefixed(
ctx.db,
item,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
)?;
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None)))
};
let mut res = Vec::with_capacity(requires.len());

View file

@ -66,6 +66,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
enable_private_editable: false,
callable: Some(CallableSnippets::FillArguments),
snippet_cap: SnippetCap::new(true),
prefer_core: false,
insert_use: InsertUseConfig {
granularity: ImportGranularity::Crate,
prefix_kind: PrefixKind::Plain,