This commit is contained in:
Lukas Wirth 2024-07-22 09:54:31 +02:00
parent 1141c35a4b
commit e3f16223c8
17 changed files with 65 additions and 139 deletions

View file

@ -4,6 +4,7 @@
//! module, and we use to statically check that we only produce snippet
//! assists if we are allowed to.
use hir::ImportPathConfig;
use ide_db::{imports::insert_use::InsertUseConfig, SnippetCap};
use crate::AssistKind;
@ -20,3 +21,13 @@ pub struct AssistConfig {
pub term_search_fuel: u64,
pub term_search_borrowck: bool,
}
impl AssistConfig {
pub fn import_path_config(&self) -> ImportPathConfig {
ImportPathConfig {
prefer_no_std: self.prefer_no_std,
prefer_prelude: self.prefer_prelude,
prefer_absolute: self.prefer_absolute,
}
}
}

View file

@ -71,11 +71,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
.filter(|pat| !matches!(pat, Pat::WildcardPat(_)))
.collect();
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
let module = ctx.sema.scope(expr.syntax())?.module();
let (mut missing_pats, is_non_exhaustive, has_hidden_variants): (

View file

@ -1,6 +1,6 @@
use std::cmp::Reverse;
use hir::{db::HirDatabase, ImportPathConfig, Module};
use hir::{db::HirDatabase, Module};
use ide_db::{
helpers::mod_path_to_ast,
imports::{
@ -90,11 +90,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel};
// # pub mod std { pub mod collections { pub struct HashMap { } } }
// ```
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
let mut proposed_imports: Vec<_> = import_assets
@ -108,7 +104,6 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
NodeOrToken::Node(node) => ctx.sema.original_range(node).range,
NodeOrToken::Token(token) => token.text_range(),
};
let group_label = group_label(import_assets.import_candidate());
let scope = ImportScope::find_insert_use_container(
&match syntax_under_caret {
NodeOrToken::Node(it) => it,
@ -121,18 +116,12 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
proposed_imports.sort_by(|a, b| a.import_path.cmp(&b.import_path));
proposed_imports.dedup_by(|a, b| a.import_path == b.import_path);
let current_node = match ctx.covering_element() {
NodeOrToken::Node(node) => Some(node),
NodeOrToken::Token(token) => token.parent(),
};
let current_module =
current_node.as_ref().and_then(|node| ctx.sema.scope(node)).map(|scope| scope.module());
let current_module = ctx.sema.scope(scope.as_syntax_node()).map(|scope| scope.module());
// prioritize more relevant imports
proposed_imports
.sort_by_key(|import| Reverse(relevance_score(ctx, import, current_module.as_ref())));
let group_label = group_label(import_assets.import_candidate());
for import in proposed_imports {
let import_path = import.import_path;
@ -226,7 +215,7 @@ fn group_label(import_candidate: &ImportCandidate) -> GroupLabel {
/// Determine how relevant a given import is in the current context. Higher scores are more
/// relevant.
fn relevance_score(
pub(crate) fn relevance_score(
ctx: &AssistContext<'_>,
import: &LocatedImport,
current_module: Option<&Module>,

View file

@ -1,5 +1,5 @@
use either::Either;
use hir::{ImportPathConfig, ModuleDef};
use hir::ModuleDef;
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
@ -337,11 +337,7 @@ fn augment_references_with_imports(
) -> Vec<FileReferenceWithImport> {
let mut visited_modules = FxHashSet::default();
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
references
.into_iter()

View file

@ -1,4 +1,3 @@
use hir::ImportPathConfig;
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast, traits::resolve_target_trait};
use syntax::ast::{self, AstNode, HasGenericArgs, HasName};
@ -44,11 +43,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
return None;
}
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
let src_type_path = {
let src_type_path = src_type.syntax().descendants().find_map(ast::Path::cast)?;

View file

@ -1,5 +1,5 @@
use either::Either;
use hir::{ImportPathConfig, ModuleDef};
use hir::ModuleDef;
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
@ -183,11 +183,7 @@ fn augment_references_with_imports(
) -> Vec<(ast::NameLike, Option<(ImportScope, ast::Path)>)> {
let mut visited_modules = FxHashSet::default();
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
references
.iter()

View file

@ -1,4 +1,4 @@
use hir::{sym, HasVisibility, ImportPathConfig};
use hir::{sym, HasVisibility};
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
@ -87,11 +87,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
let ty = ctx.sema.type_of_binding_in_pat(&ident_pat)?;
let hir::Adt::Struct(struct_type) = ty.strip_references().as_adt()? else { return None };
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
let module = ctx.sema.scope(ident_pat.syntax())?.module();
let struct_def = hir::ModuleDef::from(struct_type);

View file

@ -3,8 +3,8 @@ use std::{iter, ops::RangeInclusive};
use ast::make;
use either::Either;
use hir::{
DescendPreference, HasSource, HirDisplay, ImportPathConfig, InFile, Local, LocalSource,
ModuleDef, PathResolution, Semantics, TypeInfo, TypeParam,
DescendPreference, HasSource, HirDisplay, InFile, Local, LocalSource, ModuleDef,
PathResolution, Semantics, TypeInfo, TypeParam,
};
use ide_db::{
defs::{Definition, NameRefClass},
@ -213,11 +213,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,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
);
if let Some(mod_path) = mod_path {

View file

@ -1,7 +1,7 @@
use std::iter;
use either::Either;
use hir::{ImportPathConfig, Module, ModuleDef, Name, Variant};
use hir::{Module, ModuleDef, Name, Variant};
use ide_db::{
defs::Definition,
helpers::mod_path_to_ast,
@ -390,11 +390,7 @@ fn process_references(
ctx.sema.db,
*enum_module_def,
ctx.config.insert_use.prefix_kind,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
);
if let Some(mut mod_path) = mod_path {
mod_path.pop_segment();

View file

@ -1,6 +1,6 @@
use std::fmt::Display;
use hir::{ImportPathConfig, ModPath, ModuleDef};
use hir::{ModPath, ModuleDef};
use ide_db::{famous_defs::FamousDefs, RootDatabase};
use syntax::{
ast::{self, HasName},
@ -58,15 +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_path(
ctx.db(),
ModuleDef::Trait(trait_),
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;
let trait_path =
module.find_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.import_path_config())?;
let field_type = field.ty()?;
let field_name = field.name()?;
@ -106,15 +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_path(
ctx.db(),
ModuleDef::Trait(trait_),
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;
let trait_path =
module.find_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.import_path_config())?;
let field_type = field.ty()?;
let target = field.syntax().text_range();

View file

@ -1,4 +1,3 @@
use hir::ImportPathConfig;
use ide_db::{
imports::import_assets::item_for_path_search, use_trivial_constructor::use_trivial_constructor,
};
@ -62,11 +61,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
let type_path = current_module.find_path(
ctx.sema.db,
item_for_path_search(ctx.sema.db, item_in_ns)?,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
)?;
let expr = use_trivial_constructor(

View file

@ -1,7 +1,4 @@
use hir::{
db::HirDatabase, AsAssocItem, AssocItem, AssocItemContainer, ImportPathConfig, ItemInNs,
ModuleDef,
};
use hir::{db::HirDatabase, AsAssocItem, AssocItem, AssocItemContainer, ItemInNs, ModuleDef};
use ide_db::assists::{AssistId, AssistKind};
use syntax::{ast, AstNode};
@ -50,11 +47,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let receiver_path = current_module.find_path(
ctx.sema.db,
item_for_path_search(ctx.sema.db, item_in_ns)?,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
)?;
let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call);

View file

@ -1,6 +1,7 @@
use std::cmp::Reverse;
use std::iter;
use hir::{AsAssocItem, ImportPathConfig};
use hir::AsAssocItem;
use ide_db::RootDatabase;
use ide_db::{
helpers::mod_path_to_ast,
@ -38,11 +39,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 cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};
let cfg = ctx.config.import_path_config();
let mut proposed_imports: Vec<_> =
import_assets.search_for_relative_paths(&ctx.sema, cfg).collect();
@ -50,12 +47,8 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
return None;
}
let range = match &syntax_under_caret {
NodeOrToken::Node(node) => ctx.sema.original_range(node).range,
NodeOrToken::Token(token) => token.text_range(),
};
let candidate = import_assets.import_candidate();
let qualify_candidate = match syntax_under_caret {
let qualify_candidate = match syntax_under_caret.clone() {
NodeOrToken::Node(syntax_under_caret) => match candidate {
ImportCandidate::Path(candidate) if candidate.qualifier.is_some() => {
cov_mark::hit!(qualify_path_qualifier_start);
@ -89,6 +82,22 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
proposed_imports.sort_by(|a, b| a.import_path.cmp(&b.import_path));
proposed_imports.dedup_by(|a, b| a.import_path == b.import_path);
let range = match &syntax_under_caret {
NodeOrToken::Node(node) => ctx.sema.original_range(node).range,
NodeOrToken::Token(token) => token.text_range(),
};
let current_module = ctx
.sema
.scope(&match syntax_under_caret {
NodeOrToken::Node(node) => node.clone(),
NodeOrToken::Token(t) => t.parent()?,
})
.map(|scope| scope.module());
// prioritize more relevant imports
proposed_imports.sort_by_key(|import| {
Reverse(super::auto_import::relevance_score(ctx, import, current_module.as_ref()))
});
let group_label = group_label(candidate);
for import in proposed_imports {
acc.add_group(

View file

@ -1,4 +1,4 @@
use hir::{ImportPathConfig, InFile, MacroFileIdExt, ModuleDef};
use hir::{InFile, MacroFileIdExt, ModuleDef};
use ide_db::{helpers::mod_path_to_ast, imports::import_assets::NameToImport, items_locator};
use itertools::Itertools;
use syntax::{
@ -83,15 +83,7 @@ pub(crate) fn replace_derive_with_manual_impl(
})
.flat_map(|trait_| {
current_module
.find_path(
ctx.sema.db,
hir::ModuleDef::Trait(trait_),
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.find_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.import_path_config())
.as_ref()
.map(mod_path_to_ast)
.zip(Some(trait_))

View file

@ -1,4 +1,4 @@
use hir::{AsAssocItem, ImportPathConfig};
use hir::AsAssocItem;
use ide_db::{
helpers::mod_path_to_ast,
imports::insert_use::{insert_use, ImportScope},
@ -67,11 +67,7 @@ pub(crate) fn replace_qualified_name_with_use(
ctx.sema.db,
module,
ctx.config.insert_use.prefix_kind,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
)
})
.flatten();

View file

@ -1,8 +1,5 @@
//! Term search assist
use hir::{
term_search::{TermSearchConfig, TermSearchCtx},
ImportPathConfig,
};
use hir::term_search::{TermSearchConfig, TermSearchCtx};
use ide_db::{
assists::{AssistId, AssistKind, GroupLabel},
famous_defs::FamousDefs,
@ -54,16 +51,7 @@ pub(crate) fn term_search(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let paths = paths
.into_iter()
.filter_map(|path| {
path.gen_source_code(
&scope,
&mut formatter,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.ok()
path.gen_source_code(&scope, &mut formatter, ctx.config.import_path_config()).ok()
})
.unique();

View file

@ -1,4 +1,4 @@
use hir::{ImportPathConfig, ModuleDef};
use hir::ModuleDef;
use ide_db::{
assists::{AssistId, AssistKind},
famous_defs::FamousDefs,
@ -139,11 +139,7 @@ pub(crate) fn desugar_async_into_impl_future(
let trait_path = module.find_path(
ctx.db(),
ModuleDef::Trait(future_trait),
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
ctx.config.import_path_config(),
)?;
let trait_path = trait_path.display(ctx.db());