mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
switch CargoWorkspace to arena
This commit is contained in:
parent
1a7a68de16
commit
cbeaa541aa
3 changed files with 28 additions and 30 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -733,6 +733,7 @@ dependencies = [
|
||||||
"languageserver-types 0.53.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"languageserver-types 0.53.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"ra_arena 0.1.0",
|
||||||
"ra_ide_api 0.1.0",
|
"ra_ide_api 0.1.0",
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"ra_text_edit 0.1.0",
|
"ra_text_edit 0.1.0",
|
||||||
|
|
|
@ -30,6 +30,7 @@ thread_worker = { path = "../thread_worker" }
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_text_edit = { path = "../ra_text_edit" }
|
ra_text_edit = { path = "../ra_text_edit" }
|
||||||
ra_ide_api = { path = "../ra_ide_api" }
|
ra_ide_api = { path = "../ra_ide_api" }
|
||||||
|
ra_arena = { path = "../ra_arena" }
|
||||||
gen_lsp_server = { path = "../gen_lsp_server" }
|
gen_lsp_server = { path = "../gen_lsp_server" }
|
||||||
ra_vfs = { path = "../ra_vfs" }
|
ra_vfs = { path = "../ra_vfs" }
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use cargo_metadata::{metadata_run, CargoOpt};
|
use cargo_metadata::{metadata_run, CargoOpt};
|
||||||
use ra_syntax::SmolStr;
|
use ra_syntax::SmolStr;
|
||||||
|
use ra_arena::{Arena, RawId, impl_arena_id};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use failure::{format_err, bail};
|
use failure::{format_err, bail};
|
||||||
use thread_worker::{WorkerHandle, Worker};
|
use thread_worker::{WorkerHandle, Worker};
|
||||||
|
@ -17,14 +18,17 @@ use crate::Result;
|
||||||
/// concepts.
|
/// concepts.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CargoWorkspace {
|
pub struct CargoWorkspace {
|
||||||
packages: Vec<PackageData>,
|
packages: Arena<Package, PackageData>,
|
||||||
targets: Vec<TargetData>,
|
targets: Arena<Target, TargetData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Package(usize);
|
pub struct Package(RawId);
|
||||||
|
impl_arena_id!(Package);
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct Target(usize);
|
pub struct Target(RawId);
|
||||||
|
impl_arena_id!(Target);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct PackageData {
|
struct PackageData {
|
||||||
|
@ -61,38 +65,38 @@ pub enum TargetKind {
|
||||||
|
|
||||||
impl Package {
|
impl Package {
|
||||||
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
||||||
ws.pkg(self).name.as_str()
|
ws.packages[self].name.as_str()
|
||||||
}
|
}
|
||||||
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
||||||
ws.pkg(self).manifest.parent().unwrap()
|
ws.packages[self].manifest.parent().unwrap()
|
||||||
}
|
}
|
||||||
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
|
pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a {
|
||||||
ws.pkg(self).targets.iter().cloned()
|
ws.packages[self].targets.iter().cloned()
|
||||||
}
|
}
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub fn is_member(self, ws: &CargoWorkspace) -> bool {
|
pub fn is_member(self, ws: &CargoWorkspace) -> bool {
|
||||||
ws.pkg(self).is_member
|
ws.packages[self].is_member
|
||||||
}
|
}
|
||||||
pub fn dependencies<'a>(
|
pub fn dependencies<'a>(
|
||||||
self,
|
self,
|
||||||
ws: &'a CargoWorkspace,
|
ws: &'a CargoWorkspace,
|
||||||
) -> impl Iterator<Item = &'a PackageDependency> + 'a {
|
) -> impl Iterator<Item = &'a PackageDependency> + 'a {
|
||||||
ws.pkg(self).dependencies.iter()
|
ws.packages[self].dependencies.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Target {
|
impl Target {
|
||||||
pub fn package(self, ws: &CargoWorkspace) -> Package {
|
pub fn package(self, ws: &CargoWorkspace) -> Package {
|
||||||
ws.tgt(self).pkg
|
ws.targets[self].pkg
|
||||||
}
|
}
|
||||||
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
pub fn name(self, ws: &CargoWorkspace) -> &str {
|
||||||
ws.tgt(self).name.as_str()
|
ws.targets[self].name.as_str()
|
||||||
}
|
}
|
||||||
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
pub fn root(self, ws: &CargoWorkspace) -> &Path {
|
||||||
ws.tgt(self).root.as_path()
|
ws.targets[self].root.as_path()
|
||||||
}
|
}
|
||||||
pub fn kind(self, ws: &CargoWorkspace) -> TargetKind {
|
pub fn kind(self, ws: &CargoWorkspace) -> TargetKind {
|
||||||
ws.tgt(self).kind
|
ws.targets[self].kind
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,25 +110,24 @@ impl CargoWorkspace {
|
||||||
)
|
)
|
||||||
.map_err(|e| format_err!("cargo metadata failed: {}", e))?;
|
.map_err(|e| format_err!("cargo metadata failed: {}", e))?;
|
||||||
let mut pkg_by_id = FxHashMap::default();
|
let mut pkg_by_id = FxHashMap::default();
|
||||||
let mut packages = Vec::new();
|
let mut packages = Arena::default();
|
||||||
let mut targets = Vec::new();
|
let mut targets = Arena::default();
|
||||||
|
|
||||||
let ws_members = &meta.workspace_members;
|
let ws_members = &meta.workspace_members;
|
||||||
|
|
||||||
for meta_pkg in meta.packages {
|
for meta_pkg in meta.packages {
|
||||||
let pkg = Package(packages.len());
|
|
||||||
let is_member = ws_members.contains(&meta_pkg.id);
|
let is_member = ws_members.contains(&meta_pkg.id);
|
||||||
pkg_by_id.insert(meta_pkg.id.clone(), pkg);
|
let pkg = packages.alloc(PackageData {
|
||||||
let mut pkg_data = PackageData {
|
|
||||||
name: meta_pkg.name.into(),
|
name: meta_pkg.name.into(),
|
||||||
manifest: meta_pkg.manifest_path.clone(),
|
manifest: meta_pkg.manifest_path.clone(),
|
||||||
targets: Vec::new(),
|
targets: Vec::new(),
|
||||||
is_member,
|
is_member,
|
||||||
dependencies: Vec::new(),
|
dependencies: Vec::new(),
|
||||||
};
|
});
|
||||||
|
let pkg_data = &mut packages[pkg];
|
||||||
|
pkg_by_id.insert(meta_pkg.id.clone(), pkg);
|
||||||
for meta_tgt in meta_pkg.targets {
|
for meta_tgt in meta_pkg.targets {
|
||||||
let tgt = Target(targets.len());
|
let tgt = targets.alloc(TargetData {
|
||||||
targets.push(TargetData {
|
|
||||||
pkg,
|
pkg,
|
||||||
name: meta_tgt.name.into(),
|
name: meta_tgt.name.into(),
|
||||||
root: meta_tgt.src_path.clone(),
|
root: meta_tgt.src_path.clone(),
|
||||||
|
@ -132,7 +135,6 @@ impl CargoWorkspace {
|
||||||
});
|
});
|
||||||
pkg_data.targets.push(tgt);
|
pkg_data.targets.push(tgt);
|
||||||
}
|
}
|
||||||
packages.push(pkg_data)
|
|
||||||
}
|
}
|
||||||
let resolve = meta.resolve.expect("metadata executed with deps");
|
let resolve = meta.resolve.expect("metadata executed with deps");
|
||||||
for node in resolve.nodes {
|
for node in resolve.nodes {
|
||||||
|
@ -142,26 +144,20 @@ impl CargoWorkspace {
|
||||||
name: dep_node.name.into(),
|
name: dep_node.name.into(),
|
||||||
pkg: pkg_by_id[&dep_node.pkg],
|
pkg: pkg_by_id[&dep_node.pkg],
|
||||||
};
|
};
|
||||||
packages[source.0].dependencies.push(dep);
|
packages[source].dependencies.push(dep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(CargoWorkspace { packages, targets })
|
Ok(CargoWorkspace { packages, targets })
|
||||||
}
|
}
|
||||||
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a {
|
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a {
|
||||||
(0..self.packages.len()).map(Package)
|
self.packages.iter().map(|(id, _pkg)| id)
|
||||||
}
|
}
|
||||||
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
|
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
|
||||||
self.packages()
|
self.packages()
|
||||||
.filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root))
|
.filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root))
|
||||||
.next()
|
.next()
|
||||||
}
|
}
|
||||||
fn pkg(&self, pkg: Package) -> &PackageData {
|
|
||||||
&self.packages[pkg.0]
|
|
||||||
}
|
|
||||||
fn tgt(&self, tgt: Target) -> &TargetData {
|
|
||||||
&self.targets[tgt.0]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
fn find_cargo_toml(path: &Path) -> Result<PathBuf> {
|
||||||
|
|
Loading…
Reference in a new issue