Skip to content

Commit 39a2bc5

Browse files
committed
Expose package roots more directly
1 parent 818aeb8 commit 39a2bc5

File tree

3 files changed

+50
-54
lines changed

3 files changed

+50
-54
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,10 @@ pub enum ProjectWorkspace {
3737
/// the current workspace.
3838
#[derive(Debug, Clone)]
3939
pub struct PackageRoot {
40-
/// Path to the root folder
41-
path: AbsPathBuf,
4240
/// Is a member of the current workspace
43-
is_member: bool,
44-
out_dir: Option<AbsPathBuf>,
45-
}
46-
impl PackageRoot {
47-
pub fn new_member(path: AbsPathBuf) -> PackageRoot {
48-
Self { path, is_member: true, out_dir: None }
49-
}
50-
pub fn new_non_member(path: AbsPathBuf) -> PackageRoot {
51-
Self { path, is_member: false, out_dir: None }
52-
}
53-
pub fn path(&self) -> &AbsPath {
54-
&self.path
55-
}
56-
pub fn out_dir(&self) -> Option<&AbsPath> {
57-
self.out_dir.as_deref()
58-
}
59-
pub fn is_member(&self) -> bool {
60-
self.is_member
61-
}
41+
pub is_member: bool,
42+
pub include: Vec<AbsPathBuf>,
43+
pub exclude: Vec<AbsPathBuf>,
6244
}
6345

6446
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
@@ -195,18 +177,38 @@ impl ProjectWorkspace {
195177
/// the root is a member of the current workspace
196178
pub fn to_roots(&self) -> Vec<PackageRoot> {
197179
match self {
198-
ProjectWorkspace::Json { project } => {
199-
project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect()
200-
}
180+
ProjectWorkspace::Json { project } => project
181+
.roots
182+
.iter()
183+
.map(|r| {
184+
let path = r.path.clone();
185+
let include = vec![path];
186+
PackageRoot { is_member: true, include, exclude: Vec::new() }
187+
})
188+
.collect(),
201189
ProjectWorkspace::Cargo { cargo, sysroot } => cargo
202190
.packages()
203-
.map(|pkg| PackageRoot {
204-
path: cargo[pkg].root().to_path_buf(),
205-
is_member: cargo[pkg].is_member,
206-
out_dir: cargo[pkg].out_dir.clone(),
191+
.map(|pkg| {
192+
let is_member = cargo[pkg].is_member;
193+
let pkg_root = cargo[pkg].root().to_path_buf();
194+
195+
let mut include = vec![pkg_root.clone()];
196+
include.extend(cargo[pkg].out_dir.clone());
197+
198+
let mut exclude = vec![pkg_root.join(".git")];
199+
if is_member {
200+
exclude.push(pkg_root.join("target"));
201+
} else {
202+
exclude.push(pkg_root.join("tests"));
203+
exclude.push(pkg_root.join("examples"));
204+
exclude.push(pkg_root.join("benches"));
205+
}
206+
PackageRoot { is_member, include, exclude }
207207
})
208-
.chain(sysroot.crates().map(|krate| {
209-
PackageRoot::new_non_member(sysroot[krate].root_dir().to_path_buf())
208+
.chain(sysroot.crates().map(|krate| PackageRoot {
209+
is_member: false,
210+
include: vec![sysroot[krate].root_dir().to_path_buf()],
211+
exclude: Vec::new(),
210212
}))
211213
.collect(),
212214
}

crates/rust-analyzer/src/reload.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use flycheck::FlycheckHandle;
55
use ra_db::{CrateGraph, SourceRoot, VfsPath};
66
use ra_ide::AnalysisChange;
77
use ra_prof::profile;
8-
use ra_project_model::{PackageRoot, ProcMacroClient, ProjectWorkspace};
8+
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
99
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
1010

1111
use crate::{
@@ -149,8 +149,10 @@ impl GlobalState {
149149
watchers: workspaces
150150
.iter()
151151
.flat_map(ProjectWorkspace::to_roots)
152-
.filter(PackageRoot::is_member)
153-
.map(|root| format!("{}/**/*.rs", root.path().display()))
152+
.filter(|it| it.is_member)
153+
.flat_map(|root| {
154+
root.include.into_iter().map(|it| format!("{}/**/*.rs", it.display()))
155+
})
154156
.map(|glob_pattern| lsp_types::FileSystemWatcher { glob_pattern, kind: None })
155157
.collect(),
156158
};
@@ -261,31 +263,23 @@ impl ProjectFolders {
261263
let mut local_filesets = vec![];
262264

263265
for root in workspaces.iter().flat_map(|it| it.to_roots()) {
264-
let path = root.path().to_owned();
265-
266-
let mut file_set_roots: Vec<VfsPath> = vec![];
266+
let file_set_roots: Vec<VfsPath> =
267+
root.include.iter().cloned().map(VfsPath::from).collect();
267268

268-
let entry = if root.is_member() {
269-
vfs::loader::Entry::local_cargo_package(path.to_path_buf())
270-
} else {
271-
vfs::loader::Entry::cargo_package_dependency(path.to_path_buf())
269+
let entry = {
270+
let mut dirs = vfs::loader::Directories::default();
271+
dirs.extensions.push("rs".into());
272+
dirs.include.extend(root.include);
273+
dirs.exclude.extend(root.exclude);
274+
vfs::loader::Entry::Directories(dirs)
272275
};
273-
res.load.push(entry);
274-
if root.is_member() {
275-
res.watch.push(res.load.len() - 1);
276-
}
277276

278-
if let Some(out_dir) = root.out_dir() {
279-
let out_dir = out_dir.to_path_buf();
280-
res.load.push(vfs::loader::Entry::rs_files_recursively(out_dir.clone()));
281-
if root.is_member() {
282-
res.watch.push(res.load.len() - 1);
283-
}
284-
file_set_roots.push(out_dir.into());
277+
if root.is_member {
278+
res.watch.push(res.load.len());
285279
}
286-
file_set_roots.push(path.to_path_buf().into());
280+
res.load.push(entry);
287281

288-
if root.is_member() {
282+
if root.is_member {
289283
local_filesets.push(fsc.len());
290284
}
291285
fsc.add_file_set(file_set_roots)

crates/vfs/src/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum Entry {
1717
/// * it is not under `exclude` path
1818
///
1919
/// If many include/exclude paths match, the longest one wins.
20-
#[derive(Debug, Clone)]
20+
#[derive(Debug, Clone, Default)]
2121
pub struct Directories {
2222
pub extensions: Vec<String>,
2323
pub include: Vec<AbsPathBuf>,

0 commit comments

Comments
 (0)