Skip to content

Commit fe87aec

Browse files
committed
Replace roots with include/exclude directories
1 parent 39a2bc5 commit fe87aec

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod sysroot;
77
use std::{
88
fs::{self, read_dir, ReadDir},
99
io,
10-
path::Path,
1110
process::{Command, Output},
1211
};
1312

@@ -35,7 +34,7 @@ pub enum ProjectWorkspace {
3534
/// `PackageRoot` describes a package root folder.
3635
/// Which may be an external dependency, or a member of
3736
/// the current workspace.
38-
#[derive(Debug, Clone)]
37+
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
3938
pub struct PackageRoot {
4039
/// Is a member of the current workspace
4140
pub is_member: bool,
@@ -178,14 +177,16 @@ impl ProjectWorkspace {
178177
pub fn to_roots(&self) -> Vec<PackageRoot> {
179178
match self {
180179
ProjectWorkspace::Json { project } => project
181-
.roots
180+
.crates
182181
.iter()
183-
.map(|r| {
184-
let path = r.path.clone();
185-
let include = vec![path];
186-
PackageRoot { is_member: true, include, exclude: Vec::new() }
182+
.map(|krate| PackageRoot {
183+
is_member: krate.is_workspace_member,
184+
include: krate.include.clone(),
185+
exclude: krate.exclude.clone(),
187186
})
188-
.collect(),
187+
.collect::<FxHashSet<_>>()
188+
.into_iter()
189+
.collect::<Vec<_>>(),
189190
ProjectWorkspace::Cargo { cargo, sysroot } => cargo
190191
.packages()
191192
.map(|pkg| {
@@ -505,18 +506,6 @@ impl ProjectWorkspace {
505506
}
506507
crate_graph
507508
}
508-
509-
pub fn workspace_root_for(&self, path: &Path) -> Option<&AbsPath> {
510-
match self {
511-
ProjectWorkspace::Cargo { cargo, .. } => {
512-
Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
513-
}
514-
ProjectWorkspace::Json { project: ProjectJson { roots, .. }, .. } => roots
515-
.iter()
516-
.find(|root| path.starts_with(&root.path))
517-
.map(|root| root.path.as_path()),
518-
}
519-
}
520509
}
521510

522511
fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions {

crates/ra_project_model/src/project_json.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@ use stdx::split_delim;
1212
/// Roots and crates that compose this Rust project.
1313
#[derive(Clone, Debug, Eq, PartialEq)]
1414
pub struct ProjectJson {
15-
pub(crate) roots: Vec<Root>,
1615
pub(crate) crates: Vec<Crate>,
1716
}
1817

19-
/// A root points to the directory which contains Rust crates. rust-analyzer watches all files in
20-
/// all roots. Roots might be nested.
21-
#[derive(Clone, Debug, Eq, PartialEq)]
22-
pub struct Root {
23-
pub(crate) path: AbsPathBuf,
24-
}
25-
2618
/// A crate points to the root module of a crate and lists the dependencies of the crate. This is
2719
/// useful in creating the crate graph.
2820
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -35,12 +27,13 @@ pub struct Crate {
3527
pub(crate) out_dir: Option<AbsPathBuf>,
3628
pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
3729
pub(crate) is_workspace_member: bool,
30+
pub(crate) include: Vec<AbsPathBuf>,
31+
pub(crate) exclude: Vec<AbsPathBuf>,
3832
}
3933

4034
impl ProjectJson {
4135
pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
4236
ProjectJson {
43-
roots: data.roots.into_iter().map(|path| Root { path: base.join(path) }).collect(),
4437
crates: data
4538
.crates
4639
.into_iter()
@@ -50,8 +43,19 @@ impl ProjectJson {
5043
&& !crate_data.root_module.starts_with("..")
5144
|| crate_data.root_module.starts_with(base)
5245
});
46+
let root_module = base.join(crate_data.root_module);
47+
let (include, exclude) = match crate_data.source {
48+
Some(src) => {
49+
let absolutize = |dirs: Vec<PathBuf>| {
50+
dirs.into_iter().map(|it| base.join(it)).collect::<Vec<_>>()
51+
};
52+
(absolutize(src.include_dirs), absolutize(src.exclude_dirs))
53+
}
54+
None => (vec![root_module.parent().unwrap().to_path_buf()], Vec::new()),
55+
};
56+
5357
Crate {
54-
root_module: base.join(crate_data.root_module),
58+
root_module,
5559
edition: crate_data.edition.into(),
5660
deps: crate_data
5761
.deps
@@ -79,6 +83,8 @@ impl ProjectJson {
7983
.proc_macro_dylib_path
8084
.map(|it| base.join(it)),
8185
is_workspace_member,
86+
include,
87+
exclude,
8288
}
8389
})
8490
.collect::<Vec<_>>(),
@@ -88,7 +94,6 @@ impl ProjectJson {
8894

8995
#[derive(Deserialize)]
9096
pub struct ProjectJsonData {
91-
roots: Vec<PathBuf>,
9297
crates: Vec<CrateData>,
9398
}
9499

@@ -103,6 +108,7 @@ struct CrateData {
103108
out_dir: Option<PathBuf>,
104109
proc_macro_dylib_path: Option<PathBuf>,
105110
is_workspace_member: Option<bool>,
111+
source: Option<CrateSource>,
106112
}
107113

108114
#[derive(Deserialize)]
@@ -132,6 +138,12 @@ struct DepData {
132138
name: CrateName,
133139
}
134140

141+
#[derive(Deserialize)]
142+
struct CrateSource {
143+
include_dirs: Vec<PathBuf>,
144+
exclude_dirs: Vec<PathBuf>,
145+
}
146+
135147
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
136148
where
137149
D: de::Deserializer<'de>,

0 commit comments

Comments
 (0)