@@ -9,7 +9,7 @@ use crate::formatter::clang::ClangFormatter;
9
9
10
10
use super :: ProtolsConfig ;
11
11
12
- const CONFIG_FILE_NAME : & str = " protols.toml";
12
+ const CONFIG_FILE_NAMES : [ & str ; 2 ] = [ ". protols.toml", "protols.toml" ] ;
13
13
14
14
pub struct WorkspaceProtoConfigs {
15
15
workspaces : HashSet < Url > ,
@@ -26,13 +26,24 @@ impl WorkspaceProtoConfigs {
26
26
}
27
27
}
28
28
29
+ fn get_config_file_path ( wpath : & PathBuf ) -> Option < PathBuf > {
30
+ for file in CONFIG_FILE_NAMES {
31
+ let p = Path :: new ( & wpath) . join ( file) ;
32
+ match std:: fs:: exists ( & p) {
33
+ Ok ( exists) if exists => return Some ( p) ,
34
+ _ => continue ,
35
+ }
36
+ }
37
+ None
38
+ }
39
+
29
40
pub fn add_workspace ( & mut self , w : & WorkspaceFolder ) {
30
41
let Ok ( wpath) = w. uri . to_file_path ( ) else {
31
42
return ;
32
43
} ;
33
44
34
- let p = Path :: new ( & wpath) . join ( CONFIG_FILE_NAME ) ;
35
- let content = std:: fs:: read_to_string ( p ) . unwrap_or_default ( ) ;
45
+ let path = Self :: get_config_file_path ( & wpath) . unwrap_or_default ( ) ;
46
+ let content = std:: fs:: read_to_string ( path ) . unwrap_or_default ( ) ;
36
47
37
48
let wr: ProtolsConfig = basic_toml:: from_str ( & content) . unwrap_or_default ( ) ;
38
49
let fmt = ClangFormatter :: new (
@@ -84,7 +95,7 @@ mod test {
84
95
use insta:: assert_yaml_snapshot;
85
96
use tempfile:: tempdir;
86
97
87
- use super :: WorkspaceProtoConfigs ;
98
+ use super :: { WorkspaceProtoConfigs , CONFIG_FILE_NAMES } ;
88
99
89
100
#[ test]
90
101
fn test_get_for_workspace ( ) {
@@ -153,4 +164,24 @@ mod test {
153
164
"clang-format"
154
165
) ;
155
166
}
167
+
168
+ #[ test]
169
+ fn test_loading_different_config_files ( ) {
170
+ let tmpdir = tempdir ( ) . expect ( "failed to create temp directory" ) ;
171
+
172
+ for file in CONFIG_FILE_NAMES {
173
+ let f = tmpdir. path ( ) . join ( file) ;
174
+ std:: fs:: write ( f, include_str ! ( "input/protols-valid.toml" ) ) . unwrap ( ) ;
175
+
176
+ let mut ws = WorkspaceProtoConfigs :: new ( ) ;
177
+ ws. add_workspace ( & WorkspaceFolder {
178
+ uri : Url :: from_directory_path ( tmpdir. path ( ) ) . unwrap ( ) ,
179
+ name : "Test" . to_string ( ) ,
180
+ } ) ;
181
+
182
+ // check we really loaded the config file
183
+ let workspace = Url :: from_file_path ( tmpdir. path ( ) . join ( "foobar.proto" ) ) . unwrap ( ) ;
184
+ assert ! ( ws. get_workspace_for_uri( & workspace) . is_some( ) ) ;
185
+ }
186
+ }
156
187
}
0 commit comments