on master help sign in
Add more ways to detect runtime directory
716067bWojciech Kępka
helix-core/src/lib.rs+23−9
@@ -17,6 +17,9 @@ mod state;
17pub mod syntax;
17pub mod syntax;
18mod transaction;
18mod transaction;
19
19
20+static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
21+ once_cell::sync::Lazy::new(runtime_dir);
22+
20pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
23pub fn find_first_non_whitespace_char(line: RopeSlice) -> Option<usize> {
21 line.chars().position(|ch| !ch.is_whitespace())
24 line.chars().position(|ch| !ch.is_whitespace())
22}
25}
@@ -47,15 +50,26 @@ pub fn find_root(root: Option<&str>) -> Option<std::path::PathBuf> {
47
50
48#[cfg(not(embed_runtime))]
51#[cfg(not(embed_runtime))]
49pub fn runtime_dir() -> std::path::PathBuf {
52pub fn runtime_dir() -> std::path::PathBuf {
50− // runtime env var || dir where binary is located
51− std::env::var("HELIX_RUNTIME")
53+ if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
52− .map(|path| path.into())
54+ return dir.into();
53− .unwrap_or_else(|_| {
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
54− std::env::current_exe()
69+ std::env::current_exe()
55− .ok()
70+ .ok()
56− .and_then(|path| path.parent().map(|path| path.to_path_buf()))
71+ .and_then(|path| path.parent().map(|path| path.to_path_buf()))
57− .unwrap()
72+ .unwrap()
58− })
59}
73}
60
74
61pub fn config_dir() -> std::path::PathBuf {
75pub fn config_dir() -> std::path::PathBuf {
helix-core/src/syntax.rs+4−2
@@ -76,8 +76,10 @@ pub struct IndentQuery {
76
76
77#[cfg(not(feature = "embed_runtime"))]
77#[cfg(not(feature = "embed_runtime"))]
78fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> {
78fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> {
79− let root = crate::runtime_dir();
79+ let path = crate::RUNTIME_DIR
80+ .join("queries")
80− let path = root.join("queries").join(language).join(filename);
81+ .join(language)
82+ .join(filename);
81 std::fs::read_to_string(&path)
83 std::fs::read_to_string(&path)
82}
84}
83
85