mod keymap_file; use anyhow::Result; use gpui::{ font_cache::{FamilyId, FontCache}, AssetSource, }; use schemars::{ gen::{SchemaGenerator, SchemaSettings}, schema::{InstanceType, ObjectValidation, Schema, SchemaObject, SingleOrVec}, JsonSchema, }; use serde::{de::DeserializeOwned, Deserialize}; use serde_json::Value; use std::{collections::HashMap, num::NonZeroU32, str, sync::Arc}; use theme::{Theme, ThemeRegistry}; use util::ResultExt as _; pub use keymap_file::{keymap_file_json_schema, KeymapFileContent}; #[derive(Clone)] pub struct Settings { pub projects_online_by_default: bool, pub buffer_font_family: FamilyId, pub buffer_font_size: f32, pub default_buffer_font_size: f32, pub hover_popover_enabled: bool, pub show_completions_on_input: bool, pub vim_mode: bool, pub autosave: Autosave, pub editor_defaults: EditorSettings, pub editor_overrides: EditorSettings, pub language_defaults: HashMap, EditorSettings>, pub language_overrides: HashMap, EditorSettings>, pub theme: Arc, } #[derive(Clone, Debug, Default, Deserialize, JsonSchema)] pub struct EditorSettings { pub tab_size: Option, pub hard_tabs: Option, pub soft_wrap: Option, pub preferred_line_length: Option, pub format_on_save: Option, pub enable_language_server: Option, } #[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum SoftWrap { None, EditorWidth, PreferredLineLength, } #[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum FormatOnSave { Off, LanguageServer, External { command: String, arguments: Vec, }, } #[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] pub enum Autosave { Off, AfterDelay { milliseconds: u64 }, OnFocusChange, OnWindowChange, } #[derive(Clone, Debug, Default, Deserialize, JsonSchema)] pub struct SettingsFileContent { #[serde(default)] pub projects_online_by_default: Option, #[serde(default)] pub buffer_font_family: Option, #[serde(default)] pub buffer_font_size: Option, #[serde(default)] pub hover_popover_enabled: Option, #[serde(default)] pub show_completions_on_input: Option, #[serde(default)] pub vim_mode: Option, #[serde(default)] pub autosave: Option, #[serde(flatten)] pub editor: EditorSettings, #[serde(default)] #[serde(alias = "language_overrides")] pub languages: HashMap, EditorSettings>, #[serde(default)] pub theme: Option, } impl Settings { pub fn defaults( assets: impl AssetSource, font_cache: &FontCache, themes: &ThemeRegistry, ) -> Self { fn required(value: Option) -> Option { assert!(value.is_some(), "missing default setting value"); value } let defaults: SettingsFileContent = parse_json_with_comments( str::from_utf8(assets.load("settings/default.json").unwrap().as_ref()).unwrap(), ) .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(), show_completions_on_input: defaults.show_completions_on_input.unwrap(), projects_online_by_default: defaults.projects_online_by_default.unwrap(), vim_mode: defaults.vim_mode.unwrap(), autosave: defaults.autosave.unwrap(), editor_defaults: EditorSettings { 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.languages, editor_overrides: Default::default(), language_overrides: Default::default(), theme: themes.get(&defaults.theme.unwrap()).unwrap(), } } pub fn set_user_settings( &mut self, data: SettingsFileContent, theme_registry: &ThemeRegistry, font_cache: &FontCache, ) { if let Some(value) = &data.buffer_font_family { if let Some(id) = font_cache.load_family(&[value]).log_err() { self.buffer_font_family = id; } } if let Some(value) = &data.theme { if let Some(theme) = theme_registry.get(&value.to_string()).log_err() { self.theme = theme; } } merge( &mut self.projects_online_by_default, data.projects_online_by_default, ); merge(&mut self.buffer_font_size, data.buffer_font_size); merge(&mut self.default_buffer_font_size, data.buffer_font_size); merge(&mut self.hover_popover_enabled, data.hover_popover_enabled); merge( &mut self.show_completions_on_input, data.show_completions_on_input, ); merge(&mut self.vim_mode, data.vim_mode); merge(&mut self.autosave, data.autosave); self.editor_overrides = data.editor; self.language_overrides = data.languages; } pub fn with_language_defaults( mut self, language_name: impl Into>, overrides: EditorSettings, ) -> Self { self.language_defaults .insert(language_name.into(), overrides); self } pub fn tab_size(&self, language: Option<&str>) -> NonZeroU32 { self.language_setting(language, |settings| settings.tab_size) } pub fn hard_tabs(&self, language: Option<&str>) -> bool { self.language_setting(language, |settings| settings.hard_tabs) } pub fn soft_wrap(&self, language: Option<&str>) -> SoftWrap { self.language_setting(language, |settings| settings.soft_wrap) } pub fn preferred_line_length(&self, language: Option<&str>) -> u32 { self.language_setting(language, |settings| settings.preferred_line_length) } pub fn format_on_save(&self, language: Option<&str>) -> FormatOnSave { self.language_setting(language, |settings| settings.format_on_save.clone()) } pub fn enable_language_server(&self, language: Option<&str>) -> bool { self.language_setting(language, |settings| settings.enable_language_server) } fn language_setting(&self, language: Option<&str>, f: F) -> R where F: Fn(&EditorSettings) -> Option, { 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"))] pub fn test(cx: &gpui::AppContext) -> Settings { Settings { buffer_font_family: cx.font_cache().load_family(&["Monaco"]).unwrap(), buffer_font_size: 14., default_buffer_font_size: 14., hover_popover_enabled: true, show_completions_on_input: true, vim_mode: false, autosave: Autosave::Off, editor_defaults: EditorSettings { 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, theme: gpui::fonts::with_font_cache(cx.font_cache().clone(), || Default::default()), } } #[cfg(any(test, feature = "test-support"))] pub fn test_async(cx: &mut gpui::TestAppContext) { cx.update(|cx| { let settings = Self::test(cx); cx.set_global(settings.clone()); }); } } pub fn settings_file_json_schema( theme_names: Vec, language_names: &[String], ) -> serde_json::Value { let settings = SchemaSettings::draft07().with(|settings| { settings.option_add_null_type = false; }); let generator = SchemaGenerator::new(settings); let mut root_schema = generator.into_root_schema_for::(); // Create a schema for a theme name. let theme_name_schema = SchemaObject { instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))), enum_values: Some( theme_names .into_iter() .map(|name| Value::String(name)) .collect(), ), ..Default::default() }; // Create a schema for a 'languages overrides' object, associating editor // settings with specific langauges. assert!(root_schema.definitions.contains_key("EditorSettings")); let languages_object_schema = SchemaObject { instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))), object: Some(Box::new(ObjectValidation { properties: language_names .iter() .map(|name| { ( name.clone(), Schema::new_ref("#/definitions/EditorSettings".into()), ) }) .collect(), ..Default::default() })), ..Default::default() }; // Add these new schemas as definitions, and modify properties of the root // schema to reference them. root_schema.definitions.extend([ ("ThemeName".into(), theme_name_schema.into()), ("Languages".into(), languages_object_schema.into()), ]); root_schema .schema .object .as_mut() .unwrap() .properties .extend([ ( "theme".to_owned(), Schema::new_ref("#/definitions/ThemeName".into()), ), ( "languages".to_owned(), Schema::new_ref("#/definitions/Languages".into()), ), // For backward compatibility ( "language_overrides".to_owned(), Schema::new_ref("#/definitions/Languages".into()), ), ]); serde_json::to_value(root_schema).unwrap() } fn merge(target: &mut T, value: Option) { if let Some(value) = value { *target = value; } } pub fn parse_json_with_comments(content: &str) -> Result { Ok(serde_json::from_reader( json_comments::CommentSettings::c_style().strip_comments(content.as_bytes()), )?) }