aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_project_model/src')
-rw-r--r--crates/ra_project_model/src/cargo_workspace.rs2
-rw-r--r--crates/ra_project_model/src/lib.rs57
-rw-r--r--crates/ra_project_model/src/sysroot.rs2
3 files changed, 18 insertions, 43 deletions
diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs
index 2b06e9e37..712d8818f 100644
--- a/crates/ra_project_model/src/cargo_workspace.rs
+++ b/crates/ra_project_model/src/cargo_workspace.rs
@@ -167,7 +167,7 @@ impl CargoWorkspace {
167 Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root }) 167 Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root })
168 } 168 }
169 169
170 pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + 'a { 170 pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
171 self.packages.iter().map(|(id, _pkg)| id) 171 self.packages.iter().map(|(id, _pkg)| id)
172 } 172 }
173 173
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 08e5c1c32..55b94b911 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -9,14 +9,10 @@ use std::{
9 path::{Path, PathBuf}, 9 path::{Path, PathBuf},
10}; 10};
11 11
12use rustc_hash::FxHashMap;
13
14use ra_db::{CrateGraph, Edition, FileId}; 12use ra_db::{CrateGraph, Edition, FileId};
15 13use rustc_hash::FxHashMap;
16use serde_json::from_reader; 14use serde_json::from_reader;
17 15
18use relative_path::RelativePath;
19
20pub use crate::{ 16pub use crate::{
21 cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, 17 cargo_workspace::{CargoWorkspace, Package, Target, TargetKind},
22 json_project::JsonProject, 18 json_project::JsonProject,
@@ -34,20 +30,20 @@ pub enum ProjectWorkspace {
34 Json { project: JsonProject }, 30 Json { project: JsonProject },
35} 31}
36 32
37/// `ProjectRoot` describes a workspace root folder. 33/// `PackageRoot` describes a package root folder.
38/// Which may be an external dependency, or a member of 34/// Which may be an external dependency, or a member of
39/// the current workspace. 35/// the current workspace.
40#[derive(Clone)] 36#[derive(Clone)]
41pub struct ProjectRoot { 37pub struct PackageRoot {
42 /// Path to the root folder 38 /// Path to the root folder
43 path: PathBuf, 39 path: PathBuf,
44 /// Is a member of the current workspace 40 /// Is a member of the current workspace
45 is_member: bool, 41 is_member: bool,
46} 42}
47 43
48impl ProjectRoot { 44impl PackageRoot {
49 pub fn new(path: PathBuf, is_member: bool) -> ProjectRoot { 45 pub fn new(path: PathBuf, is_member: bool) -> PackageRoot {
50 ProjectRoot { path, is_member } 46 PackageRoot { path, is_member }
51 } 47 }
52 48
53 pub fn path(&self) -> &PathBuf { 49 pub fn path(&self) -> &PathBuf {
@@ -57,28 +53,6 @@ impl ProjectRoot {
57 pub fn is_member(&self) -> bool { 53 pub fn is_member(&self) -> bool {
58 self.is_member 54 self.is_member
59 } 55 }
60
61 pub fn include_dir(&self, dir_path: &RelativePath) -> bool {
62 const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"];
63 const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"];
64
65 let is_ignored = if self.is_member {
66 dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str()))
67 } else {
68 dir_path.components().any(|c| {
69 let path = c.as_str();
70 COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path)
71 })
72 };
73
74 let hidden = dir_path.components().any(|c| c.as_str().starts_with('.'));
75
76 !is_ignored && !hidden
77 }
78
79 pub fn include_file(&self, file_path: &RelativePath) -> bool {
80 file_path.extension() == Some("rs")
81 }
82} 56}
83 57
84impl ProjectWorkspace { 58impl ProjectWorkspace {
@@ -99,38 +73,39 @@ impl ProjectWorkspace {
99 } 73 }
100 } 74 }
101 75
102 /// Returns the roots for the current ProjectWorkspace 76 /// Returns the roots for the current `ProjectWorkspace`
103 /// The return type contains the path and whether or not 77 /// The return type contains the path and whether or not
104 /// the root is a member of the current workspace 78 /// the root is a member of the current workspace
105 pub fn to_roots(&self) -> Vec<ProjectRoot> { 79 pub fn to_roots(&self) -> Vec<PackageRoot> {
106 match self { 80 match self {
107 ProjectWorkspace::Json { project } => { 81 ProjectWorkspace::Json { project } => {
108 let mut roots = Vec::with_capacity(project.roots.len()); 82 let mut roots = Vec::with_capacity(project.roots.len());
109 for root in &project.roots { 83 for root in &project.roots {
110 roots.push(ProjectRoot::new(root.path.clone(), true)); 84 roots.push(PackageRoot::new(root.path.clone(), true));
111 } 85 }
112 roots 86 roots
113 } 87 }
114 ProjectWorkspace::Cargo { cargo, sysroot } => { 88 ProjectWorkspace::Cargo { cargo, sysroot } => {
115 let mut roots = 89 let mut roots = Vec::with_capacity(cargo.packages().len() + sysroot.crates().len());
116 Vec::with_capacity(cargo.packages().count() + sysroot.crates().count());
117 for pkg in cargo.packages() { 90 for pkg in cargo.packages() {
118 let root = pkg.root(&cargo).to_path_buf(); 91 let root = pkg.root(&cargo).to_path_buf();
119 let member = pkg.is_member(&cargo); 92 let member = pkg.is_member(&cargo);
120 roots.push(ProjectRoot::new(root, member)); 93 roots.push(PackageRoot::new(root, member));
121 } 94 }
122 for krate in sysroot.crates() { 95 for krate in sysroot.crates() {
123 roots.push(ProjectRoot::new(krate.root_dir(&sysroot).to_path_buf(), false)) 96 roots.push(PackageRoot::new(krate.root_dir(&sysroot).to_path_buf(), false))
124 } 97 }
125 roots 98 roots
126 } 99 }
127 } 100 }
128 } 101 }
129 102
130 pub fn count(&self) -> usize { 103 pub fn n_packages(&self) -> usize {
131 match self { 104 match self {
132 ProjectWorkspace::Json { project } => project.crates.len(), 105 ProjectWorkspace::Json { project } => project.crates.len(),
133 ProjectWorkspace::Cargo { cargo, .. } => cargo.packages().count(), 106 ProjectWorkspace::Cargo { cargo, sysroot } => {
107 cargo.packages().len() + sysroot.crates().len()
108 }
134 } 109 }
135 } 110 }
136 111
diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs
index 4f6e880dd..3f34d51cc 100644
--- a/crates/ra_project_model/src/sysroot.rs
+++ b/crates/ra_project_model/src/sysroot.rs
@@ -28,7 +28,7 @@ impl Sysroot {
28 self.by_name("std") 28 self.by_name("std")
29 } 29 }
30 30
31 pub fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + 'a { 31 pub fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + ExactSizeIterator + 'a {
32 self.crates.iter().map(|(id, _data)| id) 32 self.crates.iter().map(|(id, _data)| id)
33 } 33 }
34 34