diff options
Diffstat (limited to 'crates/ra_editor/src/scope/mod_scope.rs')
-rw-r--r-- | crates/ra_editor/src/scope/mod_scope.rs | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/crates/ra_editor/src/scope/mod_scope.rs b/crates/ra_editor/src/scope/mod_scope.rs deleted file mode 100644 index 818749a12..000000000 --- a/crates/ra_editor/src/scope/mod_scope.rs +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
1 | /// FIXME: this is now moved to ra_analysis::descriptors::module::scope. | ||
2 | /// | ||
3 | /// Current copy will be deleted as soon as we move the rest of the completion | ||
4 | /// to the analyezer. | ||
5 | |||
6 | |||
7 | use ra_syntax::{ | ||
8 | ast::{self, AstChildren}, | ||
9 | AstNode, SmolStr, SyntaxNode, SyntaxNodeRef, | ||
10 | }; | ||
11 | |||
12 | pub struct ModuleScope { | ||
13 | entries: Vec<Entry>, | ||
14 | } | ||
15 | |||
16 | pub struct Entry { | ||
17 | node: SyntaxNode, | ||
18 | kind: EntryKind, | ||
19 | } | ||
20 | |||
21 | enum EntryKind { | ||
22 | Item, | ||
23 | Import, | ||
24 | } | ||
25 | |||
26 | impl ModuleScope { | ||
27 | pub fn new(items: AstChildren<ast::ModuleItem>) -> ModuleScope { | ||
28 | let mut entries = Vec::new(); | ||
29 | for item in items { | ||
30 | let entry = match item { | ||
31 | ast::ModuleItem::StructDef(item) => Entry::new_item(item), | ||
32 | ast::ModuleItem::EnumDef(item) => Entry::new_item(item), | ||
33 | ast::ModuleItem::FnDef(item) => Entry::new_item(item), | ||
34 | ast::ModuleItem::ConstDef(item) => Entry::new_item(item), | ||
35 | ast::ModuleItem::StaticDef(item) => Entry::new_item(item), | ||
36 | ast::ModuleItem::TraitDef(item) => Entry::new_item(item), | ||
37 | ast::ModuleItem::TypeDef(item) => Entry::new_item(item), | ||
38 | ast::ModuleItem::Module(item) => Entry::new_item(item), | ||
39 | ast::ModuleItem::UseItem(item) => { | ||
40 | if let Some(tree) = item.use_tree() { | ||
41 | collect_imports(tree, &mut entries); | ||
42 | } | ||
43 | continue; | ||
44 | } | ||
45 | ast::ModuleItem::ExternCrateItem(_) | ast::ModuleItem::ImplItem(_) => continue, | ||
46 | }; | ||
47 | entries.extend(entry) | ||
48 | } | ||
49 | |||
50 | ModuleScope { entries } | ||
51 | } | ||
52 | |||
53 | pub fn entries(&self) -> &[Entry] { | ||
54 | self.entries.as_slice() | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl Entry { | ||
59 | fn new_item<'a>(item: impl ast::NameOwner<'a>) -> Option<Entry> { | ||
60 | let name = item.name()?; | ||
61 | Some(Entry { | ||
62 | node: name.syntax().owned(), | ||
63 | kind: EntryKind::Item, | ||
64 | }) | ||
65 | } | ||
66 | fn new_import(path: ast::Path) -> Option<Entry> { | ||
67 | let name_ref = path.segment()?.name_ref()?; | ||
68 | Some(Entry { | ||
69 | node: name_ref.syntax().owned(), | ||
70 | kind: EntryKind::Import, | ||
71 | }) | ||
72 | } | ||
73 | pub fn name(&self) -> SmolStr { | ||
74 | match self.kind { | ||
75 | EntryKind::Item => ast::Name::cast(self.node.borrowed()).unwrap().text(), | ||
76 | EntryKind::Import => ast::NameRef::cast(self.node.borrowed()).unwrap().text(), | ||
77 | } | ||
78 | } | ||
79 | pub fn syntax(&self) -> SyntaxNodeRef { | ||
80 | self.node.borrowed() | ||
81 | } | ||
82 | } | ||
83 | |||
84 | fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) { | ||
85 | if let Some(use_tree_list) = tree.use_tree_list() { | ||
86 | return use_tree_list | ||
87 | .use_trees() | ||
88 | .for_each(|it| collect_imports(it, acc)); | ||
89 | } | ||
90 | if let Some(path) = tree.path() { | ||
91 | acc.extend(Entry::new_import(path)); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | #[cfg(test)] | ||
96 | mod tests { | ||
97 | use super::*; | ||
98 | use ra_syntax::{ast::ModuleItemOwner, File}; | ||
99 | |||
100 | fn do_check(code: &str, expected: &[&str]) { | ||
101 | let file = File::parse(&code); | ||
102 | let scope = ModuleScope::new(file.ast().items()); | ||
103 | let actual = scope.entries.iter().map(|it| it.name()).collect::<Vec<_>>(); | ||
104 | assert_eq!(expected, actual.as_slice()); | ||
105 | } | ||
106 | |||
107 | #[test] | ||
108 | fn test_module_scope() { | ||
109 | do_check( | ||
110 | " | ||
111 | struct Foo; | ||
112 | enum Bar {} | ||
113 | mod baz {} | ||
114 | fn quux() {} | ||
115 | use x::{ | ||
116 | y::z, | ||
117 | t, | ||
118 | }; | ||
119 | type T = (); | ||
120 | ", | ||
121 | &["Foo", "Bar", "baz", "quux", "z", "t", "T"], | ||
122 | ) | ||
123 | } | ||
124 | } | ||