mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
add search scope stuct
This commit is contained in:
parent
018b621f61
commit
6d186ec3be
1 changed files with 30 additions and 13 deletions
|
@ -13,8 +13,26 @@ use crate::db::RootDatabase;
|
||||||
|
|
||||||
use super::{NameDefinition, NameKind};
|
use super::{NameDefinition, NameKind};
|
||||||
|
|
||||||
|
pub struct SearchScope {
|
||||||
|
entries: FxHashSet<(FileId, Option<TextRange>)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SearchScope {
|
||||||
|
fn new(entries: FxHashSet<(FileId, Option<TextRange>)>) -> SearchScope {
|
||||||
|
SearchScope { entries }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoIterator for SearchScope {
|
||||||
|
type Item = (FileId, Option<TextRange>);
|
||||||
|
type IntoIter = std::collections::hash_set::IntoIter<Self::Item>;
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.entries.into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl NameDefinition {
|
impl NameDefinition {
|
||||||
pub(crate) fn search_scope(&self, db: &RootDatabase) -> FxHashSet<(FileId, Option<TextRange>)> {
|
pub(crate) fn search_scope(&self, db: &RootDatabase) -> SearchScope {
|
||||||
let _p = profile("search_scope");
|
let _p = profile("search_scope");
|
||||||
|
|
||||||
let module_src = self.container.definition_source(db);
|
let module_src = self.container.definition_source(db);
|
||||||
|
@ -28,7 +46,7 @@ impl NameDefinition {
|
||||||
DefWithBody::Static(s) => s.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 res;
|
return SearchScope::new(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
let vis =
|
let vis =
|
||||||
|
@ -36,37 +54,36 @@ impl NameDefinition {
|
||||||
|
|
||||||
if vis.as_str() == "pub(super)" {
|
if vis.as_str() == "pub(super)" {
|
||||||
if let Some(parent_module) = self.container.parent(db) {
|
if let Some(parent_module) = self.container.parent(db) {
|
||||||
let mut files = FxHashSet::default();
|
let mut res = FxHashSet::default();
|
||||||
let parent_src = parent_module.definition_source(db);
|
let parent_src = parent_module.definition_source(db);
|
||||||
let file_id = parent_src.file_id.original_file(db);
|
let file_id = parent_src.file_id.original_file(db);
|
||||||
|
|
||||||
match parent_src.ast {
|
match parent_src.ast {
|
||||||
ModuleSource::Module(m) => {
|
ModuleSource::Module(m) => {
|
||||||
let range = Some(m.syntax().text_range());
|
let range = Some(m.syntax().text_range());
|
||||||
files.insert((file_id, range));
|
res.insert((file_id, range));
|
||||||
}
|
}
|
||||||
ModuleSource::SourceFile(_) => {
|
ModuleSource::SourceFile(_) => {
|
||||||
files.insert((file_id, None));
|
res.insert((file_id, None));
|
||||||
files.extend(parent_module.children(db).map(|m| {
|
res.extend(parent_module.children(db).map(|m| {
|
||||||
let src = m.definition_source(db);
|
let src = m.definition_source(db);
|
||||||
(src.file_id.original_file(db), None)
|
(src.file_id.original_file(db), None)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return files;
|
return SearchScope::new(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if vis.as_str() != "" {
|
if vis.as_str() != "" {
|
||||||
let source_root_id = db.file_source_root(file_id);
|
let source_root_id = db.file_source_root(file_id);
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
let mut files =
|
let mut res = source_root.walk().map(|id| (id.into(), None)).collect::<FxHashSet<_>>();
|
||||||
source_root.walk().map(|id| (id.into(), None)).collect::<FxHashSet<_>>();
|
|
||||||
|
|
||||||
// FIXME: add "pub(in path)"
|
// FIXME: add "pub(in path)"
|
||||||
|
|
||||||
if vis.as_str() == "pub(crate)" {
|
if vis.as_str() == "pub(crate)" {
|
||||||
return files;
|
return SearchScope::new(res);
|
||||||
}
|
}
|
||||||
if vis.as_str() == "pub" {
|
if vis.as_str() == "pub" {
|
||||||
let krate = self.container.krate(db).unwrap();
|
let krate = self.container.krate(db).unwrap();
|
||||||
|
@ -77,10 +94,10 @@ impl NameDefinition {
|
||||||
let root_file = crate_graph.crate_root(crate_id);
|
let root_file = crate_graph.crate_root(crate_id);
|
||||||
let source_root_id = db.file_source_root(root_file);
|
let source_root_id = db.file_source_root(root_file);
|
||||||
let source_root = db.source_root(source_root_id);
|
let source_root = db.source_root(source_root_id);
|
||||||
files.extend(source_root.walk().map(|id| (id.into(), None)));
|
res.extend(source_root.walk().map(|id| (id.into(), None)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return files;
|
return SearchScope::new(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +107,6 @@ impl NameDefinition {
|
||||||
ModuleSource::SourceFile(_) => None,
|
ModuleSource::SourceFile(_) => None,
|
||||||
};
|
};
|
||||||
res.insert((file_id, range));
|
res.insert((file_id, range));
|
||||||
res
|
SearchScope::new(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue