mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-18 08:10:11 +00:00
fetching dependencies from the server
This commit is contained in:
parent
1201b156d8
commit
09e0a00d36
9 changed files with 155 additions and 156 deletions
59
crates/ide/src/fetch_crates.rs
Normal file
59
crates/ide/src/fetch_crates.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use ide_db::{
|
||||
base_db::{CrateOrigin, SourceDatabase, SourceDatabaseExt},
|
||||
RootDatabase,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CrateInfo {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
pub(crate) fn fetch_crates(db: &RootDatabase) -> Vec<CrateInfo> {
|
||||
let crate_graph = db.crate_graph();
|
||||
crate_graph
|
||||
.iter()
|
||||
.map(|crate_id| &crate_graph[crate_id])
|
||||
.filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. }))
|
||||
.map(|data| {
|
||||
let crate_name = crate_name(data);
|
||||
let version = data.version.clone().unwrap_or_else(|| "".to_owned());
|
||||
let crate_path = crate_path(db, data, &crate_name);
|
||||
|
||||
CrateInfo { name: crate_name, version, path: crate_path }
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn crate_name(data: &ide_db::base_db::CrateData) -> String {
|
||||
data.display_name
|
||||
.clone()
|
||||
.map(|it| it.canonical_name().to_owned())
|
||||
.unwrap_or("unknown".to_string())
|
||||
}
|
||||
|
||||
fn crate_path(db: &RootDatabase, data: &ide_db::base_db::CrateData, crate_name: &str) -> String {
|
||||
let source_root_id = db.file_source_root(data.root_file_id);
|
||||
let source_root = db.source_root(source_root_id);
|
||||
let source_root_path = source_root.path_for_file(&data.root_file_id);
|
||||
match source_root_path.cloned() {
|
||||
Some(mut root_path) => {
|
||||
let mut crate_path = "".to_string();
|
||||
while let Some(vfs_path) = root_path.parent() {
|
||||
match vfs_path.name_and_extension() {
|
||||
Some((name, _)) => {
|
||||
if name.starts_with(crate_name) {
|
||||
crate_path = vfs_path.to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
root_path = vfs_path;
|
||||
}
|
||||
crate_path
|
||||
}
|
||||
None => "".to_owned(),
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue