diff options
author | Brian Dawn | 2021-06-03 20:46:56 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-06-06 01:49:17 +0000 |
commit | 62d181de78077200d7b229ce6f91b2b14129d998 (patch) | |
tree | 890a622aed18f5e61097dde4efababd2af1af7fe /helix-core/src | |
parent | 8c2fa12ffc006de787992b4022263be033ac8200 (diff) |
Provide a feature flag to be able to embed the runtime folder.
These changes provide a new feature flag "embed_runtime" that when
enabled and built in release mode will embed the runtime folder into the
resulting binary.
Diffstat (limited to 'helix-core/src')
-rw-r--r-- | helix-core/src/lib.rs | 1 | ||||
-rw-r--r-- | helix-core/src/syntax.rs | 40 |
2 files changed, 30 insertions, 11 deletions
diff --git a/helix-core/src/lib.rs b/helix-core/src/lib.rs index 3e00081f..6b93de78 100644 --- a/helix-core/src/lib.rs +++ b/helix-core/src/lib.rs @@ -44,6 +44,7 @@ pub(crate) fn find_first_non_whitespace_char(text: RopeSlice, line_num: usize) - None } +#[cfg(not(embed_runtime))] pub fn runtime_dir() -> std::path::PathBuf { // runtime env var || dir where binary is located std::env::var("HELIX_RUNTIME") diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index ec95708b..2d1cbccb 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -73,16 +73,37 @@ pub struct IndentQuery { pub outdent: HashSet<String>, } -fn read_query(language: &str, filename: &str) -> String { - static INHERITS_REGEX: Lazy<Regex> = - Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap()); - +#[cfg(not(feature = "embed_runtime"))] +fn load_runtime_file(language: &str, filename: &str) -> Result<String, std::io::Error> { let root = crate::runtime_dir(); - // let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let path = root.join("queries").join(language).join(filename); + std::fs::read_to_string(&path) +} + +#[cfg(feature = "embed_runtime")] +use rust_embed::RustEmbed; + +#[cfg(feature = "embed_runtime")] +#[derive(RustEmbed)] +#[folder = "../runtime/"] +struct Runtime; +#[cfg(feature = "embed_runtime")] +fn load_runtime_file(language: &str, filename: &str) -> Result<String, Box<dyn std::error::Error>> { + let root = PathBuf::new(); let path = root.join("queries").join(language).join(filename); - let query = std::fs::read_to_string(&path).unwrap_or_default(); + let query_bytes = Runtime::get(&path.as_path().display().to_string()).unwrap_or_default(); + std::str::from_utf8(query_bytes.as_ref()) + .map(|s| s.to_string()) + .map_err(|err| err.into()) +} + +fn read_query(language: &str, filename: &str) -> String { + static INHERITS_REGEX: Lazy<Regex> = + Lazy::new(|| Regex::new(r";+\s*inherits\s*:?\s*([a-z_,()]+)\s*").unwrap()); + + let query = load_runtime_file(language, filename).unwrap_or_default(); // TODO: the collect() is not ideal let inherits = INHERITS_REGEX @@ -146,11 +167,8 @@ impl LanguageConfiguration { .get_or_init(|| { let language = get_language_name(self.language_id).to_ascii_lowercase(); - let root = crate::runtime_dir(); - let path = root.join("queries").join(language).join("indents.toml"); - - let toml = std::fs::read(&path).ok()?; - toml::from_slice(&toml).ok() + let toml = load_runtime_file(&language, "indents.toml").ok()?; + toml::from_slice(&toml.as_bytes()).ok() }) .as_ref() } |