diff options
author | Lukas Wirth <[email protected]> | 2021-01-14 17:35:22 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-14 17:35:22 +0000 |
commit | f51457a643b768794092f73add6dda4aecd400a1 (patch) | |
tree | 8deeda89c7709aa69918d24ee4ecd18ea0e179db /crates/ide/src/references | |
parent | f88f3d688507508ae9528101e13e1c62902467a3 (diff) |
Group file source edits by FileId
Diffstat (limited to 'crates/ide/src/references')
-rw-r--r-- | crates/ide/src/references/rename.rs | 138 |
1 files changed, 57 insertions, 81 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index c3ae568c2..cd9c7c7e5 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs | |||
@@ -21,7 +21,7 @@ use text_edit::TextEdit; | |||
21 | 21 | ||
22 | use crate::{ | 22 | use crate::{ |
23 | FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange, | 23 | FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange, |
24 | SourceFileEdit, TextRange, TextSize, | 24 | SourceFileEdits, TextRange, TextSize, |
25 | }; | 25 | }; |
26 | 26 | ||
27 | type RenameResult<T> = Result<T, RenameError>; | 27 | type RenameResult<T> = Result<T, RenameError>; |
@@ -58,7 +58,7 @@ pub(crate) fn prepare_rename( | |||
58 | rename_self_to_param(&sema, position, self_token, "dummy") | 58 | rename_self_to_param(&sema, position, self_token, "dummy") |
59 | } else { | 59 | } else { |
60 | let RangeInfo { range, .. } = find_all_refs(&sema, position)?; | 60 | let RangeInfo { range, .. } = find_all_refs(&sema, position)?; |
61 | Ok(RangeInfo::new(range, SourceChange::from(vec![]))) | 61 | Ok(RangeInfo::new(range, SourceChange::default())) |
62 | } | 62 | } |
63 | .map(|info| RangeInfo::new(info.range, ())) | 63 | .map(|info| RangeInfo::new(info.range, ())) |
64 | } | 64 | } |
@@ -176,7 +176,7 @@ fn source_edit_from_references( | |||
176 | file_id: FileId, | 176 | file_id: FileId, |
177 | references: &[FileReference], | 177 | references: &[FileReference], |
178 | new_name: &str, | 178 | new_name: &str, |
179 | ) -> SourceFileEdit { | 179 | ) -> (FileId, TextEdit) { |
180 | let mut edit = TextEdit::builder(); | 180 | let mut edit = TextEdit::builder(); |
181 | for reference in references { | 181 | for reference in references { |
182 | let mut replacement_text = String::new(); | 182 | let mut replacement_text = String::new(); |
@@ -209,8 +209,7 @@ fn source_edit_from_references( | |||
209 | }; | 209 | }; |
210 | edit.replace(range, replacement_text); | 210 | edit.replace(range, replacement_text); |
211 | } | 211 | } |
212 | 212 | (file_id, edit.finish()) | |
213 | SourceFileEdit { file_id, edit: edit.finish() } | ||
214 | } | 213 | } |
215 | 214 | ||
216 | fn edit_text_range_for_record_field_expr_or_pat( | 215 | fn edit_text_range_for_record_field_expr_or_pat( |
@@ -250,7 +249,7 @@ fn rename_mod( | |||
250 | if IdentifierKind::Ident != check_identifier(new_name)? { | 249 | if IdentifierKind::Ident != check_identifier(new_name)? { |
251 | bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); | 250 | bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); |
252 | } | 251 | } |
253 | let mut source_file_edits = Vec::new(); | 252 | let mut source_file_edits = SourceFileEdits::default(); |
254 | let mut file_system_edits = Vec::new(); | 253 | let mut file_system_edits = Vec::new(); |
255 | 254 | ||
256 | let src = module.definition_source(sema.db); | 255 | let src = module.definition_source(sema.db); |
@@ -273,11 +272,8 @@ fn rename_mod( | |||
273 | if let Some(src) = module.declaration_source(sema.db) { | 272 | if let Some(src) = module.declaration_source(sema.db) { |
274 | let file_id = src.file_id.original_file(sema.db); | 273 | let file_id = src.file_id.original_file(sema.db); |
275 | let name = src.value.name().unwrap(); | 274 | let name = src.value.name().unwrap(); |
276 | let edit = SourceFileEdit { | 275 | source_file_edits |
277 | file_id, | 276 | .insert(file_id, TextEdit::replace(name.syntax().text_range(), new_name.into())); |
278 | edit: TextEdit::replace(name.syntax().text_range(), new_name.into()), | ||
279 | }; | ||
280 | source_file_edits.push(edit); | ||
281 | } | 277 | } |
282 | 278 | ||
283 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; | 279 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; |
@@ -335,20 +331,13 @@ fn rename_to_self( | |||
335 | 331 | ||
336 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; | 332 | let RangeInfo { range, info: refs } = find_all_refs(sema, position)?; |
337 | 333 | ||
338 | let mut edits = refs | 334 | let mut edits = SourceFileEdits::default(); |
339 | .references() | 335 | edits.extend(refs.references().iter().map(|(&file_id, references)| { |
340 | .iter() | 336 | source_edit_from_references(sema, file_id, references, "self") |
341 | .map(|(&file_id, references)| { | 337 | })); |
342 | source_edit_from_references(sema, file_id, references, "self") | 338 | edits.insert(position.file_id, TextEdit::replace(param_range, String::from(self_param))); |
343 | }) | ||
344 | .collect::<Vec<_>>(); | ||
345 | 339 | ||
346 | edits.push(SourceFileEdit { | 340 | Ok(RangeInfo::new(range, edits.into())) |
347 | file_id: position.file_id, | ||
348 | edit: TextEdit::replace(param_range, String::from(self_param)), | ||
349 | }); | ||
350 | |||
351 | Ok(RangeInfo::new(range, SourceChange::from(edits))) | ||
352 | } | 341 | } |
353 | 342 | ||
354 | fn text_edit_from_self_param( | 343 | fn text_edit_from_self_param( |
@@ -402,7 +391,7 @@ fn rename_self_to_param( | |||
402 | .ok_or_else(|| format_err!("No surrounding method declaration found"))?; | 391 | .ok_or_else(|| format_err!("No surrounding method declaration found"))?; |
403 | let search_range = fn_def.syntax().text_range(); | 392 | let search_range = fn_def.syntax().text_range(); |
404 | 393 | ||
405 | let mut edits: Vec<SourceFileEdit> = vec![]; | 394 | let mut edits = SourceFileEdits::default(); |
406 | 395 | ||
407 | for (idx, _) in text.match_indices("self") { | 396 | for (idx, _) in text.match_indices("self") { |
408 | let offset: TextSize = idx.try_into().unwrap(); | 397 | let offset: TextSize = idx.try_into().unwrap(); |
@@ -416,7 +405,7 @@ fn rename_self_to_param( | |||
416 | } else { | 405 | } else { |
417 | TextEdit::replace(usage.text_range(), String::from(new_name)) | 406 | TextEdit::replace(usage.text_range(), String::from(new_name)) |
418 | }; | 407 | }; |
419 | edits.push(SourceFileEdit { file_id: position.file_id, edit }); | 408 | edits.insert(position.file_id, edit); |
420 | } | 409 | } |
421 | } | 410 | } |
422 | 411 | ||
@@ -427,7 +416,7 @@ fn rename_self_to_param( | |||
427 | let range = ast::SelfParam::cast(self_token.parent()) | 416 | let range = ast::SelfParam::cast(self_token.parent()) |
428 | .map_or(self_token.text_range(), |p| p.syntax().text_range()); | 417 | .map_or(self_token.text_range(), |p| p.syntax().text_range()); |
429 | 418 | ||
430 | Ok(RangeInfo::new(range, SourceChange::from(edits))) | 419 | Ok(RangeInfo::new(range, edits.into())) |
431 | } | 420 | } |
432 | 421 | ||
433 | fn rename_reference( | 422 | fn rename_reference( |
@@ -464,14 +453,12 @@ fn rename_reference( | |||
464 | (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident), | 453 | (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident), |
465 | } | 454 | } |
466 | 455 | ||
467 | let edit = refs | 456 | let mut edits = SourceFileEdits::default(); |
468 | .into_iter() | 457 | edits.extend(refs.into_iter().map(|(file_id, references)| { |
469 | .map(|(file_id, references)| { | 458 | source_edit_from_references(sema, file_id, &references, new_name) |
470 | source_edit_from_references(sema, file_id, &references, new_name) | 459 | })); |
471 | }) | ||
472 | .collect::<Vec<_>>(); | ||
473 | 460 | ||
474 | Ok(RangeInfo::new(range, SourceChange::from(edit))) | 461 | Ok(RangeInfo::new(range, edits.into())) |
475 | } | 462 | } |
476 | 463 | ||
477 | #[cfg(test)] | 464 | #[cfg(test)] |
@@ -493,9 +480,9 @@ mod tests { | |||
493 | Ok(source_change) => { | 480 | Ok(source_change) => { |
494 | let mut text_edit_builder = TextEdit::builder(); | 481 | let mut text_edit_builder = TextEdit::builder(); |
495 | let mut file_id: Option<FileId> = None; | 482 | let mut file_id: Option<FileId> = None; |
496 | for edit in source_change.info.source_file_edits { | 483 | for edit in source_change.info.source_file_edits.edits { |
497 | file_id = Some(edit.file_id); | 484 | file_id = Some(edit.0); |
498 | for indel in edit.edit.into_iter() { | 485 | for indel in edit.1.into_iter() { |
499 | text_edit_builder.replace(indel.delete, indel.insert); | 486 | text_edit_builder.replace(indel.delete, indel.insert); |
500 | } | 487 | } |
501 | } | 488 | } |
@@ -895,12 +882,11 @@ mod foo$0; | |||
895 | RangeInfo { | 882 | RangeInfo { |
896 | range: 4..7, | 883 | range: 4..7, |
897 | info: SourceChange { | 884 | info: SourceChange { |
898 | source_file_edits: [ | 885 | source_file_edits: SourceFileEdits { |
899 | SourceFileEdit { | 886 | edits: { |
900 | file_id: FileId( | 887 | FileId( |
901 | 1, | 888 | 1, |
902 | ), | 889 | ): TextEdit { |
903 | edit: TextEdit { | ||
904 | indels: [ | 890 | indels: [ |
905 | Indel { | 891 | Indel { |
906 | insert: "foo2", | 892 | insert: "foo2", |
@@ -909,7 +895,7 @@ mod foo$0; | |||
909 | ], | 895 | ], |
910 | }, | 896 | }, |
911 | }, | 897 | }, |
912 | ], | 898 | }, |
913 | file_system_edits: [ | 899 | file_system_edits: [ |
914 | MoveFile { | 900 | MoveFile { |
915 | src: FileId( | 901 | src: FileId( |
@@ -950,12 +936,11 @@ use crate::foo$0::FooContent; | |||
950 | RangeInfo { | 936 | RangeInfo { |
951 | range: 11..14, | 937 | range: 11..14, |
952 | info: SourceChange { | 938 | info: SourceChange { |
953 | source_file_edits: [ | 939 | source_file_edits: SourceFileEdits { |
954 | SourceFileEdit { | 940 | edits: { |
955 | file_id: FileId( | 941 | FileId( |
956 | 0, | 942 | 0, |
957 | ), | 943 | ): TextEdit { |
958 | edit: TextEdit { | ||
959 | indels: [ | 944 | indels: [ |
960 | Indel { | 945 | Indel { |
961 | insert: "quux", | 946 | insert: "quux", |
@@ -963,12 +948,9 @@ use crate::foo$0::FooContent; | |||
963 | }, | 948 | }, |
964 | ], | 949 | ], |
965 | }, | 950 | }, |
966 | }, | 951 | FileId( |
967 | SourceFileEdit { | ||
968 | file_id: FileId( | ||
969 | 2, | 952 | 2, |
970 | ), | 953 | ): TextEdit { |
971 | edit: TextEdit { | ||
972 | indels: [ | 954 | indels: [ |
973 | Indel { | 955 | Indel { |
974 | insert: "quux", | 956 | insert: "quux", |
@@ -977,7 +959,7 @@ use crate::foo$0::FooContent; | |||
977 | ], | 959 | ], |
978 | }, | 960 | }, |
979 | }, | 961 | }, |
980 | ], | 962 | }, |
981 | file_system_edits: [ | 963 | file_system_edits: [ |
982 | MoveFile { | 964 | MoveFile { |
983 | src: FileId( | 965 | src: FileId( |
@@ -1012,12 +994,11 @@ mod fo$0o; | |||
1012 | RangeInfo { | 994 | RangeInfo { |
1013 | range: 4..7, | 995 | range: 4..7, |
1014 | info: SourceChange { | 996 | info: SourceChange { |
1015 | source_file_edits: [ | 997 | source_file_edits: SourceFileEdits { |
1016 | SourceFileEdit { | 998 | edits: { |
1017 | file_id: FileId( | 999 | FileId( |
1018 | 0, | 1000 | 0, |
1019 | ), | 1001 | ): TextEdit { |
1020 | edit: TextEdit { | ||
1021 | indels: [ | 1002 | indels: [ |
1022 | Indel { | 1003 | Indel { |
1023 | insert: "foo2", | 1004 | insert: "foo2", |
@@ -1026,7 +1007,7 @@ mod fo$0o; | |||
1026 | ], | 1007 | ], |
1027 | }, | 1008 | }, |
1028 | }, | 1009 | }, |
1029 | ], | 1010 | }, |
1030 | file_system_edits: [ | 1011 | file_system_edits: [ |
1031 | MoveFile { | 1012 | MoveFile { |
1032 | src: FileId( | 1013 | src: FileId( |
@@ -1062,12 +1043,11 @@ mod outer { mod fo$0o; } | |||
1062 | RangeInfo { | 1043 | RangeInfo { |
1063 | range: 16..19, | 1044 | range: 16..19, |
1064 | info: SourceChange { | 1045 | info: SourceChange { |
1065 | source_file_edits: [ | 1046 | source_file_edits: SourceFileEdits { |
1066 | SourceFileEdit { | 1047 | edits: { |
1067 | file_id: FileId( | 1048 | FileId( |
1068 | 0, | 1049 | 0, |
1069 | ), | 1050 | ): TextEdit { |
1070 | edit: TextEdit { | ||
1071 | indels: [ | 1051 | indels: [ |
1072 | Indel { | 1052 | Indel { |
1073 | insert: "bar", | 1053 | insert: "bar", |
@@ -1076,7 +1056,7 @@ mod outer { mod fo$0o; } | |||
1076 | ], | 1056 | ], |
1077 | }, | 1057 | }, |
1078 | }, | 1058 | }, |
1079 | ], | 1059 | }, |
1080 | file_system_edits: [ | 1060 | file_system_edits: [ |
1081 | MoveFile { | 1061 | MoveFile { |
1082 | src: FileId( | 1062 | src: FileId( |
@@ -1135,34 +1115,30 @@ pub mod foo$0; | |||
1135 | RangeInfo { | 1115 | RangeInfo { |
1136 | range: 8..11, | 1116 | range: 8..11, |
1137 | info: SourceChange { | 1117 | info: SourceChange { |
1138 | source_file_edits: [ | 1118 | source_file_edits: SourceFileEdits { |
1139 | SourceFileEdit { | 1119 | edits: { |
1140 | file_id: FileId( | 1120 | FileId( |
1141 | 1, | 1121 | 0, |
1142 | ), | 1122 | ): TextEdit { |
1143 | edit: TextEdit { | ||
1144 | indels: [ | 1123 | indels: [ |
1145 | Indel { | 1124 | Indel { |
1146 | insert: "foo2", | 1125 | insert: "foo2", |
1147 | delete: 8..11, | 1126 | delete: 27..30, |
1148 | }, | 1127 | }, |
1149 | ], | 1128 | ], |
1150 | }, | 1129 | }, |
1151 | }, | 1130 | FileId( |
1152 | SourceFileEdit { | 1131 | 1, |
1153 | file_id: FileId( | 1132 | ): TextEdit { |
1154 | 0, | ||
1155 | ), | ||
1156 | edit: TextEdit { | ||
1157 | indels: [ | 1133 | indels: [ |
1158 | Indel { | 1134 | Indel { |
1159 | insert: "foo2", | 1135 | insert: "foo2", |
1160 | delete: 27..30, | 1136 | delete: 8..11, |
1161 | }, | 1137 | }, |
1162 | ], | 1138 | ], |
1163 | }, | 1139 | }, |
1164 | }, | 1140 | }, |
1165 | ], | 1141 | }, |
1166 | file_system_edits: [ | 1142 | file_system_edits: [ |
1167 | MoveFile { | 1143 | MoveFile { |
1168 | src: FileId( | 1144 | src: FileId( |