Skip to content

Commit

Permalink
Restructure find_path into a separate functions for modules and non…
Browse files Browse the repository at this point in the history
…-module items

Also renames `prefer_core` imports config to `prefer_no_std` and changes the behavior of no_std path searching by preferring `core` paths `over` alloc
  • Loading branch information
Veykril committed Sep 13, 2022
1 parent b1a4ba3 commit a8ecaa1
Show file tree
Hide file tree
Showing 31 changed files with 291 additions and 200 deletions.
360 changes: 219 additions & 141 deletions crates/hir-def/src/find_path.rs

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion crates/hir-def/src/item_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,15 @@ impl ItemInNs {
/// Returns the crate defining this item (or `None` if `self` is built-in).
pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> {
match self {
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate),
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db).map(|m| m.krate),
ItemInNs::Macros(id) => Some(id.module(db).krate),
}
}

pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {
match self {
ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db),
ItemInNs::Macros(id) => Some(id.module(db)),
}
}
}
8 changes: 4 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@ impl Module {
self,
db: &dyn DefDatabase,
item: impl Into<ItemInNs>,
prefer_core: bool,
prefer_no_std: bool,
) -> Option<ModPath> {
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_core)
hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_no_std)
}

/// Finds a path that can be used to refer to the given item from within
Expand All @@ -597,14 +597,14 @@ impl Module {
db: &dyn DefDatabase,
item: impl Into<ItemInNs>,
prefix_kind: PrefixKind,
prefer_core: bool,
prefer_no_std: bool,
) -> Option<ModPath> {
hir_def::find_path::find_path_prefixed(
db,
item.into().into(),
self.into(),
prefix_kind,
prefer_core,
prefer_no_std,
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ pub struct AssistConfig {
pub snippet_cap: Option<SnippetCap>,
pub allowed: Option<Vec<AssistKind>>,
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
pub prefer_no_std: bool,
}
8 changes: 4 additions & 4 deletions crates/ide-assists/src/handlers/add_missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ctx.config.prefer_core)?,
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)?,
variant.should_be_hidden(ctx.db(), module.krate()),
))
})
Expand Down Expand Up @@ -133,7 +133,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
.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, ctx.config.prefer_core)
build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)
});

(ast::Pat::from(make::tuple_pat(patterns)), is_hidden)
Expand Down Expand Up @@ -354,12 +354,12 @@ fn build_pat(
db: &RootDatabase,
module: hir::Module,
var: ExtendedVariant,
prefer_core: bool,
prefer_no_std: bool,
) -> Option<ast::Pat> {
match var {
ExtendedVariant::Variant(var) => {
let path =
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_core)?);
mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_no_std)?);

// 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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let mut proposed_imports = import_assets.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
);
if proposed_imports.is_empty() {
return None;
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/convert_into_to_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ctx.config.prefer_core)?)
mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_no_std)?)
};

let dest_type = match &ast_trait {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +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,
ctx.config.prefer_no_std,
);

if let Some(mod_path) = mod_path {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ fn process_references(
ctx.sema.db,
*enum_module_def,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
);
if let Some(mut mod_path) = mod_path {
mod_path.pop_segment();
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/generate_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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_), ctx.config.prefer_core)?;
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;

let field_type = field.ty()?;
let field_name = field.name()?;
Expand Down Expand Up @@ -100,7 +100,7 @@ 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_), ctx.config.prefer_core)?;
module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?;

let field_type = field.ty()?;
let target = field.syntax().text_range();
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/generate_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
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,
ctx.config.prefer_no_std,
)?;

let expr = use_trivial_constructor(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/qualify_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
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,
ctx.config.prefer_no_std,
)?;

let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/qualify_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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, ctx.config.prefer_core);
import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_no_std);
if proposed_imports.is_empty() {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_), ctx.config.prefer_core)
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_no_std)
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
ctx.sema.db,
module,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
)
})
.flatten();
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
group: true,
skip_glob_imports: true,
},
prefer_core: false,
prefer_no_std: false,
};

pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
Expand Down
8 changes: 5 additions & 3 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,11 @@ fn enum_variants_with_paths(
}

for variant in variants {
if let Some(path) =
ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant), ctx.config.prefer_core)
{
if let Some(path) = ctx.module.find_use_path(
ctx.db,
hir::ModuleDef::from(variant),
ctx.config.prefer_no_std,
) {
// Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice
if path.segments().len() > 1 {
Expand Down
8 changes: 6 additions & 2 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(crate) fn complete_expr_path(
.find_use_path(
ctx.db,
hir::ModuleDef::from(strukt),
ctx.config.prefer_core,
ctx.config.prefer_no_std,
)
.filter(|it| it.len() > 1);

Expand All @@ -187,7 +187,11 @@ pub(crate) fn complete_expr_path(
hir::Adt::Union(un) => {
let path = ctx
.module
.find_use_path(ctx.db, hir::ModuleDef::from(un), ctx.config.prefer_core)
.find_use_path(
ctx.db,
hir::ModuleDef::from(un),
ctx.config.prefer_no_std,
)
.filter(|it| it.len() > 1);

acc.add_union_literal(ctx, un, path, None);
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-completion/src/completions/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ fn import_on_the_fly(
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
)
.into_iter()
.filter(ns_filter)
Expand Down Expand Up @@ -313,7 +313,7 @@ fn import_on_the_fly_pat_(
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
)
.into_iter()
.filter(ns_filter)
Expand Down Expand Up @@ -352,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, ctx.config.prefer_core)
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std)
.into_iter()
.filter(|import| {
!ctx.is_item_hidden(&import.item_to_import)
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct CompletionConfig {
pub callable: Option<CallableSnippets>,
pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
pub prefer_no_std: bool,
pub snippets: Vec<Snippet>,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn resolve_completion_edits(
db,
candidate,
config.insert_use.prefix_kind,
config.prefer_core,
config.prefer_no_std,
)
})
.find(|mod_path| mod_path.to_string() == full_import_path);
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
ctx.db,
item,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_core,
ctx.config.prefer_no_std,
)?;
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None)))
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +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,
prefer_no_std: false,
insert_use: InsertUseConfig {
granularity: ImportGranularity::Crate,
prefix_kind: PrefixKind::Plain,
Expand Down
18 changes: 9 additions & 9 deletions crates/ide-db/src/imports/import_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,20 @@ impl ImportAssets {
&self,
sema: &Semantics<'_, RootDatabase>,
prefix_kind: PrefixKind,
prefer_core: bool,
prefer_no_std: bool,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for_imports");
self.search_for(sema, Some(prefix_kind), prefer_core)
self.search_for(sema, Some(prefix_kind), prefer_no_std)
}

/// This may return non-absolute paths if a part of the returned path is already imported into scope.
pub fn search_for_relative_paths(
&self,
sema: &Semantics<'_, RootDatabase>,
prefer_core: bool,
prefer_no_std: bool,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for_relative_paths");
self.search_for(sema, None, prefer_core)
self.search_for(sema, None, prefer_no_std)
}

pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) {
Expand All @@ -244,7 +244,7 @@ impl ImportAssets {
&self,
sema: &Semantics<'_, RootDatabase>,
prefixed: Option<PrefixKind>,
prefer_core: bool,
prefer_no_std: bool,
) -> Vec<LocatedImport> {
let _p = profile::span("import_assets::search_for");

Expand All @@ -255,7 +255,7 @@ impl ImportAssets {
item_for_path_search(sema.db, item)?,
&self.module_with_candidate,
prefixed,
prefer_core,
prefer_no_std,
)
};

Expand Down Expand Up @@ -568,12 +568,12 @@ fn get_mod_path(
item_to_search: ItemInNs,
module_with_candidate: &Module,
prefixed: Option<PrefixKind>,
prefer_core: bool,
prefer_no_std: bool,
) -> Option<ModPath> {
if let Some(prefix_kind) = prefixed {
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_core)
module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_no_std)
} else {
module_with_candidate.find_use_path(db, item_to_search, prefer_core)
module_with_candidate.find_use_path(db, item_to_search, prefer_no_std)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/handlers/json_is_not_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(crate) fn json_in_items(
sema.db,
it,
config.insert_use.prefix_kind,
config.prefer_core,
config.prefer_no_std,
) {
insert_use(
&scope,
Expand All @@ -153,7 +153,7 @@ pub(crate) fn json_in_items(
sema.db,
it,
config.insert_use.prefix_kind,
config.prefer_core,
config.prefer_no_std,
) {
insert_use(
&scope,
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-diagnostics/src/handlers/missing_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
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,
ctx.config.prefer_no_std,
)?;

use_trivial_constructor(
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub struct DiagnosticsConfig {
pub expr_fill_default: ExprFillDefaultMode,
// FIXME: We may want to include a whole `AssistConfig` here
pub insert_use: InsertUseConfig,
pub prefer_core: bool,
pub prefer_no_std: bool,
}

impl DiagnosticsConfig {
Expand All @@ -171,7 +171,7 @@ impl DiagnosticsConfig {
group: false,
skip_glob_imports: false,
},
prefer_core: false,
prefer_no_std: false,
}
}
}
Expand Down
Loading

0 comments on commit a8ecaa1

Please sign in to comment.