Skip to content

Commit bcf875f

Browse files
author
Jonas Schievink
committed
Clean up import_map.rs
1 parent 781b514 commit bcf875f

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

crates/ra_hir_def/src/import_map.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! A map of all publicly exported items in a crate.
22
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};
54

65
use fst::{self, Streamer};
7-
use itertools::Itertools;
86
use ra_db::CrateId;
97
use rustc_hash::FxHashMap;
108

@@ -118,7 +116,7 @@ impl ImportMap {
118116
let start = last_batch_start;
119117
last_batch_start = idx + 1;
120118

121-
let key: String = fst_path(&importables[start].1).collect();
119+
let key = fst_path(&importables[start].1);
122120

123121
builder.insert(key, start as u64).unwrap();
124122
}
@@ -137,7 +135,8 @@ impl ImportMap {
137135

138136
impl PartialEq for ImportMap {
139137
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
141140
}
142141
}
143142

@@ -163,18 +162,16 @@ impl fmt::Debug for ImportMap {
163162
}
164163
}
165164

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
172169
}
173170

174171
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)
178175
}
179176

180177
#[derive(Debug)]
@@ -184,8 +181,8 @@ pub struct Query {
184181
}
185182

186183
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 }
189186
}
190187

191188
/// Only returns items whose paths end with the (case-insensitive) query string as their last
@@ -197,14 +194,13 @@ impl Query {
197194

198195
/// Searches dependencies of `krate` for an importable path matching `query`.
199196
///
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`.
202198
pub fn search_dependencies<'a>(
203199
db: &'a dyn DefDatabase,
204200
krate: CrateId,
205201
query: Query,
206202
) -> 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));
208204

209205
let graph = db.crate_graph();
210206
let import_maps: Vec<_> =
@@ -239,7 +235,7 @@ pub fn search_dependencies<'a>(
239235
// `importables` whose paths match `path`.
240236
res.extend(importables.iter().copied().take_while(|item| {
241237
let item_path = &import_map.map[item];
242-
fst_path(item_path).eq(fst_path(path))
238+
fst_path(item_path) == fst_path(path)
243239
}));
244240
}
245241
}
@@ -252,14 +248,15 @@ mod tests {
252248
use super::*;
253249
use crate::test_db::TestDB;
254250
use insta::assert_snapshot;
251+
use itertools::Itertools;
255252
use ra_db::fixture::WithFixture;
256253
use ra_db::{SourceDatabase, Upcast};
257254

258255
fn import_map(ra_fixture: &str) -> String {
259256
let db = TestDB::with_files(ra_fixture);
260257
let crate_graph = db.crate_graph();
261258

262-
let import_maps: Vec<_> = crate_graph
259+
let s = crate_graph
263260
.iter()
264261
.filter_map(|krate| {
265262
let cdata = &crate_graph[krate];
@@ -269,9 +266,8 @@ mod tests {
269266

270267
Some(format!("{}:\n{:?}", name, map))
271268
})
272-
.collect();
273-
274-
import_maps.join("\n")
269+
.join("\n");
270+
s
275271
}
276272

277273
fn search_dependencies_of(ra_fixture: &str, krate_name: &str, query: Query) -> String {
@@ -304,7 +300,6 @@ mod tests {
304300
)
305301
})
306302
})
307-
.collect::<Vec<_>>()
308303
.join("\n")
309304
}
310305

crates/ra_hir_expand/src/name.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ impl Name {
6767
_ => None,
6868
}
6969
}
70-
71-
pub fn as_text(&self) -> Option<&str> {
72-
match &self.0 {
73-
Repr::Text(s) => Some(s.as_str()),
74-
_ => None,
75-
}
76-
}
7770
}
7871

7972
pub trait AsName {

0 commit comments

Comments
 (0)