|
1 | 1 | use ide_db::defs::{Definition, NameRefClass};
|
2 | 2 | use syntax::{
|
3 | 3 | AstNode, SyntaxNode,
|
4 |
| - ast::{self, HasName, Name}, |
5 |
| - ted, |
| 4 | + ast::{self, HasName, Name, syntax_factory::SyntaxFactory}, |
| 5 | + syntax_editor::SyntaxEditor, |
6 | 6 | };
|
7 | 7 |
|
8 | 8 | use crate::{
|
@@ -121,34 +121,36 @@ fn find_extracted_variable(ctx: &AssistContext<'_>, arm: &ast::MatchArm) -> Opti
|
121 | 121 |
|
122 | 122 | // Rename `extracted` with `binding` in `pat`.
|
123 | 123 | fn rename_variable(pat: &ast::Pat, extracted: &[Name], binding: ast::Pat) -> SyntaxNode {
|
124 |
| - let syntax = pat.syntax().clone_for_update(); |
| 124 | + let syntax = pat.syntax().clone_subtree(); |
| 125 | + let mut editor = SyntaxEditor::new(syntax.clone()); |
| 126 | + let make = SyntaxFactory::with_mappings(); |
125 | 127 | let extracted = extracted
|
126 | 128 | .iter()
|
127 |
| - .map(|e| syntax.covering_element(e.syntax().text_range())) |
| 129 | + .map(|e| e.syntax().text_range() - pat.syntax().text_range().start()) |
| 130 | + .map(|r| syntax.covering_element(r)) |
128 | 131 | .collect::<Vec<_>>();
|
129 | 132 | for extracted_syntax in extracted {
|
130 | 133 | // If `extracted` variable is a record field, we should rename it to `binding`,
|
131 | 134 | // otherwise we just need to replace `extracted` with `binding`.
|
132 |
| - |
133 | 135 | if let Some(record_pat_field) =
|
134 | 136 | extracted_syntax.ancestors().find_map(ast::RecordPatField::cast)
|
135 | 137 | {
|
136 | 138 | if let Some(name_ref) = record_pat_field.field_name() {
|
137 |
| - ted::replace( |
| 139 | + editor.replace( |
138 | 140 | record_pat_field.syntax(),
|
139 |
| - ast::make::record_pat_field( |
140 |
| - ast::make::name_ref(&name_ref.text()), |
141 |
| - binding.clone(), |
| 141 | + make.record_pat_field( |
| 142 | + make.name_ref(&name_ref.text()), |
| 143 | + binding.clone_for_update(), |
142 | 144 | )
|
143 |
| - .syntax() |
144 |
| - .clone_for_update(), |
| 145 | + .syntax(), |
145 | 146 | );
|
146 | 147 | }
|
147 | 148 | } else {
|
148 |
| - ted::replace(extracted_syntax, binding.clone().syntax().clone_for_update()); |
| 149 | + editor.replace(extracted_syntax, binding.syntax().clone_for_update()); |
149 | 150 | }
|
150 | 151 | }
|
151 |
| - syntax |
| 152 | + editor.add_mappings(make.finish_with_mappings()); |
| 153 | + editor.finish().new_root().clone() |
152 | 154 | }
|
153 | 155 |
|
154 | 156 | #[cfg(test)]
|
|
0 commit comments