diff options
Diffstat (limited to 'crates/ra_lsp_server/src/project_model/cargo_workspace.rs')
-rw-r--r-- | crates/ra_lsp_server/src/project_model/cargo_workspace.rs | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs index e69de29bb..138fdee22 100644 --- a/crates/ra_lsp_server/src/project_model/cargo_workspace.rs +++ b/crates/ra_lsp_server/src/project_model/cargo_workspace.rs | |||
@@ -0,0 +1,173 @@ | |||
1 | use std::{ | ||
2 | path::{Path, PathBuf}, | ||
3 | }; | ||
4 | |||
5 | use cargo_metadata::{metadata_run, CargoOpt}; | ||
6 | use ra_syntax::SmolStr; | ||
7 | use ra_arena::{Arena, RawId, impl_arena_id}; | ||
8 | use rustc_hash::FxHashMap; | ||
9 | use failure::format_err; | ||
10 | |||
11 | use crate::Result; | ||
12 | |||
13 | /// `CargoWorksapce` represents the logical structure of, well, a Cargo | ||
14 | /// workspace. It pretty closely mirrors `cargo metadata` output. | ||
15 | /// | ||
16 | /// Note that internally, rust analyzer uses a differnet structure: | ||
17 | /// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates, | ||
18 | /// while this knows about `Pacakges` & `Targets`: purely cargo-related | ||
19 | /// concepts. | ||
20 | #[derive(Debug, Clone)] | ||
21 | pub struct CargoWorkspace { | ||
22 | packages: Arena<Package, PackageData>, | ||
23 | targets: Arena<Target, TargetData>, | ||
24 | } | ||
25 | |||
26 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
27 | pub struct Package(RawId); | ||
28 | impl_arena_id!(Package); | ||
29 | |||
30 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
31 | pub struct Target(RawId); | ||
32 | impl_arena_id!(Target); | ||
33 | |||
34 | #[derive(Debug, Clone)] | ||
35 | struct PackageData { | ||
36 | name: SmolStr, | ||
37 | manifest: PathBuf, | ||
38 | targets: Vec<Target>, | ||
39 | is_member: bool, | ||
40 | dependencies: Vec<PackageDependency>, | ||
41 | } | ||
42 | |||
43 | #[derive(Debug, Clone)] | ||
44 | pub struct PackageDependency { | ||
45 | pub pkg: Package, | ||
46 | pub name: SmolStr, | ||
47 | } | ||
48 | |||
49 | #[derive(Debug, Clone)] | ||
50 | struct TargetData { | ||
51 | pkg: Package, | ||
52 | name: SmolStr, | ||
53 | root: PathBuf, | ||
54 | kind: TargetKind, | ||
55 | } | ||
56 | |||
57 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
58 | pub enum TargetKind { | ||
59 | Bin, | ||
60 | Lib, | ||
61 | Example, | ||
62 | Test, | ||
63 | Bench, | ||
64 | Other, | ||
65 | } | ||
66 | |||
67 | impl TargetKind { | ||
68 | fn new(kinds: &[String]) -> TargetKind { | ||
69 | for kind in kinds { | ||
70 | return match kind.as_str() { | ||
71 | "bin" => TargetKind::Bin, | ||
72 | "test" => TargetKind::Test, | ||
73 | "bench" => TargetKind::Bench, | ||
74 | "example" => TargetKind::Example, | ||
75 | _ if kind.contains("lib") => TargetKind::Lib, | ||
76 | _ => continue, | ||
77 | }; | ||
78 | } | ||
79 | TargetKind::Other | ||
80 | } | ||
81 | } | ||
82 | |||
83 | impl Package { | ||
84 | pub fn name(self, ws: &CargoWorkspace) -> &str { | ||
85 | ws.packages[self].name.as_str() | ||
86 | } | ||
87 | pub fn root(self, ws: &CargoWorkspace) -> &Path { | ||
88 | ws.packages[self].manifest.parent().unwrap() | ||
89 | } | ||
90 | pub fn targets<'a>(self, ws: &'a CargoWorkspace) -> impl Iterator<Item = Target> + 'a { | ||
91 | ws.packages[self].targets.iter().cloned() | ||
92 | } | ||
93 | #[allow(unused)] | ||
94 | pub fn is_member(self, ws: &CargoWorkspace) -> bool { | ||
95 | ws.packages[self].is_member | ||
96 | } | ||
97 | pub fn dependencies<'a>( | ||
98 | self, | ||
99 | ws: &'a CargoWorkspace, | ||
100 | ) -> impl Iterator<Item = &'a PackageDependency> + 'a { | ||
101 | ws.packages[self].dependencies.iter() | ||
102 | } | ||
103 | } | ||
104 | |||
105 | impl Target { | ||
106 | pub fn package(self, ws: &CargoWorkspace) -> Package { | ||
107 | ws.targets[self].pkg | ||
108 | } | ||
109 | pub fn name(self, ws: &CargoWorkspace) -> &str { | ||
110 | ws.targets[self].name.as_str() | ||
111 | } | ||
112 | pub fn root(self, ws: &CargoWorkspace) -> &Path { | ||
113 | ws.targets[self].root.as_path() | ||
114 | } | ||
115 | pub fn kind(self, ws: &CargoWorkspace) -> TargetKind { | ||
116 | ws.targets[self].kind | ||
117 | } | ||
118 | } | ||
119 | |||
120 | impl CargoWorkspace { | ||
121 | pub fn from_cargo_metadata(cargo_toml: &Path) -> Result<CargoWorkspace> { | ||
122 | let meta = metadata_run(Some(cargo_toml), true, Some(CargoOpt::AllFeatures)) | ||
123 | .map_err(|e| format_err!("cargo metadata failed: {}", e))?; | ||
124 | let mut pkg_by_id = FxHashMap::default(); | ||
125 | let mut packages = Arena::default(); | ||
126 | let mut targets = Arena::default(); | ||
127 | |||
128 | let ws_members = &meta.workspace_members; | ||
129 | |||
130 | for meta_pkg in meta.packages { | ||
131 | let is_member = ws_members.contains(&meta_pkg.id); | ||
132 | let pkg = packages.alloc(PackageData { | ||
133 | name: meta_pkg.name.into(), | ||
134 | manifest: meta_pkg.manifest_path.clone(), | ||
135 | targets: Vec::new(), | ||
136 | is_member, | ||
137 | dependencies: Vec::new(), | ||
138 | }); | ||
139 | let pkg_data = &mut packages[pkg]; | ||
140 | pkg_by_id.insert(meta_pkg.id.clone(), pkg); | ||
141 | for meta_tgt in meta_pkg.targets { | ||
142 | let tgt = targets.alloc(TargetData { | ||
143 | pkg, | ||
144 | name: meta_tgt.name.into(), | ||
145 | root: meta_tgt.src_path.clone(), | ||
146 | kind: TargetKind::new(meta_tgt.kind.as_slice()), | ||
147 | }); | ||
148 | pkg_data.targets.push(tgt); | ||
149 | } | ||
150 | } | ||
151 | let resolve = meta.resolve.expect("metadata executed with deps"); | ||
152 | for node in resolve.nodes { | ||
153 | let source = pkg_by_id[&node.id]; | ||
154 | for dep_node in node.deps { | ||
155 | let dep = PackageDependency { | ||
156 | name: dep_node.name.into(), | ||
157 | pkg: pkg_by_id[&dep_node.pkg], | ||
158 | }; | ||
159 | packages[source].dependencies.push(dep); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | Ok(CargoWorkspace { packages, targets }) | ||
164 | } | ||
165 | pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a { | ||
166 | self.packages.iter().map(|(id, _pkg)| id) | ||
167 | } | ||
168 | pub fn target_by_root(&self, root: &Path) -> Option<Target> { | ||
169 | self.packages() | ||
170 | .filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)) | ||
171 | .next() | ||
172 | } | ||
173 | } | ||