aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-10-30 23:10:48 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-10-30 23:10:48 +0000
commit032d15c392e8de7936c8729e03e6229313f3b054 (patch)
tree61cfa26801e456d5fdcd2619238c05bce62a1ac2 /crates/ra_analysis/src/descriptors
parent23cad90fe98dcca7f4b6905d9fd3ed52d2896c8c (diff)
parentfbbee537228538f448a335bb0b2dabec2b3d443e (diff)
Merge #171
171: Query-based module scopes r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/descriptors')
-rw-r--r--crates/ra_analysis/src/descriptors/module/imp.rs14
-rw-r--r--crates/ra_analysis/src/descriptors/module/mod.rs8
-rw-r--r--crates/ra_analysis/src/descriptors/module/scope.rs128
3 files changed, 149 insertions, 1 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs
index aecf6e29a..5fdaad137 100644
--- a/crates/ra_analysis/src/descriptors/module/imp.rs
+++ b/crates/ra_analysis/src/descriptors/module/imp.rs
@@ -13,7 +13,7 @@ use crate::{
13}; 13};
14 14
15use super::{ 15use super::{
16 ModuleData, ModuleTree, ModuleId, LinkId, LinkData, Problem, ModulesDatabase 16 ModuleData, ModuleTree, ModuleId, LinkId, LinkData, Problem, ModulesDatabase, ModuleScope
17}; 17};
18 18
19 19
@@ -35,6 +35,18 @@ pub(super) fn modules(root: ast::Root<'_>) -> impl Iterator<Item = (SmolStr, ast
35 }) 35 })
36} 36}
37 37
38pub(super) fn module_scope(
39 db: &impl ModulesDatabase,
40 source_root_id: SourceRootId,
41 module_id: ModuleId,
42) -> Cancelable<Arc<ModuleScope>> {
43 let tree = db.module_tree(source_root_id)?;
44 let file_id = module_id.file_id(&tree);
45 let syntax = db.file_syntax(file_id);
46 let res = ModuleScope::new(&syntax);
47 Ok(Arc::new(res))
48}
49
38pub(super) fn module_tree( 50pub(super) fn module_tree(
39 db: &impl ModulesDatabase, 51 db: &impl ModulesDatabase,
40 source_root: SourceRootId, 52 source_root: SourceRootId,
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs
index 8968c4afd..9e5d73f94 100644
--- a/crates/ra_analysis/src/descriptors/module/mod.rs
+++ b/crates/ra_analysis/src/descriptors/module/mod.rs
@@ -1,4 +1,5 @@
1mod imp; 1mod imp;
2pub(crate) mod scope;
2 3
3use std::sync::Arc; 4use std::sync::Arc;
4 5
@@ -11,6 +12,8 @@ use crate::{
11 input::SourceRootId, 12 input::SourceRootId,
12}; 13};
13 14
15pub(crate) use self::scope::ModuleScope;
16
14salsa::query_group! { 17salsa::query_group! {
15 pub(crate) trait ModulesDatabase: SyntaxDatabase { 18 pub(crate) trait ModulesDatabase: SyntaxDatabase {
16 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> { 19 fn module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> {
@@ -21,6 +24,10 @@ salsa::query_group! {
21 type SubmodulesQuery; 24 type SubmodulesQuery;
22 use fn imp::submodules; 25 use fn imp::submodules;
23 } 26 }
27 fn module_scope(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<ModuleScope>> {
28 type ModuleScopeQuery;
29 use fn imp::module_scope;
30 }
24 } 31 }
25} 32}
26 33
@@ -78,6 +85,7 @@ impl ModuleId {
78 while let Some(next) = curr.parent(tree) { 85 while let Some(next) = curr.parent(tree) {
79 curr = next; 86 curr = next;
80 i += 1; 87 i += 1;
88 // simplistic cycle detection
81 if i > 100 { 89 if i > 100 {
82 return self; 90 return self;
83 } 91 }
diff --git a/crates/ra_analysis/src/descriptors/module/scope.rs b/crates/ra_analysis/src/descriptors/module/scope.rs
new file mode 100644
index 000000000..da58ddce0
--- /dev/null
+++ b/crates/ra_analysis/src/descriptors/module/scope.rs
@@ -0,0 +1,128 @@
1//! Backend for module-level scope resolution & completion
2
3
4use ra_syntax::{
5 ast::{self, AstChildren, ModuleItemOwner},
6 File, AstNode, SmolStr, SyntaxNode, SyntaxNodeRef,
7};
8
9use crate::syntax_ptr::LocalSyntaxPtr;
10
11/// `ModuleScope` contains all named items declared in the scope.
12#[derive(Debug, PartialEq, Eq)]
13pub(crate) struct ModuleScope {
14 entries: Vec<Entry>,
15}
16
17/// `Entry` is a single named declaration iside a module.
18#[derive(Debug, PartialEq, Eq)]
19pub(crate) struct Entry {
20 ptr: LocalSyntaxPtr,
21 kind: EntryKind,
22 name: SmolStr,
23}
24
25#[derive(Debug, PartialEq, Eq)]
26enum EntryKind {
27 Item,
28 Import,
29}
30
31impl ModuleScope {
32 pub fn new(file: &File) -> ModuleScope {
33 let mut entries = Vec::new();
34 for item in file.ast().items() {
35 let entry = match item {
36 ast::ModuleItem::StructDef(item) => Entry::new(item),
37 ast::ModuleItem::EnumDef(item) => Entry::new(item),
38 ast::ModuleItem::FnDef(item) => Entry::new(item),
39 ast::ModuleItem::ConstDef(item) => Entry::new(item),
40 ast::ModuleItem::StaticDef(item) => Entry::new(item),
41 ast::ModuleItem::TraitDef(item) => Entry::new(item),
42 ast::ModuleItem::TypeDef(item) => Entry::new(item),
43 ast::ModuleItem::Module(item) => Entry::new(item),
44 ast::ModuleItem::UseItem(item) => {
45 if let Some(tree) = item.use_tree() {
46 collect_imports(tree, &mut entries);
47 }
48 continue;
49 }
50 ast::ModuleItem::ExternCrateItem(_) | ast::ModuleItem::ImplItem(_) => continue,
51 };
52 entries.extend(entry)
53 }
54
55 ModuleScope { entries }
56 }
57
58 pub fn entries(&self) -> &[Entry] {
59 self.entries.as_slice()
60 }
61}
62
63impl Entry {
64 fn new<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> {
65 let name = item.name()?;
66 Some(Entry {
67 name: name.text(),
68 ptr: LocalSyntaxPtr::new(name.syntax()),
69 kind: EntryKind::Item,
70 })
71 }
72 fn new_import(path: ast::Path) -> Option<Entry> {
73 let name_ref = path.segment()?.name_ref()?;
74 Some(Entry {
75 name: name_ref.text(),
76 ptr: LocalSyntaxPtr::new(name_ref.syntax()),
77 kind: EntryKind::Import,
78 })
79 }
80 pub fn name(&self) -> &SmolStr {
81 &self.name
82 }
83 pub fn ptr(&self) -> LocalSyntaxPtr {
84 self.ptr
85 }
86}
87
88fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
89 if let Some(use_tree_list) = tree.use_tree_list() {
90 return use_tree_list
91 .use_trees()
92 .for_each(|it| collect_imports(it, acc));
93 }
94 if let Some(path) = tree.path() {
95 acc.extend(Entry::new_import(path));
96 }
97}
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102 use ra_syntax::{ast::ModuleItemOwner, File};
103
104 fn do_check(code: &str, expected: &[&str]) {
105 let file = File::parse(&code);
106 let scope = ModuleScope::new(&file);
107 let actual = scope.entries.iter().map(|it| it.name()).collect::<Vec<_>>();
108 assert_eq!(expected, actual.as_slice());
109 }
110
111 #[test]
112 fn test_module_scope() {
113 do_check(
114 "
115 struct Foo;
116 enum Bar {}
117 mod baz {}
118 fn quux() {}
119 use x::{
120 y::z,
121 t,
122 };
123 type T = ();
124 ",
125 &["Foo", "Bar", "baz", "quux", "z", "t", "T"],
126 )
127 }
128}