diff options
author | Wojciech Kępka | 2021-06-12 06:30:55 +0000 |
---|---|---|
committer | Ivan Tham | 2021-06-12 09:26:41 +0000 |
commit | 716067ba05d18ffe8fc0454a14ab75bf6e35086c (patch) | |
tree | cbc7a25d103353173d351373173e93560920581c /helix-core/src | |
parent | c754df12b38595b29575e7b58911386a16ac9951 (diff) |
Add more ways to detect runtime directory
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/lib.rs | 32 | ||||
-rw-r--r-- | helix-core/src/syntax.rs | 6 |
2 files changed, 27 insertions, 11 deletions
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index a9792562..3394859b 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -17,6 +17,9 @@ mod state; pub mod syntax; mod transaction; +static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> = + once_cell::sync::Lazy::new(runtime_dir); + pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> { line.chars().position(|ch| !ch.is_whitespace()) } @@ -47,15 +50,26 @@ pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> { #[cfg(not(embed_runtime))] pub fn runtime_dir() -> std::path::PathBuf { - // runtime env var || dir where binary is located - std::env::var("HELIX_RUNTIME") - .map(|path| path.into()) - .unwrap_or_else(|_| { - std::env::current_exe() - .ok() - .and_then(|path| path.parent().map(|path| path.to_path_buf())) - .unwrap() - }) + if let Ok(dir) = std::env::var("HELIX_RUNTIME") { + return dir.into(); + } + + const RT_DIR: &str = "runtime"; + let conf_dir = config_dir().join(RT_DIR); + if conf_dir.exists() { + return conf_dir; + } + + if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") { + // this is the directory of the crate being run by cargo, we need the workspace path so we take the parent + return std::path::PathBuf::from(dir).parent().unwrap().join(RT_DIR); + } + + // fallback to location of the executable being run + std::env::current_exe() + .ok() + .and_then(|path| path.parent().map(|path| path.to_path_buf())) + .unwrap() } pub fn config_dir() -> std::path::PathBuf { diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 5c4c95b9..ae058eb1 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -76,8 +76,10 @@ pub struct IndentQuery { #[cfg(not(feature = "embed_runtime"))] fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> { - let root = crate::runtime_dir(); - let path = root.join("queries").join(language).join(filename); + let path = crate::RUNTIME_DIR + .join("queries") + .join(language) + .join(filename); std::fs::read_to_string(&path) } |