settings_ui: Move settings UI trait to file content (#37337)
Closes #ISSUE Initially, the `SettingsUi` trait was tied to `Settings`, however, given that the `Settings::FileContent` type (which may be the same as the type that implements `Settings`) will be the type that more directly maps to the JSON structure (and therefore have the documentation, correct field names (or `serde` rename attributes), etc) it makes more sense to have the deriving of `SettingsUi` occur on the `FileContent` type rather than the `Settings` type. In order for this to work a relatively important change had to be made to the derive macro, that being that it now "unwraps" options into their inner type, so a field with type `Option<Foo>` where `Foo: SettingsUi` will treat the field as if it were just `Foo`, expecting there to be a default set in `default.json`. This imposes some restrictions on what `Settings::FileContent` can be as seen in 1e19398 where `FileContent` itself can't be optional without manually implementing `SettingsUi`, as well as introducing some risk that if the `FileContent` type has `serde(default)`, the default value will override the default value from `default.json` in the UI even though it may differ (but it should!). A future PR should probably replace the other settings with `FileContent = Option<T>` (all of which currently have `T == bool`) with wrapper structs and have `KEY = None` so the further niceties `derive(SettingsUi)` will provide such as path renaming, custom UI, auto naming and doc comment extraction can be used. Release Notes: - N/A *or* Added/Fixed/Improved ...
This commit is contained in:
@@ -100,25 +100,36 @@ impl BaseKeymap {
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings for BaseKeymap {
|
||||
const KEY: Option<&'static str> = Some("base_keymap");
|
||||
#[derive(
|
||||
Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default, SettingsUi,
|
||||
)]
|
||||
// extracted so that it can be an option, and still work with derive(SettingsUi)
|
||||
pub struct BaseKeymapSetting {
|
||||
pub base_keymap: Option<BaseKeymap>,
|
||||
}
|
||||
|
||||
type FileContent = Option<Self>;
|
||||
impl Settings for BaseKeymap {
|
||||
const KEY: Option<&'static str> = None;
|
||||
|
||||
type FileContent = BaseKeymapSetting;
|
||||
|
||||
fn load(
|
||||
sources: SettingsSources<Self::FileContent>,
|
||||
_: &mut gpui::App,
|
||||
) -> anyhow::Result<Self> {
|
||||
if let Some(Some(user_value)) = sources.user.copied() {
|
||||
if let Some(Some(user_value)) = sources.user.map(|setting| setting.base_keymap) {
|
||||
return Ok(user_value);
|
||||
}
|
||||
if let Some(Some(server_value)) = sources.server.copied() {
|
||||
if let Some(Some(server_value)) = sources.server.map(|setting| setting.base_keymap) {
|
||||
return Ok(server_value);
|
||||
}
|
||||
sources.default.ok_or_else(Self::missing_default)
|
||||
sources
|
||||
.default
|
||||
.base_keymap
|
||||
.ok_or_else(Self::missing_default)
|
||||
}
|
||||
|
||||
fn import_from_vscode(_vscode: &VsCodeSettings, current: &mut Self::FileContent) {
|
||||
*current = Some(BaseKeymap::VSCode);
|
||||
current.base_keymap = Some(BaseKeymap::VSCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ use crate::{
|
||||
/// A value that can be defined as a user setting.
|
||||
///
|
||||
/// Settings can be loaded from a combination of multiple JSON files.
|
||||
pub trait Settings: SettingsUi + 'static + Send + Sync {
|
||||
pub trait Settings: 'static + Send + Sync {
|
||||
/// The name of a key within the JSON file from which this setting should
|
||||
/// be deserialized. If this is `None`, then the setting will be deserialized
|
||||
/// from the root object.
|
||||
@@ -57,7 +57,7 @@ pub trait Settings: SettingsUi + 'static + Send + Sync {
|
||||
const PRESERVED_KEYS: Option<&'static [&'static str]> = None;
|
||||
|
||||
/// The type that is stored in an individual JSON file.
|
||||
type FileContent: Clone + Default + Serialize + DeserializeOwned + JsonSchema;
|
||||
type FileContent: Clone + Default + Serialize + DeserializeOwned + JsonSchema + SettingsUi;
|
||||
|
||||
/// The logic for combining together values from one or more JSON files into the
|
||||
/// final value for this setting.
|
||||
@@ -1565,7 +1565,7 @@ impl<T: Settings> AnySettingValue for SettingValue<T> {
|
||||
}
|
||||
|
||||
fn settings_ui_item(&self) -> SettingsUiEntry {
|
||||
<T as SettingsUi>::settings_ui_entry()
|
||||
<<T as Settings>::FileContent as SettingsUi>::settings_ui_entry()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2147,12 +2147,12 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq, SettingsUi)]
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
struct TurboSetting(bool);
|
||||
|
||||
impl Settings for TurboSetting {
|
||||
const KEY: Option<&'static str> = Some("turbo");
|
||||
type FileContent = Option<bool>;
|
||||
type FileContent = bool;
|
||||
|
||||
fn load(sources: SettingsSources<Self::FileContent>, _: &mut App) -> Result<Self> {
|
||||
sources.json_merge()
|
||||
@@ -2161,7 +2161,7 @@ mod tests {
|
||||
fn import_from_vscode(_vscode: &VsCodeSettings, _current: &mut Self::FileContent) {}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, SettingsUi)]
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
||||
struct MultiKeySettings {
|
||||
#[serde(default)]
|
||||
key1: String,
|
||||
@@ -2169,7 +2169,7 @@ mod tests {
|
||||
key2: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
|
||||
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, SettingsUi)]
|
||||
struct MultiKeySettingsJson {
|
||||
key1: Option<String>,
|
||||
key2: Option<String>,
|
||||
@@ -2194,7 +2194,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, SettingsUi)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct JournalSettings {
|
||||
pub path: String,
|
||||
pub hour_format: HourFormat,
|
||||
@@ -2207,7 +2207,7 @@ mod tests {
|
||||
Hour24,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
|
||||
#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema, SettingsUi)]
|
||||
struct JournalSettingsJson {
|
||||
pub path: Option<String>,
|
||||
pub hour_format: Option<HourFormat>,
|
||||
|
||||
@@ -7,9 +7,16 @@ use crate::SettingsStore;
|
||||
|
||||
pub trait SettingsUi {
|
||||
fn settings_ui_item() -> SettingsUiItem {
|
||||
// todo(settings_ui): remove this default impl, only entry should have a default impl
|
||||
// because it's expected that the macro or custom impl use the item and the known paths to create the entry
|
||||
SettingsUiItem::None
|
||||
}
|
||||
fn settings_ui_entry() -> SettingsUiEntry;
|
||||
|
||||
fn settings_ui_entry() -> SettingsUiEntry {
|
||||
SettingsUiEntry {
|
||||
item: SettingsUiEntryVariant::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SettingsUiEntry {
|
||||
@@ -106,11 +113,11 @@ impl SettingsUi for bool {
|
||||
fn settings_ui_item() -> SettingsUiItem {
|
||||
SettingsUiItem::Single(SettingsUiItemSingle::SwitchField)
|
||||
}
|
||||
}
|
||||
|
||||
fn settings_ui_entry() -> SettingsUiEntry {
|
||||
SettingsUiEntry {
|
||||
item: SettingsUiEntryVariant::None,
|
||||
}
|
||||
impl SettingsUi for Option<bool> {
|
||||
fn settings_ui_item() -> SettingsUiItem {
|
||||
SettingsUiItem::Single(SettingsUiItemSingle::SwitchField)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,10 +125,4 @@ impl SettingsUi for u64 {
|
||||
fn settings_ui_item() -> SettingsUiItem {
|
||||
SettingsUiItem::Single(SettingsUiItemSingle::NumericStepper)
|
||||
}
|
||||
|
||||
fn settings_ui_entry() -> SettingsUiEntry {
|
||||
SettingsUiEntry {
|
||||
item: SettingsUiEntryVariant::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user