aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-03-21 08:43:47 +0000
committerVille Penttinen <[email protected]>2019-03-21 08:43:47 +0000
commitc6d6e6c6259709ec30eadb79a1908dca707a6499 (patch)
tree998713639b3c09e8650e34c7c86c40e0d11e3f35
parente32462c6d56592acd22f1aab64f627636a476d6c (diff)
Move actual include logic to ProjectRoot
This way the two IncludeRustFiles implementations can simply call the ProjectRoots' methods, so that the include logic is in one place.
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_batch/src/vfs_filter.rs45
-rw-r--r--crates/ra_lsp_server/src/vfs_filter.rs45
-rw-r--r--crates/ra_project_model/Cargo.toml1
-rw-r--r--crates/ra_project_model/src/lib.rs30
5 files changed, 69 insertions, 53 deletions
diff --git a/Cargo.lock b/Cargo.lock
index ad9f6970b..55bef8cb9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1088,6 +1088,7 @@ dependencies = [
1088 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 1088 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
1089 "ra_arena 0.1.0", 1089 "ra_arena 0.1.0",
1090 "ra_db 0.1.0", 1090 "ra_db 0.1.0",
1091 "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
1091 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", 1092 "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
1092 "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", 1093 "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
1093 "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", 1094 "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/crates/ra_batch/src/vfs_filter.rs b/crates/ra_batch/src/vfs_filter.rs
index 290aec172..dd20c1203 100644
--- a/crates/ra_batch/src/vfs_filter.rs
+++ b/crates/ra_batch/src/vfs_filter.rs
@@ -2,9 +2,10 @@ use std::path::PathBuf;
2use ra_project_model::ProjectRoot; 2use ra_project_model::ProjectRoot;
3use ra_vfs::{RootEntry, Filter, RelativePath}; 3use ra_vfs::{RootEntry, Filter, RelativePath};
4 4
5/// `IncludeRustFiles` is used to convert
6/// from `ProjectRoot` to `RootEntry` for VFS
5pub struct IncludeRustFiles { 7pub struct IncludeRustFiles {
6 /// Is a member of the current workspace 8 root: ProjectRoot,
7 is_member: bool,
8} 9}
9 10
10impl IncludeRustFiles { 11impl IncludeRustFiles {
@@ -16,44 +17,38 @@ impl IncludeRustFiles {
16 } 17 }
17 18
18 pub fn from_root(root: ProjectRoot) -> RootEntry { 19 pub fn from_root(root: ProjectRoot) -> RootEntry {
19 let is_member = root.is_member(); 20 IncludeRustFiles::from(root).into()
20 IncludeRustFiles::into_entry(root.into_path(), is_member)
21 } 21 }
22 22
23 #[allow(unused)] 23 #[allow(unused)]
24 pub fn external(path: PathBuf) -> RootEntry { 24 pub fn external(path: PathBuf) -> RootEntry {
25 IncludeRustFiles::into_entry(path, false) 25 IncludeRustFiles::from_root(ProjectRoot::new(path, false))
26 } 26 }
27 27
28 pub fn member(path: PathBuf) -> RootEntry { 28 pub fn member(path: PathBuf) -> RootEntry {
29 IncludeRustFiles::into_entry(path, true) 29 IncludeRustFiles::from_root(ProjectRoot::new(path, true))
30 }
31
32 fn into_entry(path: PathBuf, is_member: bool) -> RootEntry {
33 RootEntry::new(path, Box::new(Self { is_member }))
34 } 30 }
35} 31}
36 32
37impl Filter for IncludeRustFiles { 33impl Filter for IncludeRustFiles {
38 fn include_dir(&self, dir_path: &RelativePath) -> bool { 34 fn include_dir(&self, dir_path: &RelativePath) -> bool {
39 const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"]; 35 self.root.include_dir(dir_path)
40 const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"]; 36 }
41
42 let is_ignored = if self.is_member {
43 dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str()))
44 } else {
45 dir_path.components().any(|c| {
46 let path = c.as_str();
47 COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path)
48 })
49 };
50 37
51 let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); 38 fn include_file(&self, file_path: &RelativePath) -> bool {
39 self.root.include_file(file_path)
40 }
41}
52 42
53 !is_ignored && !hidden 43impl std::convert::From<ProjectRoot> for IncludeRustFiles {
44 fn from(v: ProjectRoot) -> IncludeRustFiles {
45 IncludeRustFiles { root: v }
54 } 46 }
47}
55 48
56 fn include_file(&self, file_path: &RelativePath) -> bool { 49impl std::convert::From<IncludeRustFiles> for RootEntry {
57 file_path.extension() == Some("rs") 50 fn from(v: IncludeRustFiles) -> RootEntry {
51 let path = v.root.path().clone();
52 RootEntry::new(path, Box::new(v))
58 } 53 }
59} 54}
diff --git a/crates/ra_lsp_server/src/vfs_filter.rs b/crates/ra_lsp_server/src/vfs_filter.rs
index 290aec172..dd20c1203 100644
--- a/crates/ra_lsp_server/src/vfs_filter.rs
+++ b/crates/ra_lsp_server/src/vfs_filter.rs
@@ -2,9 +2,10 @@ use std::path::PathBuf;
2use ra_project_model::ProjectRoot; 2use ra_project_model::ProjectRoot;
3use ra_vfs::{RootEntry, Filter, RelativePath}; 3use ra_vfs::{RootEntry, Filter, RelativePath};
4 4
5/// `IncludeRustFiles` is used to convert
6/// from `ProjectRoot` to `RootEntry` for VFS
5pub struct IncludeRustFiles { 7pub struct IncludeRustFiles {
6 /// Is a member of the current workspace 8 root: ProjectRoot,
7 is_member: bool,
8} 9}
9 10
10impl IncludeRustFiles { 11impl IncludeRustFiles {
@@ -16,44 +17,38 @@ impl IncludeRustFiles {
16 } 17 }
17 18
18 pub fn from_root(root: ProjectRoot) -> RootEntry { 19 pub fn from_root(root: ProjectRoot) -> RootEntry {
19 let is_member = root.is_member(); 20 IncludeRustFiles::from(root).into()
20 IncludeRustFiles::into_entry(root.into_path(), is_member)
21 } 21 }
22 22
23 #[allow(unused)] 23 #[allow(unused)]
24 pub fn external(path: PathBuf) -> RootEntry { 24 pub fn external(path: PathBuf) -> RootEntry {
25 IncludeRustFiles::into_entry(path, false) 25 IncludeRustFiles::from_root(ProjectRoot::new(path, false))
26 } 26 }
27 27
28 pub fn member(path: PathBuf) -> RootEntry { 28 pub fn member(path: PathBuf) -> RootEntry {
29 IncludeRustFiles::into_entry(path, true) 29 IncludeRustFiles::from_root(ProjectRoot::new(path, true))
30 }
31
32 fn into_entry(path: PathBuf, is_member: bool) -> RootEntry {
33 RootEntry::new(path, Box::new(Self { is_member }))
34 } 30 }
35} 31}
36 32
37impl Filter for IncludeRustFiles { 33impl Filter for IncludeRustFiles {
38 fn include_dir(&self, dir_path: &RelativePath) -> bool { 34 fn include_dir(&self, dir_path: &RelativePath) -> bool {
39 const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"]; 35 self.root.include_dir(dir_path)
40 const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"]; 36 }
41
42 let is_ignored = if self.is_member {
43 dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str()))
44 } else {
45 dir_path.components().any(|c| {
46 let path = c.as_str();
47 COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path)
48 })
49 };
50 37
51 let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); 38 fn include_file(&self, file_path: &RelativePath) -> bool {
39 self.root.include_file(file_path)
40 }
41}
52 42
53 !is_ignored && !hidden 43impl std::convert::From<ProjectRoot> for IncludeRustFiles {
44 fn from(v: ProjectRoot) -> IncludeRustFiles {
45 IncludeRustFiles { root: v }
54 } 46 }
47}
55 48
56 fn include_file(&self, file_path: &RelativePath) -> bool { 49impl std::convert::From<IncludeRustFiles> for RootEntry {
57 file_path.extension() == Some("rs") 50 fn from(v: IncludeRustFiles) -> RootEntry {
51 let path = v.root.path().clone();
52 RootEntry::new(path, Box::new(v))
58 } 53 }
59} 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 b27cb55ef..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,
@@ -43,17 +45,39 @@ pub struct ProjectRoot {
43} 45}
44 46
45impl ProjectRoot { 47impl ProjectRoot {
46 fn new(path: PathBuf, is_member: bool) -> ProjectRoot { 48 pub fn new(path: PathBuf, is_member: bool) -> ProjectRoot {
47 ProjectRoot { path, is_member } 49 ProjectRoot { path, is_member }
48 } 50 }
49 51
50 pub fn into_path(self) -> PathBuf { 52 pub fn path(&self) -> &PathBuf {
51 self.path 53 &self.path
52 } 54 }
53 55
54 pub fn is_member(&self) -> bool { 56 pub fn is_member(&self) -> bool {
55 self.is_member 57 self.is_member
56 } 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 }
57} 81}
58 82
59impl ProjectWorkspace { 83impl ProjectWorkspace {