aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/references/rename.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/references/rename.rs')
-rw-r--r--crates/ide/src/references/rename.rs314
1 files changed, 148 insertions, 166 deletions
diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs
index c5010df0a..c25bcce50 100644
--- a/crates/ide/src/references/rename.rs
+++ b/crates/ide/src/references/rename.rs
@@ -63,7 +63,7 @@ pub(crate) fn rename(
63 db: &RootDatabase, 63 db: &RootDatabase,
64 position: FilePosition, 64 position: FilePosition,
65 new_name: &str, 65 new_name: &str,
66) -> RenameResult<RangeInfo<SourceChange>> { 66) -> RenameResult<SourceChange> {
67 let sema = Semantics::new(db); 67 let sema = Semantics::new(db);
68 rename_with_semantics(&sema, position, new_name) 68 rename_with_semantics(&sema, position, new_name)
69} 69}
@@ -72,7 +72,7 @@ pub(crate) fn rename_with_semantics(
72 sema: &Semantics<RootDatabase>, 72 sema: &Semantics<RootDatabase>,
73 position: FilePosition, 73 position: FilePosition,
74 new_name: &str, 74 new_name: &str,
75) -> RenameResult<RangeInfo<SourceChange>> { 75) -> RenameResult<SourceChange> {
76 let source_file = sema.parse(position.file_id); 76 let source_file = sema.parse(position.file_id);
77 let syntax = source_file.syntax(); 77 let syntax = source_file.syntax();
78 78
@@ -91,7 +91,7 @@ pub(crate) fn will_rename_file(
91) -> Option<SourceChange> { 91) -> Option<SourceChange> {
92 let sema = Semantics::new(db); 92 let sema = Semantics::new(db);
93 let module = sema.to_module_def(file_id)?; 93 let module = sema.to_module_def(file_id)?;
94 let mut change = rename_mod(&sema, module, new_name_stem).ok()?.info; 94 let mut change = rename_mod(&sema, module, new_name_stem).ok()?;
95 change.file_system_edits.clear(); 95 change.file_system_edits.clear();
96 Some(change) 96 Some(change)
97} 97}
@@ -243,7 +243,7 @@ fn rename_mod(
243 sema: &Semantics<RootDatabase>, 243 sema: &Semantics<RootDatabase>,
244 module: Module, 244 module: Module,
245 new_name: &str, 245 new_name: &str,
246) -> RenameResult<RangeInfo<SourceChange>> { 246) -> RenameResult<SourceChange> {
247 if IdentifierKind::Ident != check_identifier(new_name)? { 247 if IdentifierKind::Ident != check_identifier(new_name)? {
248 bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); 248 bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
249 } 249 }
@@ -281,13 +281,10 @@ fn rename_mod(
281 }); 281 });
282 source_change.extend(ref_edits); 282 source_change.extend(ref_edits);
283 283
284 Ok(RangeInfo::new(TextRange::default(), source_change)) 284 Ok(source_change)
285} 285}
286 286
287fn rename_to_self( 287fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange> {
288 sema: &Semantics<RootDatabase>,
289 local: hir::Local,
290) -> RenameResult<RangeInfo<SourceChange>> {
291 if assert_never!(local.is_self(sema.db)) { 288 if assert_never!(local.is_self(sema.db)) {
292 bail!("rename_to_self invoked on self"); 289 bail!("rename_to_self invoked on self");
293 } 290 }
@@ -356,7 +353,7 @@ fn rename_to_self(
356 TextEdit::replace(first_param_range, String::from(self_param)), 353 TextEdit::replace(first_param_range, String::from(self_param)),
357 ); 354 );
358 355
359 Ok(RangeInfo::new(TextRange::default(), source_change)) 356 Ok(source_change)
360} 357}
361 358
362fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit> { 359fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit> {
@@ -387,7 +384,7 @@ fn rename_self_to_param(
387 local: hir::Local, 384 local: hir::Local,
388 new_name: &str, 385 new_name: &str,
389 identifier_kind: IdentifierKind, 386 identifier_kind: IdentifierKind,
390) -> RenameResult<RangeInfo<SourceChange>> { 387) -> RenameResult<SourceChange> {
391 let (file_id, self_param) = match local.source(sema.db) { 388 let (file_id, self_param) = match local.source(sema.db) {
392 InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param), 389 InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param),
393 _ => { 390 _ => {
@@ -408,14 +405,14 @@ fn rename_self_to_param(
408 source_change.extend(usages.iter().map(|(&file_id, references)| { 405 source_change.extend(usages.iter().map(|(&file_id, references)| {
409 source_edit_from_references(sema, file_id, &references, new_name) 406 source_edit_from_references(sema, file_id, &references, new_name)
410 })); 407 }));
411 Ok(RangeInfo::new(TextRange::default(), source_change)) 408 Ok(source_change)
412} 409}
413 410
414fn rename_reference( 411fn rename_reference(
415 sema: &Semantics<RootDatabase>, 412 sema: &Semantics<RootDatabase>,
416 def: Definition, 413 def: Definition,
417 new_name: &str, 414 new_name: &str,
418) -> RenameResult<RangeInfo<SourceChange>> { 415) -> RenameResult<SourceChange> {
419 let ident_kind = check_identifier(new_name)?; 416 let ident_kind = check_identifier(new_name)?;
420 417
421 let def_is_lbl_or_lt = matches!(def, 418 let def_is_lbl_or_lt = matches!(def,
@@ -439,7 +436,7 @@ fn rename_reference(
439 (IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => { 436 (IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => {
440 // no-op 437 // no-op
441 mark::hit!(rename_self_to_self); 438 mark::hit!(rename_self_to_self);
442 return Ok(RangeInfo::new(TextRange::default(), SourceChange::default())); 439 return Ok(SourceChange::default());
443 } 440 }
444 (ident_kind, Definition::Local(local)) if local.is_self(sema.db) => { 441 (ident_kind, Definition::Local(local)) if local.is_self(sema.db) => {
445 mark::hit!(rename_self_to_param); 442 mark::hit!(rename_self_to_param);
@@ -465,7 +462,7 @@ fn rename_reference(
465 462
466 let (file_id, edit) = source_edit_from_def(sema, def, new_name)?; 463 let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
467 source_change.insert_source_edit(file_id, edit); 464 source_change.insert_source_edit(file_id, edit);
468 Ok(RangeInfo::new(TextRange::default(), source_change)) 465 Ok(source_change)
469} 466}
470 467
471fn source_edit_from_def( 468fn source_edit_from_def(
@@ -518,7 +515,7 @@ mod tests {
518 Ok(source_change) => { 515 Ok(source_change) => {
519 let mut text_edit_builder = TextEdit::builder(); 516 let mut text_edit_builder = TextEdit::builder();
520 let mut file_id: Option<FileId> = None; 517 let mut file_id: Option<FileId> = None;
521 for edit in source_change.info.source_file_edits { 518 for edit in source_change.source_file_edits {
522 file_id = Some(edit.0); 519 file_id = Some(edit.0);
523 for indel in edit.1.into_iter() { 520 for indel in edit.1.into_iter() {
524 text_edit_builder.replace(indel.delete, indel.insert); 521 text_edit_builder.replace(indel.delete, indel.insert);
@@ -917,36 +914,33 @@ mod foo$0;
917// empty 914// empty
918"#, 915"#,
919 expect![[r#" 916 expect![[r#"
920 RangeInfo { 917 SourceChange {
921 range: 0..0, 918 source_file_edits: {
922 info: SourceChange { 919 FileId(
923 source_file_edits: { 920 1,
924 FileId( 921 ): TextEdit {
925 1, 922 indels: [
926 ): TextEdit { 923 Indel {
927 indels: [ 924 insert: "foo2",
928 Indel { 925 delete: 4..7,
929 insert: "foo2", 926 },
930 delete: 4..7, 927 ],
931 },
932 ],
933 },
934 }, 928 },
935 file_system_edits: [ 929 },
936 MoveFile { 930 file_system_edits: [
937 src: FileId( 931 MoveFile {
932 src: FileId(
933 2,
934 ),
935 dst: AnchoredPathBuf {
936 anchor: FileId(
938 2, 937 2,
939 ), 938 ),
940 dst: AnchoredPathBuf { 939 path: "foo2.rs",
941 anchor: FileId(
942 2,
943 ),
944 path: "foo2.rs",
945 },
946 }, 940 },
947 ], 941 },
948 is_snippet: false, 942 ],
949 }, 943 is_snippet: false,
950 } 944 }
951 "#]], 945 "#]],
952 ); 946 );
@@ -969,46 +963,43 @@ pub struct FooContent;
969use crate::foo$0::FooContent; 963use crate::foo$0::FooContent;
970"#, 964"#,
971 expect![[r#" 965 expect![[r#"
972 RangeInfo { 966 SourceChange {
973 range: 0..0, 967 source_file_edits: {
974 info: SourceChange { 968 FileId(
975 source_file_edits: { 969 0,
976 FileId( 970 ): TextEdit {
977 0, 971 indels: [
978 ): TextEdit { 972 Indel {
979 indels: [ 973 insert: "quux",
980 Indel { 974 delete: 8..11,
981 insert: "quux", 975 },
982 delete: 8..11, 976 ],
983 }, 977 },
984 ], 978 FileId(
985 }, 979 2,
986 FileId( 980 ): TextEdit {
987 2, 981 indels: [
988 ): TextEdit { 982 Indel {
989 indels: [ 983 insert: "quux",
990 Indel { 984 delete: 11..14,
991 insert: "quux", 985 },
992 delete: 11..14, 986 ],
993 },
994 ],
995 },
996 }, 987 },
997 file_system_edits: [ 988 },
998 MoveFile { 989 file_system_edits: [
999 src: FileId( 990 MoveFile {
991 src: FileId(
992 1,
993 ),
994 dst: AnchoredPathBuf {
995 anchor: FileId(
1000 1, 996 1,
1001 ), 997 ),
1002 dst: AnchoredPathBuf { 998 path: "quux.rs",
1003 anchor: FileId(
1004 1,
1005 ),
1006 path: "quux.rs",
1007 },
1008 }, 999 },
1009 ], 1000 },
1010 is_snippet: false, 1001 ],
1011 }, 1002 is_snippet: false,
1012 } 1003 }
1013 "#]], 1004 "#]],
1014 ); 1005 );
@@ -1025,36 +1016,33 @@ mod fo$0o;
1025// empty 1016// empty
1026"#, 1017"#,
1027 expect![[r#" 1018 expect![[r#"
1028 RangeInfo { 1019 SourceChange {
1029 range: 0..0, 1020 source_file_edits: {
1030 info: SourceChange { 1021 FileId(
1031 source_file_edits: { 1022 0,
1032 FileId( 1023 ): TextEdit {
1033 0, 1024 indels: [
1034 ): TextEdit { 1025 Indel {
1035 indels: [ 1026 insert: "foo2",
1036 Indel { 1027 delete: 4..7,
1037 insert: "foo2", 1028 },
1038 delete: 4..7, 1029 ],
1039 },
1040 ],
1041 },
1042 }, 1030 },
1043 file_system_edits: [ 1031 },
1044 MoveFile { 1032 file_system_edits: [
1045 src: FileId( 1033 MoveFile {
1034 src: FileId(
1035 1,
1036 ),
1037 dst: AnchoredPathBuf {
1038 anchor: FileId(
1046 1, 1039 1,
1047 ), 1040 ),
1048 dst: AnchoredPathBuf { 1041 path: "../foo2/mod.rs",
1049 anchor: FileId(
1050 1,
1051 ),
1052 path: "../foo2/mod.rs",
1053 },
1054 }, 1042 },
1055 ], 1043 },
1056 is_snippet: false, 1044 ],
1057 }, 1045 is_snippet: false,
1058 } 1046 }
1059 "#]], 1047 "#]],
1060 ); 1048 );
@@ -1072,36 +1060,33 @@ mod outer { mod fo$0o; }
1072// empty 1060// empty
1073"#, 1061"#,
1074 expect![[r#" 1062 expect![[r#"
1075 RangeInfo { 1063 SourceChange {
1076 range: 0..0, 1064 source_file_edits: {
1077 info: SourceChange { 1065 FileId(
1078 source_file_edits: { 1066 0,
1079 FileId( 1067 ): TextEdit {
1080 0, 1068 indels: [
1081 ): TextEdit { 1069 Indel {
1082 indels: [ 1070 insert: "bar",
1083 Indel { 1071 delete: 16..19,
1084 insert: "bar", 1072 },
1085 delete: 16..19, 1073 ],
1086 },
1087 ],
1088 },
1089 }, 1074 },
1090 file_system_edits: [ 1075 },
1091 MoveFile { 1076 file_system_edits: [
1092 src: FileId( 1077 MoveFile {
1078 src: FileId(
1079 1,
1080 ),
1081 dst: AnchoredPathBuf {
1082 anchor: FileId(
1093 1, 1083 1,
1094 ), 1084 ),
1095 dst: AnchoredPathBuf { 1085 path: "bar.rs",
1096 anchor: FileId(
1097 1,
1098 ),
1099 path: "bar.rs",
1100 },
1101 }, 1086 },
1102 ], 1087 },
1103 is_snippet: false, 1088 ],
1104 }, 1089 is_snippet: false,
1105 } 1090 }
1106 "#]], 1091 "#]],
1107 ); 1092 );
@@ -1142,46 +1127,43 @@ pub mod foo$0;
1142// pub fn fun() {} 1127// pub fn fun() {}
1143"#, 1128"#,
1144 expect![[r#" 1129 expect![[r#"
1145 RangeInfo { 1130 SourceChange {
1146 range: 0..0, 1131 source_file_edits: {
1147 info: SourceChange { 1132 FileId(
1148 source_file_edits: { 1133 0,
1149 FileId( 1134 ): TextEdit {
1150 0, 1135 indels: [
1151 ): TextEdit { 1136 Indel {
1152 indels: [ 1137 insert: "foo2",
1153 Indel { 1138 delete: 27..30,
1154 insert: "foo2", 1139 },
1155 delete: 27..30, 1140 ],
1156 }, 1141 },
1157 ], 1142 FileId(
1158 }, 1143 1,
1159 FileId( 1144 ): TextEdit {
1160 1, 1145 indels: [
1161 ): TextEdit { 1146 Indel {
1162 indels: [ 1147 insert: "foo2",
1163 Indel { 1148 delete: 8..11,
1164 insert: "foo2", 1149 },
1165 delete: 8..11, 1150 ],
1166 },
1167 ],
1168 },
1169 }, 1151 },
1170 file_system_edits: [ 1152 },
1171 MoveFile { 1153 file_system_edits: [
1172 src: FileId( 1154 MoveFile {
1155 src: FileId(
1156 2,
1157 ),
1158 dst: AnchoredPathBuf {
1159 anchor: FileId(
1173 2, 1160 2,
1174 ), 1161 ),
1175 dst: AnchoredPathBuf { 1162 path: "foo2.rs",
1176 anchor: FileId(
1177 2,
1178 ),
1179 path: "foo2.rs",
1180 },
1181 }, 1163 },
1182 ], 1164 },
1183 is_snippet: false, 1165 ],
1184 }, 1166 is_snippet: false,
1185 } 1167 }
1186 "#]], 1168 "#]],
1187 ); 1169 );