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