1
1
//! A map of all publicly exported items in a crate.
2
2
3
- use std:: cmp:: Ordering ;
4
- use std:: { collections:: hash_map:: Entry , fmt, sync:: Arc } ;
3
+ use std:: { cmp:: Ordering , collections:: hash_map:: Entry , fmt, sync:: Arc } ;
5
4
6
5
use fst:: { self , Streamer } ;
7
- use itertools:: Itertools ;
8
6
use ra_db:: CrateId ;
9
7
use rustc_hash:: FxHashMap ;
10
8
@@ -118,7 +116,7 @@ impl ImportMap {
118
116
let start = last_batch_start;
119
117
last_batch_start = idx + 1 ;
120
118
121
- let key: String = fst_path ( & importables[ start] . 1 ) . collect ( ) ;
119
+ let key = fst_path ( & importables[ start] . 1 ) ;
122
120
123
121
builder. insert ( key, start as u64 ) . unwrap ( ) ;
124
122
}
@@ -137,7 +135,8 @@ impl ImportMap {
137
135
138
136
impl PartialEq for ImportMap {
139
137
fn eq ( & self , other : & Self ) -> bool {
140
- self . importables == other. importables
138
+ // `fst` and `importables` are built from `map`, so we don't need to compare them.
139
+ self . map == other. map
141
140
}
142
141
}
143
142
@@ -163,18 +162,16 @@ impl fmt::Debug for ImportMap {
163
162
}
164
163
}
165
164
166
- fn fst_path ( path : & ModPath ) -> impl Iterator < Item = char > + ' _ {
167
- path. segments
168
- . iter ( )
169
- . map ( |name| name. as_text ( ) . unwrap ( ) )
170
- . intersperse ( "::" )
171
- . flat_map ( |s| s. chars ( ) . map ( |c| c. to_ascii_lowercase ( ) ) )
165
+ fn fst_path ( path : & ModPath ) -> String {
166
+ let mut s = path. to_string ( ) ;
167
+ s. make_ascii_lowercase ( ) ;
168
+ s
172
169
}
173
170
174
171
fn cmp ( ( _, lhs) : & ( & ItemInNs , & ModPath ) , ( _, rhs) : & ( & ItemInNs , & ModPath ) ) -> Ordering {
175
- let lhs_chars = fst_path ( lhs) ;
176
- let rhs_chars = fst_path ( rhs) ;
177
- lhs_chars . cmp ( rhs_chars )
172
+ let lhs_str = fst_path ( lhs) ;
173
+ let rhs_str = fst_path ( rhs) ;
174
+ lhs_str . cmp ( & rhs_str )
178
175
}
179
176
180
177
#[ derive( Debug ) ]
@@ -184,8 +181,8 @@ pub struct Query {
184
181
}
185
182
186
183
impl Query {
187
- pub fn new ( query : impl AsRef < str > ) -> Self {
188
- Self { query : query. as_ref ( ) . to_lowercase ( ) , anchor_end : false }
184
+ pub fn new ( query : & str ) -> Self {
185
+ Self { query : query. to_lowercase ( ) , anchor_end : false }
189
186
}
190
187
191
188
/// Only returns items whose paths end with the (case-insensitive) query string as their last
@@ -197,14 +194,13 @@ impl Query {
197
194
198
195
/// Searches dependencies of `krate` for an importable path matching `query`.
199
196
///
200
- /// This returns all items that could be imported from within `krate`, excluding paths inside
201
- /// `krate` itself.
197
+ /// This returns a list of items that could be imported from dependencies of `krate`.
202
198
pub fn search_dependencies < ' a > (
203
199
db : & ' a dyn DefDatabase ,
204
200
krate : CrateId ,
205
201
query : Query ,
206
202
) -> Vec < ItemInNs > {
207
- let _p = ra_prof:: profile ( "import_map::global_search " ) . detail ( || format ! ( "{:?}" , query) ) ;
203
+ let _p = ra_prof:: profile ( "search_dependencies " ) . detail ( || format ! ( "{:?}" , query) ) ;
208
204
209
205
let graph = db. crate_graph ( ) ;
210
206
let import_maps: Vec < _ > =
@@ -239,7 +235,7 @@ pub fn search_dependencies<'a>(
239
235
// `importables` whose paths match `path`.
240
236
res. extend ( importables. iter ( ) . copied ( ) . take_while ( |item| {
241
237
let item_path = & import_map. map [ item] ;
242
- fst_path ( item_path) . eq ( fst_path ( path) )
238
+ fst_path ( item_path) == fst_path ( path)
243
239
} ) ) ;
244
240
}
245
241
}
@@ -252,14 +248,15 @@ mod tests {
252
248
use super :: * ;
253
249
use crate :: test_db:: TestDB ;
254
250
use insta:: assert_snapshot;
251
+ use itertools:: Itertools ;
255
252
use ra_db:: fixture:: WithFixture ;
256
253
use ra_db:: { SourceDatabase , Upcast } ;
257
254
258
255
fn import_map ( ra_fixture : & str ) -> String {
259
256
let db = TestDB :: with_files ( ra_fixture) ;
260
257
let crate_graph = db. crate_graph ( ) ;
261
258
262
- let import_maps : Vec < _ > = crate_graph
259
+ let s = crate_graph
263
260
. iter ( )
264
261
. filter_map ( |krate| {
265
262
let cdata = & crate_graph[ krate] ;
@@ -269,9 +266,8 @@ mod tests {
269
266
270
267
Some ( format ! ( "{}:\n {:?}" , name, map) )
271
268
} )
272
- . collect ( ) ;
273
-
274
- import_maps. join ( "\n " )
269
+ . join ( "\n " ) ;
270
+ s
275
271
}
276
272
277
273
fn search_dependencies_of ( ra_fixture : & str , krate_name : & str , query : Query ) -> String {
@@ -304,7 +300,6 @@ mod tests {
304
300
)
305
301
} )
306
302
} )
307
- . collect :: < Vec < _ > > ( )
308
303
. join ( "\n " )
309
304
}
310
305
0 commit comments