Skip to content

Commit

Permalink
use a constant for the lang config filename
Browse files Browse the repository at this point in the history
  • Loading branch information
kirawi committed Dec 27, 2021
1 parent 9b00960 commit 4460580
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ auto-format = false

# Project-level Configuration

You can create a `.helix` directory either in the current working directory or in the same directory as `.git`. Then you can write a `languages.toml` file that will overwrite both global and default language settings.
You can create a `.helix` folder containing a `languages.toml` folder in either the CWD or root directory. If `.helix` exists in both folders, the CWD config will override the root config. The local config overrides both global and default language settings.
5 changes: 2 additions & 3 deletions helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ pub fn config_dir() -> std::path::PathBuf {
path
}

/// First looks for the local `.helix` directory in CWD. If it can't find it, then
/// it searches for it within the root `.git` directory. Otherwise, it uses the CWD
/// to avoid an unwrap.
/// Looks for the `.helix` config directory in the root directory, or otherwise in the
/// current working directory.
pub fn local_config_dir() -> std::path::PathBuf {
let root = find_root(None)
.unwrap_or_else(|| std::env::current_dir().expect("unable to determine current directory"));
Expand Down
15 changes: 9 additions & 6 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,18 @@ impl Application {
Err(err) => return Err(Error::new(err)),
};

const LANG_CONFIG_FILENAME: &str = "languages.toml";

// 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`");
toml::from_slice(include_bytes!("../../languages.toml")).unwrap_or_else(|_| {
panic!("failed to read the default `{}`", LANG_CONFIG_FILENAME)
});
let lang_config: toml::Value = {
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"))?;
let local_cwd_config = load_lang_config(cwd.join(".helix").join(LANG_CONFIG_FILENAME))?;
let local_root_config = load_lang_config(local_config_dir.join(LANG_CONFIG_FILENAME))?;
let global_config = load_lang_config(config_dir.join(LANG_CONFIG_FILENAME))?;
[
local_root_config,
global_config,
Expand All @@ -109,7 +112,7 @@ impl Application {
// Convert previous `toml::Value`s into the config type.
let default_syn_loader_config: helix_core::syntax::Configuration = default_lang_config
.try_into()
.expect("failed to parse the default `languages.toml`");
.unwrap_or_else(|_| panic!("failed to parse the default `{}`", LANG_CONFIG_FILENAME));
let syn_loader_config: helix_core::syntax::Configuration =
lang_config.try_into().unwrap_or_else(|err| {
eprintln!("Bad language config: {}", err);
Expand Down

0 comments on commit 4460580

Please sign in to comment.