mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
internal: Replace Display impl for Name
This commit is contained in:
parent
2f840c2236
commit
c7ef6c25b7
108 changed files with 1045 additions and 656 deletions
|
@ -33,13 +33,23 @@ pub struct ImportPath {
|
|||
pub segments: Vec<Name>,
|
||||
}
|
||||
|
||||
impl fmt::Display for ImportPath {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.segments.iter().format("::"), f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ImportPath {
|
||||
pub fn display<'a>(&'a self, db: &'a dyn DefDatabase) -> impl fmt::Display + 'a {
|
||||
struct Display<'a> {
|
||||
db: &'a dyn DefDatabase,
|
||||
path: &'a ImportPath,
|
||||
}
|
||||
impl fmt::Display for Display<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(
|
||||
&self.path.segments.iter().map(|it| it.display(self.db.upcast())).format("::"),
|
||||
f,
|
||||
)
|
||||
}
|
||||
}
|
||||
Display { db, path: self }
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.segments.len()
|
||||
}
|
||||
|
@ -76,7 +86,7 @@ impl ImportMap {
|
|||
let mut importables = import_map
|
||||
.map
|
||||
.iter()
|
||||
.map(|(item, info)| (item, fst_path(&info.path)))
|
||||
.map(|(item, info)| (item, fst_path(db, &info.path)))
|
||||
.collect::<Vec<_>>();
|
||||
importables.sort_by(|(_, fst_path), (_, fst_path2)| fst_path.cmp(fst_path2));
|
||||
|
||||
|
@ -113,6 +123,25 @@ impl ImportMap {
|
|||
self.map.get(&item)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn fmt_for_test(&self, db: &dyn DefDatabase) -> String {
|
||||
let mut importable_paths: Vec<_> = self
|
||||
.map
|
||||
.iter()
|
||||
.map(|(item, info)| {
|
||||
let ns = match item {
|
||||
ItemInNs::Types(_) => "t",
|
||||
ItemInNs::Values(_) => "v",
|
||||
ItemInNs::Macros(_) => "m",
|
||||
};
|
||||
format!("- {} ({ns})", info.path.display(db))
|
||||
})
|
||||
.collect();
|
||||
|
||||
importable_paths.sort();
|
||||
importable_paths.join("\n")
|
||||
}
|
||||
|
||||
fn collect_trait_assoc_items(
|
||||
&mut self,
|
||||
db: &dyn DefDatabase,
|
||||
|
@ -238,13 +267,10 @@ impl fmt::Debug for ImportMap {
|
|||
let mut importable_paths: Vec<_> = self
|
||||
.map
|
||||
.iter()
|
||||
.map(|(item, info)| {
|
||||
let ns = match item {
|
||||
ItemInNs::Types(_) => "t",
|
||||
ItemInNs::Values(_) => "v",
|
||||
ItemInNs::Macros(_) => "m",
|
||||
};
|
||||
format!("- {} ({ns})", info.path)
|
||||
.map(|(item, _)| match item {
|
||||
ItemInNs::Types(it) => format!("- {it:?} (t)",),
|
||||
ItemInNs::Values(it) => format!("- {it:?} (v)",),
|
||||
ItemInNs::Macros(it) => format!("- {it:?} (m)",),
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -253,9 +279,9 @@ impl fmt::Debug for ImportMap {
|
|||
}
|
||||
}
|
||||
|
||||
fn fst_path(path: &ImportPath) -> String {
|
||||
fn fst_path(db: &dyn DefDatabase, path: &ImportPath) -> String {
|
||||
let _p = profile::span("fst_path");
|
||||
let mut s = path.to_string();
|
||||
let mut s = path.display(db).to_string();
|
||||
s.make_ascii_lowercase();
|
||||
s
|
||||
}
|
||||
|
@ -348,7 +374,12 @@ impl Query {
|
|||
self
|
||||
}
|
||||
|
||||
fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
|
||||
fn import_matches(
|
||||
&self,
|
||||
db: &dyn DefDatabase,
|
||||
import: &ImportInfo,
|
||||
enforce_lowercase: bool,
|
||||
) -> bool {
|
||||
let _p = profile::span("import_map::Query::import_matches");
|
||||
if import.is_trait_assoc_item {
|
||||
if self.exclude_import_kinds.contains(&ImportKind::AssociatedItem) {
|
||||
|
@ -359,9 +390,9 @@ impl Query {
|
|||
}
|
||||
|
||||
let mut input = if import.is_trait_assoc_item || self.name_only {
|
||||
import.path.segments.last().unwrap().to_string()
|
||||
import.path.segments.last().unwrap().display(db.upcast()).to_string()
|
||||
} else {
|
||||
import.path.to_string()
|
||||
import.path.display(db).to_string()
|
||||
};
|
||||
if enforce_lowercase || !self.case_sensitive {
|
||||
input.make_ascii_lowercase();
|
||||
|
@ -426,25 +457,27 @@ pub fn search_dependencies(
|
|||
let importables = &import_map.importables[indexed_value.value as usize..];
|
||||
|
||||
let common_importable_data = &import_map.map[&importables[0]];
|
||||
if !query.import_matches(common_importable_data, true) {
|
||||
if !query.import_matches(db, common_importable_data, true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Path shared by the importable items in this group.
|
||||
let common_importables_path_fst = fst_path(&common_importable_data.path);
|
||||
let common_importables_path_fst = fst_path(db, &common_importable_data.path);
|
||||
// Add the items from this `ModPath` group. Those are all subsequent items in
|
||||
// `importables` whose paths match `path`.
|
||||
let iter = importables
|
||||
.iter()
|
||||
.copied()
|
||||
.take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path))
|
||||
.take_while(|item| {
|
||||
common_importables_path_fst == fst_path(db, &import_map.map[item].path)
|
||||
})
|
||||
.filter(|&item| match item_import_kind(item) {
|
||||
Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
|
||||
None => true,
|
||||
})
|
||||
.filter(|item| {
|
||||
!query.case_sensitive // we've already checked the common importables path case-insensitively
|
||||
|| query.import_matches(&import_map.map[item], false)
|
||||
|| query.import_matches(db, &import_map.map[item], false)
|
||||
});
|
||||
res.extend(iter);
|
||||
|
||||
|
@ -501,7 +534,7 @@ mod tests {
|
|||
let (path, mark) = match assoc_item_path(&db, &dependency_imports, dependency) {
|
||||
Some(assoc_item_path) => (assoc_item_path, "a"),
|
||||
None => (
|
||||
dependency_imports.path_of(dependency)?.to_string(),
|
||||
dependency_imports.path_of(dependency)?.display(&db).to_string(),
|
||||
match dependency {
|
||||
ItemInNs::Types(ModuleDefId::FunctionId(_))
|
||||
| ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
|
||||
|
@ -552,7 +585,11 @@ mod tests {
|
|||
None
|
||||
}
|
||||
})?;
|
||||
return Some(format!("{}::{assoc_item_name}", dependency_imports.path_of(trait_)?));
|
||||
return Some(format!(
|
||||
"{}::{}",
|
||||
dependency_imports.path_of(trait_)?.display(db),
|
||||
assoc_item_name.display(db.upcast())
|
||||
));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
@ -592,7 +629,7 @@ mod tests {
|
|||
|
||||
let map = db.import_map(krate);
|
||||
|
||||
Some(format!("{name}:\n{map:?}\n"))
|
||||
Some(format!("{name}:\n{}\n", map.fmt_for_test(db.upcast())))
|
||||
})
|
||||
.sorted()
|
||||
.collect::<String>();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue