Skip to content

Commit d5b176a

Browse files
feat: support reading config from .protols.toml
Signed-off-by: Omar Mohamed <[email protected]>
1 parent a0f6e51 commit d5b176a

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

src/config/workspace.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::formatter::clang::ClangFormatter;
99

1010
use super::ProtolsConfig;
1111

12-
const CONFIG_FILE_NAME: &str = "protols.toml";
12+
const CONFIG_FILE_NAMES: [&str; 2] = [".protols.toml", "protols.toml"];
1313

1414
pub struct WorkspaceProtoConfigs {
1515
workspaces: HashSet<Url>,
@@ -26,13 +26,28 @@ impl WorkspaceProtoConfigs {
2626
}
2727
}
2828

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) => {
34+
if exists {
35+
return Some(p);
36+
}
37+
}
38+
_ => continue,
39+
}
40+
}
41+
None
42+
}
43+
2944
pub fn add_workspace(&mut self, w: &WorkspaceFolder) {
3045
let Ok(wpath) = w.uri.to_file_path() else {
3146
return;
3247
};
3348

34-
let p = Path::new(&wpath).join(CONFIG_FILE_NAME);
35-
let content = std::fs::read_to_string(p).unwrap_or_default();
49+
let path = Self::get_config_file_path(&wpath).unwrap_or_default();
50+
let content = std::fs::read_to_string(path).unwrap_or_default();
3651

3752
let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default();
3853
let fmt = ClangFormatter::new(
@@ -84,7 +99,7 @@ mod test {
8499
use insta::assert_yaml_snapshot;
85100
use tempfile::tempdir;
86101

87-
use super::WorkspaceProtoConfigs;
102+
use super::{WorkspaceProtoConfigs, CONFIG_FILE_NAMES};
88103

89104
#[test]
90105
fn test_get_for_workspace() {
@@ -153,4 +168,24 @@ mod test {
153168
"clang-format"
154169
);
155170
}
171+
172+
#[test]
173+
fn test_loading_different_config_files() {
174+
let tmpdir = tempdir().expect("failed to create temp directory");
175+
176+
for file in CONFIG_FILE_NAMES {
177+
let f = tmpdir.path().join(file);
178+
std::fs::write(f, include_str!("input/protols-valid.toml")).unwrap();
179+
180+
let mut ws = WorkspaceProtoConfigs::new();
181+
ws.add_workspace(&WorkspaceFolder {
182+
uri: Url::from_directory_path(tmpdir.path()).unwrap(),
183+
name: "Test".to_string(),
184+
});
185+
186+
// check we really loaded the config file
187+
let workspace = Url::from_file_path(tmpdir.path().join("foobar.proto")).unwrap();
188+
assert!(ws.get_workspace_for_uri(&workspace).is_some());
189+
}
190+
}
156191
}

0 commit comments

Comments
 (0)