mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 07:04:49 +00:00
Add config to unconditionally prefer core imports over std
Fixes https://github.com/rust-lang/rust-analyzer/issues/12979
This commit is contained in:
parent
6909556435
commit
7d19971666
33 changed files with 156 additions and 43 deletions
|
@ -13,4 +13,5 @@ pub struct AssistConfig {
|
|||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub allowed: Option<Vec<AssistKind>>,
|
||||
pub insert_use: InsertUseConfig,
|
||||
pub prefer_core: bool,
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
.into_iter()
|
||||
.filter_map(|variant| {
|
||||
Some((
|
||||
build_pat(ctx.db(), module, variant)?,
|
||||
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)?,
|
||||
variant.should_be_hidden(ctx.db(), module.krate()),
|
||||
))
|
||||
})
|
||||
|
@ -132,8 +132,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
let is_hidden = variants
|
||||
.iter()
|
||||
.any(|variant| variant.should_be_hidden(ctx.db(), module.krate()));
|
||||
let patterns =
|
||||
variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant));
|
||||
let patterns = variants.into_iter().filter_map(|variant| {
|
||||
build_pat(ctx.db(), module, variant, ctx.config.prefer_core)
|
||||
});
|
||||
|
||||
(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
|
||||
})
|
||||
|
@ -349,10 +350,16 @@ fn resolve_tuple_of_enum_def(
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> {
|
||||
fn build_pat(
|
||||
db: &RootDatabase,
|
||||
module: hir::Module,
|
||||
var: ExtendedVariant,
|
||||
prefer_core: bool,
|
||||
) -> Option<ast::Pat> {
|
||||
match var {
|
||||
ExtendedVariant::Variant(var) => {
|
||||
let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?);
|
||||
let path =
|
||||
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_core)?);
|
||||
|
||||
// FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
|
||||
let pat: ast::Pat = match var.source(db)?.value.kind() {
|
||||
|
|
|
@ -89,8 +89,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
|
|||
// ```
|
||||
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
|
||||
let mut proposed_imports =
|
||||
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
|
||||
let mut proposed_imports = import_assets.search_for_imports(
|
||||
&ctx.sema,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_core,
|
||||
);
|
||||
if proposed_imports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
|
|||
_ => return None,
|
||||
};
|
||||
|
||||
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?)
|
||||
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_core)?)
|
||||
};
|
||||
|
||||
let dest_type = match &ast_trait {
|
||||
|
|
|
@ -152,6 +152,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
|
|||
ctx.sema.db,
|
||||
ModuleDef::from(control_flow_enum),
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_core,
|
||||
);
|
||||
|
||||
if let Some(mod_path) = mod_path {
|
||||
|
|
|
@ -409,6 +409,7 @@ fn process_references(
|
|||
ctx.sema.db,
|
||||
*enum_module_def,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_core,
|
||||
);
|
||||
if let Some(mut mod_path) = mod_path {
|
||||
mod_path.pop_segment();
|
||||
|
|
|
@ -58,7 +58,8 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
|
|||
|
||||
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
|
||||
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
|
||||
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
|
||||
let trait_path =
|
||||
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
|
||||
|
||||
let field_type = field.ty()?;
|
||||
let field_name = field.name()?;
|
||||
|
@ -98,7 +99,8 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
|
|||
|
||||
let module = ctx.sema.to_def(&strukt)?.module(ctx.db());
|
||||
let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?;
|
||||
let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?;
|
||||
let trait_path =
|
||||
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_core)?;
|
||||
|
||||
let field_type = field.ty()?;
|
||||
let target = field.syntax().text_range();
|
||||
|
|
|
@ -60,8 +60,11 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
|
|||
|
||||
let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?));
|
||||
|
||||
let type_path = current_module
|
||||
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
|
||||
let type_path = current_module.find_use_path(
|
||||
ctx.sema.db,
|
||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||
ctx.config.prefer_core,
|
||||
)?;
|
||||
|
||||
let expr = use_trivial_constructor(
|
||||
&ctx.sema.db,
|
||||
|
|
|
@ -44,8 +44,11 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
|||
let current_module = ctx.sema.scope(call.syntax())?.module();
|
||||
let target_module_def = ModuleDef::from(resolved_call);
|
||||
let item_in_ns = ItemInNs::from(target_module_def);
|
||||
let receiver_path = current_module
|
||||
.find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?;
|
||||
let receiver_path = current_module.find_use_path(
|
||||
ctx.sema.db,
|
||||
item_for_path_search(ctx.sema.db, item_in_ns)?,
|
||||
ctx.config.prefer_core,
|
||||
)?;
|
||||
|
||||
let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);
|
||||
|
||||
|
|
|
@ -37,7 +37,8 @@ use crate::{
|
|||
// ```
|
||||
pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
||||
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
|
||||
let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema);
|
||||
let mut proposed_imports =
|
||||
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_core);
|
||||
if proposed_imports.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ pub(crate) fn replace_derive_with_manual_impl(
|
|||
})
|
||||
.flat_map(|trait_| {
|
||||
current_module
|
||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_core)
|
||||
.as_ref()
|
||||
.map(mod_path_to_ast)
|
||||
.zip(Some(trait_))
|
||||
|
|
|
@ -67,6 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
|
|||
ctx.sema.db,
|
||||
module,
|
||||
ctx.config.insert_use.prefix_kind,
|
||||
ctx.config.prefer_core,
|
||||
)
|
||||
})
|
||||
.flatten();
|
||||
|
|
|
@ -29,6 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
|
|||
group: true,
|
||||
skip_glob_imports: true,
|
||||
},
|
||||
prefer_core: false,
|
||||
};
|
||||
|
||||
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue