aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/references
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-01-11 23:05:07 +0000
committerLukas Wirth <[email protected]>2021-01-12 00:03:04 +0000
commitfbdb32adfc49e0d69b7fd8e44135bea59382d2cb (patch)
tree5cffb6d3f0a0a168bfa48d7adcd6fa233e30b3db /crates/ide/src/references
parent52fa926f005890f07dffc789c84c2be57a6bdccc (diff)
Group references by FileId
Diffstat (limited to 'crates/ide/src/references')
-rw-r--r--crates/ide/src/references/rename.rs94
1 files changed, 47 insertions, 47 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index 3edc43e08..dd08e1c32 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -6,9 +6,10 @@ use std::{
6}; 6};
7 7
8use hir::{Module, ModuleDef, ModuleSource, Semantics}; 8use hir::{Module, ModuleDef, ModuleSource, Semantics};
9use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt};
10use ide_db::{ 9use ide_db::{
10 base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt},
11 defs::{Definition, NameClass, NameRefClass}, 11 defs::{Definition, NameClass, NameRefClass},
12 search::FileReferences,
12 RootDatabase, 13 RootDatabase,
13}; 14};
14use syntax::{ 15use syntax::{
@@ -20,8 +21,8 @@ use test_utils::mark;
20use text_edit::TextEdit; 21use text_edit::TextEdit;
21 22
22use crate::{ 23use crate::{
23 FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, ReferenceSearchResult, 24 FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange,
24 SourceChange, SourceFileEdit, TextRange, TextSize, 25 SourceFileEdit, TextRange, TextSize,
25}; 26};
26 27
27type RenameResult<T> = Result<T, RenameError>; 28type RenameResult<T> = Result<T, RenameError>;
@@ -173,39 +174,45 @@ fn find_all_refs(
173 .ok_or_else(|| format_err!("No references found at position")) 174 .ok_or_else(|| format_err!("No references found at position"))
174} 175}
175 176
176fn source_edit_from_reference( 177fn source_edit_from_references(
177 sema: &Semantics<RootDatabase>, 178 sema: &Semantics<RootDatabase>,
178 reference: Reference, 179 &FileReferences { file_id, ref references }: &FileReferences,
179 new_name: &str, 180 new_name: &str,
180) -> SourceFileEdit { 181) -> SourceFileEdit {
181 let mut replacement_text = String::new(); 182 let mut edit = TextEdit::builder();
182 let range = match reference.kind { 183 for reference in references {
183 ReferenceKind::FieldShorthandForField => { 184 let mut replacement_text = String::new();
184 mark::hit!(test_rename_struct_field_for_shorthand); 185 let range = match reference.kind {
185 replacement_text.push_str(new_name); 186 ReferenceKind::FieldShorthandForField => {
186 replacement_text.push_str(": "); 187 mark::hit!(test_rename_struct_field_for_shorthand);
187 TextRange::new(reference.file_range.range.start(), reference.file_range.range.start()) 188 replacement_text.push_str(new_name);
188 } 189 replacement_text.push_str(": ");
189 ReferenceKind::FieldShorthandForLocal => { 190 TextRange::new(reference.range.start(), reference.range.start())
190 mark::hit!(test_rename_local_for_field_shorthand); 191 }
191 replacement_text.push_str(": "); 192 ReferenceKind::FieldShorthandForLocal => {
192 replacement_text.push_str(new_name); 193 mark::hit!(test_rename_local_for_field_shorthand);
193 TextRange::new(reference.file_range.range.end(), reference.file_range.range.end()) 194 replacement_text.push_str(": ");
194 } 195 replacement_text.push_str(new_name);
195 ReferenceKind::RecordFieldExprOrPat => { 196 TextRange::new(reference.range.end(), reference.range.end())
196 mark::hit!(test_rename_field_expr_pat); 197 }
197 replacement_text.push_str(new_name); 198 ReferenceKind::RecordFieldExprOrPat => {
198 edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name) 199 mark::hit!(test_rename_field_expr_pat);
199 } 200 replacement_text.push_str(new_name);
200 _ => { 201 edit_text_range_for_record_field_expr_or_pat(
201 replacement_text.push_str(new_name); 202 sema,
202 reference.file_range.range 203 FileRange { file_id, range: reference.range },
203 } 204 new_name,
204 }; 205 )
205 SourceFileEdit { 206 }
206 file_id: reference.file_range.file_id, 207 _ => {
207 edit: TextEdit::replace(range, replacement_text), 208 replacement_text.push_str(new_name);
209 reference.range
210 }
211 };
212 edit.replace(range, replacement_text);
208 } 213 }
214
215 SourceFileEdit { file_id, edit: edit.finish() }
209} 216}
210 217
211fn edit_text_range_for_record_field_expr_or_pat( 218fn edit_text_range_for_record_field_expr_or_pat(
@@ -277,9 +284,9 @@ fn rename_mod(
277 284
278 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; 285 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
279 let ref_edits = refs 286 let ref_edits = refs
280 .references 287 .references()
281 .into_iter() 288 .iter()
282 .map(|reference| source_edit_from_reference(sema, reference, new_name)); 289 .map(|reference| source_edit_from_references(sema, reference, new_name));
283 source_file_edits.extend(ref_edits); 290 source_file_edits.extend(ref_edits);
284 291
285 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits))) 292 Ok(RangeInfo::new(range, SourceChange::from_edits(source_file_edits, file_system_edits)))
@@ -331,17 +338,10 @@ fn rename_to_self(
331 338
332 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; 339 let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
333 340
334 let (param_ref, usages): (Vec<Reference>, Vec<Reference>) = refs 341 let mut edits = refs
335 .into_iter() 342 .references()
336 .partition(|reference| param_range.intersect(reference.file_range.range).is_some()); 343 .iter()
337 344 .map(|reference| source_edit_from_references(sema, reference, "self"))
338 if param_ref.is_empty() {
339 bail!("Parameter to rename not found");
340 }
341
342 let mut edits = usages
343 .into_iter()
344 .map(|reference| source_edit_from_reference(sema, reference, "self"))
345 .collect::<Vec<_>>(); 345 .collect::<Vec<_>>();
346 346
347 edits.push(SourceFileEdit { 347 edits.push(SourceFileEdit {
@@ -467,7 +467,7 @@ fn rename_reference(
467 467
468 let edit = refs 468 let edit = refs
469 .into_iter() 469 .into_iter()
470 .map(|reference| source_edit_from_reference(sema, reference, new_name)) 470 .map(|reference| source_edit_from_references(sema, &reference, new_name))
471 .collect::<Vec<_>>(); 471 .collect::<Vec<_>>();
472 472
473 Ok(RangeInfo::new(range, SourceChange::from(edit))) 473 Ok(RangeInfo::new(range, SourceChange::from(edit)))