mirror of https://github.com/helix-editor/helix
Refactor shebang detection to reuse the loaded buffer
parent
77dbbc73f9
commit
549cdee561
|
@ -14,8 +14,6 @@ use std::{
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
fmt,
|
fmt,
|
||||||
fs::File,
|
|
||||||
io::Read,
|
|
||||||
path::Path,
|
path::Path,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
@ -308,15 +306,12 @@ impl Loader {
|
||||||
// TODO: content_regex handling conflict resolution
|
// TODO: content_regex handling conflict resolution
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn language_config_for_shebang(&self, path: &Path) -> Option<Arc<LanguageConfiguration>> {
|
pub fn language_config_for_shebang(&self, source: &Rope) -> Option<Arc<LanguageConfiguration>> {
|
||||||
// Read the first 128 bytes of the file. If its a shebang line, try to find the language
|
let line = Cow::from(source.line(0));
|
||||||
let file = File::open(path).ok()?;
|
|
||||||
let mut buf = String::with_capacity(128);
|
|
||||||
file.take(128).read_to_string(&mut buf).ok()?;
|
|
||||||
static SHEBANG_REGEX: Lazy<Regex> =
|
static SHEBANG_REGEX: Lazy<Regex> =
|
||||||
Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
|
Lazy::new(|| Regex::new(r"^#!\s*(?:\S*[/\\](?:env\s+)?)?([^\s\.\d]+)").unwrap());
|
||||||
let configuration_id = SHEBANG_REGEX
|
let configuration_id = SHEBANG_REGEX
|
||||||
.captures(&buf)
|
.captures(&line)
|
||||||
.and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
|
.and_then(|cap| self.language_config_ids_by_shebang.get(&cap[1]));
|
||||||
|
|
||||||
configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
|
configuration_id.and_then(|&id| self.language_configs.get(id).cloned())
|
||||||
|
|
|
@ -496,7 +496,7 @@ impl Document {
|
||||||
if let Some(path) = &self.path {
|
if let Some(path) = &self.path {
|
||||||
let language_config = config_loader
|
let language_config = config_loader
|
||||||
.language_config_for_file_name(path)
|
.language_config_for_file_name(path)
|
||||||
.or_else(|| config_loader.language_config_for_shebang(path));
|
.or_else(|| config_loader.language_config_for_shebang(self.text()));
|
||||||
self.set_language(theme, language_config);
|
self.set_language(theme, language_config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue