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
+4 -4
View File
@@ -68,7 +68,7 @@ impl VimTestContext {
pub fn init_keybindings(enabled: bool, cx: &mut App) {
SettingsStore::update_global(cx, |store, cx| {
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
store.update_user_settings::<VimModeSetting>(cx, |s| s.vim_mode = Some(enabled));
});
let default_key_bindings = settings::KeymapFile::load_asset_allow_partial_failure(
"keymaps/default-macos.json",
@@ -134,7 +134,7 @@ impl VimTestContext {
pub fn enable_vim(&mut self) {
self.cx.update(|_, cx| {
SettingsStore::update_global(cx, |store, cx| {
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(true));
store.update_user_settings::<VimModeSetting>(cx, |s| s.vim_mode = Some(true));
});
})
}
@@ -142,7 +142,7 @@ impl VimTestContext {
pub fn disable_vim(&mut self) {
self.cx.update(|_, cx| {
SettingsStore::update_global(cx, |store, cx| {
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(false));
store.update_user_settings::<VimModeSetting>(cx, |s| s.vim_mode = Some(false));
});
})
}
@@ -151,7 +151,7 @@ impl VimTestContext {
self.cx.update(|_, cx| {
SettingsStore::update_global(cx, |store, cx| {
store.update_user_settings::<vim_mode_setting::HelixModeSetting>(cx, |s| {
*s = Some(true)
s.helix_mode = Some(true)
});
});
})
+6 -5
View File
@@ -39,7 +39,9 @@ use object::Object;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_derive::Serialize;
use settings::{Settings, SettingsSources, SettingsStore, SettingsUi, update_settings_file};
use settings::{
Settings, SettingsKey, SettingsSources, SettingsStore, SettingsUi, update_settings_file,
};
use state::{Mode, Operator, RecordedSelection, SearchState, VimGlobals};
use std::{mem, ops::Range, sync::Arc};
use surrounds::SurroundsType;
@@ -247,7 +249,7 @@ pub fn init(cx: &mut App) {
let fs = workspace.app_state().fs.clone();
let currently_enabled = Vim::enabled(cx);
update_settings_file::<VimModeSetting>(fs, cx, move |setting, _| {
*setting = Some(!currently_enabled)
setting.vim_mode = Some(!currently_enabled)
})
});
@@ -1785,7 +1787,8 @@ struct VimSettings {
pub cursor_shape: CursorShapeSettings,
}
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, SettingsUi)]
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, SettingsUi, SettingsKey)]
#[settings_key(key = "vim")]
struct VimSettingsContent {
pub default_mode: Option<ModeContent>,
pub toggle_relative_line_numbers: Option<bool>,
@@ -1824,8 +1827,6 @@ impl From<ModeContent> for Mode {
}
impl Settings for VimSettings {
const KEY: Option<&'static str> = Some("vim");
type FileContent = VimSettingsContent;
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {