Move all default settings from source code into the JSON file

This commit is contained in:
Max Brunsfeld 2022-07-12 11:35:19 -07:00
parent e51dc25e1d
commit ec8a493700
7 changed files with 70 additions and 58 deletions

View File

@ -1,11 +1,17 @@
{
"theme": "cave-light",
"theme": "cave-dark",
"buffer_font_family": "Zed Mono",
"buffer_font_size": 15,
"hover_popover_enabled": true,
"vim_mode": false,
"autosave": "off",
"projects_online_by_default": true,
"enable_language_server": true,
"format_on_save": "language_server",
"preferred_line_length": 80,
"soft_wrap": "none",
"hard_tabs": false,
"tab_size": 4,
"languages_overrides": {
"Plain Text": {
"soft_wrap": "preferred_line_length"

View File

@ -2010,7 +2010,7 @@ async fn test_formatting_buffer(cx_a: &mut TestAppContext, cx_b: &mut TestAppCon
// host's configuration is honored as opposed to using the guest's settings.
cx_a.update(|cx| {
cx.update_global(|settings: &mut Settings, _| {
settings.language_settings.format_on_save = Some(FormatOnSave::External {
settings.editor_defaults.format_on_save = Some(FormatOnSave::External {
command: "awk".to_string(),
arguments: vec!["{sub(/two/,\"{buffer_path}\")}1".to_string()],
});

View File

@ -983,7 +983,7 @@ pub mod tests {
language.set_theme(&theme);
cx.update(|cx| {
let mut settings = Settings::test(cx);
settings.language_settings.tab_size = Some(2.try_into().unwrap());
settings.editor_defaults.tab_size = Some(2.try_into().unwrap());
cx.set_global(settings);
});

View File

@ -7613,7 +7613,7 @@ mod tests {
let mut cx = EditorTestContext::new(cx).await;
cx.update(|cx| {
cx.update_global::<Settings, _, _>(|settings, _| {
settings.language_settings.hard_tabs = Some(true);
settings.editor_overrides.hard_tabs = Some(true);
});
});

View File

@ -29,7 +29,8 @@ pub struct Settings {
pub hover_popover_enabled: bool,
pub vim_mode: bool,
pub autosave: Autosave,
pub language_settings: LanguageSettings,
pub editor_defaults: LanguageSettings,
pub editor_overrides: LanguageSettings,
pub language_defaults: HashMap<Arc<str>, LanguageSettings>,
pub language_overrides: HashMap<Arc<str>, LanguageSettings>,
pub theme: Arc<Theme>,
@ -86,11 +87,7 @@ pub struct SettingsFileContent {
#[serde(default)]
pub vim_mode: Option<bool>,
#[serde(default)]
pub format_on_save: Option<FormatOnSave>,
#[serde(default)]
pub autosave: Option<Autosave>,
#[serde(default)]
pub enable_language_server: Option<bool>,
#[serde(flatten)]
pub editor: LanguageSettings,
#[serde(default)]
@ -105,8 +102,14 @@ impl Settings {
font_cache: &FontCache,
themes: &ThemeRegistry,
) -> Self {
let defaults = assets.load("default-settings.json").unwrap();
let defaults: SettingsFileContent = serde_json::from_slice(defaults.as_ref()).unwrap();
fn required<T>(value: Option<T>) -> Option<T> {
assert!(value.is_some(), "missing default setting value");
value
}
let defaults: SettingsFileContent =
serde_json::from_slice(assets.load("default-settings.json").unwrap().as_ref()).unwrap();
Self {
buffer_font_family: font_cache
.load_family(&[defaults.buffer_font_family.as_ref().unwrap()])
@ -117,15 +120,16 @@ impl Settings {
projects_online_by_default: defaults.projects_online_by_default.unwrap(),
vim_mode: defaults.vim_mode.unwrap(),
autosave: defaults.autosave.unwrap(),
language_settings: LanguageSettings {
tab_size: defaults.editor.tab_size,
hard_tabs: defaults.editor.hard_tabs,
soft_wrap: defaults.editor.soft_wrap,
preferred_line_length: defaults.editor.preferred_line_length,
format_on_save: defaults.editor.format_on_save,
enable_language_server: defaults.editor.enable_language_server,
editor_defaults: LanguageSettings {
tab_size: required(defaults.editor.tab_size),
hard_tabs: required(defaults.editor.hard_tabs),
soft_wrap: required(defaults.editor.soft_wrap),
preferred_line_length: required(defaults.editor.preferred_line_length),
format_on_save: required(defaults.editor.format_on_save),
enable_language_server: required(defaults.editor.enable_language_server),
},
language_defaults: defaults.language_overrides,
editor_overrides: Default::default(),
language_overrides: Default::default(),
theme: themes.get(&defaults.theme.unwrap()).unwrap(),
}
@ -143,48 +147,37 @@ impl Settings {
pub fn tab_size(&self, language: Option<&str>) -> NonZeroU32 {
self.language_setting(language, |settings| settings.tab_size)
.unwrap_or(4.try_into().unwrap())
}
pub fn hard_tabs(&self, language: Option<&str>) -> bool {
self.language_setting(language, |settings| settings.hard_tabs)
.unwrap_or(false)
}
pub fn soft_wrap(&self, language: Option<&str>) -> SoftWrap {
self.language_setting(language, |settings| settings.soft_wrap)
.unwrap_or(SoftWrap::None)
}
pub fn preferred_line_length(&self, language: Option<&str>) -> u32 {
self.language_setting(language, |settings| settings.preferred_line_length)
.unwrap_or(80)
}
pub fn format_on_save(&self, language: Option<&str>) -> FormatOnSave {
self.language_setting(language, |settings| settings.format_on_save.clone())
.unwrap_or(FormatOnSave::LanguageServer)
}
pub fn enable_language_server(&self, language: Option<&str>) -> bool {
self.language_setting(language, |settings| settings.enable_language_server)
.unwrap_or(true)
}
fn language_setting<F, R>(&self, language: Option<&str>, f: F) -> Option<R>
fn language_setting<F, R>(&self, language: Option<&str>, f: F) -> R
where
F: Fn(&LanguageSettings) -> Option<R>,
{
let mut language_override = None;
let mut language_default = None;
if let Some(language) = language {
language_override = self.language_overrides.get(language).and_then(&f);
language_default = self.language_defaults.get(language).and_then(&f);
}
language_override
.or_else(|| f(&self.language_settings))
.or(language_default)
None.or_else(|| language.and_then(|l| self.language_overrides.get(l).and_then(&f)))
.or_else(|| f(&self.editor_overrides))
.or_else(|| language.and_then(|l| self.language_defaults.get(l).and_then(&f)))
.or_else(|| f(&self.editor_defaults))
.expect("missing default")
}
#[cfg(any(test, feature = "test-support"))]
@ -196,7 +189,15 @@ impl Settings {
hover_popover_enabled: true,
vim_mode: false,
autosave: Autosave::Off,
language_settings: Default::default(),
editor_defaults: LanguageSettings {
tab_size: Some(4.try_into().unwrap()),
hard_tabs: Some(false),
soft_wrap: Some(SoftWrap::None),
preferred_line_length: Some(80),
format_on_save: Some(FormatOnSave::LanguageServer),
enable_language_server: Some(true),
},
editor_overrides: Default::default(),
language_defaults: Default::default(),
language_overrides: Default::default(),
projects_online_by_default: true,
@ -238,18 +239,19 @@ impl Settings {
merge(&mut self.hover_popover_enabled, data.hover_popover_enabled);
merge(&mut self.vim_mode, data.vim_mode);
merge(&mut self.autosave, data.autosave);
merge_option(
&mut self.language_settings.format_on_save,
data.format_on_save.clone(),
&mut self.editor_overrides.format_on_save,
data.editor.format_on_save.clone(),
);
merge_option(
&mut self.language_settings.enable_language_server,
data.enable_language_server,
&mut self.editor_overrides.enable_language_server,
data.editor.enable_language_server,
);
merge_option(&mut self.language_settings.soft_wrap, data.editor.soft_wrap);
merge_option(&mut self.language_settings.tab_size, data.editor.tab_size);
merge_option(&mut self.editor_overrides.soft_wrap, data.editor.soft_wrap);
merge_option(&mut self.editor_overrides.tab_size, data.editor.tab_size);
merge_option(
&mut self.language_settings.preferred_line_length,
&mut self.editor_overrides.preferred_line_length,
data.editor.preferred_line_length,
);

View File

@ -12,8 +12,6 @@ use std::{collections::HashMap, sync::Arc};
pub use theme_registry::*;
pub const DEFAULT_THEME_NAME: &'static str = "cave-dark";
#[derive(Deserialize, Default)]
pub struct Theme {
#[serde(default)]

View File

@ -400,7 +400,7 @@ mod tests {
collections::HashSet,
path::{Path, PathBuf},
};
use theme::{Theme, ThemeRegistry, DEFAULT_THEME_NAME};
use theme::ThemeRegistry;
use workspace::{
open_paths, pane, Item, ItemHandle, NewFile, Pane, SplitDirection, WorkspaceHandle,
};
@ -1530,23 +1530,29 @@ mod tests {
}
#[gpui::test]
fn test_bundled_themes(cx: &mut MutableAppContext) {
fn test_bundled_settings_and_themes(cx: &mut MutableAppContext) {
cx.platform()
.fonts()
.add_fonts(&[
Assets
.load("fonts/zed-sans/zed-sans-extended.ttf")
.unwrap()
.to_vec()
.into(),
Assets
.load("fonts/zed-mono/zed-mono-extended.ttf")
.unwrap()
.to_vec()
.into(),
])
.unwrap();
let themes = ThemeRegistry::new(Assets, cx.font_cache().clone());
lazy_static::lazy_static! {
static ref DEFAULT_THEME: parking_lot::Mutex<Option<Arc<Theme>>> = Default::default();
static ref FONTS: Vec<Arc<Vec<u8>>> = vec![
Assets.load("fonts/zed-sans/zed-sans-extended.ttf").unwrap().to_vec().into(),
Assets.load("fonts/zed-mono/zed-mono-extended.ttf").unwrap().to_vec().into(),
];
}
cx.platform().fonts().add_fonts(&FONTS).unwrap();
let settings = Settings::defaults(Assets, cx.font_cache(), &themes);
let mut has_default_theme = false;
for theme_name in themes.list() {
let theme = themes.get(&theme_name).unwrap();
if theme.name == DEFAULT_THEME_NAME {
if theme.name == settings.theme.name {
has_default_theme = true;
}
assert_eq!(theme.name, theme_name);