From bc0f79f74ad0ab4f663b94772ccbfabed1de625e Mon Sep 17 00:00:00 2001 From: gfreezy Date: Tue, 15 Jan 2019 00:09:03 +0800 Subject: rename mod --- crates/ra_ide_api/src/imp.rs | 90 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 6 deletions(-) (limited to 'crates/ra_ide_api/src') 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 @@ use std::sync::Arc; use hir::{ - self, Problem, source_binder, + self, Problem, source_binder::{ + self, + module_from_declaration + }, ModuleSource, }; use ra_db::{ FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase, @@ -9,16 +12,16 @@ use ra_db::{ }; use ra_ide_api_light::{self, assists, LocalEdit, Severity}; use ra_syntax::{ - TextRange, AstNode, SourceFile, - ast::{self, NameOwner}, - algo::find_node_at_offset, + algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, + SourceFile, + TextRange, }; use crate::{ AnalysisChange, CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, Query, RootChange, SourceChange, SourceFileEdit, - symbol_index::{LibrarySymbolsQuery, FileSymbol}, + symbol_index::{FileSymbol, LibrarySymbolsQuery}, }; impl db::RootDatabase { @@ -110,6 +113,7 @@ impl db::RootDatabase { }; vec![krate.crate_id()] } + pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { let file = self.source_file(position.file_id); // Find the binding associated with the offset @@ -230,20 +234,94 @@ impl db::RootDatabase { .collect() } +<<<<<<< HEAD pub(crate) fn rename(&self, position: FilePosition, new_name: &str) -> Vec { self.find_all_refs(position) .iter() .map(|(file_id, text_range)| SourceFileEdit { file_id: *file_id, +======= + pub(crate) fn rename( + &self, + position: FilePosition, + new_name: &str, + ) -> Cancelable> { + let mut source_file_edits = Vec::new(); + let mut file_system_edits = Vec::new(); + + let source_file = self.source_file(position.file_id); + let syntax = source_file.syntax(); + // We are rename a mod + if let (Some(ast_module), Some(name)) = ( + find_node_at_offset::(syntax, position.offset), + find_node_at_offset::(syntax, position.offset), + ) { + if let Some(module) = module_from_declaration(self, position.file_id, &ast_module)? { + let (file_id, module_source) = module.definition_source(self)?; + match module_source { + ModuleSource::SourceFile(..) => { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: self.file_source_root(position.file_id), + dst_path: self + .file_relative_path(file_id) + .with_file_name(new_name) + .with_extension("rs"), + }; + file_system_edits.push(move_file); + } + ModuleSource::Module(..) => {} + } + } + + let edit = SourceFileEdit { + file_id: position.file_id, +>>>>>>> rename mod edit: { let mut builder = ra_text_edit::TextEditBuilder::default(); - builder.replace(*text_range, new_name.into()); + builder.replace(name.syntax().range(), new_name.into()); builder.finish() }, +<<<<<<< HEAD }) .collect::>() } pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Vec { +======= + }; + source_file_edits.push(edit); + } + // rename references + else { + let edit = self + .find_all_refs(position)? + .iter() + .map(|(file_id, text_range)| SourceFileEdit { + file_id: *file_id, + edit: { + let mut builder = ra_text_edit::TextEditBuilder::default(); + builder.replace(*text_range, new_name.into()); + builder.finish() + }, + }) + .collect::>(); + if edit.is_empty() { + return Ok(None); + } + + source_file_edits = edit; + } + + return Ok(Some(SourceChange { + label: "rename".to_string(), + source_file_edits, + file_system_edits, + cursor_position: None, + })); + } + + pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable> { +>>>>>>> rename mod let name = name_ref.text(); let mut query = Query::new(name.to_string()); query.exact(); -- cgit v1.2.3