aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/data.rs
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-04-08 17:12:15 +0100
committerBenjamin Coenen <[email protected]>2020-04-08 17:12:15 +0100
commit8f1dba6f9ae1d8d314dd9d007e4c582ed1403e8d (patch)
treee6d269d0a10de37aff7c5f2c2849eab9a9108b02 /crates/ra_hir_def/src/data.rs
parent18a5e164838e1dc2abcc6b79d4fc2f96ffd2507c (diff)
feat: add attributes support on struct fields and method #3870
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/data.rs')
-rw-r--r--crates/ra_hir_def/src/data.rs25
1 files changed, 21 insertions, 4 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 934c3b94e..606ec48b0 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -7,6 +7,7 @@ use hir_expand::{
7 name::{name, AsName, Name}, 7 name::{name, AsName, Name},
8 AstId, InFile, 8 AstId, InFile,
9}; 9};
10use ra_cfg::CfgOptions;
10use ra_prof::profile; 11use ra_prof::profile;
11use ra_syntax::ast::{ 12use ra_syntax::ast::{
12 self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner, 13 self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner,
@@ -67,6 +68,7 @@ impl FunctionData {
67 } 68 }
68 } 69 }
69 let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id)); 70 let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id));
71
70 let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { 72 let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) {
71 TypeRef::from_ast(type_ref) 73 TypeRef::from_ast(type_ref)
72 } else { 74 } else {
@@ -215,6 +217,7 @@ impl ImplData {
215 let module_id = impl_loc.container.module(db); 217 let module_id = impl_loc.container.module(db);
216 218
217 let mut items = Vec::new(); 219 let mut items = Vec::new();
220
218 if let Some(item_list) = src.value.item_list() { 221 if let Some(item_list) = src.value.item_list() {
219 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id)); 222 items.extend(collect_impl_items(db, item_list.impl_items(), src.file_id, id));
220 items.extend(collect_impl_items_in_macros( 223 items.extend(collect_impl_items_in_macros(
@@ -315,6 +318,10 @@ fn collect_impl_items_in_macro(
315 } 318 }
316} 319}
317 320
321fn is_cfg_enabled(cfg_options: &CfgOptions, attrs: &Attrs) -> bool {
322 attrs.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false))
323}
324
318fn collect_impl_items( 325fn collect_impl_items(
319 db: &dyn DefDatabase, 326 db: &dyn DefDatabase,
320 impl_items: impl Iterator<Item = ImplItem>, 327 impl_items: impl Iterator<Item = ImplItem>,
@@ -322,16 +329,26 @@ fn collect_impl_items(
322 id: ImplId, 329 id: ImplId,
323) -> Vec<AssocItemId> { 330) -> Vec<AssocItemId> {
324 let items = db.ast_id_map(file_id); 331 let items = db.ast_id_map(file_id);
332 let crate_graph = db.crate_graph();
333 let module_id = id.lookup(db).container.module(db);
325 334
326 impl_items 335 impl_items
327 .map(|item_node| match item_node { 336 .filter_map(|item_node| match item_node {
328 ast::ImplItem::FnDef(it) => { 337 ast::ImplItem::FnDef(it) => {
329 let def = FunctionLoc { 338 let def = FunctionLoc {
330 container: AssocContainerId::ImplId(id), 339 container: AssocContainerId::ImplId(id),
331 ast_id: AstId::new(file_id, items.ast_id(&it)), 340 ast_id: AstId::new(file_id, items.ast_id(&it)),
332 } 341 }
333 .intern(db); 342 .intern(db);
334 def.into() 343
344 if !is_cfg_enabled(
345 &crate_graph[module_id.krate].cfg_options,
346 &db.function_data(def).attrs,
347 ) {
348 None
349 } else {
350 Some(def.into())
351 }
335 } 352 }
336 ast::ImplItem::ConstDef(it) => { 353 ast::ImplItem::ConstDef(it) => {
337 let def = ConstLoc { 354 let def = ConstLoc {
@@ -339,7 +356,7 @@ fn collect_impl_items(
339 ast_id: AstId::new(file_id, items.ast_id(&it)), 356 ast_id: AstId::new(file_id, items.ast_id(&it)),
340 } 357 }
341 .intern(db); 358 .intern(db);
342 def.into() 359 Some(def.into())
343 } 360 }
344 ast::ImplItem::TypeAliasDef(it) => { 361 ast::ImplItem::TypeAliasDef(it) => {
345 let def = TypeAliasLoc { 362 let def = TypeAliasLoc {
@@ -347,7 +364,7 @@ fn collect_impl_items(
347 ast_id: AstId::new(file_id, items.ast_id(&it)), 364 ast_id: AstId::new(file_id, items.ast_id(&it)),
348 } 365 }
349 .intern(db); 366 .intern(db);
350 def.into() 367 Some(def.into())
351 } 368 }
352 }) 369 })
353 .collect() 370 .collect()