Start moving default settings into a JSON file

Co-authored-by: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Max Brunsfeld 2022-07-12 11:00:02 -07:00
parent 1ac8265028
commit e51dc25e1d
5 changed files with 82 additions and 89 deletions

View File

@ -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
}
}
}

View File

@ -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(

View File

@ -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(

View File

@ -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<Theme>,
) -> Result<Self> {
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(

View File

@ -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());