diff options
author | Ekaterina Babshukova <[email protected]> | 2019-10-12 16:47:17 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-10-22 21:47:31 +0100 |
commit | d26d0ada50fd0063c03e28bc2673f9f63fd23d95 (patch) | |
tree | 1292e2a6352b2bf886baed12cc69dab04dd82462 /crates/ra_ide_api/src/references/search_scope.rs | |
parent | 0dd08b8023eba053725d5032149808b8733be263 (diff) |
restructure a bit
Diffstat (limited to 'crates/ra_ide_api/src/references/search_scope.rs')
-rw-r--r-- | crates/ra_ide_api/src/references/search_scope.rs | 61 |
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 @@ | |||
1 | use hir::{DefWithBody, HasSource, ModuleSource}; | ||
2 | use ra_db::{FileId, SourceDatabase}; | ||
3 | use ra_syntax::{AstNode, TextRange}; | ||
4 | |||
5 | use crate::db::RootDatabase; | ||
6 | |||
7 | use super::{Definition, NameKind}; | ||
8 | |||
9 | pub(crate) struct SearchScope { | ||
10 | pub scope: Vec<(FileId, Option<TextRange>)>, | ||
11 | } | ||
12 | |||
13 | impl 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 | } | ||