diff options
Diffstat (limited to 'crates/ra_project_model/src/lib.rs')
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 87 |
1 files changed, 36 insertions, 51 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 6ca53c6d8..8053712ff 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -7,7 +7,6 @@ mod sysroot; | |||
7 | use std::{ | 7 | use std::{ |
8 | fs::{self, read_dir, ReadDir}, | 8 | fs::{self, read_dir, ReadDir}, |
9 | io, | 9 | io, |
10 | path::Path, | ||
11 | process::{Command, Output}, | 10 | process::{Command, Output}, |
12 | }; | 11 | }; |
13 | 12 | ||
@@ -36,30 +35,12 @@ pub enum ProjectWorkspace { | |||
36 | /// `PackageRoot` describes a package root folder. | 35 | /// `PackageRoot` describes a package root folder. |
37 | /// Which may be an external dependency, or a member of | 36 | /// Which may be an external dependency, or a member of |
38 | /// the current workspace. | 37 | /// the current workspace. |
39 | #[derive(Debug, Clone)] | 38 | #[derive(Debug, Clone, Eq, PartialEq, Hash)] |
40 | pub struct PackageRoot { | 39 | pub struct PackageRoot { |
41 | /// Path to the root folder | ||
42 | path: AbsPathBuf, | ||
43 | /// Is a member of the current workspace | 40 | /// Is a member of the current workspace |
44 | is_member: bool, | 41 | pub is_member: bool, |
45 | out_dir: Option<AbsPathBuf>, | 42 | pub include: Vec<AbsPathBuf>, |
46 | } | 43 | pub exclude: Vec<AbsPathBuf>, |
47 | impl PackageRoot { | ||
48 | pub fn new_member(path: AbsPathBuf) -> PackageRoot { | ||
49 | Self { path, is_member: true, out_dir: None } | ||
50 | } | ||
51 | pub fn new_non_member(path: AbsPathBuf) -> PackageRoot { | ||
52 | Self { path, is_member: false, out_dir: None } | ||
53 | } | ||
54 | pub fn path(&self) -> &AbsPath { | ||
55 | &self.path | ||
56 | } | ||
57 | pub fn out_dir(&self) -> Option<&AbsPath> { | ||
58 | self.out_dir.as_deref() | ||
59 | } | ||
60 | pub fn is_member(&self) -> bool { | ||
61 | self.is_member | ||
62 | } | ||
63 | } | 44 | } |
64 | 45 | ||
65 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] | 46 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] |
@@ -196,18 +177,40 @@ impl ProjectWorkspace { | |||
196 | /// the root is a member of the current workspace | 177 | /// the root is a member of the current workspace |
197 | pub fn to_roots(&self) -> Vec<PackageRoot> { | 178 | pub fn to_roots(&self) -> Vec<PackageRoot> { |
198 | match self { | 179 | match self { |
199 | ProjectWorkspace::Json { project } => { | 180 | ProjectWorkspace::Json { project } => project |
200 | project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect() | 181 | .crates |
201 | } | 182 | .iter() |
183 | .map(|krate| PackageRoot { | ||
184 | is_member: krate.is_workspace_member, | ||
185 | include: krate.include.clone(), | ||
186 | exclude: krate.exclude.clone(), | ||
187 | }) | ||
188 | .collect::<FxHashSet<_>>() | ||
189 | .into_iter() | ||
190 | .collect::<Vec<_>>(), | ||
202 | ProjectWorkspace::Cargo { cargo, sysroot } => cargo | 191 | ProjectWorkspace::Cargo { cargo, sysroot } => cargo |
203 | .packages() | 192 | .packages() |
204 | .map(|pkg| PackageRoot { | 193 | .map(|pkg| { |
205 | path: cargo[pkg].root().to_path_buf(), | 194 | let is_member = cargo[pkg].is_member; |
206 | is_member: cargo[pkg].is_member, | 195 | let pkg_root = cargo[pkg].root().to_path_buf(); |
207 | out_dir: cargo[pkg].out_dir.clone(), | 196 | |
197 | let mut include = vec![pkg_root.clone()]; | ||
198 | include.extend(cargo[pkg].out_dir.clone()); | ||
199 | |||
200 | let mut exclude = vec![pkg_root.join(".git")]; | ||
201 | if is_member { | ||
202 | exclude.push(pkg_root.join("target")); | ||
203 | } else { | ||
204 | exclude.push(pkg_root.join("tests")); | ||
205 | exclude.push(pkg_root.join("examples")); | ||
206 | exclude.push(pkg_root.join("benches")); | ||
207 | } | ||
208 | PackageRoot { is_member, include, exclude } | ||
208 | }) | 209 | }) |
209 | .chain(sysroot.crates().map(|krate| { | 210 | .chain(sysroot.crates().map(|krate| PackageRoot { |
210 | PackageRoot::new_non_member(sysroot[krate].root_dir().to_path_buf()) | 211 | is_member: false, |
212 | include: vec![sysroot[krate].root_dir().to_path_buf()], | ||
213 | exclude: Vec::new(), | ||
211 | })) | 214 | })) |
212 | .collect(), | 215 | .collect(), |
213 | } | 216 | } |
@@ -256,13 +259,7 @@ impl ProjectWorkspace { | |||
256 | let file_path = &krate.root_module; | 259 | let file_path = &krate.root_module; |
257 | let file_id = load(&file_path)?; | 260 | let file_id = load(&file_path)?; |
258 | 261 | ||
259 | let mut env = Env::default(); | 262 | let env = krate.env.clone().into_iter().collect(); |
260 | if let Some(out_dir) = &krate.out_dir { | ||
261 | // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!() | ||
262 | if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) { | ||
263 | env.set("OUT_DIR", out_dir); | ||
264 | } | ||
265 | } | ||
266 | let proc_macro = krate | 263 | let proc_macro = krate |
267 | .proc_macro_dylib_path | 264 | .proc_macro_dylib_path |
268 | .clone() | 265 | .clone() |
@@ -504,18 +501,6 @@ impl ProjectWorkspace { | |||
504 | } | 501 | } |
505 | crate_graph | 502 | crate_graph |
506 | } | 503 | } |
507 | |||
508 | pub fn workspace_root_for(&self, path: &Path) -> Option<&AbsPath> { | ||
509 | match self { | ||
510 | ProjectWorkspace::Cargo { cargo, .. } => { | ||
511 | Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) | ||
512 | } | ||
513 | ProjectWorkspace::Json { project: ProjectJson { roots, .. }, .. } => roots | ||
514 | .iter() | ||
515 | .find(|root| path.starts_with(&root.path)) | ||
516 | .map(|root| root.path.as_path()), | ||
517 | } | ||
518 | } | ||
519 | } | 504 | } |
520 | 505 | ||
521 | fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions { | 506 | fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions { |