rust-analyzer/crates/ide/src/fetch_crates.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.2 KiB
Rust
Raw Normal View History

2023-04-03 00:58:20 +00:00
use ide_db::{
2023-04-13 16:06:43 +00:00
base_db::{CrateOrigin, FileId, SourceDatabase},
FxIndexSet, RootDatabase,
2023-04-03 00:58:20 +00:00
};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
2023-04-03 00:58:20 +00:00
pub struct CrateInfo {
2023-04-13 16:06:43 +00:00
pub name: Option<String>,
pub version: Option<String>,
pub root_file_id: FileId,
2023-04-03 00:58:20 +00:00
}
2023-04-03 02:04:32 +00:00
// Feature: Show Dependency Tree
//
// Shows a view tree with all the dependencies of this project
//
// |===
2023-05-08 18:27:35 +00:00
// | Editor | Panel Name
//
// | VS Code | **Rust Dependencies**
// |===
//
2023-04-03 02:04:32 +00:00
// image::https://user-images.githubusercontent.com/5748995/229394139-2625beab-f4c9-484b-84ed-ad5dee0b1e1a.png[]
pub(crate) fn fetch_crates(db: &RootDatabase) -> FxIndexSet<CrateInfo> {
2023-04-03 00:58:20 +00:00
let crate_graph = db.crate_graph();
crate_graph
.iter()
.map(|crate_id| &crate_graph[crate_id])
.filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. }))
2023-04-13 16:06:43 +00:00
.map(|data| crate_info(data))
2023-04-03 00:58:20 +00:00
.collect()
}
2023-04-13 16:06:43 +00:00
fn crate_info(data: &ide_db::base_db::CrateData) -> CrateInfo {
2023-04-04 16:47:01 +00:00
let crate_name = crate_name(data);
2023-04-13 16:06:43 +00:00
let version = data.version.clone();
CrateInfo { name: crate_name, version, root_file_id: data.root_file_id }
2023-04-04 16:47:01 +00:00
}
2023-04-13 16:06:43 +00:00
fn crate_name(data: &ide_db::base_db::CrateData) -> Option<String> {
data.display_name.as_ref().map(|it| it.canonical_name().to_owned())
2023-04-03 00:58:20 +00:00
}