Skip to content

Commit

Permalink
cwd overwrites root lang config
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Dec 12, 2021
1 parent c3491d5 commit 0c2a03e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
10 changes: 3 additions & 7 deletions helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,9 @@ pub fn config_dir() -> std::path::PathBuf {
/// it searches for it within the root `.git` directory. Otherwise, it uses the CWD
/// to avoid an unwrap.
pub fn local_config_dir() -> std::path::PathBuf {
let cwd = std::env::current_dir().expect("unable to determine current directory");
if cwd.join(".helix").is_dir() {
cwd.join(".helix")
} else {
let root = find_root(None).unwrap_or(cwd);
root.join(".helix")
}
let root = find_root(None)
.unwrap_or_else(|| std::env::current_dir().expect("unable to determine current directory"));
root.join(".helix")
}

pub fn cache_dir() -> std::path::PathBuf {
Expand Down
46 changes: 23 additions & 23 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ pub struct Application {
lsp_progress: LspProgressMap,
}

fn load_lang_config(path: std::path::PathBuf) -> Result<toml::Value, Error> {
match std::fs::read(&path) {
Ok(config) => Ok(toml::from_slice(&config).unwrap()),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
Ok(toml::Value::Table(toml::value::Table::default()))
}
Err(err) => Err(Error::new(err)),
}
}

impl Application {
pub fn new(args: Args, mut config: Config) -> Result<Self, Error> {
use helix_view::editor::Action;
Expand All @@ -57,36 +67,26 @@ impl Application {
// `local_config_dir` is a `.helix` folder within the projec directory.
let config_dir = helix_core::config_dir();
let local_config_dir = helix_core::local_config_dir();
let cwd = std::env::current_dir().expect("unable to determine current directory");

// Config override order: local -> global -> default.
// Config override order: local (cwd) -> local (root) -> global -> default.
// Read and parse the `languages.toml` files as TOML objects.
let default_lang_config: toml::Value =
toml::from_slice(include_bytes!("../../languages.toml"))
.expect("failed to read the default `languages.toml`");
let lang_config =
{
let local_config = match std::fs::read(local_config_dir.join("languages.toml")) {
Ok(config) => toml::from_slice(&config)
.expect("failed to read the local `languages.toml`"),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
toml::Value::Table(toml::value::Table::default())
}
Err(err) => return Err(Error::new(err)),
};
let global_config = match std::fs::read(config_dir.join("languages.toml")) {
Ok(config) => toml::from_slice(&config)
.expect("failed to read the global `languages.toml`"),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
toml::Value::Table(toml::value::Table::default())
}
Err(err) => return Err(Error::new(err)),
};
let lang_config = {
let local_cwd_config = load_lang_config(cwd.join(".helix/languages.toml"))?;
let local_root_config = load_lang_config(local_config_dir.join("languages.toml"))?;
let global_config = load_lang_config(config_dir.join("languages.toml"))?;

merge_toml_values(
default_lang_config.clone(),
merge_toml_values(
default_lang_config.clone(),
merge_toml_values(global_config, local_config),
)
};
global_config,
merge_toml_values(local_root_config, local_cwd_config),
),
)
};

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

0 comments on commit 0c2a03e

Please sign in to comment.