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

@ -24,7 +24,7 @@ pub(crate) mod vis;
use std::iter;
use hir::{known, HasAttrs, ScopeDef, Variant};
use hir::{known, HasAttrs, ImportPathConfig, ScopeDef, Variant};
use ide_db::{imports::import_assets::LocatedImport, RootDatabase, SymbolKind};
use syntax::{ast, SmolStr};
@ -636,8 +636,10 @@ fn enum_variants_with_paths(
if let Some(path) = ctx.module.find_path(
ctx.db,
hir::ModuleDef::from(variant),
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
},
) {
// Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice

View file

@ -1,6 +1,6 @@
//! Completion of names from the current scope in expression position.
use hir::ScopeDef;
use hir::{ImportPathConfig, ScopeDef};
use syntax::ast;
use crate::{
@ -174,8 +174,10 @@ pub(crate) fn complete_expr_path(
.find_path(
ctx.db,
hir::ModuleDef::from(strukt),
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
},
)
.filter(|it| it.len() > 1);
@ -197,8 +199,10 @@ pub(crate) fn complete_expr_path(
.find_path(
ctx.db,
hir::ModuleDef::from(un),
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
},
)
.filter(|it| it.len() > 1);

View file

@ -1,5 +1,5 @@
//! See [`import_on_the_fly`].
use hir::{ItemInNs, ModuleDef};
use hir::{ImportPathConfig, ItemInNs, ModuleDef};
use ide_db::imports::{
import_assets::{ImportAssets, LocatedImport},
insert_use::ImportScope,
@ -257,13 +257,13 @@ fn import_on_the_fly(
};
let user_input_lowercased = potential_import_name.to_lowercase();
let import_cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
import_assets
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
)
.search_for_imports(&ctx.sema, import_cfg, ctx.config.insert_use.prefix_kind)
.filter(ns_filter)
.filter(|import| {
let original_item = &import.original_item;
@ -308,13 +308,13 @@ fn import_on_the_fly_pat_(
};
let user_input_lowercased = potential_import_name.to_lowercase();
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
import_assets
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
)
.search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)
.filter(ns_filter)
.filter(|import| {
let original_item = &import.original_item;
@ -355,13 +355,13 @@ fn import_on_the_fly_method(
let user_input_lowercased = potential_import_name.to_lowercase();
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
import_assets
.search_for_imports(
&ctx.sema,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
)
.search_for_imports(&ctx.sema, cfg, ctx.config.insert_use.prefix_kind)
.filter(|import| {
!ctx.is_item_hidden(&import.item_to_import)
&& !ctx.is_item_hidden(&import.original_item)

View file

@ -2,7 +2,7 @@
mod format_like;
use hir::ItemInNs;
use hir::{ImportPathConfig, ItemInNs};
use ide_db::{
documentation::{Documentation, HasDocs},
imports::insert_use::ImportScope,
@ -60,15 +60,17 @@ pub(crate) fn complete_postfix(
None => return,
};
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {
if let Some(drop_fn) = ctx.famous_defs().core_mem_drop() {
if let Some(path) = ctx.module.find_path(
ctx.db,
ItemInNs::Values(drop_fn.into()),
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
) {
if let Some(path) =
ctx.module.find_path(ctx.db, ItemInNs::Values(drop_fn.into()), cfg)
{
cov_mark::hit!(postfix_drop_completion);
let mut item = postfix_snippet(
"drop",

View file

@ -12,6 +12,7 @@ mod snippet;
#[cfg(test)]
mod tests;
use hir::ImportPathConfig;
use ide_db::{
base_db::FilePosition,
helpers::mod_path_to_ast,
@ -251,6 +252,11 @@ pub fn resolve_completion_edits(
let new_ast = scope.clone_for_update();
let mut import_insert = TextEdit::builder();
let cfg = ImportPathConfig {
prefer_no_std: config.prefer_no_std,
prefer_prelude: config.prefer_prelude,
};
imports.into_iter().for_each(|(full_import_path, imported_name)| {
let items_with_name = items_locator::items_with_name(
&sema,
@ -264,8 +270,7 @@ pub fn resolve_completion_edits(
db,
candidate,
config.insert_use.prefix_kind,
config.prefer_no_std,
config.prefer_prelude,
cfg,
)
})
.find(|mod_path| mod_path.display(db).to_string() == full_import_path);

View file

@ -10,7 +10,7 @@ pub(crate) mod type_alias;
pub(crate) mod union_literal;
pub(crate) mod variant;
use hir::{AsAssocItem, HasAttrs, HirDisplay, ModuleDef, ScopeDef, Type};
use hir::{AsAssocItem, HasAttrs, HirDisplay, ImportPathConfig, ModuleDef, ScopeDef, Type};
use ide_db::{
documentation::{Documentation, HasDocs},
helpers::item_name,
@ -295,14 +295,12 @@ pub(crate) fn render_expr(
.unwrap_or_else(|| String::from("..."))
};
let label = expr
.gen_source_code(
&ctx.scope,
&mut label_formatter,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
)
.ok()?;
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
let label = expr.gen_source_code(&ctx.scope, &mut label_formatter, cfg).ok()?;
let source_range = match ctx.original_token.parent() {
Some(node) => match node.ancestors().find_map(ast::Path::cast) {
@ -314,16 +312,8 @@ pub(crate) fn render_expr(
let mut item = CompletionItem::new(CompletionItemKind::Expression, source_range, label);
let snippet = format!(
"{}$0",
expr.gen_source_code(
&ctx.scope,
&mut snippet_formatter,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude
)
.ok()?
);
let snippet =
format!("{}$0", expr.gen_source_code(&ctx.scope, &mut snippet_formatter, cfg).ok()?);
let edit = TextEdit::replace(source_range, snippet);
item.snippet_edit(ctx.config.snippet_cap?, edit);
item.documentation(Documentation::new(String::from("Autogenerated expression by term search")));
@ -333,12 +323,7 @@ pub(crate) fn render_expr(
});
for trait_ in expr.traits_used(ctx.db) {
let trait_item = hir::ItemInNs::from(hir::ModuleDef::from(trait_));
let Some(path) = ctx.module.find_path(
ctx.db,
trait_item,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
) else {
let Some(path) = ctx.module.find_path(ctx.db, trait_item, cfg) else {
continue;
};

View file

@ -100,6 +100,7 @@
// }
// ----
use hir::ImportPathConfig;
use ide_db::imports::import_assets::LocatedImport;
use itertools::Itertools;
use syntax::{ast, AstNode, GreenNode, SyntaxNode};
@ -168,6 +169,11 @@ impl Snippet {
}
fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<Vec<LocatedImport>> {
let import_cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
};
let resolve = |import: &GreenNode| {
let path = ast::Path::cast(SyntaxNode::new_root(import.clone()))?;
let item = match ctx.scope.speculative_resolve(&path)? {
@ -178,8 +184,7 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
ctx.db,
item,
ctx.config.insert_use.prefix_kind,
ctx.config.prefer_no_std,
ctx.config.prefer_prelude,
import_cfg,
)?;
Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item)))
};

View file

@ -66,11 +66,10 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
enable_self_on_the_fly: true,
enable_private_editable: false,
enable_term_search: true,
term_search_fuel: 200,
full_function_signatures: false,
callable: Some(CallableSnippets::FillArguments),
snippet_cap: SnippetCap::new(true),
prefer_no_std: false,
prefer_prelude: true,
insert_use: InsertUseConfig {
granularity: ImportGranularity::Crate,
prefix_kind: PrefixKind::Plain,
@ -78,9 +77,10 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
group: true,
skip_glob_imports: true,
},
prefer_no_std: false,
prefer_prelude: true,
snippets: Vec::new(),
limit: None,
term_search_fuel: 200,
};
pub(crate) fn completion_list(ra_fixture: &str) -> String {