Skip to content

Commit

Permalink
Auto merge of rust-lang#17666 - Veykril:simplify, r=Veykril
Browse files Browse the repository at this point in the history
Simplify
  • Loading branch information
bors committed Jul 22, 2024
2 parents 5236347 + 69d8e32 commit 377192f
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 139 deletions.
11 changes: 11 additions & 0 deletions src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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): (
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use either::Either;
use hir::{ImportPathConfig, ModuleDef};
use hir::ModuleDef;
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use either::Either;
use hir::{ImportPathConfig, ModuleDef};
use hir::ModuleDef;
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hir::{sym, HasVisibility, ImportPathConfig};
use hir::{sym, HasVisibility};
use ide_db::{
assists::{AssistId, AssistKind},
defs::Definition,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use hir::ImportPathConfig;
use ide_db::{
imports::import_assets::item_for_path_search, use_trivial_constructor::use_trivial_constructor,
};
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -38,24 +39,16 @@ 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();
if proposed_imports.is_empty() {
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);
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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_))
Expand Down
Loading

0 comments on commit 377192f

Please sign in to comment.