Skip to content

Commit 0c2a03e

Browse files
committed
cwd overwrites root lang config
1 parent c3491d5 commit 0c2a03e

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

helix-core/src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,9 @@ pub fn config_dir() -> std::path::PathBuf {
9696
/// it searches for it within the root `.git` directory. Otherwise, it uses the CWD
9797
/// to avoid an unwrap.
9898
pub fn local_config_dir() -> std::path::PathBuf {
99-
let cwd = std::env::current_dir().expect("unable to determine current directory");
100-
if cwd.join(".helix").is_dir() {
101-
cwd.join(".helix")
102-
} else {
103-
let root = find_root(None).unwrap_or(cwd);
104-
root.join(".helix")
105-
}
99+
let root = find_root(None)
100+
.unwrap_or_else(|| std::env::current_dir().expect("unable to determine current directory"));
101+
root.join(".helix")
106102
}
107103

108104
pub fn cache_dir() -> std::path::PathBuf {

helix-term/src/application.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ pub struct Application {
4949
lsp_progress: LspProgressMap,
5050
}
5151

52+
fn load_lang_config(path: std::path::PathBuf) -> Result<toml::Value, Error> {
53+
match std::fs::read(&path) {
54+
Ok(config) => Ok(toml::from_slice(&config).unwrap()),
55+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
56+
Ok(toml::Value::Table(toml::value::Table::default()))
57+
}
58+
Err(err) => Err(Error::new(err)),
59+
}
60+
}
61+
5262
impl Application {
5363
pub fn new(args: Args, mut config: Config) -> Result<Self, Error> {
5464
use helix_view::editor::Action;
@@ -57,36 +67,26 @@ impl Application {
5767
// `local_config_dir` is a `.helix` folder within the projec directory.
5868
let config_dir = helix_core::config_dir();
5969
let local_config_dir = helix_core::local_config_dir();
70+
let cwd = std::env::current_dir().expect("unable to determine current directory");
6071

61-
// Config override order: local -> global -> default.
72+
// Config override order: local (cwd) -> local (root) -> global -> default.
6273
// Read and parse the `languages.toml` files as TOML objects.
6374
let default_lang_config: toml::Value =
6475
toml::from_slice(include_bytes!("../../languages.toml"))
6576
.expect("failed to read the default `languages.toml`");
66-
let lang_config =
67-
{
68-
let local_config = match std::fs::read(local_config_dir.join("languages.toml")) {
69-
Ok(config) => toml::from_slice(&config)
70-
.expect("failed to read the local `languages.toml`"),
71-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
72-
toml::Value::Table(toml::value::Table::default())
73-
}
74-
Err(err) => return Err(Error::new(err)),
75-
};
76-
let global_config = match std::fs::read(config_dir.join("languages.toml")) {
77-
Ok(config) => toml::from_slice(&config)
78-
.expect("failed to read the global `languages.toml`"),
79-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
80-
toml::Value::Table(toml::value::Table::default())
81-
}
82-
Err(err) => return Err(Error::new(err)),
83-
};
77+
let lang_config = {
78+
let local_cwd_config = load_lang_config(cwd.join(".helix/languages.toml"))?;
79+
let local_root_config = load_lang_config(local_config_dir.join("languages.toml"))?;
80+
let global_config = load_lang_config(config_dir.join("languages.toml"))?;
8481

82+
merge_toml_values(
83+
default_lang_config.clone(),
8584
merge_toml_values(
86-
default_lang_config.clone(),
87-
merge_toml_values(global_config, local_config),
88-
)
89-
};
85+
global_config,
86+
merge_toml_values(local_root_config, local_cwd_config),
87+
),
88+
)
89+
};
9090

9191
// Convert previous `toml::Value`s into the config type.
9292
let default_syn_loader_config: helix_core::syntax::Configuration = default_lang_config

0 commit comments

Comments
 (0)