From 4529da906db7f18aaf384c079332e4ea12c82d55 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 24 Oct 2019 14:01:02 +0300 Subject: for highlighting, search only the current file --- crates/ra_ide_api/src/references/rename.rs | 6 +-- crates/ra_ide_api/src/references/search_scope.rs | 58 +++++++++++++++++++----- 2 files changed, 49 insertions(+), 15 deletions(-) (limited to 'crates/ra_ide_api/src/references') diff --git a/crates/ra_ide_api/src/references/rename.rs b/crates/ra_ide_api/src/references/rename.rs index 0e2e088e0..ee6e73e1b 100644 --- a/crates/ra_ide_api/src/references/rename.rs +++ b/crates/ra_ide_api/src/references/rename.rs @@ -110,7 +110,7 @@ fn rename_reference( position: FilePosition, new_name: &str, ) -> Option> { - let RangeInfo { range, info: refs } = find_all_refs(db, position)?; + let RangeInfo { range, info: refs } = find_all_refs(db, position, None)?; let edit = refs .into_iter() @@ -255,13 +255,13 @@ mod tests { "#; let (analysis, pos) = analysis_and_position(code); - let refs = analysis.find_all_refs(pos).unwrap().unwrap(); + let refs = analysis.find_all_refs(pos, None).unwrap().unwrap(); assert_eq!(refs.len(), 3); } fn get_all_refs(text: &str) -> ReferenceSearchResult { let (analysis, position) = single_file_with_position(text); - analysis.find_all_refs(position).unwrap().unwrap() + analysis.find_all_refs(position, None).unwrap().unwrap() } #[test] diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs index 1c4fb742f..b6eb248b7 100644 --- a/crates/ra_ide_api/src/references/search_scope.rs +++ b/crates/ra_ide_api/src/references/search_scope.rs @@ -2,30 +2,63 @@ //! For `pub(crate)` things it's a crate, for `pub` things it's a crate and dependant crates. //! In some cases, the location of the references is known to within a `TextRange`, //! e.g. for things like local variables. +use std::mem; use hir::{DefWithBody, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase, SourceDatabaseExt}; use ra_prof::profile; use ra_syntax::{AstNode, TextRange}; -use rustc_hash::FxHashSet; +use rustc_hash::FxHashMap; use crate::db::RootDatabase; use super::{NameDefinition, NameKind}; pub struct SearchScope { - entries: FxHashSet<(FileId, Option)>, + entries: FxHashMap>, } impl SearchScope { - fn new(entries: FxHashSet<(FileId, Option)>) -> SearchScope { + fn new(entries: FxHashMap>) -> SearchScope { SearchScope { entries } } + pub fn single_file(file: FileId) -> SearchScope { + SearchScope::new(std::iter::once((file, None)).collect()) + } + pub(crate) fn intersection(&self, other: &SearchScope) -> SearchScope { + let (mut small, mut large) = (&self.entries, &other.entries); + if small.len() > large.len() { + mem::swap(&mut small, &mut large) + } + + let res = small + .iter() + .filter_map(|(file_id, r1)| { + let r2 = large.get(file_id)?; + let r = intersect_ranges(*r1, *r2)?; + Some((*file_id, r)) + }) + .collect(); + return SearchScope::new(res); + + fn intersect_ranges( + r1: Option, + r2: Option, + ) -> Option> { + match (r1, r2) { + (None, r) | (r, None) => Some(r), + (Some(r1), Some(r2)) => { + let r = r1.intersection(&r2)?; + Some(Some(r)) + } + } + } + } } impl IntoIterator for SearchScope { type Item = (FileId, Option); - type IntoIter = std::collections::hash_set::IntoIter; + type IntoIter = std::collections::hash_map::IntoIter>; fn into_iter(self) -> Self::IntoIter { self.entries.into_iter() } @@ -39,13 +72,13 @@ impl NameDefinition { let file_id = module_src.file_id.original_file(db); if let NameKind::Pat((def, _)) = self.kind { - let mut res = FxHashSet::default(); + let mut res = FxHashMap::default(); let range = match def { DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(), DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(), DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), }; - res.insert((file_id, Some(range))); + res.insert(file_id, Some(range)); return SearchScope::new(res); } @@ -54,17 +87,17 @@ impl NameDefinition { if vis.as_str() == "pub(super)" { if let Some(parent_module) = self.container.parent(db) { - let mut res = FxHashSet::default(); + let mut res = FxHashMap::default(); let parent_src = parent_module.definition_source(db); let file_id = parent_src.file_id.original_file(db); match parent_src.ast { ModuleSource::Module(m) => { let range = Some(m.syntax().text_range()); - res.insert((file_id, range)); + res.insert(file_id, range); } ModuleSource::SourceFile(_) => { - res.insert((file_id, None)); + res.insert(file_id, None); res.extend(parent_module.children(db).map(|m| { let src = m.definition_source(db); (src.file_id.original_file(db), None) @@ -78,7 +111,8 @@ impl NameDefinition { if vis.as_str() != "" { let source_root_id = db.file_source_root(file_id); let source_root = db.source_root(source_root_id); - let mut res = source_root.walk().map(|id| (id.into(), None)).collect::>(); + let mut res = + source_root.walk().map(|id| (id.into(), None)).collect::>(); // FIXME: add "pub(in path)" @@ -101,12 +135,12 @@ impl NameDefinition { } } - let mut res = FxHashSet::default(); + let mut res = FxHashMap::default(); let range = match module_src.ast { ModuleSource::Module(m) => Some(m.syntax().text_range()), ModuleSource::SourceFile(_) => None, }; - res.insert((file_id, range)); + res.insert(file_id, range); SearchScope::new(res) } } -- cgit v1.2.3