on master help sign in
Use default `languages.toml` if user's is invalid (#994)
commita424ef4e209c0483385acedffd2331534f0ce906 authorOmnikar <omnikar5@gmail.com> dateMon 8 Nov 2021, 21:07 (5 years ago) parenteb68cd3
2 files changed+22−9
helix-core/src/syntax.rs+2−1 helix-term/src/application.rs+20−8
helix-core/src/syntax.rs+2−1
@@ -40,13 +40,14 @@ where
4040}
4141
4242#[derive(Debug, Serialize, Deserialize)]
43+#[serde(deny_unknown_fields)]
4344pub struct Configuration {
4445 pub language: Vec<LanguageConfiguration>,
4546}
4647
4748// largely based on tree-sitter/cli/src/loader.rs
4849#[derive(Debug, Serialize, Deserialize)]
49−#[serde(rename_all = "kebab-case")]
50+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
5051pub struct LanguageConfiguration {
5152 #[serde(rename = "name")]
5253 pub language_id: String,
helix-term/src/application.rs+20−8
@@ -60,14 +60,19 @@ impl Application {
6060 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
6161
6262 // load default and user config, and merge both
63− let def_lang_conf: toml::Value = toml::from_slice(include_bytes!("../../languages.toml"))
64− .expect("Could not parse built-in languages.toml, something must be very wrong");
65− let user_lang_conf: Option<toml::Value> = std::fs::read(conf_dir.join("languages.toml"))
63+ let builtin_err_msg =
64+ "Could not parse built-in languages.toml, something must be very wrong";
65+ let def_lang_conf: toml::Value =
66+ toml::from_slice(include_bytes!("../../languages.toml")).expect(builtin_err_msg);
67+ let def_syn_loader_conf: helix_core::syntax::Configuration =
68+ def_lang_conf.clone().try_into().expect(builtin_err_msg);
69+ let user_lang_conf = std::fs::read(conf_dir.join("languages.toml"))
6670 .ok()
67− .map(|raw| toml::from_slice(&raw).expect("Could not parse user languages.toml"));
71+ .map(|raw| toml::from_slice(&raw));
6872 let lang_conf = match user_lang_conf {
69− Some(value) => merge_toml_values(def_lang_conf, value),
70− None => def_lang_conf,
73+ Some(Ok(value)) => Ok(merge_toml_values(def_lang_conf, value)),
74+ Some(err @ Err(_)) => err,
75+ None => Ok(def_lang_conf),
7176 };
7277
7378 let theme = if let Some(theme) = &config.theme {
@@ -83,8 +88,15 @@ impl Application {
8388 };
8489
8590 let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
86− .try_into()
87− .expect("Could not parse merged (built-in + user) languages.toml");
91+ .and_then(|conf| conf.try_into())
92+ .unwrap_or_else(|err| {
93+ eprintln!("Bad language config: {}", err);
94+ eprintln!("Press <ENTER> to continue with default language config");
95+ use std::io::Read;
96+ // This waits for an enter press.
97+ let _ = std::io::stdin().read(&mut []);
98+ def_syn_loader_conf
99+ });
88100 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
89101
90102 let mut editor = Editor::new(