From 98d68fa6beec87020b89d01c060288794dc948f1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 4 Mar 2020 12:46:40 +0100 Subject: [PATCH] Cleanup API --- crates/ra_ide/src/lib.rs | 5 +- crates/ra_ide/src/references.rs | 4 +- crates/ra_ide/src/references/search_scope.rs | 1 - crates/ra_ide_db/src/search.rs | 94 ++++++++++---------- 4 files changed, 51 insertions(+), 53 deletions(-) delete mode 100644 crates/ra_ide/src/references/search_scope.rs diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index f61028f78c..5a41f702ef 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -68,9 +68,7 @@ pub use crate::{ folding_ranges::{Fold, FoldKind}, hover::HoverResult, inlay_hints::{InlayHint, InlayKind}, - references::{ - Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope, - }, + references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult}, runnables::{Runnable, RunnableKind, TestId}, source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, ssr::SsrError, @@ -88,6 +86,7 @@ pub use ra_ide_db::{ feature_flags::FeatureFlags, line_index::{LineCol, LineIndex}, line_index_utils::translate_offset_with_edit, + search::SearchScope, symbol_index::Query, RootDatabase, }; diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 8eba13e99c..6e2bf4ded9 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -10,11 +10,11 @@ //! resolved to the search element definition, we get a reference. mod rename; -mod search_scope; use hir::Semantics; use ra_ide_db::{ defs::{classify_name, classify_name_ref, Definition}, + search::SearchScope, RootDatabase, }; use ra_prof::profile; @@ -28,7 +28,7 @@ use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeI pub(crate) use self::rename::rename; -pub use ra_ide_db::search::{Reference, ReferenceAccess, ReferenceKind, SearchScope}; +pub use ra_ide_db::search::{Reference, ReferenceAccess, ReferenceKind}; #[derive(Debug, Clone)] pub struct ReferenceSearchResult { diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/crates/ra_ide/src/references/search_scope.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 0963ea2578..7bd99d4cc5 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs @@ -55,16 +55,58 @@ impl SearchScope { SearchScope::new(std::iter::once((file, None)).collect()) } - pub fn for_def(def: &Definition, db: &RootDatabase) -> SearchScope { + pub 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_map::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + self.entries.into_iter() + } +} + +impl Definition { + fn search_scope(&self, db: &RootDatabase) -> SearchScope { let _p = profile("search_scope"); - let module = match def.module(db) { + let module = match self.module(db) { Some(it) => it, None => return SearchScope::empty(), }; let module_src = module.definition_source(db); let file_id = module_src.file_id.original_file(db); - if let Definition::Local(var) = def { + if let Definition::Local(var) = self { let range = match var.parent(db) { DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), @@ -75,7 +117,7 @@ impl SearchScope { return SearchScope::new(res); } - let vis = def.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); + let vis = self.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); if vis.as_str() == "pub(super)" { if let Some(parent_module) = module.parent(db) { @@ -131,48 +173,6 @@ impl SearchScope { SearchScope::new(res) } - pub 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_map::IntoIter>; - - fn into_iter(self) -> Self::IntoIter { - self.entries.into_iter() - } -} - -impl Definition { pub fn find_usages( &self, db: &RootDatabase, @@ -181,7 +181,7 @@ impl Definition { let _p = profile("Definition::find_usages"); let search_scope = { - let base = SearchScope::for_def(self, db); + let base = self.search_scope(db); match search_scope { None => base, Some(scope) => base.intersection(&scope),