on master help sign in
Refactor language config loading (#1658)
4c424d5Gokul Soumya
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;
3pub mod auto_pairs;
3pub mod auto_pairs;
4pub mod chars;
4pub mod chars;
5pub mod comment;
5pub mod comment;
6+pub mod config;
6pub mod diagnostic;
7pub mod diagnostic;
7pub mod diff;
8pub mod diff;
8pub mod graphemes;
9pub mod graphemes;
helix-term/src/application.rs+12−26
@@ -1,4 +1,7 @@
1+use helix_core::{
2+ config::{default_syntax_loader, user_syntax_loader},
1−use helix_core::{merge_toml_values, pos_at_coords, syntax, Selection};
3+ pos_at_coords, syntax, Selection,
4+};
2use helix_dap::{self as dap, Payload, Request};
5use helix_dap::{self as dap, Payload, Request};
3use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
6use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
4use helix_view::{editor::Breakpoint, theme, Editor};
7use helix_view::{editor::Breakpoint, theme, Editor};
@@ -69,21 +72,6 @@ impl Application {
69 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
72 std::sync::Arc::new(theme::Loader::new(&conf_dir, &helix_core::runtime_dir()));
70
73
71 // load default and user config, and merge both
74 // 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−
87 let true_color = config.editor.true_color || crate::true_color();
75 let true_color = config.editor.true_color || crate::true_color();
88 let theme = config
76 let theme = config
89 .theme
77 .theme
@@ -106,16 +94,14 @@ impl Application {
106 }
94 }
107 });
95 });
108
96
109− let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
110− .and_then(|conf| conf.try_into())
111− .unwrap_or_else(|err| {
97+ let syn_loader_conf = user_syntax_loader().unwrap_or_else(|err| {
112− eprintln!("Bad language config: {}", err);
98+ eprintln!("Bad language config: {}", err);
113− eprintln!("Press <ENTER> to continue with default language config");
99+ eprintln!("Press <ENTER> to continue with default language config");
114− use std::io::Read;
100+ use std::io::Read;
115− // This waits for an enter press.
101+ // This waits for an enter press.
116− let _ = std::io::stdin().read(&mut []);
102+ let _ = std::io::stdin().read(&mut []);
117− def_syn_loader_conf
103+ default_syntax_loader()
118− });
104+ });
119 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
105 let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf));
120
106
121 let mut editor = Editor::new(
107 let mut editor = Editor::new(