aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/references/search_scope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/references/search_scope.rs')
-rw-r--r--crates/ra_ide_api/src/references/search_scope.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs
new file mode 100644
index 000000000..557ee7b57
--- /dev/null
+++ b/crates/ra_ide_api/src/references/search_scope.rs
@@ -0,0 +1,61 @@
1use hir::{DefWithBody, HasSource, ModuleSource};
2use ra_db::{FileId, SourceDatabase};
3use ra_syntax::{AstNode, TextRange};
4
5use crate::db::RootDatabase;
6
7use super::{Definition, NameKind};
8
9pub(crate) struct SearchScope {
10 pub scope: Vec<(FileId, Option<TextRange>)>,
11}
12
13impl Definition {
14 pub fn scope(&self, db: &RootDatabase) -> SearchScope {
15 let module_src = self.container.definition_source(db);
16 let file_id = module_src.file_id.original_file(db);
17
18 if let NameKind::Pat((def, _)) = self.item {
19 let range = match def {
20 DefWithBody::Function(f) => f.source(db).ast.syntax().text_range(),
21 DefWithBody::Const(c) => c.source(db).ast.syntax().text_range(),
22 DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(),
23 };
24 return SearchScope { scope: vec![(file_id, Some(range))] };
25 }
26
27 if let Some(ref vis) = self.visibility {
28 let source_root_id = db.file_source_root(file_id);
29 let source_root = db.source_root(source_root_id);
30 let mut files = source_root.walk().map(|id| (id.into(), None)).collect::<Vec<_>>();
31
32 if vis.syntax().to_string().as_str() == "pub(crate)" {
33 return SearchScope { scope: files };
34 }
35 if vis.syntax().to_string().as_str() == "pub" {
36 let krate = self.container.krate(db).unwrap();
37 let crate_graph = db.crate_graph();
38
39 for crate_id in crate_graph.iter() {
40 let mut crate_deps = crate_graph.dependencies(crate_id);
41
42 if crate_deps.any(|dep| dep.crate_id() == krate.crate_id()) {
43 let root_file = crate_graph.crate_root(crate_id);
44 let source_root_id = db.file_source_root(root_file);
45 let source_root = db.source_root(source_root_id);
46 files.extend(source_root.walk().map(|id| (id.into(), None)));
47 }
48 }
49
50 return SearchScope { scope: files };
51 }
52 // FIXME: "pub(super)", "pub(in path)"
53 }
54
55 let range = match module_src.ast {
56 ModuleSource::Module(m) => Some(m.syntax().text_range()),
57 ModuleSource::SourceFile(_) => None,
58 };
59 SearchScope { scope: vec![(file_id, range)] }
60 }
61}