diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 2eb388e0e7..a59ab78535 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, + 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/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs new file mode 100644 index 0000000000..a499ac7c60 --- /dev/null +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -0,0 +1,69 @@ +use hir::{Problem, source_binder}; +use ra_ide_api_light::Severity; +use ra_db::SourceDatabase; + +use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, db::RootDatabase}; + +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(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), + } + } + }; + res.push(diag) + } + }; + res +} diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 681f36623d..69f2d2bf62 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -118,8 +118,7 @@ pub(crate) fn reference_definition( } } // If that fails try the index based approach. - let navs = db - .index_resolve(name_ref) + let navs = crate::symbol_index::index_resolve(db, name_ref) .into_iter() .map(NavigationTarget::from_symbol) .collect(); diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs deleted file mode 100644 index b139efabf6..0000000000 --- a/crates/ra_ide_api/src/imp.rs +++ /dev/null @@ -1,263 +0,0 @@ -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 crate::{ - AnalysisChange, - CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit, - Query, RootChange, SourceChange, SourceFileEdit, - symbol_index::{FileSymbol, SymbolsDatabase}, - status::syntax_tree_stats -}; - -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 { - 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 - 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); - - 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 - } - - pub(crate) fn index_resolve(&self, 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(self, query) - } -} - -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/impls.rs b/crates/ra_ide_api/src/impls.rs index 91fa41f1f4..4fb0541395 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 68d59aae19..1f43b76233 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -14,10 +14,10 @@ #![recursion_limit = "128"] mod db; -mod imp; pub mod mock_analysis; mod symbol_index; mod navigation_target; +mod change; mod status; mod completion; @@ -28,14 +28,15 @@ mod hover; mod call_info; mod syntax_highlighting; mod parent_module; -mod rename; +mod references; mod impls; mod assists; +mod diagnostics; #[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,22 +44,21 @@ 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, }; pub use crate::{ + change::{AnalysisChange, LibraryData}, completion::{CompletionItem, CompletionItemKind, InsertTextFormat}, runnables::{Runnable, RunnableKind}, navigation_target::NavigationTarget, }; 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::{ @@ -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, @@ -431,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. @@ -451,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. @@ -482,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. @@ -497,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>( @@ -508,44 +399,19 @@ 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, +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/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs index e94297fe38..603c3db6ae 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; diff --git a/crates/ra_ide_api/src/rename.rs b/crates/ra_ide_api/src/references.rs similarity index 76% rename from crates/ra_ide_api/src/rename.rs rename to crates/ra_ide_api/src/references.rs index 1c9491a0a3..b129f31349 100644 --- a/crates/ra_ide_api/src/rename.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, diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index 9f939c650c..3d0b2369ed 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, diff --git a/crates/ra_mbe/src/mbe_expander.rs b/crates/ra_mbe/src/mbe_expander.rs index 04b5a40352..fb1066eec6 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]] /// ```