settings: Use a derive macro for refine (#38451)

When we refactored settings to not pass JSON blobs around, we ended up
needing
to write *a lot* of code that just merged things (like json merge used
to do).

Use a derive macro to prevent typos in this logic.

Release Notes:

- N/A
This commit is contained in:
Conrad Irwin
2025-09-18 21:13:49 +00:00
committed by GitHub
parent 5f4f0a873e
commit b09764c54a
74 changed files with 1089 additions and 2818 deletions
+3 -33
View File
@@ -2,21 +2,17 @@ use std::fmt::{Display, Formatter};
use crate::{
self as settings,
settings_content::{self, BaseKeymapContent, SettingsContent},
settings_content::{BaseKeymapContent, SettingsContent},
};
use gpui::App;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use settings::{Settings, VsCodeSettings};
use settings_ui_macros::{SettingsKey, SettingsUi};
/// Base key bindings scheme. Base keymaps can be overridden with user keymaps.
///
/// Default: VSCode
#[derive(
Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default, SettingsUi,
)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)]
pub enum BaseKeymap {
#[default]
VSCode,
@@ -134,37 +130,11 @@ impl BaseKeymap {
}
}
#[derive(
Copy,
Clone,
Debug,
Serialize,
Deserialize,
JsonSchema,
PartialEq,
Eq,
Default,
SettingsUi,
SettingsKey,
)]
// extracted so that it can be an option, and still work with derive(SettingsUi)
#[settings_key(None)]
#[skip_serializing_none]
pub struct BaseKeymapSetting {
pub base_keymap: Option<BaseKeymap>,
}
impl Settings for BaseKeymap {
fn from_defaults(s: &crate::settings_content::SettingsContent, _cx: &mut App) -> Self {
fn from_settings(s: &crate::settings_content::SettingsContent, _cx: &mut App) -> Self {
s.base_keymap.unwrap().into()
}
fn refine(&mut self, s: &settings_content::SettingsContent, _cx: &mut App) {
if let Some(base_keymap) = s.base_keymap {
*self = base_keymap.into();
};
}
fn import_from_vscode(_vscode: &VsCodeSettings, current: &mut SettingsContent) {
current.base_keymap = Some(BaseKeymapContent::VSCode);
}