diff options
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/rename.rs | 136 | ||||
-rw-r--r-- | crates/ra_ide_api/src/snapshots/tests__rename_mod.snap | 36 | ||||
-rw-r--r-- | crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap | 36 |
3 files changed, 208 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/rename.rs index 9ab6f2a77..53dc273c6 100644 --- a/crates/ra_ide_api/src/rename.rs +++ b/crates/ra_ide_api/src/rename.rs | |||
@@ -132,3 +132,139 @@ fn rename_reference( | |||
132 | cursor_position: None, | 132 | cursor_position: None, |
133 | }); | 133 | }); |
134 | } | 134 | } |
135 | |||
136 | #[cfg(test)] | ||
137 | mod tests { | ||
138 | use insta::assert_debug_snapshot_matches; | ||
139 | use test_utils::assert_eq_text; | ||
140 | use crate::{ | ||
141 | mock_analysis::single_file_with_position, | ||
142 | mock_analysis::analysis_and_position, | ||
143 | FileId | ||
144 | }; | ||
145 | |||
146 | #[test] | ||
147 | fn test_rename_for_local() { | ||
148 | test_rename( | ||
149 | r#" | ||
150 | fn main() { | ||
151 | let mut i = 1; | ||
152 | let j = 1; | ||
153 | i = i<|> + j; | ||
154 | |||
155 | { | ||
156 | i = 0; | ||
157 | } | ||
158 | |||
159 | i = 5; | ||
160 | }"#, | ||
161 | "k", | ||
162 | r#" | ||
163 | fn main() { | ||
164 | let mut k = 1; | ||
165 | let j = 1; | ||
166 | k = k + j; | ||
167 | |||
168 | { | ||
169 | k = 0; | ||
170 | } | ||
171 | |||
172 | k = 5; | ||
173 | }"#, | ||
174 | ); | ||
175 | } | ||
176 | |||
177 | #[test] | ||
178 | fn test_rename_for_param_inside() { | ||
179 | test_rename( | ||
180 | r#" | ||
181 | fn foo(i : u32) -> u32 { | ||
182 | i<|> | ||
183 | }"#, | ||
184 | "j", | ||
185 | r#" | ||
186 | fn foo(j : u32) -> u32 { | ||
187 | j | ||
188 | }"#, | ||
189 | ); | ||
190 | } | ||
191 | |||
192 | #[test] | ||
193 | fn test_rename_refs_for_fn_param() { | ||
194 | test_rename( | ||
195 | r#" | ||
196 | fn foo(i<|> : u32) -> u32 { | ||
197 | i | ||
198 | }"#, | ||
199 | "new_name", | ||
200 | r#" | ||
201 | fn foo(new_name : u32) -> u32 { | ||
202 | new_name | ||
203 | }"#, | ||
204 | ); | ||
205 | } | ||
206 | |||
207 | #[test] | ||
208 | fn test_rename_for_mut_param() { | ||
209 | test_rename( | ||
210 | r#" | ||
211 | fn foo(mut i<|> : u32) -> u32 { | ||
212 | i | ||
213 | }"#, | ||
214 | "new_name", | ||
215 | r#" | ||
216 | fn foo(mut new_name : u32) -> u32 { | ||
217 | new_name | ||
218 | }"#, | ||
219 | ); | ||
220 | } | ||
221 | |||
222 | #[test] | ||
223 | fn test_rename_mod() { | ||
224 | let (analysis, position) = analysis_and_position( | ||
225 | " | ||
226 | //- /bar.rs | ||
227 | mod fo<|>o; | ||
228 | //- /bar/foo.rs | ||
229 | // emtpy | ||
230 | ", | ||
231 | ); | ||
232 | let new_name = "foo2"; | ||
233 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
234 | assert_debug_snapshot_matches!("rename_mod", &source_change); | ||
235 | } | ||
236 | |||
237 | #[test] | ||
238 | fn test_rename_mod_in_dir() { | ||
239 | let (analysis, position) = analysis_and_position( | ||
240 | " | ||
241 | //- /lib.rs | ||
242 | mod fo<|>o; | ||
243 | //- /foo/mod.rs | ||
244 | // emtpy | ||
245 | ", | ||
246 | ); | ||
247 | let new_name = "foo2"; | ||
248 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
249 | assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change); | ||
250 | } | ||
251 | |||
252 | fn test_rename(text: &str, new_name: &str, expected: &str) { | ||
253 | let (analysis, position) = single_file_with_position(text); | ||
254 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
255 | let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default(); | ||
256 | let mut file_id: Option<FileId> = None; | ||
257 | if let Some(change) = source_change { | ||
258 | for edit in change.source_file_edits { | ||
259 | file_id = Some(edit.file_id); | ||
260 | for atom in edit.edit.as_atoms() { | ||
261 | text_edit_bulder.replace(atom.delete, atom.insert.clone()); | ||
262 | } | ||
263 | } | ||
264 | } | ||
265 | let result = text_edit_bulder | ||
266 | .finish() | ||
267 | .apply(&*analysis.file_text(file_id.unwrap())); | ||
268 | assert_eq_text!(expected, &*result); | ||
269 | } | ||
270 | } | ||
diff --git a/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap new file mode 100644 index 000000000..dc97b3334 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__rename_mod.snap | |||
@@ -0,0 +1,36 @@ | |||
1 | Created: 2019-01-18T08:26:43.427092+00:00 | ||
2 | Creator: [email protected] | ||
3 | Source: crates/ra_ide_api/src/rename.rs | ||
4 | |||
5 | Some( | ||
6 | SourceChange { | ||
7 | label: "rename", | ||
8 | source_file_edits: [ | ||
9 | SourceFileEdit { | ||
10 | file_id: FileId( | ||
11 | 1 | ||
12 | ), | ||
13 | edit: TextEdit { | ||
14 | atoms: [ | ||
15 | AtomTextEdit { | ||
16 | delete: [4; 7), | ||
17 | insert: "foo2" | ||
18 | } | ||
19 | ] | ||
20 | } | ||
21 | } | ||
22 | ], | ||
23 | file_system_edits: [ | ||
24 | MoveFile { | ||
25 | src: FileId( | ||
26 | 2 | ||
27 | ), | ||
28 | dst_source_root: SourceRootId( | ||
29 | 0 | ||
30 | ), | ||
31 | dst_path: "bar/foo2.rs" | ||
32 | } | ||
33 | ], | ||
34 | cursor_position: None | ||
35 | } | ||
36 | ) | ||
diff --git a/crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap b/crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap new file mode 100644 index 000000000..7dcd65185 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__rename_mod_in_dir.snap | |||
@@ -0,0 +1,36 @@ | |||
1 | Created: 2019-01-18T08:26:43.427095+00:00 | ||
2 | Creator: [email protected] | ||
3 | Source: crates/ra_ide_api/src/rename.rs | ||
4 | |||
5 | Some( | ||
6 | SourceChange { | ||
7 | label: "rename", | ||
8 | source_file_edits: [ | ||
9 | SourceFileEdit { | ||
10 | file_id: FileId( | ||
11 | 1 | ||
12 | ), | ||
13 | edit: TextEdit { | ||
14 | atoms: [ | ||
15 | AtomTextEdit { | ||
16 | delete: [4; 7), | ||
17 | insert: "foo2" | ||
18 | } | ||
19 | ] | ||
20 | } | ||
21 | } | ||
22 | ], | ||
23 | file_system_edits: [ | ||
24 | MoveFile { | ||
25 | src: FileId( | ||
26 | 2 | ||
27 | ), | ||
28 | dst_source_root: SourceRootId( | ||
29 | 0 | ||
30 | ), | ||
31 | dst_path: "foo2/mod.rs" | ||
32 | } | ||
33 | ], | ||
34 | cursor_position: None | ||
35 | } | ||
36 | ) | ||