on master help sign in
Refactor shebang detection to reuse the loaded buffer
commit549cdee56159bed4266990ae66591dd6299293d4 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 9 Nov 2021, 00:30 (5 years ago) parent77dbbc7
2 files changed+4−9
helix-core/src/syntax.rs+3−8 helix-view/src/document.rs+1−1
helix-core/src/syntax.rs+3−8
@@ -14,8 +14,6 @@ use std::{
1414 cell::RefCell,
1515 collections::{HashMap, HashSet},
1616 fmt,
17− fs::File,
18− io::Read,
1917 path::Path,
2018 sync::Arc,
2119};
@@ -308,15 +306,12 @@ impl Loader {
308306 // TODO: content_regex handling conflict resolution
309307 }
310308
311− pub fn language_config_for_shebang(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
312− // Read the first 128 bytes of the file. If its a shebang line, try to find the language
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()?;
309+ pub fn language_config_for_shebang(&self, source: &Rope) -> Option<Arc<LanguageConfiguration>> {
310+ let line = Cow::from(source.line(0));
316311 static SHEBANG_REGEX: Lazy<Regex> =
317312 Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
318313 let configuration_id = SHEBANG_REGEX
319− .captures(&buf)
314+ .captures(&line)
320315 .and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
321316
322317 configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
helix-view/src/document.rs+1−1
@@ -496,7 +496,7 @@ impl Document {
496496 if let Some(path) = &self.path {
497497 let language_config = config_loader
498498 .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()));
500500 self.set_language(theme, language_config);
501501 }
502502 }