diff options
author | Ekaterina Babshukova <[email protected]> | 2019-10-12 16:47:17 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-10-22 21:47:31 +0100 |
commit | d26d0ada50fd0063c03e28bc2673f9f63fd23d95 (patch) | |
tree | 1292e2a6352b2bf886baed12cc69dab04dd82462 /crates/ra_ide_api/src/references/rename.rs | |
parent | 0dd08b8023eba053725d5032149808b8733be263 (diff) |
restructure a bit
Diffstat (limited to 'crates/ra_ide_api/src/references/rename.rs')
-rw-r--r-- | crates/ra_ide_api/src/references/rename.rs | 467 |
1 files changed, 467 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs new file mode 100644 index 000000000..7e564a40e --- /dev/null +++ b/crates/ra_ide_api/src/references/rename.rs | |||
@@ -0,0 +1,467 @@ | |||
1 | use hir::ModuleSource; | ||
2 | use ra_db::SourceDatabase; | ||
3 | use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode}; | ||
4 | use relative_path::{RelativePath, RelativePathBuf}; | ||
5 | |||
6 | use crate::{ | ||
7 | db::RootDatabase, FileId, FilePosition, FileSystemEdit, RangeInfo, SourceChange, | ||
8 | SourceFileEdit, TextRange, | ||
9 | }; | ||
10 | |||
11 | use super::find_all_refs; | ||
12 | |||
13 | pub(crate) fn rename( | ||
14 | db: &RootDatabase, | ||
15 | position: FilePosition, | ||
16 | new_name: &str, | ||
17 | ) -> Option<RangeInfo<SourceChange>> { | ||
18 | let parse = db.parse(position.file_id); | ||
19 | if let Some((ast_name, ast_module)) = | ||
20 | find_name_and_module_at_offset(parse.tree().syntax(), position) | ||
21 | { | ||
22 | let range = ast_name.syntax().text_range(); | ||
23 | rename_mod(db, &ast_name, &ast_module, position, new_name) | ||
24 | .map(|info| RangeInfo::new(range, info)) | ||
25 | } else { | ||
26 | rename_reference(db, position, new_name) | ||
27 | } | ||
28 | } | ||
29 | |||
30 | fn find_name_and_module_at_offset( | ||
31 | syntax: &SyntaxNode, | ||
32 | position: FilePosition, | ||
33 | ) -> Option<(ast::Name, ast::Module)> { | ||
34 | let ast_name = find_node_at_offset::<ast::Name>(syntax, position.offset)?; | ||
35 | let ast_module = ast::Module::cast(ast_name.syntax().parent()?)?; | ||
36 | Some((ast_name, ast_module)) | ||
37 | } | ||
38 | |||
39 | fn source_edit_from_file_id_range( | ||
40 | file_id: FileId, | ||
41 | range: TextRange, | ||
42 | new_name: &str, | ||
43 | ) -> SourceFileEdit { | ||
44 | SourceFileEdit { | ||
45 | file_id, | ||
46 | edit: { | ||
47 | let mut builder = ra_text_edit::TextEditBuilder::default(); | ||
48 | builder.replace(range, new_name.into()); | ||
49 | builder.finish() | ||
50 | }, | ||
51 | } | ||
52 | } | ||
53 | |||
54 | fn rename_mod( | ||
55 | db: &RootDatabase, | ||
56 | ast_name: &ast::Name, | ||
57 | ast_module: &ast::Module, | ||
58 | position: FilePosition, | ||
59 | new_name: &str, | ||
60 | ) -> Option<SourceChange> { | ||
61 | let mut source_file_edits = Vec::new(); | ||
62 | let mut file_system_edits = Vec::new(); | ||
63 | let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() }; | ||
64 | if let Some(module) = hir::Module::from_declaration(db, module_src) { | ||
65 | let src = module.definition_source(db); | ||
66 | let file_id = src.file_id.original_file(db); | ||
67 | match src.ast { | ||
68 | ModuleSource::SourceFile(..) => { | ||
69 | let mod_path: RelativePathBuf = db.file_relative_path(file_id); | ||
70 | // mod is defined in path/to/dir/mod.rs | ||
71 | let dst_path = if mod_path.file_stem() == Some("mod") { | ||
72 | mod_path | ||
73 | .parent() | ||
74 | .and_then(|p| p.parent()) | ||
75 | .or_else(|| Some(RelativePath::new(""))) | ||
76 | .map(|p| p.join(new_name).join("mod.rs")) | ||
77 | } else { | ||
78 | Some(mod_path.with_file_name(new_name).with_extension("rs")) | ||
79 | }; | ||
80 | if let Some(path) = dst_path { | ||
81 | let move_file = FileSystemEdit::MoveFile { | ||
82 | src: file_id, | ||
83 | dst_source_root: db.file_source_root(position.file_id), | ||
84 | dst_path: path, | ||
85 | }; | ||
86 | file_system_edits.push(move_file); | ||
87 | } | ||
88 | } | ||
89 | ModuleSource::Module(..) => {} | ||
90 | } | ||
91 | } | ||
92 | |||
93 | let edit = SourceFileEdit { | ||
94 | file_id: position.file_id, | ||
95 | edit: { | ||
96 | let mut builder = ra_text_edit::TextEditBuilder::default(); | ||
97 | builder.replace(ast_name.syntax().text_range(), new_name.into()); | ||
98 | builder.finish() | ||
99 | }, | ||
100 | }; | ||
101 | source_file_edits.push(edit); | ||
102 | |||
103 | Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) | ||
104 | } | ||
105 | |||
106 | fn rename_reference( | ||
107 | db: &RootDatabase, | ||
108 | position: FilePosition, | ||
109 | new_name: &str, | ||
110 | ) -> Option<RangeInfo<SourceChange>> { | ||
111 | let RangeInfo { range, info: refs } = find_all_refs(db, position)?; | ||
112 | |||
113 | let edit = refs | ||
114 | .into_iter() | ||
115 | .map(|range| source_edit_from_file_id_range(range.file_id, range.range, new_name)) | ||
116 | .collect::<Vec<_>>(); | ||
117 | |||
118 | if edit.is_empty() { | ||
119 | return None; | ||
120 | } | ||
121 | |||
122 | Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit))) | ||
123 | } | ||
124 | |||
125 | #[cfg(test)] | ||
126 | mod tests { | ||
127 | use crate::{ | ||
128 | mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId, | ||
129 | ReferenceSearchResult, | ||
130 | }; | ||
131 | use insta::assert_debug_snapshot; | ||
132 | use test_utils::assert_eq_text; | ||
133 | |||
134 | #[test] | ||
135 | fn test_find_all_refs_for_local() { | ||
136 | let code = r#" | ||
137 | fn main() { | ||
138 | let mut i = 1; | ||
139 | let j = 1; | ||
140 | i = i<|> + j; | ||
141 | |||
142 | { | ||
143 | i = 0; | ||
144 | } | ||
145 | |||
146 | i = 5; | ||
147 | }"#; | ||
148 | |||
149 | let refs = get_all_refs(code); | ||
150 | assert_eq!(refs.len(), 5); | ||
151 | } | ||
152 | |||
153 | #[test] | ||
154 | fn test_find_all_refs_for_param_inside() { | ||
155 | let code = r#" | ||
156 | fn foo(i : u32) -> u32 { | ||
157 | i<|> | ||
158 | }"#; | ||
159 | |||
160 | let refs = get_all_refs(code); | ||
161 | assert_eq!(refs.len(), 2); | ||
162 | } | ||
163 | |||
164 | #[test] | ||
165 | fn test_find_all_refs_for_fn_param() { | ||
166 | let code = r#" | ||
167 | fn foo(i<|> : u32) -> u32 { | ||
168 | i | ||
169 | }"#; | ||
170 | |||
171 | let refs = get_all_refs(code); | ||
172 | assert_eq!(refs.len(), 2); | ||
173 | } | ||
174 | |||
175 | #[test] | ||
176 | fn test_find_all_refs_field_name() { | ||
177 | let code = r#" | ||
178 | //- /lib.rs | ||
179 | struct Foo { | ||
180 | pub spam<|>: u32, | ||
181 | } | ||
182 | |||
183 | fn main(s: Foo) { | ||
184 | let f = s.spam; | ||
185 | } | ||
186 | "#; | ||
187 | |||
188 | let refs = get_all_refs(code); | ||
189 | assert_eq!(refs.len(), 2); | ||
190 | } | ||
191 | |||
192 | #[test] | ||
193 | fn test_find_all_refs_impl_item_name() { | ||
194 | let code = r#" | ||
195 | //- /lib.rs | ||
196 | struct Foo; | ||
197 | impl Foo { | ||
198 | fn f<|>(&self) { } | ||
199 | } | ||
200 | "#; | ||
201 | |||
202 | let refs = get_all_refs(code); | ||
203 | assert_eq!(refs.len(), 1); | ||
204 | } | ||
205 | |||
206 | #[test] | ||
207 | fn test_find_all_refs_enum_var_name() { | ||
208 | let code = r#" | ||
209 | //- /lib.rs | ||
210 | enum Foo { | ||
211 | A, | ||
212 | B<|>, | ||
213 | C, | ||
214 | } | ||
215 | "#; | ||
216 | |||
217 | let refs = get_all_refs(code); | ||
218 | assert_eq!(refs.len(), 1); | ||
219 | } | ||
220 | |||
221 | #[test] | ||
222 | fn test_find_all_refs_modules() { | ||
223 | let code = r#" | ||
224 | //- /lib.rs | ||
225 | pub mod foo; | ||
226 | pub mod bar; | ||
227 | |||
228 | fn f() { | ||
229 | let i = foo::Foo { n: 5 }; | ||
230 | } | ||
231 | |||
232 | //- /foo.rs | ||
233 | use crate::bar; | ||
234 | |||
235 | pub struct Foo { | ||
236 | pub n: u32, | ||
237 | } | ||
238 | |||
239 | fn f() { | ||
240 | let i = bar::Bar { n: 5 }; | ||
241 | } | ||
242 | |||
243 | //- /bar.rs | ||
244 | use crate::foo; | ||
245 | |||
246 | pub struct Bar { | ||
247 | pub n: u32, | ||
248 | } | ||
249 | |||
250 | fn f() { | ||
251 | let i = foo::Foo<|> { n: 5 }; | ||
252 | } | ||
253 | "#; | ||
254 | |||
255 | let (analysis, pos) = analysis_and_position(code); | ||
256 | let refs = analysis.find_all_refs(pos).unwrap().unwrap(); | ||
257 | assert_eq!(refs.len(), 3); | ||
258 | } | ||
259 | |||
260 | fn get_all_refs(text: &str) -> ReferenceSearchResult { | ||
261 | let (analysis, position) = single_file_with_position(text); | ||
262 | analysis.find_all_refs(position).unwrap().unwrap() | ||
263 | } | ||
264 | |||
265 | #[test] | ||
266 | fn test_rename_for_local() { | ||
267 | test_rename( | ||
268 | r#" | ||
269 | fn main() { | ||
270 | let mut i = 1; | ||
271 | let j = 1; | ||
272 | i = i<|> + j; | ||
273 | |||
274 | { | ||
275 | i = 0; | ||
276 | } | ||
277 | |||
278 | i = 5; | ||
279 | }"#, | ||
280 | "k", | ||
281 | r#" | ||
282 | fn main() { | ||
283 | let mut k = 1; | ||
284 | let j = 1; | ||
285 | k = k + j; | ||
286 | |||
287 | { | ||
288 | k = 0; | ||
289 | } | ||
290 | |||
291 | k = 5; | ||
292 | }"#, | ||
293 | ); | ||
294 | } | ||
295 | |||
296 | #[test] | ||
297 | fn test_rename_for_param_inside() { | ||
298 | test_rename( | ||
299 | r#" | ||
300 | fn foo(i : u32) -> u32 { | ||
301 | i<|> | ||
302 | }"#, | ||
303 | "j", | ||
304 | r#" | ||
305 | fn foo(j : u32) -> u32 { | ||
306 | j | ||
307 | }"#, | ||
308 | ); | ||
309 | } | ||
310 | |||
311 | #[test] | ||
312 | fn test_rename_refs_for_fn_param() { | ||
313 | test_rename( | ||
314 | r#" | ||
315 | fn foo(i<|> : u32) -> u32 { | ||
316 | i | ||
317 | }"#, | ||
318 | "new_name", | ||
319 | r#" | ||
320 | fn foo(new_name : u32) -> u32 { | ||
321 | new_name | ||
322 | }"#, | ||
323 | ); | ||
324 | } | ||
325 | |||
326 | #[test] | ||
327 | fn test_rename_for_mut_param() { | ||
328 | test_rename( | ||
329 | r#" | ||
330 | fn foo(mut i<|> : u32) -> u32 { | ||
331 | i | ||
332 | }"#, | ||
333 | "new_name", | ||
334 | r#" | ||
335 | fn foo(mut new_name : u32) -> u32 { | ||
336 | new_name | ||
337 | }"#, | ||
338 | ); | ||
339 | } | ||
340 | |||
341 | #[test] | ||
342 | fn test_rename_mod() { | ||
343 | let (analysis, position) = analysis_and_position( | ||
344 | " | ||
345 | //- /lib.rs | ||
346 | mod bar; | ||
347 | |||
348 | //- /bar.rs | ||
349 | mod foo<|>; | ||
350 | |||
351 | //- /bar/foo.rs | ||
352 | // emtpy | ||
353 | ", | ||
354 | ); | ||
355 | let new_name = "foo2"; | ||
356 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
357 | assert_debug_snapshot!(&source_change, | ||
358 | @r###" | ||
359 | Some( | ||
360 | RangeInfo { | ||
361 | range: [4; 7), | ||
362 | info: SourceChange { | ||
363 | label: "rename", | ||
364 | source_file_edits: [ | ||
365 | SourceFileEdit { | ||
366 | file_id: FileId( | ||
367 | 2, | ||
368 | ), | ||
369 | edit: TextEdit { | ||
370 | atoms: [ | ||
371 | AtomTextEdit { | ||
372 | delete: [4; 7), | ||
373 | insert: "foo2", | ||
374 | }, | ||
375 | ], | ||
376 | }, | ||
377 | }, | ||
378 | ], | ||
379 | file_system_edits: [ | ||
380 | MoveFile { | ||
381 | src: FileId( | ||
382 | 3, | ||
383 | ), | ||
384 | dst_source_root: SourceRootId( | ||
385 | 0, | ||
386 | ), | ||
387 | dst_path: "bar/foo2.rs", | ||
388 | }, | ||
389 | ], | ||
390 | cursor_position: None, | ||
391 | }, | ||
392 | }, | ||
393 | ) | ||
394 | "###); | ||
395 | } | ||
396 | |||
397 | #[test] | ||
398 | fn test_rename_mod_in_dir() { | ||
399 | let (analysis, position) = analysis_and_position( | ||
400 | " | ||
401 | //- /lib.rs | ||
402 | mod fo<|>o; | ||
403 | //- /foo/mod.rs | ||
404 | // emtpy | ||
405 | ", | ||
406 | ); | ||
407 | let new_name = "foo2"; | ||
408 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
409 | assert_debug_snapshot!(&source_change, | ||
410 | @r###" | ||
411 | Some( | ||
412 | RangeInfo { | ||
413 | range: [4; 7), | ||
414 | info: SourceChange { | ||
415 | label: "rename", | ||
416 | source_file_edits: [ | ||
417 | SourceFileEdit { | ||
418 | file_id: FileId( | ||
419 | 1, | ||
420 | ), | ||
421 | edit: TextEdit { | ||
422 | atoms: [ | ||
423 | AtomTextEdit { | ||
424 | delete: [4; 7), | ||
425 | insert: "foo2", | ||
426 | }, | ||
427 | ], | ||
428 | }, | ||
429 | }, | ||
430 | ], | ||
431 | file_system_edits: [ | ||
432 | MoveFile { | ||
433 | src: FileId( | ||
434 | 2, | ||
435 | ), | ||
436 | dst_source_root: SourceRootId( | ||
437 | 0, | ||
438 | ), | ||
439 | dst_path: "foo2/mod.rs", | ||
440 | }, | ||
441 | ], | ||
442 | cursor_position: None, | ||
443 | }, | ||
444 | }, | ||
445 | ) | ||
446 | "### | ||
447 | ); | ||
448 | } | ||
449 | |||
450 | fn test_rename(text: &str, new_name: &str, expected: &str) { | ||
451 | let (analysis, position) = single_file_with_position(text); | ||
452 | let source_change = analysis.rename(position, new_name).unwrap(); | ||
453 | let mut text_edit_builder = ra_text_edit::TextEditBuilder::default(); | ||
454 | let mut file_id: Option<FileId> = None; | ||
455 | if let Some(change) = source_change { | ||
456 | for edit in change.info.source_file_edits { | ||
457 | file_id = Some(edit.file_id); | ||
458 | for atom in edit.edit.as_atoms() { | ||
459 | text_edit_builder.replace(atom.delete, atom.insert.clone()); | ||
460 | } | ||
461 | } | ||
462 | } | ||
463 | let result = | ||
464 | text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap()); | ||
465 | assert_eq_text!(expected, &*result); | ||
466 | } | ||
467 | } | ||