From e51dc25e1d2fd1a0ff58e2098efd135851daff60 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 12 Jul 2022 11:00:02 -0700 Subject: [PATCH] Start moving default settings into a JSON file Co-authored-by: Antonio Scandurra --- assets/default-settings.json | 39 ++++++++++++++++ crates/search/src/buffer_search.rs | 7 ++- crates/search/src/project_search.rs | 7 ++- crates/settings/src/settings.rs | 48 +++++++++++++------- crates/zed/src/main.rs | 70 +---------------------------- 5 files changed, 82 insertions(+), 89 deletions(-) create mode 100644 assets/default-settings.json diff --git a/assets/default-settings.json b/assets/default-settings.json new file mode 100644 index 0000000000..e88df5c2c5 --- /dev/null +++ b/assets/default-settings.json @@ -0,0 +1,39 @@ +{ + "theme": "cave-light", + "buffer_font_family": "Zed Mono", + "buffer_font_size": 15, + "hover_popover_enabled": true, + "vim_mode": false, + "autosave": "off", + "projects_online_by_default": true, + "languages_overrides": { + "Plain Text": { + "soft_wrap": "preferred_line_length" + }, + "C": { + "tab_size": 2 + }, + "C++": { + "tab_size": 2 + }, + "Go": { + "tab_size": 4, + "hard_tabs": true + }, + "Markdown": { + "soft_wrap": "preferred_line_length" + }, + "Rust": { + "tab_size": 4 + }, + "JavaScript": { + "tab_size": 2 + }, + "TypeScript": { + "tab_size": 2 + }, + "TSX": { + "tab_size": 2 + } + } +} diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 59f78cdc33..5303c20e89 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -608,8 +608,11 @@ mod tests { let fonts = cx.font_cache(); let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default()); theme.search.match_background = Color::red(); - let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap(); - cx.update(|cx| cx.set_global(settings)); + cx.update(|cx| { + let mut settings = Settings::test(cx); + settings.theme = Arc::new(theme); + cx.set_global(settings) + }); let buffer = cx.add_model(|cx| { Buffer::new( diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 5ee2dcbb27..e1acc6a771 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -911,8 +911,11 @@ mod tests { let fonts = cx.font_cache(); let mut theme = gpui::fonts::with_font_cache(fonts.clone(), || theme::Theme::default()); theme.search.match_background = Color::red(); - let settings = Settings::new("Courier", &fonts, Arc::new(theme)).unwrap(); - cx.update(|cx| cx.set_global(settings)); + cx.update(|cx| { + let mut settings = Settings::test(cx); + settings.theme = Arc::new(theme); + cx.set_global(settings) + }); let fs = FakeFs::new(cx.background()); fs.insert_tree( diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index 98df5e2f1f..2e418e502d 100644 --- a/crates/settings/src/settings.rs +++ b/crates/settings/src/settings.rs @@ -1,7 +1,10 @@ mod keymap_file; use anyhow::Result; -use gpui::font_cache::{FamilyId, FontCache}; +use gpui::{ + font_cache::{FamilyId, FontCache}, + AssetSource, +}; use schemars::{ gen::{SchemaGenerator, SchemaSettings}, schema::{ @@ -97,24 +100,35 @@ pub struct SettingsFileContent { } impl Settings { - pub fn new( - buffer_font_family: &str, + pub fn defaults( + assets: impl AssetSource, font_cache: &FontCache, - theme: Arc, - ) -> Result { - Ok(Self { - buffer_font_family: font_cache.load_family(&[buffer_font_family])?, - buffer_font_size: 15., - default_buffer_font_size: 15., - hover_popover_enabled: true, - vim_mode: false, - autosave: Autosave::Off, - language_settings: Default::default(), - language_defaults: Default::default(), + themes: &ThemeRegistry, + ) -> Self { + let defaults = assets.load("default-settings.json").unwrap(); + let defaults: SettingsFileContent = serde_json::from_slice(defaults.as_ref()).unwrap(); + Self { + buffer_font_family: font_cache + .load_family(&[defaults.buffer_font_family.as_ref().unwrap()]) + .unwrap(), + buffer_font_size: defaults.buffer_font_size.unwrap(), + default_buffer_font_size: defaults.buffer_font_size.unwrap(), + hover_popover_enabled: defaults.hover_popover_enabled.unwrap(), + 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, + }, + language_defaults: defaults.language_overrides, language_overrides: Default::default(), - projects_online_by_default: true, - theme, - }) + theme: themes.get(&defaults.theme.unwrap()).unwrap(), + } } pub fn with_language_defaults( diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 723738cf28..02f8008028 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -38,7 +38,7 @@ use std::{ time::Duration, }; use terminal; -use theme::{ThemeRegistry, DEFAULT_THEME_NAME}; +use theme::ThemeRegistry; use util::{ResultExt, TryFutureExt}; use workspace::{self, AppState, NewFile, OpenPaths}; use zed::{ @@ -72,73 +72,7 @@ fn main() { let fs = Arc::new(RealFs); let themes = ThemeRegistry::new(Assets, app.font_cache()); - let theme = themes.get(DEFAULT_THEME_NAME).unwrap(); - let default_settings = Settings::new("Zed Mono", &app.font_cache(), theme) - .unwrap() - .with_language_defaults( - languages::PLAIN_TEXT.name(), - settings::LanguageSettings { - soft_wrap: Some(settings::SoftWrap::PreferredLineLength), - ..Default::default() - }, - ) - .with_language_defaults( - "C", - settings::LanguageSettings { - tab_size: Some(2.try_into().unwrap()), - ..Default::default() - }, - ) - .with_language_defaults( - "C++", - settings::LanguageSettings { - tab_size: Some(2.try_into().unwrap()), - ..Default::default() - }, - ) - .with_language_defaults( - "Go", - settings::LanguageSettings { - tab_size: Some(4.try_into().unwrap()), - hard_tabs: Some(true), - ..Default::default() - }, - ) - .with_language_defaults( - "Markdown", - settings::LanguageSettings { - soft_wrap: Some(settings::SoftWrap::PreferredLineLength), - ..Default::default() - }, - ) - .with_language_defaults( - "Rust", - settings::LanguageSettings { - tab_size: Some(4.try_into().unwrap()), - ..Default::default() - }, - ) - .with_language_defaults( - "JavaScript", - settings::LanguageSettings { - tab_size: Some(2.try_into().unwrap()), - ..Default::default() - }, - ) - .with_language_defaults( - "TypeScript", - settings::LanguageSettings { - tab_size: Some(2.try_into().unwrap()), - ..Default::default() - }, - ) - .with_language_defaults( - "TSX", - settings::LanguageSettings { - tab_size: Some(2.try_into().unwrap()), - ..Default::default() - }, - ); + let default_settings = Settings::defaults(Assets, &app.font_cache(), &themes); let config_files = load_config_files(&app, fs.clone());