1
1
use std:: sync:: Arc ;
2
2
use rustc_hash:: FxHashMap ;
3
3
4
- use ra_syntax:: { SmolStr , ast:: AttrsOwner } ;
4
+ use ra_syntax:: { SmolStr , TreeArc , ast:: AttrsOwner } ;
5
5
6
6
use 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
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
97
for def in module. declarations ( db) {
98
98
match def {
99
99
ModuleDef :: Trait ( trait_) => {
100
- let node = trait_. source ( db) . ast ;
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
- }
100
+ self . collect_lang_item ( db, trait_, LangItemTarget :: Trait )
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
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
140
132
fn lang_item_name < T : AttrsOwner > ( node : & T ) -> Option < SmolStr > {
0 commit comments