on master help sign in
Refactor language config loading (#1658)
commit4c424d5ee442788b32eaaee67b4256aaa93aafa2 authorGokul Soumya <gokulps15@gmail.com> dateMon 14 Feb 2022, 22:11 (5 years ago) parente267dc8
3 files changed+46−26
helix-core/src/config.rs+33 helix-core/src/lib.rs+1 helix-term/src/application.rs+12−26
helix-core/src/config.rsnew file+33
@@ -0,0 +1,33 @@
1+use crate::merge_toml_values;
2+
3+/// Default bultin-in languages.toml.
4+pub fn default_lang_config() -> toml::Value {
5+ toml::from_slice(include_bytes!("../../languages.toml"))
6+ .expect("Could not parse bultin-in languages.toml to valid toml")
7+}
8+
9+/// User configured languages.toml file, merged with the default config.
10+pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
11+ let def_lang_conf = default_lang_config();
12+ let data = std::fs::read(crate::config_dir().join("languages.toml"));
13+ let user_lang_conf = match data {
14+ Ok(raw) => {
15+ let value = toml::from_slice(&raw)?;
16+ merge_toml_values(def_lang_conf, value)
17+ }
18+ Err(_) => def_lang_conf,
19+ };
20+
21+ Ok(user_lang_conf)
22+}
23+
24+/// Syntax configuration loader based on built-in languages.toml.
25+pub fn default_syntax_loader() -> crate::syntax::Configuration {
26+ default_lang_config()
27+ .try_into()
28+ .expect("Could not serialize built-in language.toml")
29+}
30+/// Syntax configuration loader based on user configured languages.toml.
31+pub fn user_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> {
32+ user_lang_config()?.try_into()
33+}
helix-core/src/lib.rs+1
@@ -3,6 +3,7 @@ pub use encoding_rs as encoding;
33pub mod auto_pairs;
44pub mod chars;
55pub mod comment;
6+pub mod config;
67pub mod diagnostic;
78pub mod diff;
89pub mod graphemes;
helix-term/src/application.rs+12−26
@@ -1,4 +1,7 @@
1−use helix_core::{merge_toml_values, pos_at_coords, syntax, Selection};
1+use helix_core::{
2+ config::{default_syntax_loader, user_syntax_loader},
3+ pos_at_coords, syntax, Selection,
4+};
25use helix_dap::{self as dap, Payload, Request};
36use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
47use helix_view::{editor::Breakpoint, theme, Editor};
@@ -69,21 +72,6 @@ impl Application {
6972 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
7073
7174 // load default and user config, and merge both
72− let builtin_err_msg =
73− "Could not parse built-in languages.toml, something must be very wrong";
74− let def_lang_conf: toml::Value =
75− toml::from_slice(include_bytes!("../../languages.toml")).expect(builtin_err_msg);
76− let def_syn_loader_conf: helix_core::syntax::Configuration =
77− def_lang_conf.clone().try_into().expect(builtin_err_msg);
78− let user_lang_conf = std::fs::read(conf_dir.join("languages.toml"))
79− .ok()
80− .map(|raw| toml::from_slice(&raw));
81− let lang_conf = match user_lang_conf {
82− Some(Ok(value)) => Ok(merge_toml_values(def_lang_conf, value)),
83− Some(err @ Err(_)) => err,
84− None => Ok(def_lang_conf),
85− };
86−
8775 let true_color = config.editor.true_color || crate::true_color();
8876 let theme = config
8977 .theme
@@ -106,16 +94,14 @@ impl Application {
10694 }
10795 });
10896
109− let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
110− .and_then(|conf| conf.try_into())
111− .unwrap_or_else(|err| {
112− eprintln!("Bad language config: {}", err);
113− eprintln!("Press <ENTER> to continue with default language config");
114− use std::io::Read;
115− // This waits for an enter press.
116− let _ = std::io::stdin().read(&mut []);
117− def_syn_loader_conf
118− });
97+ let syn_loader_conf = user_syntax_loader().unwrap_or_else(|err| {
98+ eprintln!("Bad language config: {}", err);
99+ eprintln!("Press <ENTER> to continue with default language config");
100+ use std::io::Read;
101+ // This waits for an enter press.
102+ let _ = std::io::stdin().read(&mut []);
103+ default_syntax_loader()
104+ });
119105 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
120106
121107 let mut editor = Editor::new(