Cleanup API

This commit is contained in:
Aleksey Kladov 2020-03-04 12:46:40 +01:00
parent 072ec1a8ae
commit 98d68fa6be
4 changed files with 51 additions and 53 deletions

View file

@ -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,
};

View file

@ -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 {

View file

@ -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<TextRange>,
r2: Option<TextRange>,
) -> Option<Option<TextRange>> {
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<TextRange>);
type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
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<TextRange>,
r2: Option<TextRange>,
) -> Option<Option<TextRange>> {
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<TextRange>);
type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
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),