@@ -60,32 +60,50 @@ fn load_lang_config(path: std::path::PathBuf) -> Result<toml::Value, Error> {
60
60
}
61
61
62
62
impl Application {
63
- pub fn new ( args : Args , mut config : Config ) -> Result < Self , Error > {
63
+ pub fn new ( args : Args ) -> Result < Self , Error > {
64
64
use helix_view:: editor:: Action ;
65
65
66
66
// These configuration directories can contain `config.toml` and `languages.toml`.
67
67
// `local_config_dir` is a `.helix` folder within the projec directory.
68
68
let config_dir = helix_core:: config_dir ( ) ;
69
69
let local_config_dir = helix_core:: local_config_dir ( ) ;
70
70
let cwd = std:: env:: current_dir ( ) . expect ( "unable to determine current directory" ) ;
71
+ if !config_dir. exists ( ) {
72
+ std:: fs:: create_dir_all ( & config_dir) . ok ( ) ;
73
+ }
74
+
75
+ // `config.toml`
76
+ let mut config = match std:: fs:: read_to_string ( config_dir. join ( "config.toml" ) ) {
77
+ Ok ( config) => toml:: from_str ( & config)
78
+ . map ( crate :: keymap:: merge_keys)
79
+ . unwrap_or_else ( |err| {
80
+ eprintln ! ( "Bad config: {}" , err) ;
81
+ eprintln ! ( "Press <ENTER> to continue with default config" ) ;
82
+ use std:: io:: Read ;
83
+ // This waits for an enter press.
84
+ let _ = std:: io:: stdin ( ) . read ( & mut [ ] ) ;
85
+ Config :: default ( )
86
+ } ) ,
87
+ Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => Config :: default ( ) ,
88
+ Err ( err) => return Err ( Error :: new ( err) ) ,
89
+ } ;
71
90
72
91
// Config override order: local (cwd) -> local (root) -> global -> default.
73
92
// Read and parse the `languages.toml` files as TOML objects.
74
93
let default_lang_config: toml:: Value =
75
94
toml:: from_slice ( include_bytes ! ( "../../languages.toml" ) )
76
95
. expect ( "failed to read the default `languages.toml`" ) ;
77
- let lang_config = {
96
+ let lang_config: toml :: Value = {
78
97
let local_cwd_config = load_lang_config ( cwd. join ( ".helix/languages.toml" ) ) ?;
79
98
let local_root_config = load_lang_config ( local_config_dir. join ( "languages.toml" ) ) ?;
80
99
let global_config = load_lang_config ( config_dir. join ( "languages.toml" ) ) ?;
81
-
82
- merge_toml_values (
100
+ [
101
+ local_root_config,
102
+ global_config,
83
103
default_lang_config. clone ( ) ,
84
- merge_toml_values (
85
- global_config,
86
- merge_toml_values ( local_root_config, local_cwd_config) ,
87
- ) ,
88
- )
104
+ ]
105
+ . into_iter ( )
106
+ . fold ( local_cwd_config, |a, b| merge_toml_values ( b, a) )
89
107
} ;
90
108
91
109
// Convert previous `toml::Value`s into the config type.
0 commit comments