on master help sign in
Refactor shebang detection to reuse the loaded buffer
549cdeeBlaž Hrastnik
helix-core/src/syntax.rs+3−8
@@ -14,8 +14,6 @@ use std::{
14 cell::RefCell,
14 cell::RefCell,
15 collections::{HashMap, HashSet},
15 collections::{HashMap, HashSet},
16 fmt,
16 fmt,
17− fs::File,
18− io::Read,
19 path::Path,
17 path::Path,
20 sync::Arc,
18 sync::Arc,
21};
19};
@@ -308,15 +306,12 @@ impl Loader {
308 // TODO: content_regex handling conflict resolution
306 // TODO: content_regex handling conflict resolution
309 }
307 }
310
308
311− pub fn language_config_for_shebang(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
309+ pub fn language_config_for_shebang(&self, source: &Rope) -> Option<Arc<LanguageConfiguration>> {
312− // Read the first 128 bytes of the file. If its a shebang line, try to find the language
310+ let line = Cow::from(source.line(0));
313− let file = File::open(path).ok()?;
314− let mut buf = String::with_capacity(128);
315− file.take(128).read_to_string(&mut buf).ok()?;
316 static SHEBANG_REGEX: Lazy<Regex> =
311 static SHEBANG_REGEX: Lazy<Regex> =
317 Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
312 Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
318 let configuration_id = SHEBANG_REGEX
313 let configuration_id = SHEBANG_REGEX
319− .captures(&buf)
314+ .captures(&line)
320 .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
315 .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
321
316
322 configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
317 configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
helix-view/src/document.rs+1−1
@@ -496,7 +496,7 @@ impl Document {
496 if let Some(path) = &self.path {
496 if let Some(path) = &self.path {
497 let language_config = config_loader
497 let language_config = config_loader
498 .language_config_for_file_name(path)
498 .language_config_for_file_name(path)
499− .or_else(|| config_loader.language_config_for_shebang(path));
499+ .or_else(|| config_loader.language_config_for_shebang(self.text()));
500 self.set_language(theme, language_config);
500 self.set_language(theme, language_config);
501 }
501 }
502 }
502 }