mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Cleanup API
This commit is contained in:
parent
072ec1a8ae
commit
98d68fa6be
4 changed files with 51 additions and 53 deletions
|
@ -68,9 +68,7 @@ pub use crate::{
|
||||||
folding_ranges::{Fold, FoldKind},
|
folding_ranges::{Fold, FoldKind},
|
||||||
hover::HoverResult,
|
hover::HoverResult,
|
||||||
inlay_hints::{InlayHint, InlayKind},
|
inlay_hints::{InlayHint, InlayKind},
|
||||||
references::{
|
references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult},
|
||||||
Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope,
|
|
||||||
},
|
|
||||||
runnables::{Runnable, RunnableKind, TestId},
|
runnables::{Runnable, RunnableKind, TestId},
|
||||||
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
|
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
|
||||||
ssr::SsrError,
|
ssr::SsrError,
|
||||||
|
@ -88,6 +86,7 @@ pub use ra_ide_db::{
|
||||||
feature_flags::FeatureFlags,
|
feature_flags::FeatureFlags,
|
||||||
line_index::{LineCol, LineIndex},
|
line_index::{LineCol, LineIndex},
|
||||||
line_index_utils::translate_offset_with_edit,
|
line_index_utils::translate_offset_with_edit,
|
||||||
|
search::SearchScope,
|
||||||
symbol_index::Query,
|
symbol_index::Query,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,11 +10,11 @@
|
||||||
//! resolved to the search element definition, we get a reference.
|
//! resolved to the search element definition, we get a reference.
|
||||||
|
|
||||||
mod rename;
|
mod rename;
|
||||||
mod search_scope;
|
|
||||||
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ra_ide_db::{
|
use ra_ide_db::{
|
||||||
defs::{classify_name, classify_name_ref, Definition},
|
defs::{classify_name, classify_name_ref, Definition},
|
||||||
|
search::SearchScope,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
|
@ -28,7 +28,7 @@ use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeI
|
||||||
|
|
||||||
pub(crate) use self::rename::rename;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ReferenceSearchResult {
|
pub struct ReferenceSearchResult {
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
|
@ -55,16 +55,58 @@ impl SearchScope {
|
||||||
SearchScope::new(std::iter::once((file, None)).collect())
|
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 _p = profile("search_scope");
|
||||||
let module = match def.module(db) {
|
let module = match self.module(db) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return SearchScope::empty(),
|
None => return SearchScope::empty(),
|
||||||
};
|
};
|
||||||
let module_src = module.definition_source(db);
|
let module_src = module.definition_source(db);
|
||||||
let file_id = module_src.file_id.original_file(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) {
|
let range = match var.parent(db) {
|
||||||
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
|
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
|
||||||
DefWithBody::Const(c) => c.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);
|
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 vis.as_str() == "pub(super)" {
|
||||||
if let Some(parent_module) = module.parent(db) {
|
if let Some(parent_module) = module.parent(db) {
|
||||||
|
@ -131,48 +173,6 @@ impl SearchScope {
|
||||||
SearchScope::new(res)
|
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(
|
pub fn find_usages(
|
||||||
&self,
|
&self,
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
|
@ -181,7 +181,7 @@ impl Definition {
|
||||||
let _p = profile("Definition::find_usages");
|
let _p = profile("Definition::find_usages");
|
||||||
|
|
||||||
let search_scope = {
|
let search_scope = {
|
||||||
let base = SearchScope::for_def(self, db);
|
let base = self.search_scope(db);
|
||||||
match search_scope {
|
match search_scope {
|
||||||
None => base,
|
None => base,
|
||||||
Some(scope) => base.intersection(&scope),
|
Some(scope) => base.intersection(&scope),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue