1use std::path::Path;
2
3use anyhow::Context as _;
4use gpui::App;
5use settings::{Settings, SettingsContent};
6use util::{
7 ResultExt,
8 paths::{PathMatcher, PathStyle},
9 rel_path::RelPath,
10};
11
12#[derive(Clone, PartialEq, Eq)]
13pub struct WorktreeSettings {
14 pub project_name: Option<String>,
15 pub file_scan_inclusions: PathMatcher,
16 pub file_scan_exclusions: PathMatcher,
17 pub private_files: PathMatcher,
18}
19
20impl WorktreeSettings {
21 pub fn is_path_private(&self, path: &RelPath) -> bool {
22 path.ancestors()
23 .any(|ancestor| self.private_files.is_match(ancestor.as_std_path()))
24 }
25
26 pub fn is_path_excluded(&self, path: &RelPath) -> bool {
27 path.ancestors()
28 .any(|ancestor| self.file_scan_exclusions.is_match(ancestor.as_std_path()))
29 }
30
31 pub fn is_path_always_included(&self, path: &RelPath) -> bool {
32 path.ancestors()
33 .any(|ancestor| self.file_scan_inclusions.is_match(ancestor.as_std_path()))
34 }
35}
36
37impl Settings for WorktreeSettings {
38 fn from_settings(content: &settings::SettingsContent, _cx: &mut App) -> Self {
39 let worktree = content.project.worktree.clone();
40 let file_scan_exclusions = worktree.file_scan_exclusions.unwrap();
41 let file_scan_inclusions = worktree.file_scan_inclusions.unwrap();
42 let private_files = worktree.private_files.unwrap().0;
43 let parsed_file_scan_inclusions: Vec<String> = file_scan_inclusions
44 .iter()
45 .flat_map(|glob| {
46 Path::new(glob)
47 .ancestors()
48 .map(|a| a.to_string_lossy().into())
49 })
50 .filter(|p: &String| !p.is_empty())
51 .collect();
52
53 Self {
54 project_name: None,
55 file_scan_exclusions: path_matchers(file_scan_exclusions, "file_scan_exclusions")
56 .log_err()
57 .unwrap_or_default(),
58 file_scan_inclusions: path_matchers(
59 parsed_file_scan_inclusions,
60 "file_scan_inclusions",
61 )
62 .unwrap(),
63 private_files: path_matchers(private_files, "private_files")
64 .log_err()
65 .unwrap_or_default(),
66 }
67 }
68
69 fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut SettingsContent) {
70 if let Some(inclusions) = vscode
71 .read_value("files.watcherInclude")
72 .and_then(|v| v.as_array())
73 .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
74 {
75 if let Some(old) = current.project.worktree.file_scan_inclusions.as_mut() {
76 old.extend(inclusions)
77 } else {
78 current.project.worktree.file_scan_inclusions = Some(inclusions)
79 }
80 }
81 if let Some(exclusions) = vscode
82 .read_value("files.watcherExclude")
83 .and_then(|v| v.as_array())
84 .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
85 {
86 if let Some(old) = current.project.worktree.file_scan_exclusions.as_mut() {
87 old.extend(exclusions)
88 } else {
89 current.project.worktree.file_scan_exclusions = Some(exclusions)
90 }
91 }
92 }
93}
94
95fn path_matchers(mut values: Vec<String>, context: &'static str) -> anyhow::Result<PathMatcher> {
96 values.sort();
97 PathMatcher::new(values, PathStyle::local())
98 .with_context(|| format!("Failed to parse globs from {}", context))
99}