diff options
author | gfreezy <[email protected]> | 2019-01-14 16:09:03 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-19 12:36:58 +0000 |
commit | bc0f79f74ad0ab4f663b94772ccbfabed1de625e (patch) | |
tree | 21c125b75a7b757435ce641788cdcde0b9dd4f14 /crates/ra_ide_api/src | |
parent | b82fe73d1ab9727ff650382d9c86a231b06245be (diff) |
rename mod
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/imp.rs | 90 |
1 files changed, 84 insertions, 6 deletions
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index 28e497965..3f0de8b5b 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs | |||
@@ -1,7 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use hir::{ | 3 | use hir::{ |
4 | self, Problem, source_binder, | 4 | self, Problem, source_binder::{ |
5 | self, | ||
6 | module_from_declaration | ||
7 | }, ModuleSource, | ||
5 | }; | 8 | }; |
6 | use ra_db::{ | 9 | use ra_db::{ |
7 | FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase, | 10 | FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase, |
@@ -9,16 +12,16 @@ use ra_db::{ | |||
9 | }; | 12 | }; |
10 | use ra_ide_api_light::{self, assists, LocalEdit, Severity}; | 13 | use ra_ide_api_light::{self, assists, LocalEdit, Severity}; |
11 | use ra_syntax::{ | 14 | use ra_syntax::{ |
12 | TextRange, AstNode, SourceFile, | 15 | algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, |
13 | ast::{self, NameOwner}, | 16 | SourceFile, |
14 | algo::find_node_at_offset, | 17 | TextRange, |
15 | }; | 18 | }; |
16 | 19 | ||
17 | use crate::{ | 20 | use crate::{ |
18 | AnalysisChange, | 21 | AnalysisChange, |
19 | CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, | 22 | CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, |
20 | Query, RootChange, SourceChange, SourceFileEdit, | 23 | Query, RootChange, SourceChange, SourceFileEdit, |
21 | symbol_index::{LibrarySymbolsQuery, FileSymbol}, | 24 | symbol_index::{FileSymbol, LibrarySymbolsQuery}, |
22 | }; | 25 | }; |
23 | 26 | ||
24 | impl db::RootDatabase { | 27 | impl db::RootDatabase { |
@@ -110,6 +113,7 @@ impl db::RootDatabase { | |||
110 | }; | 113 | }; |
111 | vec![krate.crate_id()] | 114 | vec![krate.crate_id()] |
112 | } | 115 | } |
116 | |||
113 | pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { | 117 | pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { |
114 | let file = self.source_file(position.file_id); | 118 | let file = self.source_file(position.file_id); |
115 | // Find the binding associated with the offset | 119 | // Find the binding associated with the offset |
@@ -230,20 +234,94 @@ impl db::RootDatabase { | |||
230 | .collect() | 234 | .collect() |
231 | } | 235 | } |
232 | 236 | ||
237 | <<<<<<< HEAD | ||
233 | pub(crate) fn rename(&self, position: FilePosition, new_name: &str) -> Vec<SourceFileEdit> { | 238 | pub(crate) fn rename(&self, position: FilePosition, new_name: &str) -> Vec<SourceFileEdit> { |
234 | self.find_all_refs(position) | 239 | self.find_all_refs(position) |
235 | .iter() | 240 | .iter() |
236 | .map(|(file_id, text_range)| SourceFileEdit { | 241 | .map(|(file_id, text_range)| SourceFileEdit { |
237 | file_id: *file_id, | 242 | file_id: *file_id, |
243 | ======= | ||
244 | pub(crate) fn rename( | ||
245 | &self, | ||
246 | position: FilePosition, | ||
247 | new_name: &str, | ||
248 | ) -> Cancelable<Option<SourceChange>> { | ||
249 | let mut source_file_edits = Vec::new(); | ||
250 | let mut file_system_edits = Vec::new(); | ||
251 | |||
252 | let source_file = self.source_file(position.file_id); | ||
253 | let syntax = source_file.syntax(); | ||
254 | // We are rename a mod | ||
255 | if let (Some(ast_module), Some(name)) = ( | ||
256 | find_node_at_offset::<ast::Module>(syntax, position.offset), | ||
257 | find_node_at_offset::<ast::Name>(syntax, position.offset), | ||
258 | ) { | ||
259 | if let Some(module) = module_from_declaration(self, position.file_id, &ast_module)? { | ||
260 | let (file_id, module_source) = module.definition_source(self)?; | ||
261 | match module_source { | ||
262 | ModuleSource::SourceFile(..) => { | ||
263 | let move_file = FileSystemEdit::MoveFile { | ||
264 | src: file_id, | ||
265 | dst_source_root: self.file_source_root(position.file_id), | ||
266 | dst_path: self | ||
267 | .file_relative_path(file_id) | ||
268 | .with_file_name(new_name) | ||
269 | .with_extension("rs"), | ||
270 | }; | ||
271 | file_system_edits.push(move_file); | ||
272 | } | ||
273 | ModuleSource::Module(..) => {} | ||
274 | } | ||
275 | } | ||
276 | |||
277 | let edit = SourceFileEdit { | ||
278 | file_id: position.file_id, | ||
279 | >>>>>>> rename mod | ||
238 | edit: { | 280 | edit: { |
239 | let mut builder = ra_text_edit::TextEditBuilder::default(); | 281 | let mut builder = ra_text_edit::TextEditBuilder::default(); |
240 | builder.replace(*text_range, new_name.into()); | 282 | builder.replace(name.syntax().range(), new_name.into()); |
241 | builder.finish() | 283 | builder.finish() |
242 | }, | 284 | }, |
285 | <<<<<<< HEAD | ||
243 | }) | 286 | }) |
244 | .collect::<Vec<_>>() | 287 | .collect::<Vec<_>>() |
245 | } | 288 | } |
246 | pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> { | 289 | pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec<FileSymbol> { |
290 | ======= | ||
291 | }; | ||
292 | source_file_edits.push(edit); | ||
293 | } | ||
294 | // rename references | ||
295 | else { | ||
296 | let edit = self | ||
297 | .find_all_refs(position)? | ||
298 | .iter() | ||
299 | .map(|(file_id, text_range)| SourceFileEdit { | ||
300 | file_id: *file_id, | ||
301 | edit: { | ||
302 | let mut builder = ra_text_edit::TextEditBuilder::default(); | ||
303 | builder.replace(*text_range, new_name.into()); | ||
304 | builder.finish() | ||
305 | }, | ||
306 | }) | ||
307 | .collect::<Vec<_>>(); | ||
308 | if edit.is_empty() { | ||
309 | return Ok(None); | ||
310 | } | ||
311 | |||
312 | source_file_edits = edit; | ||
313 | } | ||
314 | |||
315 | return Ok(Some(SourceChange { | ||
316 | label: "rename".to_string(), | ||
317 | source_file_edits, | ||
318 | file_system_edits, | ||
319 | cursor_position: None, | ||
320 | })); | ||
321 | } | ||
322 | |||
323 | pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable<Vec<FileSymbol>> { | ||
324 | >>>>>>> rename mod | ||
247 | let name = name_ref.text(); | 325 | let name = name_ref.text(); |
248 | let mut query = Query::new(name.to_string()); | 326 | let mut query = Query::new(name.to_string()); |
249 | query.exact(); | 327 | query.exact(); |