on master help sign in
Use default `languages.toml` if user's is invalid (#994)
a424ef4Omnikar
helix-core/src/syntax.rs+2−1
@@ -40,13 +40,14 @@ where
40}
40}
41
41
42#[derive(Debug, Serialize, Deserialize)]
42#[derive(Debug, Serialize, Deserialize)]
43+#[serde(deny_unknown_fields)]
43pub struct Configuration {
44pub struct Configuration {
44 pub language: Vec<LanguageConfiguration>,
45 pub language: Vec<LanguageConfiguration>,
45}
46}
46
47
47// largely based on tree-sitter/cli/src/loader.rs
48// largely based on tree-sitter/cli/src/loader.rs
48#[derive(Debug, Serialize, Deserialize)]
49#[derive(Debug, Serialize, Deserialize)]
49−#[serde(rename_all = "kebab-case")]
50+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
50pub struct LanguageConfiguration {
51pub struct LanguageConfiguration {
51 #[serde(rename = "name")]
52 #[serde(rename = "name")]
52 pub language_id: String,
53 pub language_id: String,
helix-term/src/application.rs+20−8
@@ -60,14 +60,19 @@ impl Application {
60 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
60 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
61
61
62 // load default and user config, and merge both
62 // load default and user config, and merge both
63− let def_lang_conf: toml::Value = toml::from_slice(include_bytes!("../../languages.toml"))
63+ let builtin_err_msg =
64− .expect("Could not parse built-in languages.toml, something must be very wrong");
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);
65− let user_lang_conf: Option<toml::Value> = std::fs::read(conf_dir.join("languages.toml"))
69+ let user_lang_conf = std::fs::read(conf_dir.join("languages.toml"))
66 .ok()
70 .ok()
67− .map(|raw| toml::from_slice(&raw).expect("Could not parse user languages.toml"));
71+ .map(|raw| toml::from_slice(&raw));
68 let lang_conf = match user_lang_conf {
72 let lang_conf = match user_lang_conf {
69− Some(value) => merge_toml_values(def_lang_conf, value),
73+ Some(Ok(value)) => Ok(merge_toml_values(def_lang_conf, value)),
74+ Some(err @ Err(_)) => err,
70− None => def_lang_conf,
75+ None => Ok(def_lang_conf),
71 };
76 };
72
77
73 let theme = if let Some(theme) = &config.theme {
78 let theme = if let Some(theme) = &config.theme {
@@ -83,8 +88,15 @@ impl Application {
83 };
88 };
84
89
85 let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
90 let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
86− .try_into()
91+ .and_then(|conf| conf.try_into())
87− .expect("Could not parse merged (built-in + user) languages.toml");
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+ });
88 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
100 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
89
101
90 let mut editor = Editor::new(
102 let mut editor = Editor::new(