Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit de55520

Browse files
committed
refactor: simplify edit_tuple_usages in destructure_tuple_binding
1 parent 4704a96 commit de55520

File tree

1 file changed

+30
-46
lines changed

1 file changed

+30
-46
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use ide_db::{
22
assists::{AssistId, AssistKind},
33
defs::Definition,
4-
search::{FileReference, SearchScope, UsageSearchResult},
4+
search::{FileReference, SearchScope},
55
syntax_helpers::suggest_name,
66
text_edit::TextRange,
77
};
@@ -124,22 +124,25 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat
124124
return None;
125125
}
126126

127-
let name = ident_pat.name()?.to_string();
128-
129-
let usages = ctx.sema.to_def(&ident_pat).map(|def| {
127+
let usages = ctx.sema.to_def(&ident_pat).and_then(|def| {
130128
Definition::Local(def)
131129
.usages(&ctx.sema)
132130
.in_scope(&SearchScope::single_file(ctx.file_id()))
133131
.all()
132+
.iter()
133+
.next()
134+
.map(|(_, refs)| refs.to_vec())
134135
});
135136

136137
let mut name_generator = {
137138
let mut names = vec![];
138-
ctx.sema.scope(ident_pat.syntax())?.process_all_names(&mut |name, scope| {
139-
if let hir::ScopeDef::Local(_) = scope {
140-
names.push(name.as_str().into())
141-
}
142-
});
139+
if let Some(scope) = ctx.sema.scope(ident_pat.syntax()) {
140+
scope.process_all_names(&mut |name, scope| {
141+
if let hir::ScopeDef::Local(_) = scope {
142+
names.push(name.as_str().into())
143+
}
144+
})
145+
}
143146
suggest_name::NameGenerator::new_with_names(names.iter().map(|s: &SmolStr| s.as_str()))
144147
};
145148

@@ -166,7 +169,7 @@ struct TupleData {
166169
ident_pat: IdentPat,
167170
ref_type: Option<RefType>,
168171
field_names: Vec<String>,
169-
usages: Option<UsageSearchResult>,
172+
usages: Option<Vec<FileReference>>,
170173
}
171174
fn edit_tuple_assignment(
172175
ctx: &AssistContext<'_>,
@@ -222,42 +225,23 @@ fn edit_tuple_usages(
222225
ctx: &AssistContext<'_>,
223226
in_sub_pattern: bool,
224227
) -> Option<Vec<EditTupleUsage>> {
225-
let mut current_file_usages = None;
226-
227-
if let Some(usages) = data.usages.as_ref() {
228-
// We need to collect edits first before actually applying them
229-
// as mapping nodes to their mutable node versions requires an
230-
// unmodified syntax tree.
231-
//
232-
// We also defer editing usages in the current file first since
233-
// tree mutation in the same file breaks when `builder.edit_file`
234-
// is called
235-
236-
if let Some((_, refs)) = usages.iter().find(|(file_id, _)| *file_id == ctx.file_id()) {
237-
current_file_usages = Some(
238-
refs.iter()
239-
.filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern))
240-
.collect_vec(),
241-
);
242-
}
243-
244-
for (file_id, refs) in usages.iter() {
245-
if file_id == ctx.file_id() {
246-
continue;
247-
}
248-
249-
edit.edit_file(file_id.file_id());
250-
251-
let tuple_edits = refs
252-
.iter()
253-
.filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern))
254-
.collect_vec();
255-
256-
tuple_edits.into_iter().for_each(|tuple_edit| tuple_edit.apply(edit))
257-
}
258-
}
259-
260-
current_file_usages
228+
// We need to collect edits first before actually applying them
229+
// as mapping nodes to their mutable node versions requires an
230+
// unmodified syntax tree.
231+
//
232+
// We also defer editing usages in the current file first since
233+
// tree mutation in the same file breaks when `builder.edit_file`
234+
// is called
235+
236+
let edits = data
237+
.usages
238+
.as_ref()?
239+
.as_slice()
240+
.iter()
241+
.filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern))
242+
.collect_vec();
243+
244+
Some(edits)
261245
}
262246
fn edit_tuple_usage(
263247
ctx: &AssistContext<'_>,

0 commit comments

Comments
 (0)