Skip to content

Commit 98d68fa

Browse files
committed
Cleanup API
1 parent 072ec1a commit 98d68fa

File tree

4 files changed

+51
-53
lines changed

4 files changed

+51
-53
lines changed

crates/ra_ide/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ pub use crate::{
6868
folding_ranges::{Fold, FoldKind},
6969
hover::HoverResult,
7070
inlay_hints::{InlayHint, InlayKind},
71-
references::{
72-
Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult, SearchScope,
73-
},
71+
references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult},
7472
runnables::{Runnable, RunnableKind, TestId},
7573
source_change::{FileSystemEdit, SourceChange, SourceFileEdit},
7674
ssr::SsrError,
@@ -88,6 +86,7 @@ pub use ra_ide_db::{
8886
feature_flags::FeatureFlags,
8987
line_index::{LineCol, LineIndex},
9088
line_index_utils::translate_offset_with_edit,
89+
search::SearchScope,
9190
symbol_index::Query,
9291
RootDatabase,
9392
};

crates/ra_ide/src/references.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
//! resolved to the search element definition, we get a reference.
1111
1212
mod rename;
13-
mod search_scope;
1413

1514
use hir::Semantics;
1615
use ra_ide_db::{
1716
defs::{classify_name, classify_name_ref, Definition},
17+
search::SearchScope,
1818
RootDatabase,
1919
};
2020
use ra_prof::profile;
@@ -28,7 +28,7 @@ use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeI
2828

2929
pub(crate) use self::rename::rename;
3030

31-
pub use ra_ide_db::search::{Reference, ReferenceAccess, ReferenceKind, SearchScope};
31+
pub use ra_ide_db::search::{Reference, ReferenceAccess, ReferenceKind};
3232

3333
#[derive(Debug, Clone)]
3434
pub struct ReferenceSearchResult {

crates/ra_ide/src/references/search_scope.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/ra_ide_db/src/search.rs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,58 @@ impl SearchScope {
5555
SearchScope::new(std::iter::once((file, None)).collect())
5656
}
5757

58-
pub fn for_def(def: &Definition, db: &RootDatabase) -> SearchScope {
58+
pub fn intersection(&self, other: &SearchScope) -> SearchScope {
59+
let (mut small, mut large) = (&self.entries, &other.entries);
60+
if small.len() > large.len() {
61+
mem::swap(&mut small, &mut large)
62+
}
63+
64+
let res = small
65+
.iter()
66+
.filter_map(|(file_id, r1)| {
67+
let r2 = large.get(file_id)?;
68+
let r = intersect_ranges(*r1, *r2)?;
69+
Some((*file_id, r))
70+
})
71+
.collect();
72+
73+
return SearchScope::new(res);
74+
75+
fn intersect_ranges(
76+
r1: Option<TextRange>,
77+
r2: Option<TextRange>,
78+
) -> Option<Option<TextRange>> {
79+
match (r1, r2) {
80+
(None, r) | (r, None) => Some(r),
81+
(Some(r1), Some(r2)) => {
82+
let r = r1.intersection(&r2)?;
83+
Some(Some(r))
84+
}
85+
}
86+
}
87+
}
88+
}
89+
90+
impl IntoIterator for SearchScope {
91+
type Item = (FileId, Option<TextRange>);
92+
type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
93+
94+
fn into_iter(self) -> Self::IntoIter {
95+
self.entries.into_iter()
96+
}
97+
}
98+
99+
impl Definition {
100+
fn search_scope(&self, db: &RootDatabase) -> SearchScope {
59101
let _p = profile("search_scope");
60-
let module = match def.module(db) {
102+
let module = match self.module(db) {
61103
Some(it) => it,
62104
None => return SearchScope::empty(),
63105
};
64106
let module_src = module.definition_source(db);
65107
let file_id = module_src.file_id.original_file(db);
66108

67-
if let Definition::Local(var) = def {
109+
if let Definition::Local(var) = self {
68110
let range = match var.parent(db) {
69111
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
70112
DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
@@ -75,7 +117,7 @@ impl SearchScope {
75117
return SearchScope::new(res);
76118
}
77119

78-
let vis = def.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default();
120+
let vis = self.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default();
79121

80122
if vis.as_str() == "pub(super)" {
81123
if let Some(parent_module) = module.parent(db) {
@@ -131,48 +173,6 @@ impl SearchScope {
131173
SearchScope::new(res)
132174
}
133175

134-
pub fn intersection(&self, other: &SearchScope) -> SearchScope {
135-
let (mut small, mut large) = (&self.entries, &other.entries);
136-
if small.len() > large.len() {
137-
mem::swap(&mut small, &mut large)
138-
}
139-
140-
let res = small
141-
.iter()
142-
.filter_map(|(file_id, r1)| {
143-
let r2 = large.get(file_id)?;
144-
let r = intersect_ranges(*r1, *r2)?;
145-
Some((*file_id, r))
146-
})
147-
.collect();
148-
149-
return SearchScope::new(res);
150-
151-
fn intersect_ranges(
152-
r1: Option<TextRange>,
153-
r2: Option<TextRange>,
154-
) -> Option<Option<TextRange>> {
155-
match (r1, r2) {
156-
(None, r) | (r, None) => Some(r),
157-
(Some(r1), Some(r2)) => {
158-
let r = r1.intersection(&r2)?;
159-
Some(Some(r))
160-
}
161-
}
162-
}
163-
}
164-
}
165-
166-
impl IntoIterator for SearchScope {
167-
type Item = (FileId, Option<TextRange>);
168-
type IntoIter = std::collections::hash_map::IntoIter<FileId, Option<TextRange>>;
169-
170-
fn into_iter(self) -> Self::IntoIter {
171-
self.entries.into_iter()
172-
}
173-
}
174-
175-
impl Definition {
176176
pub fn find_usages(
177177
&self,
178178
db: &RootDatabase,
@@ -181,7 +181,7 @@ impl Definition {
181181
let _p = profile("Definition::find_usages");
182182

183183
let search_scope = {
184-
let base = SearchScope::for_def(self, db);
184+
let base = self.search_scope(db);
185185
match search_scope {
186186
None => base,
187187
Some(scope) => base.intersection(&scope),

0 commit comments

Comments
 (0)