1use std::path::Path;
2
3use anyhow::Context as _;
4use gpui::App;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use settings::{Settings, SettingsSources, SettingsUi};
8use util::paths::PathMatcher;
9
10#[derive(Clone, PartialEq, Eq)]
11pub struct WorktreeSettings {
12 pub file_scan_inclusions: PathMatcher,
13 pub file_scan_exclusions: PathMatcher,
14 pub private_files: PathMatcher,
15}
16
17impl WorktreeSettings {
18 pub fn is_path_private(&self, path: &Path) -> bool {
19 path.ancestors()
20 .any(|ancestor| self.private_files.is_match(ancestor))
21 }
22
23 pub fn is_path_excluded(&self, path: &Path) -> bool {
24 path.ancestors()
25 .any(|ancestor| self.file_scan_exclusions.is_match(&ancestor))
26 }
27
28 pub fn is_path_always_included(&self, path: &Path) -> bool {
29 path.ancestors()
30 .any(|ancestor| self.file_scan_inclusions.is_match(&ancestor))
31 }
32}
33
34#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, SettingsUi)]
35pub struct WorktreeSettingsContent {
36 /// Completely ignore files matching globs from `file_scan_exclusions`. Overrides
37 /// `file_scan_inclusions`.
38 ///
39 /// Default: [
40 /// "**/.git",
41 /// "**/.svn",
42 /// "**/.hg",
43 /// "**/.jj",
44 /// "**/CVS",
45 /// "**/.DS_Store",
46 /// "**/Thumbs.db",
47 /// "**/.classpath",
48 /// "**/.settings"
49 /// ]
50 #[serde(default)]
51 pub file_scan_exclusions: Option<Vec<String>>,
52
53 /// Always include files that match these globs when scanning for files, even if they're
54 /// ignored by git. This setting is overridden by `file_scan_exclusions`.
55 /// Default: [
56 /// ".env*",
57 /// "docker-compose.*.yml",
58 /// ]
59 #[serde(default)]
60 pub file_scan_inclusions: Option<Vec<String>>,
61
62 /// Treat the files matching these globs as `.env` files.
63 /// Default: [ "**/.env*" ]
64 pub private_files: Option<Vec<String>>,
65}
66
67impl Settings for WorktreeSettings {
68 const KEY: Option<&'static str> = None;
69
70 type FileContent = WorktreeSettingsContent;
71
72 fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
73 let result: WorktreeSettingsContent = sources.json_merge()?;
74 let mut file_scan_exclusions = result.file_scan_exclusions.unwrap_or_default();
75 let mut private_files = result.private_files.unwrap_or_default();
76 let mut parsed_file_scan_inclusions: Vec<String> = result
77 .file_scan_inclusions
78 .unwrap_or_default()
79 .iter()
80 .flat_map(|glob| {
81 Path::new(glob)
82 .ancestors()
83 .map(|a| a.to_string_lossy().into())
84 })
85 .filter(|p: &String| !p.is_empty())
86 .collect();
87 file_scan_exclusions.sort();
88 private_files.sort();
89 parsed_file_scan_inclusions.sort();
90 Ok(Self {
91 file_scan_exclusions: path_matchers(&file_scan_exclusions, "file_scan_exclusions")?,
92 private_files: path_matchers(&private_files, "private_files")?,
93 file_scan_inclusions: path_matchers(
94 &parsed_file_scan_inclusions,
95 "file_scan_inclusions",
96 )?,
97 })
98 }
99
100 fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
101 if let Some(inclusions) = vscode
102 .read_value("files.watcherInclude")
103 .and_then(|v| v.as_array())
104 .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
105 {
106 if let Some(old) = current.file_scan_inclusions.as_mut() {
107 old.extend(inclusions)
108 } else {
109 current.file_scan_inclusions = Some(inclusions)
110 }
111 }
112 if let Some(exclusions) = vscode
113 .read_value("files.watcherExclude")
114 .and_then(|v| v.as_array())
115 .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
116 {
117 if let Some(old) = current.file_scan_exclusions.as_mut() {
118 old.extend(exclusions)
119 } else {
120 current.file_scan_exclusions = Some(exclusions)
121 }
122 }
123 }
124}
125
126fn path_matchers(values: &[String], context: &'static str) -> anyhow::Result<PathMatcher> {
127 PathMatcher::new(values).with_context(|| format!("Failed to parse globs from {}", context))
128}