Use XML file headers as language detection hints

Fixes #630
pull/633/head
Wilfred Hughes 2024-01-30 21:22:12 +07:00
parent 5219977d88
commit 052b3a62a3
2 changed files with 16 additions and 0 deletions

@ -4,6 +4,8 @@
Added support for Objective-C and VHDL.
Files starting with `<?xml` are now parsed as XML.
### Display
The default display width for tabs has changed to 4.

@ -416,6 +416,10 @@ fn looks_like_objc(path: &Path, src: &str) -> bool {
false
}
fn looks_like_xml(src: &str) -> bool {
src.starts_with("<?xml")
}
pub(crate) fn guess(
path: &Path,
src: &str,
@ -458,6 +462,10 @@ pub(crate) fn guess(
return Some(lang);
}
if looks_like_xml(src) {
return Some(Language::Xml);
}
None
}
@ -651,6 +659,12 @@ mod tests {
assert_eq!(guess(path, "# -*-python-*-", &[]), Some(Python));
}
#[test]
fn test_guess_by_xml_header() {
let path = Path::new("foo");
assert_eq!(guess(path, "<?xml version=\"1.0\" encoding=\"utf-8\"?>", &[]), Some(Xml));
}
#[test]
fn test_guess_unknown() {
let path = Path::new("jfkdlsjfkdsljfkdsljf");