aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/raw.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/nameres/raw.rs')
-rw-r--r--crates/ra_hir/src/nameres/raw.rs53
1 files changed, 12 insertions, 41 deletions
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index 623b343c4..57f2929c3 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -5,7 +5,7 @@ use std::{ops::Index, sync::Arc};
5use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; 5use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId};
6use ra_syntax::{ 6use ra_syntax::{
7 ast::{self, AttrsOwner, NameOwner}, 7 ast::{self, AttrsOwner, NameOwner},
8 AstNode, AstPtr, SmolStr, SourceFile, 8 AstNode, AstPtr, SourceFile,
9}; 9};
10use test_utils::tested_by; 10use test_utils::tested_by;
11 11
@@ -121,14 +121,20 @@ impl Index<Macro> for RawItems {
121} 121}
122 122
123// Avoid heap allocation on items without attributes. 123// Avoid heap allocation on items without attributes.
124pub(super) type Attrs = Option<Arc<[Attr]>>; 124type Attrs = Option<Arc<[Attr]>>;
125 125
126#[derive(Debug, PartialEq, Eq, Clone)] 126#[derive(Debug, PartialEq, Eq, Clone)]
127pub(super) struct RawItem { 127pub(super) struct RawItem {
128 pub(super) attrs: Attrs, 128 attrs: Attrs,
129 pub(super) kind: RawItemKind, 129 pub(super) kind: RawItemKind,
130} 130}
131 131
132impl RawItem {
133 pub(super) fn attrs(&self) -> &[Attr] {
134 self.attrs.as_ref().map_or(&[], |it| &*it)
135 }
136}
137
132#[derive(Debug, PartialEq, Eq, Clone, Copy)] 138#[derive(Debug, PartialEq, Eq, Clone, Copy)]
133pub(super) enum RawItemKind { 139pub(super) enum RawItemKind {
134 Module(Module), 140 Module(Module),
@@ -143,19 +149,8 @@ impl_arena_id!(Module);
143 149
144#[derive(Debug, PartialEq, Eq)] 150#[derive(Debug, PartialEq, Eq)]
145pub(super) enum ModuleData { 151pub(super) enum ModuleData {
146 Declaration { 152 Declaration { name: Name, ast_id: FileAstId<ast::Module> },
147 name: Name, 153 Definition { name: Name, ast_id: FileAstId<ast::Module>, items: Vec<RawItem> },
148 ast_id: FileAstId<ast::Module>,
149 attr_path: Option<SmolStr>,
150 is_macro_use: bool,
151 },
152 Definition {
153 name: Name,
154 ast_id: FileAstId<ast::Module>,
155 items: Vec<RawItem>,
156 attr_path: Option<SmolStr>,
157 is_macro_use: bool,
158 },
159} 154}
160 155
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -286,28 +281,17 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
286 let attrs = self.parse_attrs(&module); 281 let attrs = self.parse_attrs(&module);
287 282
288 let ast_id = self.source_ast_id_map.ast_id(&module); 283 let ast_id = self.source_ast_id_map.ast_id(&module);
289 // FIXME: cfg_attr
290 let is_macro_use = module.has_atom_attr("macro_use");
291 if module.has_semi() { 284 if module.has_semi() {
292 let attr_path = extract_mod_path_attribute(&module); 285 let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
293 let item = self.raw_items.modules.alloc(ModuleData::Declaration {
294 name,
295 ast_id,
296 attr_path,
297 is_macro_use,
298 });
299 self.push_item(current_module, attrs, RawItemKind::Module(item)); 286 self.push_item(current_module, attrs, RawItemKind::Module(item));
300 return; 287 return;
301 } 288 }
302 289
303 if let Some(item_list) = module.item_list() { 290 if let Some(item_list) = module.item_list() {
304 let attr_path = extract_mod_path_attribute(&module);
305 let item = self.raw_items.modules.alloc(ModuleData::Definition { 291 let item = self.raw_items.modules.alloc(ModuleData::Definition {
306 name, 292 name,
307 ast_id, 293 ast_id,
308 items: Vec::new(), 294 items: Vec::new(),
309 attr_path,
310 is_macro_use,
311 }); 295 });
312 self.process_module(Some(item), item_list); 296 self.process_module(Some(item), item_list);
313 self.push_item(current_module, attrs, RawItemKind::Module(item)); 297 self.push_item(current_module, attrs, RawItemKind::Module(item));
@@ -417,16 +401,3 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
417 Attr::from_attrs_owner(self.file_id, item, self.db) 401 Attr::from_attrs_owner(self.file_id, item, self.db)
418 } 402 }
419} 403}
420
421fn extract_mod_path_attribute(module: &ast::Module) -> Option<SmolStr> {
422 module.attrs().into_iter().find_map(|attr| {
423 attr.as_simple_key_value().and_then(|(name, value)| {
424 let is_path = name == "path";
425 if is_path {
426 Some(value)
427 } else {
428 None
429 }
430 })
431 })
432}