@@ -2,11 +2,28 @@ pub mod config;
2
2
pub mod grammar;
3
3
4
4
use etcetera:: base_strategy:: { choose_base_strategy, BaseStrategy } ;
5
+ use std:: path:: PathBuf ;
5
6
6
- pub static RUNTIME_DIR : once_cell:: sync:: Lazy < std:: path:: PathBuf > =
7
- once_cell:: sync:: Lazy :: new ( runtime_dir) ;
7
+ pub static RUNTIME_DIR : once_cell:: sync:: Lazy < PathBuf > = once_cell:: sync:: Lazy :: new ( runtime_dir) ;
8
8
9
- pub fn runtime_dir ( ) -> std:: path:: PathBuf {
9
+ static CONFIG_FILE : once_cell:: sync:: OnceCell < PathBuf > = once_cell:: sync:: OnceCell :: new ( ) ;
10
+
11
+ pub fn initialize_config_file ( specified_file : Option < PathBuf > ) {
12
+ let config_file = specified_file. unwrap_or_else ( || {
13
+ let config_dir = config_dir ( ) ;
14
+
15
+ if !config_dir. exists ( ) {
16
+ std:: fs:: create_dir_all ( & config_dir) . ok ( ) ;
17
+ }
18
+
19
+ config_dir. join ( "config.toml" )
20
+ } ) ;
21
+
22
+ // We should only initialize this value once.
23
+ CONFIG_FILE . set ( config_file) . ok ( ) ;
24
+ }
25
+
26
+ pub fn runtime_dir ( ) -> PathBuf {
10
27
if let Ok ( dir) = std:: env:: var ( "HELIX_RUNTIME" ) {
11
28
return dir. into ( ) ;
12
29
}
@@ -31,15 +48,15 @@ pub fn runtime_dir() -> std::path::PathBuf {
31
48
. unwrap ( )
32
49
}
33
50
34
- pub fn config_dir ( ) -> std :: path :: PathBuf {
51
+ pub fn config_dir ( ) -> PathBuf {
35
52
// TODO: allow env var override
36
53
let strategy = choose_base_strategy ( ) . expect ( "Unable to find the config directory!" ) ;
37
54
let mut path = strategy. config_dir ( ) ;
38
55
path. push ( "helix" ) ;
39
56
path
40
57
}
41
58
42
- pub fn local_config_dirs ( ) -> Vec < std :: path :: PathBuf > {
59
+ pub fn local_config_dirs ( ) -> Vec < PathBuf > {
43
60
let directories = find_root_impl ( None , & [ ".helix" . to_string ( ) ] )
44
61
. into_iter ( )
45
62
. map ( |path| path. join ( ".helix" ) )
@@ -48,27 +65,30 @@ pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
48
65
directories
49
66
}
50
67
51
- pub fn cache_dir ( ) -> std :: path :: PathBuf {
68
+ pub fn cache_dir ( ) -> PathBuf {
52
69
// TODO: allow env var override
53
70
let strategy = choose_base_strategy ( ) . expect ( "Unable to find the config directory!" ) ;
54
71
let mut path = strategy. cache_dir ( ) ;
55
72
path. push ( "helix" ) ;
56
73
path
57
74
}
58
75
59
- pub fn config_file ( ) -> std:: path:: PathBuf {
60
- config_dir ( ) . join ( "config.toml" )
76
+ pub fn config_file ( ) -> PathBuf {
77
+ CONFIG_FILE
78
+ . get ( )
79
+ . map ( |path| path. to_path_buf ( ) )
80
+ . unwrap_or_else ( || config_dir ( ) . join ( "config.toml" ) )
61
81
}
62
82
63
- pub fn lang_config_file ( ) -> std :: path :: PathBuf {
83
+ pub fn lang_config_file ( ) -> PathBuf {
64
84
config_dir ( ) . join ( "languages.toml" )
65
85
}
66
86
67
- pub fn log_file ( ) -> std :: path :: PathBuf {
87
+ pub fn log_file ( ) -> PathBuf {
68
88
cache_dir ( ) . join ( "helix.log" )
69
89
}
70
90
71
- pub fn find_root_impl ( root : Option < & str > , root_markers : & [ String ] ) -> Vec < std :: path :: PathBuf > {
91
+ pub fn find_root_impl ( root : Option < & str > , root_markers : & [ String ] ) -> Vec < PathBuf > {
72
92
let current_dir = std:: env:: current_dir ( ) . expect ( "unable to determine current directory" ) ;
73
93
let mut directories = Vec :: new ( ) ;
74
94
0 commit comments