Skip to content

Commit

Permalink
Use XML file headers as language detection hints
Browse files Browse the repository at this point in the history
Fixes #630
  • Loading branch information
Wilfred committed Jan 31, 2024
1 parent 5219977 commit 052b3a6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/parse/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -458,6 +462,10 @@ pub(crate) fn guess(
return Some(lang);
}

if looks_like_xml(src) {
return Some(Language::Xml);
}

None
}

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 052b3a6

Please sign in to comment.