worktree_settings.rs

  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 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, SettingsKey)]
 35#[settings_key(None)]
 36pub struct WorktreeSettingsContent {
 37    /// Completely ignore files matching globs from `file_scan_exclusions`. Overrides
 38    /// `file_scan_inclusions`.
 39    ///
 40    /// Default: [
 41    ///   "**/.git",
 42    ///   "**/.svn",
 43    ///   "**/.hg",
 44    ///   "**/.jj",
 45    ///   "**/CVS",
 46    ///   "**/.DS_Store",
 47    ///   "**/Thumbs.db",
 48    ///   "**/.classpath",
 49    ///   "**/.settings"
 50    /// ]
 51    #[serde(default)]
 52    pub file_scan_exclusions: Option<Vec<String>>,
 53
 54    /// Always include files that match these globs when scanning for files, even if they're
 55    /// ignored by git. This setting is overridden by `file_scan_exclusions`.
 56    /// Default: [
 57    ///  ".env*",
 58    ///  "docker-compose.*.yml",
 59    /// ]
 60    #[serde(default)]
 61    pub file_scan_inclusions: Option<Vec<String>>,
 62
 63    /// Treat the files matching these globs as `.env` files.
 64    /// Default: [ "**/.env*" ]
 65    pub private_files: Option<Vec<String>>,
 66}
 67
 68impl Settings for WorktreeSettings {
 69    type FileContent = WorktreeSettingsContent;
 70
 71    fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
 72        let result: WorktreeSettingsContent = sources.json_merge()?;
 73        let mut file_scan_exclusions = result.file_scan_exclusions.unwrap_or_default();
 74        let mut private_files = result.private_files.unwrap_or_default();
 75        let mut parsed_file_scan_inclusions: Vec<String> = result
 76            .file_scan_inclusions
 77            .unwrap_or_default()
 78            .iter()
 79            .flat_map(|glob| {
 80                Path::new(glob)
 81                    .ancestors()
 82                    .map(|a| a.to_string_lossy().into())
 83            })
 84            .filter(|p: &String| !p.is_empty())
 85            .collect();
 86        file_scan_exclusions.sort();
 87        private_files.sort();
 88        parsed_file_scan_inclusions.sort();
 89        Ok(Self {
 90            file_scan_exclusions: path_matchers(&file_scan_exclusions, "file_scan_exclusions")?,
 91            private_files: path_matchers(&private_files, "private_files")?,
 92            file_scan_inclusions: path_matchers(
 93                &parsed_file_scan_inclusions,
 94                "file_scan_inclusions",
 95            )?,
 96        })
 97    }
 98
 99    fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
100        if let Some(inclusions) = vscode
101            .read_value("files.watcherInclude")
102            .and_then(|v| v.as_array())
103            .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
104        {
105            if let Some(old) = current.file_scan_inclusions.as_mut() {
106                old.extend(inclusions)
107            } else {
108                current.file_scan_inclusions = Some(inclusions)
109            }
110        }
111        if let Some(exclusions) = vscode
112            .read_value("files.watcherExclude")
113            .and_then(|v| v.as_array())
114            .and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
115        {
116            if let Some(old) = current.file_scan_exclusions.as_mut() {
117                old.extend(exclusions)
118            } else {
119                current.file_scan_exclusions = Some(exclusions)
120            }
121        }
122    }
123}
124
125fn path_matchers(values: &[String], context: &'static str) -> anyhow::Result<PathMatcher> {
126    PathMatcher::new(values).with_context(|| format!("Failed to parse globs from {}", context))
127}