aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/references
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-24 11:41:25 +0100
committerAleksey Kladov <[email protected]>2019-10-24 11:41:25 +0100
commit6d186ec3beff14bc161d1c7b7c98b412c4f833d9 (patch)
tree948bfd9f761a16d8843ca7e4701f543cd00117f9 /crates/ra_ide_api/src/references
parent018b621f613e87af26d8f7880ea1012cb374216c (diff)
add search scope stuct
Diffstat (limited to 'crates/ra_ide_api/src/references')
-rw-r--r--crates/ra_ide_api/src/references/search_scope.rs43
1 files changed, 30 insertions, 13 deletions
diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs
index 21e667c83..1c4fb742f 100644
--- a/crates/ra_ide_api/src/references/search_scope.rs
+++ b/crates/ra_ide_api/src/references/search_scope.rs
@@ -13,8 +13,26 @@ use crate::db::RootDatabase;
13 13
14use super::{NameDefinition, NameKind}; 14use super::{NameDefinition, NameKind};
15 15
16pub struct SearchScope {
17 entries: FxHashSet<(FileId, Option<TextRange>)>,
18}
19
20impl SearchScope {
21 fn new(entries: FxHashSet<(FileId, Option<TextRange>)>) -> SearchScope {
22 SearchScope { entries }
23 }
24}
25
26impl IntoIterator for SearchScope {
27 type Item = (FileId, Option<TextRange>);
28 type IntoIter = std::collections::hash_set::IntoIter<Self::Item>;
29 fn into_iter(self) -> Self::IntoIter {
30 self.entries.into_iter()
31 }
32}
33
16impl NameDefinition { 34impl NameDefinition {
17 pub(crate) fn search_scope(&self, db: &RootDatabase) -> FxHashSet<(FileId, Option<TextRange>)> { 35 pub(crate) fn search_scope(&self, db: &RootDatabase) -> SearchScope {
18 let _p = profile("search_scope"); 36 let _p = profile("search_scope");
19 37
20 let module_src = self.container.definition_source(db); 38 let module_src = self.container.definition_source(db);
@@ -28,7 +46,7 @@ impl NameDefinition {
28 DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(), 46 DefWithBody::Static(s) => s.source(db).ast.syntax().text_range(),
29 }; 47 };
30 res.insert((file_id, Some(range))); 48 res.insert((file_id, Some(range)));
31 return res; 49 return SearchScope::new(res);
32 } 50 }
33 51
34 let vis = 52 let vis =
@@ -36,37 +54,36 @@ impl NameDefinition {
36 54
37 if vis.as_str() == "pub(super)" { 55 if vis.as_str() == "pub(super)" {
38 if let Some(parent_module) = self.container.parent(db) { 56 if let Some(parent_module) = self.container.parent(db) {
39 let mut files = FxHashSet::default(); 57 let mut res = FxHashSet::default();
40 let parent_src = parent_module.definition_source(db); 58 let parent_src = parent_module.definition_source(db);
41 let file_id = parent_src.file_id.original_file(db); 59 let file_id = parent_src.file_id.original_file(db);
42 60
43 match parent_src.ast { 61 match parent_src.ast {
44 ModuleSource::Module(m) => { 62 ModuleSource::Module(m) => {
45 let range = Some(m.syntax().text_range()); 63 let range = Some(m.syntax().text_range());
46 files.insert((file_id, range)); 64 res.insert((file_id, range));
47 } 65 }
48 ModuleSource::SourceFile(_) => { 66 ModuleSource::SourceFile(_) => {
49 files.insert((file_id, None)); 67 res.insert((file_id, None));
50 files.extend(parent_module.children(db).map(|m| { 68 res.extend(parent_module.children(db).map(|m| {
51 let src = m.definition_source(db); 69 let src = m.definition_source(db);
52 (src.file_id.original_file(db), None) 70 (src.file_id.original_file(db), None)
53 })); 71 }));
54 } 72 }
55 } 73 }
56 return files; 74 return SearchScope::new(res);
57 } 75 }
58 } 76 }
59 77
60 if vis.as_str() != "" { 78 if vis.as_str() != "" {
61 let source_root_id = db.file_source_root(file_id); 79 let source_root_id = db.file_source_root(file_id);
62 let source_root = db.source_root(source_root_id); 80 let source_root = db.source_root(source_root_id);
63 let mut files = 81 let mut res = source_root.walk().map(|id| (id.into(), None)).collect::<FxHashSet<_>>();
64 source_root.walk().map(|id| (id.into(), None)).collect::<FxHashSet<_>>();
65 82
66 // FIXME: add "pub(in path)" 83 // FIXME: add "pub(in path)"
67 84
68 if vis.as_str() == "pub(crate)" { 85 if vis.as_str() == "pub(crate)" {
69 return files; 86 return SearchScope::new(res);
70 } 87 }
71 if vis.as_str() == "pub" { 88 if vis.as_str() == "pub" {
72 let krate = self.container.krate(db).unwrap(); 89 let krate = self.container.krate(db).unwrap();
@@ -77,10 +94,10 @@ impl NameDefinition {
77 let root_file = crate_graph.crate_root(crate_id); 94 let root_file = crate_graph.crate_root(crate_id);
78 let source_root_id = db.file_source_root(root_file); 95 let source_root_id = db.file_source_root(root_file);
79 let source_root = db.source_root(source_root_id); 96 let source_root = db.source_root(source_root_id);
80 files.extend(source_root.walk().map(|id| (id.into(), None))); 97 res.extend(source_root.walk().map(|id| (id.into(), None)));
81 } 98 }
82 } 99 }
83 return files; 100 return SearchScope::new(res);
84 } 101 }
85 } 102 }
86 103
@@ -90,6 +107,6 @@ impl NameDefinition {
90 ModuleSource::SourceFile(_) => None, 107 ModuleSource::SourceFile(_) => None,
91 }; 108 };
92 res.insert((file_id, range)); 109 res.insert((file_id, range));
93 res 110 SearchScope::new(res)
94 } 111 }
95} 112}