From c6d6e6c6259709ec30eadb79a1908dca707a6499 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 21 Mar 2019 10:43:47 +0200 Subject: 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. --- crates/ra_project_model/Cargo.toml | 1 + crates/ra_project_model/src/lib.rs | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'crates/ra_project_model') 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"] [dependencies] log = "0.4.5" rustc-hash = "1.0" +relative-path = "0.4.0" failure = "0.1.4" 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}; use serde_json::from_reader; +use relative_path::RelativePath; + pub use crate::{ cargo_workspace::{CargoWorkspace, Package, Target, TargetKind}, json_project::JsonProject, @@ -43,17 +45,39 @@ pub struct ProjectRoot { } impl ProjectRoot { - fn new(path: PathBuf, is_member: bool) -> ProjectRoot { + pub fn new(path: PathBuf, is_member: bool) -> ProjectRoot { ProjectRoot { path, is_member } } - pub fn into_path(self) -> PathBuf { - self.path + pub fn path(&self) -> &PathBuf { + &self.path } pub fn is_member(&self) -> bool { self.is_member } + + pub fn include_dir(&self, dir_path: &RelativePath) -> bool { + const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"]; + const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"]; + + let is_ignored = if self.is_member { + dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str())) + } else { + dir_path.components().any(|c| { + let path = c.as_str(); + COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path) + }) + }; + + let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); + + !is_ignored && !hidden + } + + pub fn include_file(&self, file_path: &RelativePath) -> bool { + file_path.extension() == Some("rs") + } } impl ProjectWorkspace { -- cgit v1.2.3