mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Merge #10434
10434: Allow `Locate parent module` command in Cargo.toml r=Veykril a=rainy-me close #10355 Co-authored-by: rainy-me <github@rainy.me> Co-authored-by: rainy-me <github@yue.coffee>
This commit is contained in:
commit
f87debcf87
5 changed files with 108 additions and 9 deletions
|
@ -1,6 +1,6 @@
|
|||
//! See [`CargoWorkspace`].
|
||||
|
||||
use std::convert::TryInto;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::iter;
|
||||
use std::path::PathBuf;
|
||||
use std::{ops, process::Command};
|
||||
|
@ -400,6 +400,39 @@ impl CargoWorkspace {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>> {
|
||||
let mut found = false;
|
||||
let parent_manifests = self
|
||||
.packages()
|
||||
.filter_map(|pkg| {
|
||||
if !found && &self[pkg].manifest == manifest_path {
|
||||
found = true
|
||||
}
|
||||
self[pkg].dependencies.iter().find_map(|dep| {
|
||||
if &self[dep.pkg].manifest == manifest_path {
|
||||
return Some(self[pkg].manifest.clone());
|
||||
}
|
||||
None
|
||||
})
|
||||
})
|
||||
.collect::<Vec<ManifestPath>>();
|
||||
|
||||
// some packages has this pkg as dep. return their manifests
|
||||
if parent_manifests.len() > 0 {
|
||||
return Some(parent_manifests);
|
||||
}
|
||||
|
||||
// this pkg is inside this cargo workspace, fallback to workspace root
|
||||
if found {
|
||||
return Some(vec![
|
||||
ManifestPath::try_from(self.workspace_root().join("Cargo.toml")).ok()?
|
||||
]);
|
||||
}
|
||||
|
||||
// not in this workspace
|
||||
None
|
||||
}
|
||||
|
||||
fn is_unique(&self, name: &str) -> bool {
|
||||
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue