settings ui: Create settings key trait (#37489)

This PR separates out the associated constant `KEY` from the `Settings`
trait into a new trait `SettingsKey`. This allows for the key trait to
be derived using attributes to specify the path so that the new
`SettingsUi` derive macro can use the same attributes to determine top
level settings paths thereby removing the need to duplicate the path in
both `Settings::KEY` and `#[settings_ui(path = "...")]`

Co-authored-by: Ben Kunkle <ben@zed.dev>

Release Notes:

- N/A

---------

Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
Anthony Eid
2025-09-04 15:19:02 -04:00
committed by GitHub
co-authored by Ben Kunkle
parent 223fda2fe2
commit 5f03202b5c
44 changed files with 473 additions and 255 deletions
+55 -23
View File
@@ -6,7 +6,8 @@
use anyhow::Result;
use gpui::App;
use settings::{Settings, SettingsSources, SettingsUi};
use schemars::JsonSchema;
use settings::{Settings, SettingsKey, SettingsSources, SettingsUi};
/// Initializes the `vim_mode_setting` crate.
pub fn init(cx: &mut App) {
@@ -14,25 +15,40 @@ pub fn init(cx: &mut App) {
HelixModeSetting::register(cx);
}
/// Whether or not to enable Vim mode.
///
/// Default: false
#[derive(SettingsUi)]
pub struct VimModeSetting(pub bool);
impl Settings for VimModeSetting {
const KEY: Option<&'static str> = Some("vim_mode");
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Debug,
Default,
serde::Serialize,
serde::Deserialize,
SettingsUi,
SettingsKey,
JsonSchema,
)]
#[settings_key(None)]
pub struct VimModeSettingContent {
/// Whether or not to enable Vim mode.
///
/// Default: false
pub vim_mode: Option<bool>,
}
type FileContent = Option<bool>;
impl Settings for VimModeSetting {
type FileContent = VimModeSettingContent;
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
Ok(Self(
sources
.user
.or(sources.server)
.copied()
.flatten()
.unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
.and_then(|mode| mode.vim_mode)
.or(sources.server.and_then(|mode| mode.vim_mode))
.or(sources.default.vim_mode)
.ok_or_else(Self::missing_default)?,
))
}
@@ -41,25 +57,41 @@ impl Settings for VimModeSetting {
}
}
/// Whether or not to enable Helix mode.
///
/// Default: false
#[derive(SettingsUi)]
#[derive(Debug)]
pub struct HelixModeSetting(pub bool);
impl Settings for HelixModeSetting {
const KEY: Option<&'static str> = Some("helix_mode");
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Debug,
Default,
serde::Serialize,
serde::Deserialize,
SettingsUi,
SettingsKey,
JsonSchema,
)]
#[settings_key(None)]
pub struct HelixModeSettingContent {
/// Whether or not to enable Helix mode.
///
/// Default: false
pub helix_mode: Option<bool>,
}
type FileContent = Option<bool>;
impl Settings for HelixModeSetting {
type FileContent = HelixModeSettingContent;
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
Ok(Self(
sources
.user
.or(sources.server)
.copied()
.flatten()
.unwrap_or(sources.default.ok_or_else(Self::missing_default)?),
.and_then(|mode| mode.helix_mode)
.or(sources.server.and_then(|mode| mode.helix_mode))
.or(sources.default.helix_mode)
.ok_or_else(Self::missing_default)?,
))
}