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