aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-21 10:56:05 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-21 10:56:05 +0000
commitb03883279528986bd829e14f967db4b4c980049d (patch)
treeb891f591e240e84d14dc8060e3706ee025cc0906
parent1eb3bf41d75a8c744a0ecb586d4a2e92a1175c08 (diff)
parentc6d6e6c6259709ec30eadb79a1908dca707a6499 (diff)
Merge #997
997: Improve filtering of file roots r=matklad a=vipentti `ProjectWorkspace::to_roots` now returns a new `ProjectRoot` which contains information regarding whether or not the given path is part of the current workspace or an external dependency. This information can then be used in `ra_batch` and `ra_lsp_server` to implement more advanced filtering. This allows us to filter some unnecessary folders from external dependencies such as tests, examples and benches. Relates to discussion in #869 Co-authored-by: Ville Penttinen <[email protected]>
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_batch/src/lib.rs36
-rw-r--r--crates/ra_batch/src/vfs_filter.rs54
-rw-r--r--crates/ra_lsp_server/src/lib.rs1
-rw-r--r--crates/ra_lsp_server/src/server_world.rs34
-rw-r--r--crates/ra_lsp_server/src/vfs_filter.rs54
-rw-r--r--crates/ra_project_model/Cargo.toml1
-rw-r--r--crates/ra_project_model/src/lib.rs61
8 files changed, 180 insertions, 62 deletions
diff --git a/Cargo.lock b/Cargo.lock
index eef6fa621..50fe1fa23 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1089,6 +1089,7 @@ dependencies = [
1089 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 1089 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
1090 "ra_arena 0.1.0", 1090 "ra_arena 0.1.0",
1091 "ra_db 0.1.0", 1091 "ra_db 0.1.0",
1092 "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
1092 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 1093 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
1093 "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", 1094 "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
1094 "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", 1095 "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs
index a054d0da3..deb9f95d7 100644
--- a/crates/ra_batch/src/lib.rs
+++ b/crates/ra_batch/src/lib.rs
@@ -1,5 +1,7 @@
1mod vfs_filter;
2
1use std::sync::Arc; 3use std::sync::Arc;
2use std::path::{Path, PathBuf}; 4use std::path::Path;
3use std::collections::HashSet; 5use std::collections::HashSet;
4 6
5use rustc_hash::FxHashMap; 7use rustc_hash::FxHashMap;
@@ -9,7 +11,8 @@ use ra_db::{
9}; 11};
10use ra_hir::{db, HirInterner}; 12use ra_hir::{db, HirInterner};
11use ra_project_model::ProjectWorkspace; 13use ra_project_model::ProjectWorkspace;
12use ra_vfs::{Vfs, VfsChange, RootEntry, Filter, RelativePath}; 14use ra_vfs::{Vfs, VfsChange};
15use vfs_filter::IncludeRustFiles;
13 16
14type Result<T> = std::result::Result<T, failure::Error>; 17type Result<T> = std::result::Result<T, failure::Error>;
15 18
@@ -43,30 +46,6 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
43 SourceRootId(r.0.into()) 46 SourceRootId(r.0.into())
44} 47}
45 48
46struct IncludeRustFiles;
47
48impl IncludeRustFiles {
49 fn to_entry(path: PathBuf) -> RootEntry {
50 RootEntry::new(path, Box::new(Self {}))
51 }
52}
53
54impl Filter for IncludeRustFiles {
55 fn include_dir(&self, dir_path: &RelativePath) -> bool {
56 const IGNORED_FOLDERS: &[&str] = &["node_modules", "target", ".git"];
57
58 let is_ignored = dir_path.components().any(|c| IGNORED_FOLDERS.contains(&c.as_str()));
59
60 let hidden = dir_path.components().any(|c| c.as_str().starts_with("."));
61
62 !is_ignored && !hidden
63 }
64
65 fn include_file(&self, file_path: &RelativePath) -> bool {
66 file_path.extension() == Some("rs")
67 }
68}
69
70impl BatchDatabase { 49impl BatchDatabase {
71 pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase { 50 pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase {
72 let mut db = 51 let mut db =
@@ -122,9 +101,8 @@ impl BatchDatabase {
122 let root = std::env::current_dir()?.join(root); 101 let root = std::env::current_dir()?.join(root);
123 let ws = ProjectWorkspace::discover(root.as_ref())?; 102 let ws = ProjectWorkspace::discover(root.as_ref())?;
124 let mut roots = Vec::new(); 103 let mut roots = Vec::new();
125 roots.push(root.clone()); 104 roots.push(IncludeRustFiles::member(root.clone()));
126 roots.extend(ws.to_roots()); 105 roots.extend(IncludeRustFiles::from_roots(ws.to_roots()));
127 let roots = roots.into_iter().map(IncludeRustFiles::to_entry).collect::<Vec<_>>();
128 let (mut vfs, roots) = Vfs::new(roots); 106 let (mut vfs, roots) = Vfs::new(roots);
129 let mut load = |path: &Path| { 107 let mut load = |path: &Path| {
130 let vfs_file = vfs.load(path); 108 let vfs_file = vfs.load(path);
diff --git a/crates/ra_batch/src/vfs_filter.rs b/crates/ra_batch/src/vfs_filter.rs
new file mode 100644
index 000000000..dd20c1203
--- /dev/null
+++ b/crates/ra_batch/src/vfs_filter.rs
@@ -0,0 +1,54 @@
1use std::path::PathBuf;
2use ra_project_model::ProjectRoot;
3use ra_vfs::{RootEntry, Filter, RelativePath};
4
5/// `IncludeRustFiles` is used to convert
6/// from `ProjectRoot` to `RootEntry` for VFS
7pub struct IncludeRustFiles {
8 root: ProjectRoot,
9}
10
11impl IncludeRustFiles {
12 pub fn from_roots<R>(roots: R) -> impl Iterator<Item = RootEntry>
13 where
14 R: IntoIterator<Item = ProjectRoot>,
15 {
16 roots.into_iter().map(IncludeRustFiles::from_root)
17 }
18
19 pub fn from_root(root: ProjectRoot) -> RootEntry {
20 IncludeRustFiles::from(root).into()
21 }
22
23 #[allow(unused)]
24 pub fn external(path: PathBuf) -> RootEntry {
25 IncludeRustFiles::from_root(ProjectRoot::new(path, false))
26 }
27
28 pub fn member(path: PathBuf) -> RootEntry {
29 IncludeRustFiles::from_root(ProjectRoot::new(path, true))
30 }
31}
32
33impl Filter for IncludeRustFiles {
34 fn include_dir(&self, dir_path: &RelativePath) -> bool {
35 self.root.include_dir(dir_path)
36 }
37
38 fn include_file(&self, file_path: &RelativePath) -> bool {
39 self.root.include_file(file_path)
40 }
41}
42
43impl std::convert::From<ProjectRoot> for IncludeRustFiles {
44 fn from(v: ProjectRoot) -> IncludeRustFiles {
45 IncludeRustFiles { root: v }
46 }
47}
48
49impl std::convert::From<IncludeRustFiles> for RootEntry {
50 fn from(v: IncludeRustFiles) -> RootEntry {
51 let path = v.root.path().clone();
52 RootEntry::new(path, Box::new(v))
53 }
54}
diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs
index 59e16a47c..113883bdd 100644
--- a/crates/ra_lsp_server/src/lib.rs
+++ b/crates/ra_lsp_server/src/lib.rs
@@ -4,6 +4,7 @@ mod conv;
4mod main_loop; 4mod main_loop;
5mod markdown; 5mod markdown;
6mod project_model; 6mod project_model;
7mod vfs_filter;
7pub mod req; 8pub mod req;
8pub mod init; 9pub mod init;
9mod server_world; 10mod server_world;
diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs
index cf7c17c5c..af4798494 100644
--- a/crates/ra_lsp_server/src/server_world.rs
+++ b/crates/ra_lsp_server/src/server_world.rs
@@ -8,13 +8,14 @@ use ra_ide_api::{
8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, 8 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData,
9 SourceRootId 9 SourceRootId
10}; 10};
11use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot, RootEntry, Filter}; 11use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot};
12use relative_path::{RelativePath, RelativePathBuf}; 12use relative_path::RelativePathBuf;
13use parking_lot::RwLock; 13use parking_lot::RwLock;
14use failure::format_err; 14use failure::format_err;
15 15
16use crate::{ 16use crate::{
17 project_model::ProjectWorkspace, 17 project_model::ProjectWorkspace,
18 vfs_filter::IncludeRustFiles,
18 Result, 19 Result,
19}; 20};
20 21
@@ -33,40 +34,15 @@ pub struct ServerWorld {
33 pub vfs: Arc<RwLock<Vfs>>, 34 pub vfs: Arc<RwLock<Vfs>>,
34} 35}
35 36
36struct IncludeRustFiles;
37
38impl IncludeRustFiles {
39 fn to_entry(path: PathBuf) -> RootEntry {
40 RootEntry::new(path, Box::new(Self {}))
41 }
42}
43
44impl Filter for IncludeRustFiles {
45 fn include_dir(&self, dir_path: &RelativePath) -> bool {
46 const IGNORED_FOLDERS: &[&str] = &["node_modules", "target", ".git"];
47
48 let is_ignored = dir_path.components().any(|c| IGNORED_FOLDERS.contains(&c.as_str()));
49
50 let hidden = dir_path.components().any(|c| c.as_str().starts_with("."));
51
52 !is_ignored && !hidden
53 }
54
55 fn include_file(&self, file_path: &RelativePath) -> bool {
56 file_path.extension() == Some("rs")
57 }
58}
59
60impl ServerWorldState { 37impl ServerWorldState {
61 pub fn new(root: PathBuf, workspaces: Vec<ProjectWorkspace>) -> ServerWorldState { 38 pub fn new(root: PathBuf, workspaces: Vec<ProjectWorkspace>) -> ServerWorldState {
62 let mut change = AnalysisChange::new(); 39 let mut change = AnalysisChange::new();
63 40
64 let mut roots = Vec::new(); 41 let mut roots = Vec::new();
65 roots.push(root.clone()); 42 roots.push(IncludeRustFiles::member(root.clone()));
66 for ws in workspaces.iter() { 43 for ws in workspaces.iter() {
67 roots.extend(ws.to_roots()); 44 roots.extend(IncludeRustFiles::from_roots(ws.to_roots()));
68 } 45 }
69 let roots = roots.into_iter().map(IncludeRustFiles::to_entry).collect::<Vec<_>>();
70 46
71 let (mut vfs, roots) = Vfs::new(roots); 47 let (mut vfs, roots) = Vfs::new(roots);
72 let roots_to_scan = roots.len(); 48 let roots_to_scan = roots.len();
diff --git a/crates/ra_lsp_server/src/vfs_filter.rs b/crates/ra_lsp_server/src/vfs_filter.rs
new file mode 100644
index 000000000..dd20c1203
--- /dev/null
+++ b/crates/ra_lsp_server/src/vfs_filter.rs
@@ -0,0 +1,54 @@
1use std::path::PathBuf;
2use ra_project_model::ProjectRoot;
3use ra_vfs::{RootEntry, Filter, RelativePath};
4
5/// `IncludeRustFiles` is used to convert
6/// from `ProjectRoot` to `RootEntry` for VFS
7pub struct IncludeRustFiles {
8 root: ProjectRoot,
9}
10
11impl IncludeRustFiles {
12 pub fn from_roots<R>(roots: R) -> impl Iterator<Item = RootEntry>
13 where
14 R: IntoIterator<Item = ProjectRoot>,
15 {
16 roots.into_iter().map(IncludeRustFiles::from_root)
17 }
18
19 pub fn from_root(root: ProjectRoot) -> RootEntry {
20 IncludeRustFiles::from(root).into()
21 }
22
23 #[allow(unused)]
24 pub fn external(path: PathBuf) -> RootEntry {
25 IncludeRustFiles::from_root(ProjectRoot::new(path, false))
26 }
27
28 pub fn member(path: PathBuf) -> RootEntry {
29 IncludeRustFiles::from_root(ProjectRoot::new(path, true))
30 }
31}
32
33impl Filter for IncludeRustFiles {
34 fn include_dir(&self, dir_path: &RelativePath) -> bool {
35 self.root.include_dir(dir_path)
36 }
37
38 fn include_file(&self, file_path: &RelativePath) -> bool {
39 self.root.include_file(file_path)
40 }
41}
42
43impl std::convert::From<ProjectRoot> for IncludeRustFiles {
44 fn from(v: ProjectRoot) -> IncludeRustFiles {
45 IncludeRustFiles { root: v }
46 }
47}
48
49impl std::convert::From<IncludeRustFiles> for RootEntry {
50 fn from(v: IncludeRustFiles) -> RootEntry {
51 let path = v.root.path().clone();
52 RootEntry::new(path, Box::new(v))
53 }
54}
diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml
index 34d33531e..cf4adf35c 100644
--- a/crates/ra_project_model/Cargo.toml
+++ b/crates/ra_project_model/Cargo.toml
@@ -7,6 +7,7 @@ authors = ["rust-analyzer developers"]
7[dependencies] 7[dependencies]
8log = "0.4.5" 8log = "0.4.5"
9rustc-hash = "1.0" 9rustc-hash = "1.0"
10relative-path = "0.4.0"
10 11
11failure = "0.1.4" 12failure = "0.1.4"
12 13
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index c566ec0fb..6f46a2d43 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -15,6 +15,8 @@ use ra_db::{CrateGraph, FileId, Edition};
15 15
16use serde_json::from_reader; 16use serde_json::from_reader;
17 17
18use relative_path::RelativePath;
19
18pub use crate::{ 20pub use crate::{
19 cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, 21 cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
20 json_project::JsonProject, 22 json_project::JsonProject,
@@ -32,6 +34,52 @@ pub enum ProjectWorkspace {
32 Json { project: JsonProject }, 34 Json { project: JsonProject },
33} 35}
34 36
37/// `ProjectRoot` describes a workspace root folder.
38/// Which may be an external dependency, or a member of
39/// the current workspace.
40pub struct ProjectRoot {
41 /// Path to the root folder
42 path: PathBuf,
43 /// Is a member of the current workspace
44 is_member: bool,
45}
46
47impl ProjectRoot {
48 pub fn new(path: PathBuf, is_member: bool) -> ProjectRoot {
49 ProjectRoot { path, is_member }
50 }
51
52 pub fn path(&self) -> &PathBuf {
53 &self.path
54 }
55
56 pub fn is_member(&self) -> bool {
57 self.is_member
58 }
59
60 pub fn include_dir(&self, dir_path: &RelativePath) -> bool {
61 const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"];
62 const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"];
63
64 let is_ignored = if self.is_member {
65 dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str()))
66 } else {
67 dir_path.components().any(|c| {
68 let path = c.as_str();
69 COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path)
70 })
71 };
72
73 let hidden = dir_path.components().any(|c| c.as_str().starts_with("."));
74
75 !is_ignored && !hidden
76 }
77
78 pub fn include_file(&self, file_path: &RelativePath) -> bool {
79 file_path.extension() == Some("rs")
80 }
81}
82
35impl ProjectWorkspace { 83impl ProjectWorkspace {
36 pub fn discover(path: &Path) -> Result<ProjectWorkspace> { 84 pub fn discover(path: &Path) -> Result<ProjectWorkspace> {
37 match find_rust_project_json(path) { 85 match find_rust_project_json(path) {
@@ -50,12 +98,15 @@ impl ProjectWorkspace {
50 } 98 }
51 } 99 }
52 100
53 pub fn to_roots(&self) -> Vec<PathBuf> { 101 /// Returns the roots for the current ProjectWorkspace
102 /// The return type contains the path and whether or not
103 /// the root is a member of the current workspace
104 pub fn to_roots(&self) -> Vec<ProjectRoot> {
54 match self { 105 match self {
55 ProjectWorkspace::Json { project } => { 106 ProjectWorkspace::Json { project } => {
56 let mut roots = Vec::with_capacity(project.roots.len()); 107 let mut roots = Vec::with_capacity(project.roots.len());
57 for root in &project.roots { 108 for root in &project.roots {
58 roots.push(root.path.clone()); 109 roots.push(ProjectRoot::new(root.path.clone(), true));
59 } 110 }
60 roots 111 roots
61 } 112 }
@@ -63,10 +114,12 @@ impl ProjectWorkspace {
63 let mut roots = 114 let mut roots =
64 Vec::with_capacity(cargo.packages().count() + sysroot.crates().count()); 115 Vec::with_capacity(cargo.packages().count() + sysroot.crates().count());
65 for pkg in cargo.packages() { 116 for pkg in cargo.packages() {
66 roots.push(pkg.root(&cargo).to_path_buf()); 117 let root = pkg.root(&cargo).to_path_buf();
118 let member = pkg.is_member(&cargo);
119 roots.push(ProjectRoot::new(root, member));
67 } 120 }
68 for krate in sysroot.crates() { 121 for krate in sysroot.crates() {
69 roots.push(krate.root_dir(&sysroot).to_path_buf()) 122 roots.push(ProjectRoot::new(krate.root_dir(&sysroot).to_path_buf(), false))
70 } 123 }
71 roots 124 roots
72 } 125 }