diff options
| author | roife <roifewu@gmail.com> | 2024-12-13 23:40:02 +0800 |
|---|---|---|
| committer | roife <roifewu@gmail.com> | 2024-12-16 14:12:19 +0800 |
| commit | de555200f7914ca126b8a2e9a91526121c09b33b (patch) | |
| tree | 181698038b0c213aef90a3b9c13ffa97f1cf9ba8 /src/tools/rust-analyzer | |
| parent | 4704a962e768e0133ee43348fe5bb41da580d0af (diff) | |
| download | rust-de555200f7914ca126b8a2e9a91526121c09b33b.tar.gz rust-de555200f7914ca126b8a2e9a91526121c09b33b.zip | |
refactor: simplify `edit_tuple_usages` in destructure_tuple_binding
Diffstat (limited to 'src/tools/rust-analyzer')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs | 76 |
1 files changed, 30 insertions, 46 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs index 50d89a0a3b1..b9142d0318a 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_tuple_binding.rs @@ -1,7 +1,7 @@ use ide_db::{ assists::{AssistId, AssistKind}, defs::Definition, - search::{FileReference, SearchScope, UsageSearchResult}, + search::{FileReference, SearchScope}, syntax_helpers::suggest_name, text_edit::TextRange, }; @@ -124,22 +124,25 @@ fn collect_data(ident_pat: IdentPat, ctx: &AssistContext<'_>) -> Option<TupleDat return None; } - let name = ident_pat.name()?.to_string(); - - let usages = ctx.sema.to_def(&ident_pat).map(|def| { + let usages = ctx.sema.to_def(&ident_pat).and_then(|def| { Definition::Local(def) .usages(&ctx.sema) .in_scope(&SearchScope::single_file(ctx.file_id())) .all() + .iter() + .next() + .map(|(_, refs)| refs.to_vec()) }); let mut name_generator = { let mut names = vec![]; - ctx.sema.scope(ident_pat.syntax())?.process_all_names(&mut |name, scope| { - if let hir::ScopeDef::Local(_) = scope { - names.push(name.as_str().into()) - } - }); + if let Some(scope) = ctx.sema.scope(ident_pat.syntax()) { + scope.process_all_names(&mut |name, scope| { + if let hir::ScopeDef::Local(_) = scope { + names.push(name.as_str().into()) + } + }) + } suggest_name::NameGenerator::new_with_names(names.iter().map(|s: &SmolStr| s.as_str())) }; @@ -166,7 +169,7 @@ struct TupleData { ident_pat: IdentPat, ref_type: Option<RefType>, field_names: Vec<String>, - usages: Option<UsageSearchResult>, + usages: Option<Vec<FileReference>>, } fn edit_tuple_assignment( ctx: &AssistContext<'_>, @@ -222,42 +225,23 @@ fn edit_tuple_usages( ctx: &AssistContext<'_>, in_sub_pattern: bool, ) -> 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 - // as mapping nodes to their mutable node versions requires an - // unmodified syntax tree. - // - // We also defer editing usages in the current file first since - // tree mutation in the same file breaks when `builder.edit_file` - // is called - - if let Some((_, refs)) = usages.iter().find(|(file_id, _)| *file_id == ctx.file_id()) { - current_file_usages = Some( - refs.iter() - .filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern)) - .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() - .filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern)) - .collect_vec(); - - tuple_edits.into_iter().for_each(|tuple_edit| tuple_edit.apply(edit)) - } - } - - current_file_usages + // We need to collect edits first before actually applying them + // as mapping nodes to their mutable node versions requires an + // unmodified syntax tree. + // + // We also defer editing usages in the current file first since + // tree mutation in the same file breaks when `builder.edit_file` + // is called + + let edits = data + .usages + .as_ref()? + .as_slice() + .iter() + .filter_map(|r| edit_tuple_usage(ctx, edit, r, data, in_sub_pattern)) + .collect_vec(); + + Some(edits) } fn edit_tuple_usage( ctx: &AssistContext<'_>, |
