From 842e8001b287b0e3d77215235ae96a3bd8944207 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 11:52:18 +0300 Subject: move changes to a separate file --- crates/ra_ide_api/src/change.rs | 255 ++++++++++++++++++++++++++++++++++++++++ crates/ra_ide_api/src/imp.rs | 101 +--------------- crates/ra_ide_api/src/impls.rs | 2 +- crates/ra_ide_api/src/lib.rs | 159 +------------------------ 4 files changed, 263 insertions(+), 254 deletions(-) create mode 100644 crates/ra_ide_api/src/change.rs (limited to 'crates') diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs new file mode 100644 index 000000000..992955740 --- /dev/null +++ b/crates/ra_ide_api/src/change.rs @@ -0,0 +1,255 @@ +use std::{ + fmt, time, + sync::Arc, +}; + +use rustc_hash::FxHashMap; +use ra_db::{ + SourceRootId, FileId, CrateGraph, SourceDatabase, SourceRoot, + salsa::{Database, SweepStrategy}, +}; +use ra_syntax::SourceFile; +use relative_path::RelativePathBuf; +use rayon::prelude::*; + +use crate::{ + db::RootDatabase, + symbol_index::{SymbolIndex, SymbolsDatabase}, + status::syntax_tree_stats, +}; + +#[derive(Default)] +pub struct AnalysisChange { + new_roots: Vec<(SourceRootId, bool)>, + roots_changed: FxHashMap, + files_changed: Vec<(FileId, Arc)>, + libraries_added: Vec, + crate_graph: Option, +} + +impl fmt::Debug for AnalysisChange { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let mut d = fmt.debug_struct("AnalysisChange"); + if !self.new_roots.is_empty() { + d.field("new_roots", &self.new_roots); + } + if !self.roots_changed.is_empty() { + d.field("roots_changed", &self.roots_changed); + } + if !self.files_changed.is_empty() { + d.field("files_changed", &self.files_changed.len()); + } + if !self.libraries_added.is_empty() { + d.field("libraries_added", &self.libraries_added.len()); + } + if !self.crate_graph.is_some() { + d.field("crate_graph", &self.crate_graph); + } + d.finish() + } +} + +impl AnalysisChange { + pub fn new() -> AnalysisChange { + AnalysisChange::default() + } + + pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) { + self.new_roots.push((root_id, is_local)); + } + + pub fn add_file( + &mut self, + root_id: SourceRootId, + file_id: FileId, + path: RelativePathBuf, + text: Arc, + ) { + let file = AddFile { + file_id, + path, + text, + }; + self.roots_changed + .entry(root_id) + .or_default() + .added + .push(file); + } + + pub fn change_file(&mut self, file_id: FileId, new_text: Arc) { + self.files_changed.push((file_id, new_text)) + } + + pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) { + let file = RemoveFile { file_id, path }; + self.roots_changed + .entry(root_id) + .or_default() + .removed + .push(file); + } + + pub fn add_library(&mut self, data: LibraryData) { + self.libraries_added.push(data) + } + + pub fn set_crate_graph(&mut self, graph: CrateGraph) { + self.crate_graph = Some(graph); + } +} + +#[derive(Debug)] +struct AddFile { + file_id: FileId, + path: RelativePathBuf, + text: Arc, +} + +#[derive(Debug)] +struct RemoveFile { + file_id: FileId, + path: RelativePathBuf, +} + +#[derive(Default)] +struct RootChange { + added: Vec, + removed: Vec, +} + +impl fmt::Debug for RootChange { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("AnalysisChange") + .field("added", &self.added.len()) + .field("removed", &self.removed.len()) + .finish() + } +} + +pub struct LibraryData { + root_id: SourceRootId, + root_change: RootChange, + symbol_index: SymbolIndex, +} + +impl fmt::Debug for LibraryData { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("LibraryData") + .field("root_id", &self.root_id) + .field("root_change", &self.root_change) + .field("n_symbols", &self.symbol_index.len()) + .finish() + } +} + +impl LibraryData { + pub fn prepare( + root_id: SourceRootId, + files: Vec<(FileId, RelativePathBuf, Arc)>, + ) -> LibraryData { + let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { + let file = SourceFile::parse(text); + (*file_id, file) + })); + let mut root_change = RootChange::default(); + root_change.added = files + .into_iter() + .map(|(file_id, path, text)| AddFile { + file_id, + path, + text, + }) + .collect(); + LibraryData { + root_id, + root_change, + symbol_index, + } + } +} + +const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); + +impl RootDatabase { + pub(crate) fn apply_change(&mut self, change: AnalysisChange) { + log::info!("apply_change {:?}", change); + if !change.new_roots.is_empty() { + let mut local_roots = Vec::clone(&self.local_roots()); + for (root_id, is_local) in change.new_roots { + self.set_source_root(root_id, Default::default()); + if is_local { + local_roots.push(root_id); + } + } + self.set_local_roots(Arc::new(local_roots)); + } + + for (root_id, root_change) in change.roots_changed { + self.apply_root_change(root_id, root_change); + } + for (file_id, text) in change.files_changed { + self.set_file_text(file_id, text) + } + if !change.libraries_added.is_empty() { + let mut libraries = Vec::clone(&self.library_roots()); + for library in change.libraries_added { + libraries.push(library.root_id); + self.set_source_root(library.root_id, Default::default()); + self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index)); + self.apply_root_change(library.root_id, library.root_change); + } + self.set_library_roots(Arc::new(libraries)); + } + if let Some(crate_graph) = change.crate_graph { + self.set_crate_graph(Arc::new(crate_graph)) + } + } + + fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { + let mut source_root = SourceRoot::clone(&self.source_root(root_id)); + for add_file in root_change.added { + self.set_file_text(add_file.file_id, add_file.text); + self.set_file_relative_path(add_file.file_id, add_file.path.clone()); + self.set_file_source_root(add_file.file_id, root_id); + source_root.files.insert(add_file.path, add_file.file_id); + } + for remove_file in root_change.removed { + self.set_file_text(remove_file.file_id, Default::default()); + source_root.files.remove(&remove_file.path); + } + self.set_source_root(root_id, Arc::new(source_root)); + } + + pub(crate) fn maybe_collect_garbage(&mut self) { + if self.last_gc_check.elapsed() > GC_COOLDOWN { + self.last_gc_check = time::Instant::now(); + let retained_trees = syntax_tree_stats(self).retained; + if retained_trees > 100 { + log::info!( + "automatic garbadge collection, {} retained trees", + retained_trees + ); + self.collect_garbage(); + } + } + } + + pub(crate) fn collect_garbage(&mut self) { + self.last_gc = time::Instant::now(); + + let sweep = SweepStrategy::default() + .discard_values() + .sweep_all_revisions(); + + self.query(ra_db::ParseQuery).sweep(sweep); + + self.query(hir::db::HirParseQuery).sweep(sweep); + self.query(hir::db::FileItemsQuery).sweep(sweep); + self.query(hir::db::FileItemQuery).sweep(sweep); + + self.query(hir::db::LowerModuleQuery).sweep(sweep); + self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep); + self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep); + } +} diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index b139efabf..7d672656f 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -1,115 +1,20 @@ -use std::{ - sync::Arc, - time, -}; - use hir::{ self, Problem, source_binder }; -use ra_db::{ - SourceDatabase, SourceRoot, SourceRootId, - salsa::{Database, SweepStrategy}, -}; use ra_ide_api_light::{self, LocalEdit, Severity}; use ra_syntax::{ algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, SourceFile, TextRange, }; +use ra_db::SourceDatabase; use crate::{ - AnalysisChange, CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit, - Query, RootChange, SourceChange, SourceFileEdit, - symbol_index::{FileSymbol, SymbolsDatabase}, - status::syntax_tree_stats + Query, SourceChange, SourceFileEdit, + symbol_index::FileSymbol, }; -const GC_COOLDOWN: time::Duration = time::Duration::from_millis(100); - -impl db::RootDatabase { - pub(crate) fn apply_change(&mut self, change: AnalysisChange) { - log::info!("apply_change {:?}", change); - if !change.new_roots.is_empty() { - let mut local_roots = Vec::clone(&self.local_roots()); - for (root_id, is_local) in change.new_roots { - self.set_source_root(root_id, Default::default()); - if is_local { - local_roots.push(root_id); - } - } - self.set_local_roots(Arc::new(local_roots)); - } - - for (root_id, root_change) in change.roots_changed { - self.apply_root_change(root_id, root_change); - } - for (file_id, text) in change.files_changed { - self.set_file_text(file_id, text) - } - if !change.libraries_added.is_empty() { - let mut libraries = Vec::clone(&self.library_roots()); - for library in change.libraries_added { - libraries.push(library.root_id); - self.set_source_root(library.root_id, Default::default()); - self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index)); - self.apply_root_change(library.root_id, library.root_change); - } - self.set_library_roots(Arc::new(libraries)); - } - if let Some(crate_graph) = change.crate_graph { - self.set_crate_graph(Arc::new(crate_graph)) - } - } - - fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { - let mut source_root = SourceRoot::clone(&self.source_root(root_id)); - for add_file in root_change.added { - self.set_file_text(add_file.file_id, add_file.text); - self.set_file_relative_path(add_file.file_id, add_file.path.clone()); - self.set_file_source_root(add_file.file_id, root_id); - source_root.files.insert(add_file.path, add_file.file_id); - } - for remove_file in root_change.removed { - self.set_file_text(remove_file.file_id, Default::default()); - source_root.files.remove(&remove_file.path); - } - self.set_source_root(root_id, Arc::new(source_root)); - } - - pub(crate) fn maybe_collect_garbage(&mut self) { - if self.last_gc_check.elapsed() > GC_COOLDOWN { - self.last_gc_check = time::Instant::now(); - let retained_trees = syntax_tree_stats(self).retained; - if retained_trees > 100 { - log::info!( - "automatic garbadge collection, {} retained trees", - retained_trees - ); - self.collect_garbage(); - } - } - } - - pub(crate) fn collect_garbage(&mut self) { - self.last_gc = time::Instant::now(); - - let sweep = SweepStrategy::default() - .discard_values() - .sweep_all_revisions(); - - self.query(ra_db::ParseQuery).sweep(sweep); - - self.query(hir::db::HirParseQuery).sweep(sweep); - self.query(hir::db::FileItemsQuery).sweep(sweep); - self.query(hir::db::FileItemQuery).sweep(sweep); - - self.query(hir::db::LowerModuleQuery).sweep(sweep); - self.query(hir::db::LowerModuleSourceMapQuery).sweep(sweep); - self.query(hir::db::BodySyntaxMappingQuery).sweep(sweep); - } -} - impl db::RootDatabase { /// Returns `Vec` for the same reason as `parent_module` pub(crate) fn crate_for(&self, file_id: FileId) -> Vec { diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index 91fa41f1f..4fb054139 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -1,4 +1,4 @@ -use ra_db::{SourceDatabase}; +use ra_db::SourceDatabase; use ra_syntax::{ AstNode, ast, algo::find_node_at_offset, diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 68d59aae1..22a601ec8 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -18,6 +18,7 @@ mod imp; pub mod mock_analysis; mod symbol_index; mod navigation_target; +mod change; mod status; mod completion; @@ -35,7 +36,7 @@ mod assists; #[cfg(test)] mod marks; -use std::{fmt, sync::Arc}; +use std::sync::Arc; use ra_syntax::{SourceFile, TreeArc, TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -43,12 +44,10 @@ use ra_db::{ SourceDatabase, CheckCanceled, salsa::{self, ParallelDatabase}, }; -use rayon::prelude::*; use relative_path::RelativePathBuf; -use rustc_hash::FxHashMap; use crate::{ - symbol_index::{FileSymbol, SymbolIndex}, + symbol_index::FileSymbol, db::LineIndexDatabase, }; @@ -56,6 +55,7 @@ pub use crate::{ completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, runnables::{Runnable, RunnableKind}, navigation_target::NavigationTarget, + change::{AnalysisChange, LibraryData}, }; pub use ra_ide_api_light::{ Fold, FoldKind, HighlightedRange, Severity, StructureNode, @@ -74,115 +74,6 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; pub type Cancelable = Result; -#[derive(Default)] -pub struct AnalysisChange { - new_roots: Vec<(SourceRootId, bool)>, - roots_changed: FxHashMap, - files_changed: Vec<(FileId, Arc)>, - libraries_added: Vec, - crate_graph: Option, -} - -#[derive(Default)] -struct RootChange { - added: Vec, - removed: Vec, -} - -#[derive(Debug)] -struct AddFile { - file_id: FileId, - path: RelativePathBuf, - text: Arc, -} - -#[derive(Debug)] -struct RemoveFile { - file_id: FileId, - path: RelativePathBuf, -} - -impl fmt::Debug for AnalysisChange { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let mut d = fmt.debug_struct("AnalysisChange"); - if !self.new_roots.is_empty() { - d.field("new_roots", &self.new_roots); - } - if !self.roots_changed.is_empty() { - d.field("roots_changed", &self.roots_changed); - } - if !self.files_changed.is_empty() { - d.field("files_changed", &self.files_changed.len()); - } - if !self.libraries_added.is_empty() { - d.field("libraries_added", &self.libraries_added.len()); - } - if self.crate_graph.is_none() { - d.field("crate_graph", &self.crate_graph); - } - d.finish() - } -} - -impl fmt::Debug for RootChange { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - fmt.debug_struct("AnalysisChange") - .field("added", &self.added.len()) - .field("removed", &self.removed.len()) - .finish() - } -} - -impl AnalysisChange { - pub fn new() -> AnalysisChange { - AnalysisChange::default() - } - - pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) { - self.new_roots.push((root_id, is_local)); - } - - pub fn add_file( - &mut self, - root_id: SourceRootId, - file_id: FileId, - path: RelativePathBuf, - text: Arc, - ) { - let file = AddFile { - file_id, - path, - text, - }; - self.roots_changed - .entry(root_id) - .or_default() - .added - .push(file); - } - - pub fn change_file(&mut self, file_id: FileId, new_text: Arc) { - self.files_changed.push((file_id, new_text)) - } - - pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) { - let file = RemoveFile { file_id, path }; - self.roots_changed - .entry(root_id) - .or_default() - .removed - .push(file); - } - - pub fn add_library(&mut self, data: LibraryData) { - self.libraries_added.push(data) - } - - pub fn set_crate_graph(&mut self, graph: CrateGraph) { - self.crate_graph = Some(graph); - } -} - #[derive(Debug)] pub struct SourceChange { pub label: String, @@ -508,48 +399,6 @@ impl Analysis { } } -pub struct LibraryData { - root_id: SourceRootId, - root_change: RootChange, - symbol_index: SymbolIndex, -} - -impl fmt::Debug for LibraryData { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("LibraryData") - .field("root_id", &self.root_id) - .field("root_change", &self.root_change) - .field("n_symbols", &self.symbol_index.len()) - .finish() - } -} - -impl LibraryData { - pub fn prepare( - root_id: SourceRootId, - files: Vec<(FileId, RelativePathBuf, Arc)>, - ) -> LibraryData { - let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { - let file = SourceFile::parse(text); - (*file_id, file) - })); - let mut root_change = RootChange::default(); - root_change.added = files - .into_iter() - .map(|(file_id, path, text)| AddFile { - file_id, - path, - text, - }) - .collect(); - LibraryData { - root_id, - root_change, - symbol_index, - } - } -} - #[test] fn analysis_is_send() { fn is_send() {} -- cgit v1.2.3 From bddd1242986f3155bdb1ca65495bc0623e3d211d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 13:50:18 +0300 Subject: move crate for --- crates/ra_ide_api/src/imp.rs | 15 +-------------- crates/ra_ide_api/src/lib.rs | 2 +- crates/ra_ide_api/src/parent_module.rs | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index 7d672656f..dea71740c 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -10,25 +10,12 @@ use ra_syntax::{ use ra_db::SourceDatabase; use crate::{ - CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit, + db, Diagnostic, FileId, FilePosition, FileSystemEdit, Query, SourceChange, SourceFileEdit, symbol_index::FileSymbol, }; impl db::RootDatabase { - /// Returns `Vec` for the same reason as `parent_module` - pub(crate) fn crate_for(&self, file_id: FileId) -> Vec { - let module = match source_binder::module_from_file_id(self, file_id) { - Some(it) => it, - None => return Vec::new(), - }; - let krate = match module.krate(self) { - Some(it) => it, - None => return Vec::new(), - }; - vec![krate.crate_id()] - } - pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { let file = self.parse(position.file_id); // Find the binding associated with the offset diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 22a601ec8..bb60e8d40 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -342,7 +342,7 @@ impl Analysis { /// Returns crates this file belongs too. pub fn crate_for(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| db.crate_for(file_id)) + self.with_db(|db| parent_module::crate_for(db, file_id)) } /// Returns the root file of the given crate. diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index e94297fe3..603c3db6a 100644 --- a/crates/ra_ide_api/src/parent_module.rs +++ b/crates/ra_ide_api/src/parent_module.rs @@ -1,4 +1,4 @@ -use ra_db::FilePosition; +use ra_db::{FilePosition, FileId, CrateId}; use crate::{NavigationTarget, db::RootDatabase}; @@ -13,6 +13,19 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec Vec { + let module = match hir::source_binder::module_from_file_id(db, file_id) { + Some(it) => it, + None => return Vec::new(), + }; + let krate = match module.krate(db) { + Some(it) => it, + None => return Vec::new(), + }; + vec![krate.crate_id()] +} + #[cfg(test)] mod tests { use crate::mock_analysis::analysis_and_position; -- cgit v1.2.3 From 4d0e58afef1722d5f5bf5970bed44594c27ecf34 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 13:51:22 +0300 Subject: rename rename to references --- crates/ra_ide_api/src/lib.rs | 4 +- crates/ra_ide_api/src/references.rs | 273 ++++++++++++++++++++++++++++++++++++ crates/ra_ide_api/src/rename.rs | 273 ------------------------------------ 3 files changed, 275 insertions(+), 275 deletions(-) create mode 100644 crates/ra_ide_api/src/references.rs delete mode 100644 crates/ra_ide_api/src/rename.rs (limited to 'crates') diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index bb60e8d40..0bb21245d 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -29,7 +29,7 @@ mod hover; mod call_info; mod syntax_highlighting; mod parent_module; -mod rename; +mod references; mod impls; mod assists; @@ -388,7 +388,7 @@ impl Analysis { position: FilePosition, new_name: &str, ) -> Cancelable> { - self.with_db(|db| rename::rename(db, position, new_name)) + self.with_db(|db| references::rename(db, position, new_name)) } fn with_db T + std::panic::UnwindSafe, T>( diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs new file mode 100644 index 000000000..1c9491a0a --- /dev/null +++ b/crates/ra_ide_api/src/references.rs @@ -0,0 +1,273 @@ +use relative_path::RelativePathBuf; + +use hir::{ + self, ModuleSource, source_binder::module_from_declaration, +}; +use ra_syntax::{ + algo::find_node_at_offset, + ast, + AstNode, + SyntaxNode +}; + +use crate::{ + db::RootDatabase, + FilePosition, + FileSystemEdit, + SourceChange, + SourceFileEdit, +}; +use ra_db::SourceDatabase; +use relative_path::RelativePath; + +pub(crate) fn rename( + db: &RootDatabase, + position: FilePosition, + new_name: &str, +) -> Option { + let source_file = db.parse(position.file_id); + let syntax = source_file.syntax(); + + if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(syntax, position) { + rename_mod(db, ast_name, ast_module, position, new_name) + } else { + rename_reference(db, position, new_name) + } +} + +fn find_name_and_module_at_offset( + syntax: &SyntaxNode, + position: FilePosition, +) -> Option<(&ast::Name, &ast::Module)> { + let ast_name = find_node_at_offset::(syntax, position.offset); + let ast_name_parent = ast::Module::cast(ast_name?.syntax().parent()?); + + if let (Some(ast_module), Some(name)) = (ast_name_parent, ast_name) { + return Some((name, ast_module)); + } + None +} + +fn rename_mod( + db: &RootDatabase, + ast_name: &ast::Name, + ast_module: &ast::Module, + position: FilePosition, + new_name: &str, +) -> Option { + let mut source_file_edits = Vec::new(); + let mut file_system_edits = Vec::new(); + if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { + let (file_id, module_source) = module.definition_source(db); + match module_source { + ModuleSource::SourceFile(..) => { + let mod_path: RelativePathBuf = db.file_relative_path(file_id); + // mod is defined in path/to/dir/mod.rs + let dst_path = if mod_path.file_stem() == Some("mod") { + mod_path + .parent() + .and_then(|p| p.parent()) + .or_else(|| Some(RelativePath::new(""))) + .map(|p| p.join(new_name).join("mod.rs")) + } else { + Some(mod_path.with_file_name(new_name).with_extension("rs")) + }; + if let Some(path) = dst_path { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: db.file_source_root(position.file_id), + dst_path: path, + }; + file_system_edits.push(move_file); + } + } + ModuleSource::Module(..) => {} + } + } + + let edit = SourceFileEdit { + file_id: position.file_id, + edit: { + let mut builder = ra_text_edit::TextEditBuilder::default(); + builder.replace(ast_name.syntax().range(), new_name.into()); + builder.finish() + }, + }; + source_file_edits.push(edit); + + Some(SourceChange { + label: "rename".to_string(), + source_file_edits, + file_system_edits, + cursor_position: None, + }) +} + +fn rename_reference( + db: &RootDatabase, + position: FilePosition, + new_name: &str, +) -> Option { + let edit = db + .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 None; + } + + Some(SourceChange { + label: "rename".to_string(), + source_file_edits: edit, + file_system_edits: Vec::new(), + cursor_position: None, + }) +} + +#[cfg(test)] +mod tests { + use insta::assert_debug_snapshot_matches; + use test_utils::assert_eq_text; + use crate::{ + mock_analysis::single_file_with_position, + mock_analysis::analysis_and_position, + FileId +}; + + #[test] + fn test_rename_for_local() { + test_rename( + r#" + fn main() { + let mut i = 1; + let j = 1; + i = i<|> + j; + + { + i = 0; + } + + i = 5; + }"#, + "k", + r#" + fn main() { + let mut k = 1; + let j = 1; + k = k + j; + + { + k = 0; + } + + k = 5; + }"#, + ); + } + + #[test] + fn test_rename_for_param_inside() { + test_rename( + r#" + fn foo(i : u32) -> u32 { + i<|> + }"#, + "j", + r#" + fn foo(j : u32) -> u32 { + j + }"#, + ); + } + + #[test] + fn test_rename_refs_for_fn_param() { + test_rename( + r#" + fn foo(i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(new_name : u32) -> u32 { + new_name + }"#, + ); + } + + #[test] + fn test_rename_for_mut_param() { + test_rename( + r#" + fn foo(mut i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(mut new_name : u32) -> u32 { + new_name + }"#, + ); + } + + #[test] + fn test_rename_mod() { + let (analysis, position) = analysis_and_position( + " + //- /lib.rs + mod bar; + + //- /bar.rs + mod foo<|>; + + //- /bar/foo.rs + // emtpy + ", + ); + let new_name = "foo2"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert_debug_snapshot_matches!("rename_mod", &source_change); + } + + #[test] + fn test_rename_mod_in_dir() { + let (analysis, position) = analysis_and_position( + " + //- /lib.rs + mod fo<|>o; + //- /foo/mod.rs + // emtpy + ", + ); + let new_name = "foo2"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change); + } + + fn test_rename(text: &str, new_name: &str, expected: &str) { + let (analysis, position) = single_file_with_position(text); + let source_change = analysis.rename(position, new_name).unwrap(); + let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default(); + let mut file_id: Option = None; + if let Some(change) = source_change { + for edit in change.source_file_edits { + file_id = Some(edit.file_id); + for atom in edit.edit.as_atoms() { + text_edit_bulder.replace(atom.delete, atom.insert.clone()); + } + } + } + let result = text_edit_bulder + .finish() + .apply(&*analysis.file_text(file_id.unwrap())); + assert_eq_text!(expected, &*result); + } +} diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/rename.rs deleted file mode 100644 index 1c9491a0a..000000000 --- a/crates/ra_ide_api/src/rename.rs +++ /dev/null @@ -1,273 +0,0 @@ -use relative_path::RelativePathBuf; - -use hir::{ - self, ModuleSource, source_binder::module_from_declaration, -}; -use ra_syntax::{ - algo::find_node_at_offset, - ast, - AstNode, - SyntaxNode -}; - -use crate::{ - db::RootDatabase, - FilePosition, - FileSystemEdit, - SourceChange, - SourceFileEdit, -}; -use ra_db::SourceDatabase; -use relative_path::RelativePath; - -pub(crate) fn rename( - db: &RootDatabase, - position: FilePosition, - new_name: &str, -) -> Option { - let source_file = db.parse(position.file_id); - let syntax = source_file.syntax(); - - if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(syntax, position) { - rename_mod(db, ast_name, ast_module, position, new_name) - } else { - rename_reference(db, position, new_name) - } -} - -fn find_name_and_module_at_offset( - syntax: &SyntaxNode, - position: FilePosition, -) -> Option<(&ast::Name, &ast::Module)> { - let ast_name = find_node_at_offset::(syntax, position.offset); - let ast_name_parent = ast::Module::cast(ast_name?.syntax().parent()?); - - if let (Some(ast_module), Some(name)) = (ast_name_parent, ast_name) { - return Some((name, ast_module)); - } - None -} - -fn rename_mod( - db: &RootDatabase, - ast_name: &ast::Name, - ast_module: &ast::Module, - position: FilePosition, - new_name: &str, -) -> Option { - let mut source_file_edits = Vec::new(); - let mut file_system_edits = Vec::new(); - if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { - let (file_id, module_source) = module.definition_source(db); - match module_source { - ModuleSource::SourceFile(..) => { - let mod_path: RelativePathBuf = db.file_relative_path(file_id); - // mod is defined in path/to/dir/mod.rs - let dst_path = if mod_path.file_stem() == Some("mod") { - mod_path - .parent() - .and_then(|p| p.parent()) - .or_else(|| Some(RelativePath::new(""))) - .map(|p| p.join(new_name).join("mod.rs")) - } else { - Some(mod_path.with_file_name(new_name).with_extension("rs")) - }; - if let Some(path) = dst_path { - let move_file = FileSystemEdit::MoveFile { - src: file_id, - dst_source_root: db.file_source_root(position.file_id), - dst_path: path, - }; - file_system_edits.push(move_file); - } - } - ModuleSource::Module(..) => {} - } - } - - let edit = SourceFileEdit { - file_id: position.file_id, - edit: { - let mut builder = ra_text_edit::TextEditBuilder::default(); - builder.replace(ast_name.syntax().range(), new_name.into()); - builder.finish() - }, - }; - source_file_edits.push(edit); - - Some(SourceChange { - label: "rename".to_string(), - source_file_edits, - file_system_edits, - cursor_position: None, - }) -} - -fn rename_reference( - db: &RootDatabase, - position: FilePosition, - new_name: &str, -) -> Option { - let edit = db - .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 None; - } - - Some(SourceChange { - label: "rename".to_string(), - source_file_edits: edit, - file_system_edits: Vec::new(), - cursor_position: None, - }) -} - -#[cfg(test)] -mod tests { - use insta::assert_debug_snapshot_matches; - use test_utils::assert_eq_text; - use crate::{ - mock_analysis::single_file_with_position, - mock_analysis::analysis_and_position, - FileId -}; - - #[test] - fn test_rename_for_local() { - test_rename( - r#" - fn main() { - let mut i = 1; - let j = 1; - i = i<|> + j; - - { - i = 0; - } - - i = 5; - }"#, - "k", - r#" - fn main() { - let mut k = 1; - let j = 1; - k = k + j; - - { - k = 0; - } - - k = 5; - }"#, - ); - } - - #[test] - fn test_rename_for_param_inside() { - test_rename( - r#" - fn foo(i : u32) -> u32 { - i<|> - }"#, - "j", - r#" - fn foo(j : u32) -> u32 { - j - }"#, - ); - } - - #[test] - fn test_rename_refs_for_fn_param() { - test_rename( - r#" - fn foo(i<|> : u32) -> u32 { - i - }"#, - "new_name", - r#" - fn foo(new_name : u32) -> u32 { - new_name - }"#, - ); - } - - #[test] - fn test_rename_for_mut_param() { - test_rename( - r#" - fn foo(mut i<|> : u32) -> u32 { - i - }"#, - "new_name", - r#" - fn foo(mut new_name : u32) -> u32 { - new_name - }"#, - ); - } - - #[test] - fn test_rename_mod() { - let (analysis, position) = analysis_and_position( - " - //- /lib.rs - mod bar; - - //- /bar.rs - mod foo<|>; - - //- /bar/foo.rs - // emtpy - ", - ); - let new_name = "foo2"; - let source_change = analysis.rename(position, new_name).unwrap(); - assert_debug_snapshot_matches!("rename_mod", &source_change); - } - - #[test] - fn test_rename_mod_in_dir() { - let (analysis, position) = analysis_and_position( - " - //- /lib.rs - mod fo<|>o; - //- /foo/mod.rs - // emtpy - ", - ); - let new_name = "foo2"; - let source_change = analysis.rename(position, new_name).unwrap(); - assert_debug_snapshot_matches!("rename_mod_in_dir", &source_change); - } - - fn test_rename(text: &str, new_name: &str, expected: &str) { - let (analysis, position) = single_file_with_position(text); - let source_change = analysis.rename(position, new_name).unwrap(); - let mut text_edit_bulder = ra_text_edit::TextEditBuilder::default(); - let mut file_id: Option = None; - if let Some(change) = source_change { - for edit in change.source_file_edits { - file_id = Some(edit.file_id); - for atom in edit.edit.as_atoms() { - text_edit_bulder.replace(atom.delete, atom.insert.clone()); - } - } - } - let result = text_edit_bulder - .finish() - .apply(&*analysis.file_text(file_id.unwrap())); - assert_eq_text!(expected, &*result); - } -} -- cgit v1.2.3 From f5bb7045685a2e050c1b431f4cddd569b517eb77 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 13:55:45 +0300 Subject: avoid 'ignored' in test output --- crates/ra_mbe/src/mbe_expander.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 04b5a4035..fb1066eec 100644 --- a/crates/ra_mbe/src/mbe_expander.rs +++ b/crates/ra_mbe/src/mbe_expander.rs @@ -28,7 +28,7 @@ fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option { /// /// The tricky bit is dealing with repetitions (`$()*`). Consider this example: /// -/// ```ignore +/// ```not_rust /// macro_rules! foo { /// ($($ i:ident $($ e:expr),*);*) => { /// $(fn $ i() { $($ e);*; })* @@ -46,7 +46,7 @@ fn expand_rule(rule: &crate::Rule, input: &tt::Subtree) -> Option { /// /// For the above example, the bindings would store /// -/// ```ignore +/// ```not_rust /// i -> [foo, bar] /// e -> [[1, 2, 3], [4, 5, 6]] /// ``` -- cgit v1.2.3 From 5173c6295b9c48e6990d6fb6467fc35cd0dfc902 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:06:26 +0300 Subject: move find_references to references --- crates/ra_ide_api/src/imp.rs | 54 +----------------------------- crates/ra_ide_api/src/lib.rs | 2 +- crates/ra_ide_api/src/references.rs | 66 +++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 67 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs index dea71740c..435cc7d4b 100644 --- a/crates/ra_ide_api/src/imp.rs +++ b/crates/ra_ide_api/src/imp.rs @@ -2,11 +2,7 @@ use hir::{ self, Problem, source_binder }; use ra_ide_api_light::{self, LocalEdit, Severity}; -use ra_syntax::{ - algo::find_node_at_offset, ast::{self, NameOwner}, AstNode, - SourceFile, - TextRange, -}; +use ra_syntax::ast; use ra_db::SourceDatabase; use crate::{ @@ -16,54 +12,6 @@ use crate::{ }; impl db::RootDatabase { - pub(crate) fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> { - let file = self.parse(position.file_id); - // Find the binding associated with the offset - let (binding, descr) = match find_binding(self, &file, position) { - None => return Vec::new(), - Some(it) => it, - }; - - let mut ret = binding - .name() - .into_iter() - .map(|name| (position.file_id, name.syntax().range())) - .collect::>(); - ret.extend( - descr - .scopes(self) - .find_all_refs(binding) - .into_iter() - .map(|ref_desc| (position.file_id, ref_desc.range)), - ); - - return ret; - - fn find_binding<'a>( - db: &db::RootDatabase, - source_file: &'a SourceFile, - position: FilePosition, - ) -> Option<(&'a ast::BindPat, hir::Function)> { - let syntax = source_file.syntax(); - if let Some(binding) = find_node_at_offset::(syntax, position.offset) { - let descr = source_binder::function_from_child_node( - db, - position.file_id, - binding.syntax(), - )?; - return Some((binding, descr)); - }; - let name_ref = find_node_at_offset::(syntax, position.offset)?; - let descr = - source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; - let scope = descr.scopes(db); - let resolved = scope.resolve_local_name(name_ref)?; - let resolved = resolved.ptr().to_node(source_file); - let binding = find_node_at_offset::(syntax, resolved.range().end())?; - Some((binding, descr)) - } - } - pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { let syntax = self.parse(file_id); diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 0bb21245d..f5c1aa036 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -322,7 +322,7 @@ impl Analysis { /// Finds all usages of the reference at point. pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { - self.with_db(|db| db.find_all_refs(position)) + self.with_db(|db| references::find_all_refs(db, position)) } /// Returns a short text descrbing element at position. diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 1c9491a0a..b129f3134 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -1,13 +1,10 @@ -use relative_path::RelativePathBuf; - -use hir::{ - self, ModuleSource, source_binder::module_from_declaration, -}; +use relative_path::{RelativePath, RelativePathBuf}; +use hir::{ModuleSource, source_binder}; +use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ + AstNode, SyntaxNode, TextRange, SourceFile, + ast::{self, NameOwner}, algo::find_node_at_offset, - ast, - AstNode, - SyntaxNode }; use crate::{ @@ -17,8 +14,51 @@ use crate::{ SourceChange, SourceFileEdit, }; -use ra_db::SourceDatabase; -use relative_path::RelativePath; + +pub(crate) fn find_all_refs(db: &RootDatabase, position: FilePosition) -> Vec<(FileId, TextRange)> { + let file = db.parse(position.file_id); + // Find the binding associated with the offset + let (binding, descr) = match find_binding(db, &file, position) { + None => return Vec::new(), + Some(it) => it, + }; + + let mut ret = binding + .name() + .into_iter() + .map(|name| (position.file_id, name.syntax().range())) + .collect::>(); + ret.extend( + descr + .scopes(db) + .find_all_refs(binding) + .into_iter() + .map(|ref_desc| (position.file_id, ref_desc.range)), + ); + + return ret; + + fn find_binding<'a>( + db: &RootDatabase, + source_file: &'a SourceFile, + position: FilePosition, + ) -> Option<(&'a ast::BindPat, hir::Function)> { + let syntax = source_file.syntax(); + if let Some(binding) = find_node_at_offset::(syntax, position.offset) { + let descr = + source_binder::function_from_child_node(db, position.file_id, binding.syntax())?; + return Some((binding, descr)); + }; + let name_ref = find_node_at_offset::(syntax, position.offset)?; + let descr = + source_binder::function_from_child_node(db, position.file_id, name_ref.syntax())?; + let scope = descr.scopes(db); + let resolved = scope.resolve_local_name(name_ref)?; + let resolved = resolved.ptr().to_node(source_file); + let binding = find_node_at_offset::(syntax, resolved.range().end())?; + Some((binding, descr)) + } +} pub(crate) fn rename( db: &RootDatabase, @@ -57,7 +97,8 @@ fn rename_mod( ) -> Option { let mut source_file_edits = Vec::new(); let mut file_system_edits = Vec::new(); - if let Some(module) = module_from_declaration(db, position.file_id, &ast_module) { + if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module) + { let (file_id, module_source) = module.definition_source(db); match module_source { ModuleSource::SourceFile(..) => { @@ -108,8 +149,7 @@ fn rename_reference( position: FilePosition, new_name: &str, ) -> Option { - let edit = db - .find_all_refs(position) + let edit = find_all_refs(db, position) .iter() .map(|(file_id, text_range)| SourceFileEdit { file_id: *file_id, -- cgit v1.2.3 From e4a6343e47a7dc87192b762b3b2ebd100240d194 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:09:57 +0300 Subject: move index_resolve to symbol index --- crates/ra_ide_api/src/call_info.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 3 +-- crates/ra_ide_api/src/imp.rs | 12 +----------- crates/ra_ide_api/src/symbol_index.rs | 8 ++++++++ 4 files changed, 11 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 2eb388e0e..a59ab7853 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -20,7 +20,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option Vec { - let name = name_ref.text(); - let mut query = Query::new(name.to_string()); - query.exact(); - query.limit(4); - crate::symbol_index::world_symbols(self, query) - } } impl SourceChange { diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index 9f939c650..3d0b2369e 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs @@ -109,6 +109,14 @@ pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Vec query.search(&buf) } +pub(crate) fn index_resolve(db: &RootDatabase, name_ref: &ast::NameRef) -> Vec { + let name = name_ref.text(); + let mut query = Query::new(name.to_string()); + query.exact(); + query.limit(4); + crate::symbol_index::world_symbols(db, query) +} + #[derive(Default, Debug)] pub(crate) struct SymbolIndex { symbols: Vec, -- cgit v1.2.3 From 8328e196dd093f85e51420fa27d9d9bcdf65e866 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:24:39 +0300 Subject: move diagnostics to a separate file --- crates/ra_ide_api/src/diagnostics.rs | 71 +++++++++++++++++++++++++++ crates/ra_ide_api/src/imp.rs | 93 ------------------------------------ crates/ra_ide_api/src/lib.rs | 21 +++++++- 3 files changed, 90 insertions(+), 95 deletions(-) create mode 100644 crates/ra_ide_api/src/diagnostics.rs delete mode 100644 crates/ra_ide_api/src/imp.rs (limited to 'crates') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs new file mode 100644 index 000000000..c6ca91af8 --- /dev/null +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -0,0 +1,71 @@ +use hir::{Problem, source_binder}; +use ra_ide_api_light::Severity; +use ra_db::SourceDatabase; + +use crate::{db, Diagnostic, FileId, FileSystemEdit, SourceChange}; + +impl db::RootDatabase { + pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { + let syntax = self.parse(file_id); + + let mut res = ra_ide_api_light::diagnostics(&syntax) + .into_iter() + .map(|d| Diagnostic { + range: d.range, + message: d.msg, + severity: d.severity, + fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), + }) + .collect::>(); + if let Some(m) = source_binder::module_from_file_id(self, file_id) { + for (name_node, problem) in m.problems(self) { + let source_root = self.file_source_root(file_id); + let diag = match problem { + Problem::UnresolvedModule { candidate } => { + let create_file = FileSystemEdit::CreateFile { + source_root, + path: candidate.clone(), + }; + let fix = SourceChange { + label: "create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "unresolved module".to_string(), + severity: Severity::Error, + fix: Some(fix), + } + } + Problem::NotDirOwner { move_to, candidate } => { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: source_root, + dst_path: move_to.clone(), + }; + let create_file = FileSystemEdit::CreateFile { + source_root, + path: move_to.join(candidate), + }; + let fix = SourceChange { + label: "move file and create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![move_file, create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "can't declare module at this location".to_string(), + severity: Severity::Error, + fix: Some(fix), + } + } + }; + res.push(diag) + } + }; + res + } +} diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs deleted file mode 100644 index a351c9373..000000000 --- a/crates/ra_ide_api/src/imp.rs +++ /dev/null @@ -1,93 +0,0 @@ -use hir::{ - self, Problem, source_binder -}; -use ra_ide_api_light::{self, LocalEdit, Severity}; -use ra_db::SourceDatabase; - -use crate::{ - db, Diagnostic, FileId, FilePosition, FileSystemEdit, - SourceChange, SourceFileEdit, -}; - -impl db::RootDatabase { - pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { - let syntax = self.parse(file_id); - - let mut res = ra_ide_api_light::diagnostics(&syntax) - .into_iter() - .map(|d| Diagnostic { - range: d.range, - message: d.msg, - severity: d.severity, - fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), - }) - .collect::>(); - if let Some(m) = source_binder::module_from_file_id(self, file_id) { - for (name_node, problem) in m.problems(self) { - let source_root = self.file_source_root(file_id); - let diag = match problem { - Problem::UnresolvedModule { candidate } => { - let create_file = FileSystemEdit::CreateFile { - source_root, - path: candidate.clone(), - }; - let fix = SourceChange { - label: "create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "unresolved module".to_string(), - severity: Severity::Error, - fix: Some(fix), - } - } - Problem::NotDirOwner { move_to, candidate } => { - let move_file = FileSystemEdit::MoveFile { - src: file_id, - dst_source_root: source_root, - dst_path: move_to.clone(), - }; - let create_file = FileSystemEdit::CreateFile { - source_root, - path: move_to.join(candidate), - }; - let fix = SourceChange { - label: "move file and create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![move_file, create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "can't declare module at this location".to_string(), - severity: Severity::Error, - fix: Some(fix), - } - } - }; - res.push(diag) - } - }; - res - } -} - -impl SourceChange { - pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange { - let file_edit = SourceFileEdit { - file_id, - edit: edit.edit, - }; - SourceChange { - label: edit.label, - source_file_edits: vec![file_edit], - file_system_edits: vec![], - cursor_position: edit - .cursor_position - .map(|offset| FilePosition { offset, file_id }), - } - } -} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index f5c1aa036..312b11a82 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -14,7 +14,6 @@ #![recursion_limit = "128"] mod db; -mod imp; pub mod mock_analysis; mod symbol_index; mod navigation_target; @@ -32,6 +31,7 @@ mod parent_module; mod references; mod impls; mod assists; +mod diagnostics; #[cfg(test)] mod marks; @@ -58,7 +58,7 @@ pub use crate::{ change::{AnalysisChange, LibraryData}, }; pub use ra_ide_api_light::{ - Fold, FoldKind, HighlightedRange, Severity, StructureNode, + Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit, LineIndex, LineCol, translate_offset_with_edit, }; pub use ra_db::{ @@ -399,6 +399,23 @@ impl Analysis { } } +impl SourceChange { + pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange { + let file_edit = SourceFileEdit { + file_id, + edit: edit.edit, + }; + SourceChange { + label: edit.label, + source_file_edits: vec![file_edit], + file_system_edits: vec![], + cursor_position: edit + .cursor_position + .map(|offset| FilePosition { offset, file_id }), + } + } +} + #[test] fn analysis_is_send() { fn is_send() {} -- cgit v1.2.3 From 884f04670aea239f06fe5b6ff7a9f2073034f8bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 8 Feb 2019 14:30:21 +0300 Subject: diagnostics is now a function --- crates/ra_ide_api/src/diagnostics.rs | 122 +++++++++++++++++------------------ crates/ra_ide_api/src/lib.rs | 4 +- 2 files changed, 62 insertions(+), 64 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index c6ca91af8..a499ac7c6 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -2,70 +2,68 @@ use hir::{Problem, source_binder}; use ra_ide_api_light::Severity; use ra_db::SourceDatabase; -use crate::{db, Diagnostic, FileId, FileSystemEdit, SourceChange}; +use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, db::RootDatabase}; -impl db::RootDatabase { - pub(crate) fn diagnostics(&self, file_id: FileId) -> Vec { - let syntax = self.parse(file_id); +pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec { + let syntax = db.parse(file_id); - let mut res = ra_ide_api_light::diagnostics(&syntax) - .into_iter() - .map(|d| Diagnostic { - range: d.range, - message: d.msg, - severity: d.severity, - fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), - }) - .collect::>(); - if let Some(m) = source_binder::module_from_file_id(self, file_id) { - for (name_node, problem) in m.problems(self) { - let source_root = self.file_source_root(file_id); - let diag = match problem { - Problem::UnresolvedModule { candidate } => { - let create_file = FileSystemEdit::CreateFile { - source_root, - path: candidate.clone(), - }; - let fix = SourceChange { - label: "create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "unresolved module".to_string(), - severity: Severity::Error, - fix: Some(fix), - } + let mut res = ra_ide_api_light::diagnostics(&syntax) + .into_iter() + .map(|d| Diagnostic { + range: d.range, + message: d.msg, + severity: d.severity, + fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), + }) + .collect::>(); + if let Some(m) = source_binder::module_from_file_id(db, file_id) { + for (name_node, problem) in m.problems(db) { + let source_root = db.file_source_root(file_id); + let diag = match problem { + Problem::UnresolvedModule { candidate } => { + let create_file = FileSystemEdit::CreateFile { + source_root, + path: candidate.clone(), + }; + let fix = SourceChange { + label: "create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "unresolved module".to_string(), + severity: Severity::Error, + fix: Some(fix), } - Problem::NotDirOwner { move_to, candidate } => { - let move_file = FileSystemEdit::MoveFile { - src: file_id, - dst_source_root: source_root, - dst_path: move_to.clone(), - }; - let create_file = FileSystemEdit::CreateFile { - source_root, - path: move_to.join(candidate), - }; - let fix = SourceChange { - label: "move file and create module".to_string(), - source_file_edits: Vec::new(), - file_system_edits: vec![move_file, create_file], - cursor_position: None, - }; - Diagnostic { - range: name_node.range(), - message: "can't declare module at this location".to_string(), - severity: Severity::Error, - fix: Some(fix), - } + } + Problem::NotDirOwner { move_to, candidate } => { + let move_file = FileSystemEdit::MoveFile { + src: file_id, + dst_source_root: source_root, + dst_path: move_to.clone(), + }; + let create_file = FileSystemEdit::CreateFile { + source_root, + path: move_to.join(candidate), + }; + let fix = SourceChange { + label: "move file and create module".to_string(), + source_file_edits: Vec::new(), + file_system_edits: vec![move_file, create_file], + cursor_position: None, + }; + Diagnostic { + range: name_node.range(), + message: "can't declare module at this location".to_string(), + severity: Severity::Error, + fix: Some(fix), } - }; - res.push(diag) - } - }; - res - } + } + }; + res.push(diag) + } + }; + res } diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 312b11a82..1f43b7623 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -52,10 +52,10 @@ use crate::{ }; pub use crate::{ + change::{AnalysisChange, LibraryData}, completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, runnables::{Runnable, RunnableKind}, navigation_target::NavigationTarget, - change::{AnalysisChange, LibraryData}, }; pub use ra_ide_api_light::{ Fold, FoldKind, HighlightedRange, Severity, StructureNode, LocalEdit, @@ -373,7 +373,7 @@ impl Analysis { /// Computes the set of diagnostics for the given file. pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { - self.with_db(|db| db.diagnostics(file_id)) + self.with_db(|db| diagnostics::diagnostics(db, file_id)) } /// Computes the type of the expression at the given position. -- cgit v1.2.3