internal: refactor prefer_no_std/prefer_prelude bools into a struct

This commit is contained in:
David Barsky 2024-05-17 15:00:21 -04:00 committed by Lukas Wirth
parent 6a16749eb0
commit b75301cec8
37 changed files with 304 additions and 351 deletions

View file

@ -1,8 +1,8 @@
//! Look up accessible paths for items.
use hir::{
db::HirDatabase, AsAssocItem, AssocItem, AssocItemContainer, Crate, HasCrate, ItemInNs,
ModPath, Module, ModuleDef, Name, PathResolution, PrefixKind, ScopeDef, Semantics,
db::HirDatabase, AsAssocItem, AssocItem, AssocItemContainer, Crate, HasCrate, ImportPathConfig,
ItemInNs, ModPath, Module, ModuleDef, Name, PathResolution, PrefixKind, ScopeDef, Semantics,
SemanticsScope, Trait, Type,
};
use itertools::{EitherOrBoth, Itertools};
@ -205,24 +205,22 @@ impl ImportAssets {
pub fn search_for_imports(
&self,
sema: &Semantics<'_, RootDatabase>,
cfg: ImportPathConfig,
prefix_kind: PrefixKind,
prefer_no_std: bool,
prefer_prelude: bool,
) -> impl Iterator<Item = LocatedImport> {
let _p = tracing::span!(tracing::Level::INFO, "ImportAssets::search_for_imports").entered();
self.search_for(sema, Some(prefix_kind), prefer_no_std, prefer_prelude)
self.search_for(sema, Some(prefix_kind), cfg)
}
/// 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_no_std: bool,
prefer_prelude: bool,
cfg: ImportPathConfig,
) -> impl Iterator<Item = LocatedImport> {
let _p = tracing::span!(tracing::Level::INFO, "ImportAssets::search_for_relative_paths")
.entered();
self.search_for(sema, None, prefer_no_std, prefer_prelude)
self.search_for(sema, None, cfg)
}
/// Requires imports to by prefix instead of fuzzily.
@ -259,8 +257,7 @@ impl ImportAssets {
&self,
sema: &Semantics<'_, RootDatabase>,
prefixed: Option<PrefixKind>,
prefer_no_std: bool,
prefer_prelude: bool,
cfg: ImportPathConfig,
) -> impl Iterator<Item = LocatedImport> {
let _p = tracing::span!(tracing::Level::INFO, "ImportAssets::search_for").entered();
@ -277,8 +274,7 @@ impl ImportAssets {
item_for_path_search(sema.db, item)?,
&self.module_with_candidate,
prefixed,
prefer_no_std,
prefer_prelude,
cfg,
)
.filter(|path| path.len() > 1)
};
@ -634,19 +630,12 @@ fn get_mod_path(
item_to_search: ItemInNs,
module_with_candidate: &Module,
prefixed: Option<PrefixKind>,
prefer_no_std: bool,
prefer_prelude: bool,
cfg: ImportPathConfig,
) -> Option<ModPath> {
if let Some(prefix_kind) = prefixed {
module_with_candidate.find_use_path(
db,
item_to_search,
prefix_kind,
prefer_no_std,
prefer_prelude,
)
module_with_candidate.find_use_path(db, item_to_search, prefix_kind, cfg)
} else {
module_with_candidate.find_path(db, item_to_search, prefer_no_std, prefer_prelude)
module_with_candidate.find_path(db, item_to_search, cfg)
}
}

View file

@ -2,7 +2,7 @@
use crate::helpers::mod_path_to_ast;
use either::Either;
use hir::{AsAssocItem, HirDisplay, ModuleDef, SemanticsScope};
use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
use itertools::Itertools;
use rustc_hash::FxHashMap;
use syntax::{
@ -308,11 +308,12 @@ impl Ctx<'_> {
parent.segment()?.name_ref()?,
)
.and_then(|trait_ref| {
let cfg =
ImportPathConfig { prefer_no_std: false, prefer_prelude: true };
let found_path = self.target_module.find_path(
self.source_scope.db.upcast(),
hir::ModuleDef::Trait(trait_ref),
false,
true,
cfg,
)?;
match make::ty_path(mod_path_to_ast(&found_path)) {
ast::Type::PathType(path_ty) => Some(path_ty),
@ -347,12 +348,9 @@ impl Ctx<'_> {
}
}
let found_path = self.target_module.find_path(
self.source_scope.db.upcast(),
def,
false,
true,
)?;
let cfg = ImportPathConfig { prefer_no_std: false, prefer_prelude: true };
let found_path =
self.target_module.find_path(self.source_scope.db.upcast(), def, cfg)?;
let res = mod_path_to_ast(&found_path).clone_for_update();
if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) {
if let Some(segment) = res.segment() {
@ -385,11 +383,11 @@ impl Ctx<'_> {
if let Some(adt) = ty.as_adt() {
if let ast::Type::PathType(path_ty) = &ast_ty {
let cfg = ImportPathConfig { prefer_no_std: false, prefer_prelude: true };
let found_path = self.target_module.find_path(
self.source_scope.db.upcast(),
ModuleDef::from(adt),
false,
true,
cfg,
)?;
if let Some(qual) = mod_path_to_ast(&found_path).qualifier() {