refactor: simplify edit_tuple_usages in destructure_tuple_binding

This commit is contained in:
roife 2024-12-13 23:40:02 +08:00
parent cb1533f7e9
commit ad63e6957f

View file

@ -1,7 +1,7 @@
use ide_db::{ use ide_db::{
assists::{AssistId, AssistKind}, assists::{AssistId, AssistKind},
defs::Definition, defs::Definition,
search::{FileReference, SearchScope, UsageSearchResult}, search::{FileReference, SearchScope},
syntax_helpers::suggest_name, syntax_helpers::suggest_name,
text_edit::TextRange, text_edit::TextRange,
}; };
@ -124,22 +124,25 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
return None; return None;
} }
let name = ident_pat.name()?.to_string(); let usages = ctx.sema.to_def(&ident_pat).and_then(|def| {
let usages = ctx.sema.to_def(&ident_pat).map(|def| {
Definition::Local(def) Definition::Local(def)
.usages(&ctx.sema) .usages(&ctx.sema)
.in_scope(&SearchScope::single_file(ctx.file_id())) .in_scope(&SearchScope::single_file(ctx.file_id()))
.all() .all()
.iter()
.next()
.map(|(_, refs)| refs.to_vec())
}); });
let mut name_generator = { let mut name_generator = {
let mut names = vec![]; let mut names = vec![];
ctx.sema.scope(ident_pat.syntax())?.process_all_names(&mut |name, scope| { if let Some(scope) = ctx.sema.scope(ident_pat.syntax()) {
scope.process_all_names(&mut |name, scope| {
if let hir::ScopeDef::Local(_) = scope { if let hir::ScopeDef::Local(_) = scope {
names.push(name.as_str().into()) names.push(name.as_str().into())
} }
}); })
}
suggest_name::NameGenerator::new_with_names(names.iter().map(|s: &SmolStr| s.as_str())) suggest_name::NameGenerator::new_with_names(names.iter().map(|s: &SmolStr| s.as_str()))
}; };
@ -166,7 +169,7 @@ struct TupleData {
ident_pat: IdentPat, ident_pat: IdentPat,
ref_type: Option<RefType>, ref_type: Option<RefType>,
field_names: Vec<String>, field_names: Vec<String>,
usages: Option<UsageSearchResult>, usages: Option<Vec<FileReference>>,
} }
fn edit_tuple_assignment( fn edit_tuple_assignment(
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,
@ -222,9 +225,6 @@ fn edit_tuple_usages(
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,
in_sub_pattern: bool, in_sub_pattern: bool,
) -> Option<Vec<EditTupleUsage>> { ) -> Option<Vec<EditTupleUsage>> {
let mut current_file_usages = None;
if let Some(usages) = data.usages.as_ref() {
// We need to collect edits first before actually applying them // We need to collect edits first before actually applying them
// as mapping nodes to their mutable node versions requires an // as mapping nodes to their mutable node versions requires an
// unmodified syntax tree. // unmodified syntax tree.
@ -233,31 +233,15 @@ fn edit_tuple_usages(
// tree mutation in the same file breaks when `builder.edit_file` // tree mutation in the same file breaks when `builder.edit_file`
// is called // is called
if let Some((_, refs)) = usages.iter().find(|(file_id, _)| *file_id == ctx.file_id()) { let edits = data
current_file_usages = Some( .usages
refs.iter() .as_ref()?
.filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern)) .as_slice()
.collect_vec(),
);
}
for (file_id, refs) in usages.iter() {
if file_id == ctx.file_id() {
continue;
}
edit.edit_file(file_id.file_id());
let tuple_edits = refs
.iter() .iter()
.filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern)) .filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern))
.collect_vec(); .collect_vec();
tuple_edits.into_iter().for_each(|tuple_edit| tuple_edit.apply(edit)) Some(edits)
}
}
current_file_usages
} }
fn edit_tuple_usage( fn edit_tuple_usage(
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,