on master help sign in
Add more ways to detect runtime directory
commit716067ba05d18ffe8fc0454a14ab75bf6e35086c authorWojciech Kępka <wojtek.kepka@protonmail.com> dateSat 12 Jun 2021, 08:30 (5 years ago) parentc754df1
2 files changed+27−11
helix-core/src/lib.rs+23−9 helix-core/src/syntax.rs+4−2
helix-core/src/lib.rs+23−9
@@ -17,6 +17,9 @@ mod state;
1717pub mod syntax;
1818mod transaction;
1919
20+static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
21+ once_cell::sync::Lazy::new(runtime_dir);
22+
2023pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
2124 line.chars().position(|ch| !ch.is_whitespace())
2225}
@@ -47,15 +50,26 @@ pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
4750
4851#[cfg(not(embed_runtime))]
4952pub fn runtime_dir() -> std::path::PathBuf {
50− // runtime env var || dir where binary is located
51− std::env::var("HELIX_RUNTIME")
52− .map(|path| path.into())
53− .unwrap_or_else(|_| {
54− std::env::current_exe()
55− .ok()
56− .and_then(|path| path.parent().map(|path| path.to_path_buf()))
57− .unwrap()
58− })
53+ if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
54+ return dir.into();
55+ }
56+
57+ const RT_DIR: &str = "runtime";
58+ let conf_dir = config_dir().join(RT_DIR);
59+ if conf_dir.exists() {
60+ return conf_dir;
61+ }
62+
63+ if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
64+ // this is the directory of the crate being run by cargo, we need the workspace path so we take the parent
65+ return std::path::PathBuf::from(dir).parent().unwrap().join(RT_DIR);
66+ }
67+
68+ // fallback to location of the executable being run
69+ std::env::current_exe()
70+ .ok()
71+ .and_then(|path| path.parent().map(|path| path.to_path_buf()))
72+ .unwrap()
5973}
6074
6175pub fn config_dir() -> std::path::PathBuf {
helix-core/src/syntax.rs+4−2
@@ -76,8 +76,10 @@ pub struct IndentQuery {
7676
7777#[cfg(not(feature = "embed_runtime"))]
7878fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> {
79− let root = crate::runtime_dir();
80− let path = root.join("queries").join(language).join(filename);
79+ let path = crate::RUNTIME_DIR
80+ .join("queries")
81+ .join(language)
82+ .join(filename);
8183 std::fs::read_to_string(&path)
8284}
8385