aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/lang_item.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/lang_item.rs')
-rw-r--r--crates/ra_hir/src/lang_item.rs54
1 files changed, 23 insertions, 31 deletions
diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs
index ada8aeb5b..18ac0fcf9 100644
--- a/crates/ra_hir/src/lang_item.rs
+++ b/crates/ra_hir/src/lang_item.rs
@@ -1,10 +1,11 @@
1use std::sync::Arc; 1use std::sync::Arc;
2use rustc_hash::FxHashMap; 2use rustc_hash::FxHashMap;
3 3
4use ra_syntax::{SmolStr, ast::AttrsOwner}; 4use ra_syntax::{SmolStr, TreeArc, ast::AttrsOwner};
5 5
6use crate::{ 6use crate::{
7 Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait, ModuleDef, AstDatabase, HasSource 7 Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module,
8 Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
8}; 9};
9 10
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -93,39 +94,15 @@ impl LangItems {
93 } 94 }
94 } 95 }
95 96
96 // FIXME make this nicer
97 for def in module.declarations(db) { 97 for def in module.declarations(db) {
98 match def { 98 match def {
99 ModuleDef::Trait(trait_) => { 99 ModuleDef::Trait(trait_) => {
100 let node = trait_.source(db).ast; 100 self.collect_lang_item(db, trait_, LangItemTarget::Trait)
101 if let Some(lang_item_name) = lang_item_name(&*node) {
102 self.items.entry(lang_item_name).or_insert(LangItemTarget::Trait(trait_));
103 }
104 }
105 ModuleDef::Enum(e) => {
106 let node = e.source(db).ast;
107 if let Some(lang_item_name) = lang_item_name(&*node) {
108 self.items.entry(lang_item_name).or_insert(LangItemTarget::Enum(e));
109 }
110 }
111 ModuleDef::Struct(s) => {
112 let node = s.source(db).ast;
113 if let Some(lang_item_name) = lang_item_name(&*node) {
114 self.items.entry(lang_item_name).or_insert(LangItemTarget::Struct(s));
115 }
116 }
117 ModuleDef::Function(f) => {
118 let node = f.source(db).ast;
119 if let Some(lang_item_name) = lang_item_name(&*node) {
120 self.items.entry(lang_item_name).or_insert(LangItemTarget::Function(f));
121 }
122 }
123 ModuleDef::Static(s) => {
124 let node = s.source(db).ast;
125 if let Some(lang_item_name) = lang_item_name(&*node) {
126 self.items.entry(lang_item_name).or_insert(LangItemTarget::Static(s));
127 }
128 } 101 }
102 ModuleDef::Enum(e) => self.collect_lang_item(db, e, LangItemTarget::Enum),
103 ModuleDef::Struct(s) => self.collect_lang_item(db, s, LangItemTarget::Struct),
104 ModuleDef::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function),
105 ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static),
129 _ => {} 106 _ => {}
130 } 107 }
131 } 108 }
@@ -135,6 +112,21 @@ impl LangItems {
135 self.collect_lang_items_recursive(db, &child); 112 self.collect_lang_items_recursive(db, &child);
136 } 113 }
137 } 114 }
115
116 fn collect_lang_item<T, N>(
117 &mut self,
118 db: &(impl DefDatabase + AstDatabase),
119 item: T,
120 constructor: fn(T) -> LangItemTarget,
121 ) where
122 T: Copy + HasSource<Ast = TreeArc<N>>,
123 N: AttrsOwner,
124 {
125 let node = item.source(db).ast;
126 if let Some(lang_item_name) = lang_item_name(&*node) {
127 self.items.entry(lang_item_name).or_insert(constructor(item));
128 }
129 }
138} 130}
139 131
140fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> { 132fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> {