2022-04-11 23:50:44 +00:00
|
|
|
mod keymap_file;
|
2022-04-11 22:22:18 +00:00
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
use anyhow::Result;
|
2022-07-12 18:00:02 +00:00
|
|
|
use gpui::{
|
|
|
|
font_cache::{FamilyId, FontCache},
|
|
|
|
AssetSource,
|
|
|
|
};
|
2022-04-07 23:00:51 +00:00
|
|
|
use schemars::{
|
|
|
|
gen::{SchemaGenerator, SchemaSettings},
|
2022-07-12 19:19:01 +00:00
|
|
|
schema::{InstanceType, ObjectValidation, Schema, SchemaObject, SingleOrVec},
|
2022-04-07 23:00:51 +00:00
|
|
|
JsonSchema,
|
|
|
|
};
|
2022-04-21 18:58:18 +00:00
|
|
|
use serde::{de::DeserializeOwned, Deserialize};
|
2022-04-08 01:15:02 +00:00
|
|
|
use serde_json::Value;
|
2022-07-12 18:42:43 +00:00
|
|
|
use std::{collections::HashMap, num::NonZeroU32, str, sync::Arc};
|
2022-04-06 00:10:17 +00:00
|
|
|
use theme::{Theme, ThemeRegistry};
|
|
|
|
use util::ResultExt as _;
|
|
|
|
|
2022-04-21 20:33:39 +00:00
|
|
|
pub use keymap_file::{keymap_file_json_schema, KeymapFileContent};
|
2022-04-11 23:50:44 +00:00
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Settings {
|
2022-08-22 21:49:01 +00:00
|
|
|
pub experiments: FeatureFlags,
|
2022-06-03 23:57:50 +00:00
|
|
|
pub projects_online_by_default: bool,
|
2022-04-06 00:10:17 +00:00
|
|
|
pub buffer_font_family: FamilyId,
|
2022-05-27 20:02:52 +00:00
|
|
|
pub default_buffer_font_size: f32,
|
2022-07-16 01:27:10 +00:00
|
|
|
pub buffer_font_size: f32,
|
2022-06-27 23:21:22 +00:00
|
|
|
pub hover_popover_enabled: bool,
|
2022-07-12 20:09:01 +00:00
|
|
|
pub show_completions_on_input: bool,
|
2022-04-06 00:10:17 +00:00
|
|
|
pub vim_mode: bool,
|
2022-07-05 11:01:27 +00:00
|
|
|
pub autosave: Autosave,
|
2022-07-12 18:42:43 +00:00
|
|
|
pub editor_defaults: EditorSettings,
|
|
|
|
pub editor_overrides: EditorSettings,
|
2022-07-16 01:27:10 +00:00
|
|
|
pub terminal_defaults: TerminalSettings,
|
|
|
|
pub terminal_overrides: TerminalSettings,
|
2022-07-12 18:42:43 +00:00
|
|
|
pub language_defaults: HashMap<Arc<str>, EditorSettings>,
|
|
|
|
pub language_overrides: HashMap<Arc<str>, EditorSettings>,
|
2022-08-04 02:32:18 +00:00
|
|
|
pub lsp: HashMap<Arc<str>, LspSettings>,
|
2022-04-06 00:10:17 +00:00
|
|
|
pub theme: Arc<Theme>,
|
2022-09-08 20:31:04 +00:00
|
|
|
pub internal: bool,
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 21:49:01 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, Deserialize, JsonSchema)]
|
2022-09-08 22:47:27 +00:00
|
|
|
pub struct FeatureFlags {
|
|
|
|
pub experimental_themes: bool,
|
|
|
|
}
|
2022-08-22 21:49:01 +00:00
|
|
|
|
|
|
|
impl FeatureFlags {
|
|
|
|
pub fn keymap_files(&self) -> Vec<&'static str> {
|
2022-08-31 20:06:15 +00:00
|
|
|
vec![]
|
2022-08-22 21:49:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
|
2022-07-12 18:42:43 +00:00
|
|
|
pub struct EditorSettings {
|
2022-06-23 00:13:52 +00:00
|
|
|
pub tab_size: Option<NonZeroU32>,
|
2022-06-09 01:08:07 +00:00
|
|
|
pub hard_tabs: Option<bool>,
|
2022-04-06 00:10:17 +00:00
|
|
|
pub soft_wrap: Option<SoftWrap>,
|
|
|
|
pub preferred_line_length: Option<u32>,
|
2022-07-07 09:03:37 +00:00
|
|
|
pub format_on_save: Option<FormatOnSave>,
|
2022-06-09 08:07:31 +00:00
|
|
|
pub enable_language_server: Option<bool>,
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum SoftWrap {
|
|
|
|
None,
|
|
|
|
EditorWidth,
|
|
|
|
PreferredLineLength,
|
|
|
|
}
|
|
|
|
|
2022-07-07 09:03:37 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum FormatOnSave {
|
|
|
|
Off,
|
|
|
|
LanguageServer,
|
|
|
|
External {
|
|
|
|
command: String,
|
|
|
|
arguments: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-07-05 07:25:22 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum Autosave {
|
|
|
|
Off,
|
2022-07-05 11:01:27 +00:00
|
|
|
AfterDelay { milliseconds: u64 },
|
2022-07-05 07:25:22 +00:00
|
|
|
OnFocusChange,
|
|
|
|
OnWindowChange,
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:27:10 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
|
|
|
|
pub struct TerminalSettings {
|
|
|
|
pub shell: Option<Shell>,
|
|
|
|
pub working_directory: Option<WorkingDirectory>,
|
|
|
|
pub font_size: Option<f32>,
|
|
|
|
pub font_family: Option<String>,
|
2022-07-18 21:50:33 +00:00
|
|
|
pub env: Option<HashMap<String, String>>,
|
2022-08-16 01:05:07 +00:00
|
|
|
pub blinking: Option<TerminalBlink>,
|
2022-08-19 20:54:25 +00:00
|
|
|
pub alternate_scroll: Option<AlternateScroll>,
|
2022-08-16 01:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum TerminalBlink {
|
2022-08-18 18:28:18 +00:00
|
|
|
Off,
|
2022-08-17 21:41:28 +00:00
|
|
|
TerminalControlled,
|
2022-08-18 18:28:18 +00:00
|
|
|
On,
|
2022-08-16 01:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TerminalBlink {
|
|
|
|
fn default() -> Self {
|
2022-08-17 21:41:28 +00:00
|
|
|
TerminalBlink::TerminalControlled
|
2022-08-16 01:05:07 +00:00
|
|
|
}
|
2022-07-16 01:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum Shell {
|
|
|
|
System,
|
|
|
|
Program(String),
|
|
|
|
WithArguments { program: String, args: Vec<String> },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Shell {
|
|
|
|
fn default() -> Self {
|
|
|
|
Shell::System
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 20:54:25 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum AlternateScroll {
|
|
|
|
On,
|
|
|
|
Off,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for AlternateScroll {
|
|
|
|
fn default() -> Self {
|
|
|
|
AlternateScroll::On
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:27:10 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub enum WorkingDirectory {
|
|
|
|
CurrentProjectDirectory,
|
|
|
|
FirstProjectDirectory,
|
|
|
|
AlwaysHome,
|
|
|
|
Always { directory: String },
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, JsonSchema)]
|
|
|
|
pub struct SettingsFileContent {
|
2022-08-22 21:49:01 +00:00
|
|
|
pub experiments: Option<FeatureFlags>,
|
2022-06-03 23:57:50 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub projects_online_by_default: Option<bool>,
|
2022-04-06 00:10:17 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub buffer_font_family: Option<String>,
|
|
|
|
#[serde(default)]
|
|
|
|
pub buffer_font_size: Option<f32>,
|
|
|
|
#[serde(default)]
|
2022-06-27 23:21:22 +00:00
|
|
|
pub hover_popover_enabled: Option<bool>,
|
|
|
|
#[serde(default)]
|
2022-07-12 20:09:01 +00:00
|
|
|
pub show_completions_on_input: Option<bool>,
|
|
|
|
#[serde(default)]
|
2022-04-06 00:10:17 +00:00
|
|
|
pub vim_mode: Option<bool>,
|
2022-04-28 21:31:06 +00:00
|
|
|
#[serde(default)]
|
2022-07-05 11:01:27 +00:00
|
|
|
pub autosave: Option<Autosave>,
|
2022-04-06 00:10:17 +00:00
|
|
|
#[serde(flatten)]
|
2022-07-12 18:42:43 +00:00
|
|
|
pub editor: EditorSettings,
|
2022-04-06 00:10:17 +00:00
|
|
|
#[serde(default)]
|
2022-07-16 01:27:10 +00:00
|
|
|
pub terminal: TerminalSettings,
|
|
|
|
#[serde(default)]
|
2022-07-12 18:42:43 +00:00
|
|
|
#[serde(alias = "language_overrides")]
|
|
|
|
pub languages: HashMap<Arc<str>, EditorSettings>,
|
2022-04-06 00:10:17 +00:00
|
|
|
#[serde(default)]
|
2022-08-04 02:32:18 +00:00
|
|
|
pub lsp: HashMap<Arc<str>, LspSettings>,
|
|
|
|
#[serde(default)]
|
2022-04-06 00:10:17 +00:00
|
|
|
pub theme: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-08-04 02:32:18 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, JsonSchema)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
pub struct LspSettings {
|
|
|
|
pub initialization_options: Option<Value>,
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
impl Settings {
|
2022-07-12 18:00:02 +00:00
|
|
|
pub fn defaults(
|
|
|
|
assets: impl AssetSource,
|
2022-04-06 00:10:17 +00:00
|
|
|
font_cache: &FontCache,
|
2022-07-12 18:00:02 +00:00
|
|
|
themes: &ThemeRegistry,
|
|
|
|
) -> Self {
|
2022-07-12 18:35:19 +00:00
|
|
|
fn required<T>(value: Option<T>) -> Option<T> {
|
|
|
|
assert!(value.is_some(), "missing default setting value");
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:42:43 +00:00
|
|
|
let defaults: SettingsFileContent = parse_json_with_comments(
|
2022-07-12 21:19:32 +00:00
|
|
|
str::from_utf8(assets.load("settings/default.json").unwrap().as_ref()).unwrap(),
|
2022-07-12 18:42:43 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-07-12 18:35:19 +00:00
|
|
|
|
2022-07-12 18:00:02 +00:00
|
|
|
Self {
|
2022-08-22 21:49:01 +00:00
|
|
|
experiments: FeatureFlags::default(),
|
2022-07-12 18:00:02 +00:00
|
|
|
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(),
|
2022-07-12 20:09:01 +00:00
|
|
|
show_completions_on_input: defaults.show_completions_on_input.unwrap(),
|
2022-07-12 18:00:02 +00:00
|
|
|
projects_online_by_default: defaults.projects_online_by_default.unwrap(),
|
|
|
|
vim_mode: defaults.vim_mode.unwrap(),
|
|
|
|
autosave: defaults.autosave.unwrap(),
|
2022-07-12 18:42:43 +00:00
|
|
|
editor_defaults: EditorSettings {
|
2022-07-12 18:35:19 +00:00
|
|
|
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),
|
2022-07-12 18:00:02 +00:00
|
|
|
},
|
2022-07-12 18:35:19 +00:00
|
|
|
editor_overrides: Default::default(),
|
2022-07-16 01:27:10 +00:00
|
|
|
terminal_defaults: Default::default(),
|
|
|
|
terminal_overrides: Default::default(),
|
|
|
|
language_defaults: defaults.languages,
|
2022-04-06 00:10:17 +00:00
|
|
|
language_overrides: Default::default(),
|
2022-08-04 02:32:18 +00:00
|
|
|
lsp: defaults.lsp.clone(),
|
2022-07-12 18:00:02 +00:00
|
|
|
theme: themes.get(&defaults.theme.unwrap()).unwrap(),
|
2022-09-08 20:31:04 +00:00
|
|
|
internal: false,
|
2022-07-12 18:00:02 +00:00
|
|
|
}
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 00:43:00 +00:00
|
|
|
pub fn set_user_settings(
|
|
|
|
&mut self,
|
|
|
|
data: SettingsFileContent,
|
|
|
|
theme_registry: &ThemeRegistry,
|
|
|
|
font_cache: &FontCache,
|
2022-09-08 20:31:04 +00:00
|
|
|
internal: bool,
|
2022-07-13 00:43:00 +00:00
|
|
|
) {
|
|
|
|
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 {
|
2022-08-10 21:39:24 +00:00
|
|
|
if let Some(theme) = theme_registry.get(value).log_err() {
|
2022-07-13 00:43:00 +00:00
|
|
|
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);
|
2022-07-13 21:29:47 +00:00
|
|
|
merge(
|
|
|
|
&mut self.show_completions_on_input,
|
|
|
|
data.show_completions_on_input,
|
|
|
|
);
|
2022-07-13 00:43:00 +00:00
|
|
|
merge(&mut self.vim_mode, data.vim_mode);
|
|
|
|
merge(&mut self.autosave, data.autosave);
|
2022-08-22 21:49:01 +00:00
|
|
|
merge(&mut self.experiments, data.experiments);
|
2022-08-22 20:54:18 +00:00
|
|
|
|
2022-07-16 01:27:10 +00:00
|
|
|
// Ensure terminal font is loaded, so we can request it in terminal_element layout
|
|
|
|
if let Some(terminal_font) = &data.terminal.font_family {
|
|
|
|
font_cache.load_family(&[terminal_font]).log_err();
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:43:00 +00:00
|
|
|
self.editor_overrides = data.editor;
|
2022-07-16 01:27:10 +00:00
|
|
|
self.terminal_defaults.font_size = data.terminal.font_size;
|
|
|
|
self.terminal_overrides = data.terminal;
|
2022-07-13 00:43:00 +00:00
|
|
|
self.language_overrides = data.languages;
|
2022-08-04 02:32:18 +00:00
|
|
|
self.lsp = data.lsp;
|
2022-09-08 20:31:04 +00:00
|
|
|
self.internal = internal
|
2022-07-13 00:43:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 14:40:57 +00:00
|
|
|
pub fn with_language_defaults(
|
2022-04-06 00:10:17 +00:00
|
|
|
mut self,
|
|
|
|
language_name: impl Into<Arc<str>>,
|
2022-07-12 18:42:43 +00:00
|
|
|
overrides: EditorSettings,
|
2022-04-06 00:10:17 +00:00
|
|
|
) -> Self {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_defaults
|
2022-04-06 00:10:17 +00:00
|
|
|
.insert(language_name.into(), overrides);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-06-23 00:13:52 +00:00
|
|
|
pub fn tab_size(&self, language: Option<&str>) -> NonZeroU32 {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_setting(language, |settings| settings.tab_size)
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 01:08:07 +00:00
|
|
|
pub fn hard_tabs(&self, language: Option<&str>) -> bool {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_setting(language, |settings| settings.hard_tabs)
|
2022-06-09 01:08:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
pub fn soft_wrap(&self, language: Option<&str>) -> SoftWrap {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_setting(language, |settings| settings.soft_wrap)
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn preferred_line_length(&self, language: Option<&str>) -> u32 {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_setting(language, |settings| settings.preferred_line_length)
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:03:37 +00:00
|
|
|
pub fn format_on_save(&self, language: Option<&str>) -> FormatOnSave {
|
|
|
|
self.language_setting(language, |settings| settings.format_on_save.clone())
|
2022-04-28 21:31:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 08:07:31 +00:00
|
|
|
pub fn enable_language_server(&self, language: Option<&str>) -> bool {
|
2022-06-23 14:40:57 +00:00
|
|
|
self.language_setting(language, |settings| settings.enable_language_server)
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:35:19 +00:00
|
|
|
fn language_setting<F, R>(&self, language: Option<&str>, f: F) -> R
|
2022-06-23 14:40:57 +00:00
|
|
|
where
|
2022-07-12 18:42:43 +00:00
|
|
|
F: Fn(&EditorSettings) -> Option<R>,
|
2022-06-23 14:40:57 +00:00
|
|
|
{
|
2022-07-12 18:35:19 +00:00
|
|
|
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")
|
2022-06-09 08:07:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn test(cx: &gpui::AppContext) -> Settings {
|
|
|
|
Settings {
|
2022-08-22 21:49:01 +00:00
|
|
|
experiments: FeatureFlags::default(),
|
2022-04-06 00:10:17 +00:00
|
|
|
buffer_font_family: cx.font_cache().load_family(&["Monaco"]).unwrap(),
|
|
|
|
buffer_font_size: 14.,
|
2022-05-27 20:02:52 +00:00
|
|
|
default_buffer_font_size: 14.,
|
2022-06-27 23:21:22 +00:00
|
|
|
hover_popover_enabled: true,
|
2022-07-12 20:09:01 +00:00
|
|
|
show_completions_on_input: true,
|
2022-04-06 00:10:17 +00:00
|
|
|
vim_mode: false,
|
2022-07-05 11:01:27 +00:00
|
|
|
autosave: Autosave::Off,
|
2022-07-12 18:42:43 +00:00
|
|
|
editor_defaults: EditorSettings {
|
2022-07-12 18:35:19 +00:00
|
|
|
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(),
|
2022-07-16 01:27:10 +00:00
|
|
|
terminal_defaults: Default::default(),
|
|
|
|
terminal_overrides: Default::default(),
|
2022-06-23 14:40:57 +00:00
|
|
|
language_defaults: Default::default(),
|
2022-04-06 00:10:17 +00:00
|
|
|
language_overrides: Default::default(),
|
2022-08-04 02:32:18 +00:00
|
|
|
lsp: Default::default(),
|
2022-06-03 23:57:50 +00:00
|
|
|
projects_online_by_default: true,
|
2022-08-10 21:39:24 +00:00
|
|
|
theme: gpui::fonts::with_font_cache(cx.font_cache().clone(), Default::default),
|
2022-09-08 20:31:04 +00:00
|
|
|
internal: false,
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:03:00 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn test_async(cx: &mut gpui::TestAppContext) {
|
|
|
|
cx.update(|cx| {
|
|
|
|
let settings = Self::test(cx);
|
2022-08-10 21:39:24 +00:00
|
|
|
cx.set_global(settings);
|
2022-05-23 23:03:00 +00:00
|
|
|
});
|
|
|
|
}
|
2022-04-06 00:10:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 20:33:39 +00:00
|
|
|
pub fn settings_file_json_schema(
|
|
|
|
theme_names: Vec<String>,
|
2022-07-13 22:04:03 +00:00
|
|
|
language_names: &[String],
|
2022-04-21 20:33:39 +00:00
|
|
|
) -> 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::<SettingsFileContent>();
|
|
|
|
|
2022-07-12 19:19:01 +00:00
|
|
|
// Create a schema for a theme name.
|
|
|
|
let theme_name_schema = SchemaObject {
|
2022-04-21 20:33:39 +00:00
|
|
|
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::String))),
|
2022-08-10 21:39:24 +00:00
|
|
|
enum_values: Some(theme_names.into_iter().map(Value::String).collect()),
|
2022-04-21 20:33:39 +00:00
|
|
|
..Default::default()
|
2022-07-12 19:19:01 +00:00
|
|
|
};
|
2022-04-21 20:33:39 +00:00
|
|
|
|
2022-07-12 19:19:01 +00:00
|
|
|
// 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 {
|
2022-04-21 20:33:39 +00:00
|
|
|
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
|
|
|
|
object: Some(Box::new(ObjectValidation {
|
2022-07-12 19:19:01 +00:00
|
|
|
properties: language_names
|
2022-07-13 22:04:03 +00:00
|
|
|
.iter()
|
|
|
|
.map(|name| {
|
|
|
|
(
|
|
|
|
name.clone(),
|
|
|
|
Schema::new_ref("#/definitions/EditorSettings".into()),
|
|
|
|
)
|
|
|
|
})
|
2022-07-12 19:19:01 +00:00
|
|
|
.collect(),
|
2022-04-21 20:33:39 +00:00
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
2022-07-12 19:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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()),
|
|
|
|
]);
|
2022-04-21 20:33:39 +00:00
|
|
|
root_schema
|
2022-07-12 19:19:01 +00:00
|
|
|
.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()),
|
|
|
|
),
|
|
|
|
]);
|
2022-04-21 20:33:39 +00:00
|
|
|
|
|
|
|
serde_json::to_value(root_schema).unwrap()
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
fn merge<T: Copy>(target: &mut T, value: Option<T>) {
|
|
|
|
if let Some(value) = value {
|
|
|
|
*target = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:58:18 +00:00
|
|
|
pub fn parse_json_with_comments<T: DeserializeOwned>(content: &str) -> Result<T> {
|
|
|
|
Ok(serde_json::from_reader(
|
|
|
|
json_comments::CommentSettings::c_style().strip_comments(content.as_bytes()),
|
|
|
|
)?)
|
|
|
|
}
|