aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/syntax.rs
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-11-08 15:30:34 +0000
committerBlaž Hrastnik2021-11-08 15:30:34 +0000
commit549cdee56159bed4266990ae66591dd6299293d4 (patch)
treec70b573b9b4e44dc978b955f44e0c23160012a75 /helix-core/src/syntax.rs
parent77dbbc73f9c9b6599bc39b18625285685fe2e4b1 (diff)
Refactor shebang detection to reuse the loaded buffer
Diffstat (limited to 'helix-core/src/syntax.rs')
-rw-r--r--helix-core/src/syntax.rs11
1 files changed, 3 insertions, 8 deletions
diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs
index 84952248..0164092d 100644
--- a/helix-core/src/syntax.rs
+++ b/helix-core/src/syntax.rs
@@ -14,8 +14,6 @@ use std::{
cell::RefCell,
collections::{HashMap, HashSet},
fmt,
- fs::File,
- io::Read,
path::Path,
sync::Arc,
};
@@ -308,15 +306,12 @@ impl Loader {
// TODO: content_regex handling conflict resolution
}
- pub fn language_config_for_shebang(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
- // Read the first 128 bytes of the file. If its a shebang line, try to find the language
- let file = File::open(path).ok()?;
- let mut buf = String::with_capacity(128);
- file.take(128).read_to_string(&mut buf).ok()?;
+ pub fn language_config_for_shebang(&self, source: &Rope) -> Option<Arc<LanguageConfiguration>> {
+ let line = Cow::from(source.line(0));
static SHEBANG_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
let configuration_id = SHEBANG_REGEX
- .captures(&buf)
+ .captures(&line)
.and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
configuration_id.and_then(|&id| self.language_configs.get(id).cloned())