## Goal This PR creates the initial settings ui structure with the primary goal of making a settings UI that is - Comprehensive: All settings are available through the UI - Correct: Easy to understand the underlying JSON file from the UI - Intuitive - Easy to implement per setting so that UI is not a hindrance to future settings changes ### Structure The overall structure is settings layer -> data layer -> ui layer. The settings layer is the pre-existing settings definitions, that implement the `Settings` trait. The data layer is constructed from settings primarily through the `SettingsUi` trait, and it's associated derive macro. The data layer tracks the grouping of the settings, the json path of the settings, and a data representation of how to render the controls for the setting in the UI, that is either a marker value for the component to use (avoiding a dependency on the `ui` crate) or a custom render function. Abstracting the data layer from the ui layer allows crates depending on `settings` to implement their own UI without having to add additional UI dependencies, thus avoiding circular dependencies. In cases where custom UI is desired, and a creating a custom render function in the same crate is infeasible due to circular dependencies, the current solution is to implement a marker for the component in the `settings` crate, and then handle the rendering of that component in `settings_ui`. ### Foundation This PR creates a macro and a trait both called `SettingsUi`. The `SettingsUi` trait is added as a new trait bound on the `Settings` trait, this allows the type system to guarantee that all settings implement UI functionality. The macro is used to derived the trait for most types, and can be modified through attributes for unique cases as well. A derive-macro is used to generate the settings UI trait impl, allowing it the UI generation to be generated from the static information in our code base (`default.json`, Struct/Enum names, field names, `serde` attributes, etc). This allows the UI to be auto-generated for the most part, and ensures consistency across the UI. #### Immediate Follow ups - Add a new `SettingsPath` trait that will be a trait bound on `SettingsUi` and `Settings` - This trait will replace the `Settings::key` value to enable `SettingsUi` to infer the json path of it's derived type - Figure out how to render `Option<T> where T: SettingsUi` correctly - Handle `serde` attributes in the `SettingsUi` proc macro to correctly get json path from a type's field and identity Release Notes: - N/A --------- Co-authored-by: Ben Kunkle <ben@zed.dev>
129 lines
4.3 KiB
Rust
129 lines
4.3 KiB
Rust
use std::path::Path;
|
|
|
|
use anyhow::Context as _;
|
|
use gpui::App;
|
|
use schemars::JsonSchema;
|
|
use serde::{Deserialize, Serialize};
|
|
use settings::{Settings, SettingsSources, SettingsUi};
|
|
use util::paths::PathMatcher;
|
|
|
|
#[derive(Clone, PartialEq, Eq, SettingsUi)]
|
|
pub struct WorktreeSettings {
|
|
pub file_scan_inclusions: PathMatcher,
|
|
pub file_scan_exclusions: PathMatcher,
|
|
pub private_files: PathMatcher,
|
|
}
|
|
|
|
impl WorktreeSettings {
|
|
pub fn is_path_private(&self, path: &Path) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.private_files.is_match(ancestor))
|
|
}
|
|
|
|
pub fn is_path_excluded(&self, path: &Path) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.file_scan_exclusions.is_match(&ancestor))
|
|
}
|
|
|
|
pub fn is_path_always_included(&self, path: &Path) -> bool {
|
|
path.ancestors()
|
|
.any(|ancestor| self.file_scan_inclusions.is_match(&ancestor))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
|
|
pub struct WorktreeSettingsContent {
|
|
/// Completely ignore files matching globs from `file_scan_exclusions`. Overrides
|
|
/// `file_scan_inclusions`.
|
|
///
|
|
/// Default: [
|
|
/// "**/.git",
|
|
/// "**/.svn",
|
|
/// "**/.hg",
|
|
/// "**/.jj",
|
|
/// "**/CVS",
|
|
/// "**/.DS_Store",
|
|
/// "**/Thumbs.db",
|
|
/// "**/.classpath",
|
|
/// "**/.settings"
|
|
/// ]
|
|
#[serde(default)]
|
|
pub file_scan_exclusions: Option<Vec<String>>,
|
|
|
|
/// Always include files that match these globs when scanning for files, even if they're
|
|
/// ignored by git. This setting is overridden by `file_scan_exclusions`.
|
|
/// Default: [
|
|
/// ".env*",
|
|
/// "docker-compose.*.yml",
|
|
/// ]
|
|
#[serde(default)]
|
|
pub file_scan_inclusions: Option<Vec<String>>,
|
|
|
|
/// Treat the files matching these globs as `.env` files.
|
|
/// Default: [ "**/.env*" ]
|
|
pub private_files: Option<Vec<String>>,
|
|
}
|
|
|
|
impl Settings for WorktreeSettings {
|
|
const KEY: Option<&'static str> = None;
|
|
|
|
type FileContent = WorktreeSettingsContent;
|
|
|
|
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> anyhow::Result<Self> {
|
|
let result: WorktreeSettingsContent = sources.json_merge()?;
|
|
let mut file_scan_exclusions = result.file_scan_exclusions.unwrap_or_default();
|
|
let mut private_files = result.private_files.unwrap_or_default();
|
|
let mut parsed_file_scan_inclusions: Vec<String> = result
|
|
.file_scan_inclusions
|
|
.unwrap_or_default()
|
|
.iter()
|
|
.flat_map(|glob| {
|
|
Path::new(glob)
|
|
.ancestors()
|
|
.map(|a| a.to_string_lossy().into())
|
|
})
|
|
.filter(|p: &String| !p.is_empty())
|
|
.collect();
|
|
file_scan_exclusions.sort();
|
|
private_files.sort();
|
|
parsed_file_scan_inclusions.sort();
|
|
Ok(Self {
|
|
file_scan_exclusions: path_matchers(&file_scan_exclusions, "file_scan_exclusions")?,
|
|
private_files: path_matchers(&private_files, "private_files")?,
|
|
file_scan_inclusions: path_matchers(
|
|
&parsed_file_scan_inclusions,
|
|
"file_scan_inclusions",
|
|
)?,
|
|
})
|
|
}
|
|
|
|
fn import_from_vscode(vscode: &settings::VsCodeSettings, current: &mut Self::FileContent) {
|
|
if let Some(inclusions) = vscode
|
|
.read_value("files.watcherInclude")
|
|
.and_then(|v| v.as_array())
|
|
.and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
|
|
{
|
|
if let Some(old) = current.file_scan_inclusions.as_mut() {
|
|
old.extend(inclusions)
|
|
} else {
|
|
current.file_scan_inclusions = Some(inclusions)
|
|
}
|
|
}
|
|
if let Some(exclusions) = vscode
|
|
.read_value("files.watcherExclude")
|
|
.and_then(|v| v.as_array())
|
|
.and_then(|v| v.iter().map(|n| n.as_str().map(str::to_owned)).collect())
|
|
{
|
|
if let Some(old) = current.file_scan_exclusions.as_mut() {
|
|
old.extend(exclusions)
|
|
} else {
|
|
current.file_scan_exclusions = Some(exclusions)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn path_matchers(values: &[String], context: &'static str) -> anyhow::Result<PathMatcher> {
|
|
PathMatcher::new(values).with_context(|| format!("Failed to parse globs from {}", context))
|
|
}
|