on master help sign in
Launch with defaults upon invalid config/theme (#982)

* Launch with defaults upon invalid config/theme * Startup message if there is a problematic config * Statusline error if trying to switch to an invalid theme * Use serde `deny_unknown_fields` for config

commited23057ff8e01404ab608682445b4f293b6142ed authorOmnikar <omnikar5@gmail.com> dateSat 6 Nov 2021, 11:57 (5 years ago) parent2c1313c
3 files changed+19−3
helix-term/src/config.rs+2−1 helix-term/src/main.rs+10−1 helix-view/src/editor.rs+7−1
helix-term/src/config.rs+2−1
@@ -3,6 +3,7 @@ use serde::Deserialize;
33use crate::keymap::Keymaps;
44
55#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
6+#[serde(deny_unknown_fields)]
67pub struct Config {
78 pub theme: Option<String>,
89 #[serde(default)]
@@ -14,7 +15,7 @@ pub struct Config {
1415}
1516
1617#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
17−#[serde(rename_all = "kebab-case")]
18+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
1819pub struct LspConfig {
1920 pub display_messages: bool,
2021}
helix-term/src/main.rs+10−1
@@ -91,7 +91,16 @@ FLAGS:
9191 }
9292
9393 let config = match std::fs::read_to_string(conf_dir.join("config.toml")) {
94− Ok(config) => merge_keys(toml::from_str(&config)?),
94+ Ok(config) => toml::from_str(&config)
95+ .map(merge_keys)
96+ .unwrap_or_else(|err| {
97+ eprintln!("Bad config: {}", err);
98+ eprintln!("Press <ENTER> to continue with default config");
99+ use std::io::Read;
100+ // This waits for an enter press.
101+ let _ = std::io::stdin().read(&mut []);
102+ Config::default()
103+ }),
95104 Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
96105 Err(err) => return Err(Error::new(err)),
97106 };
helix-view/src/editor.rs+7−1
@@ -34,7 +34,7 @@ where
3434}
3535
3636#[derive(Debug, Clone, PartialEq, Deserialize)]
37−#[serde(rename_all = "kebab-case", default)]
37+#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
3838pub struct Config {
3939 /// Padding to keep between the edge of the screen and the cursor when scrolling. Defaults to 5.
4040 pub scrolloff: usize,
@@ -195,6 +195,12 @@ impl Editor {
195195 }
196196
197197 pub fn set_theme(&mut self, theme: Theme) {
198+ // `ui.selection` is the only scope required to be able to render a theme.
199+ if theme.find_scope_index("ui.selection").is_none() {
200+ self.set_error("Invalid theme: `ui.selection` required".to_owned());
201+ return;
202+ }
203+
198204 let scopes = theme.scopes();
199205 for config in self
200206 .syn_loader