Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore UTF-8 BOM on syntax detection #530

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/parsing/syntax_set.rs
Expand Up @@ -213,6 +213,7 @@ impl SyntaxSet {
/// This uses regexes that come with some sublime syntax grammars for matching things like
/// shebangs and mode lines like `-*- Mode: C -*-`
pub fn find_syntax_by_first_line<'a>(&'a self, s: &str) -> Option<&'a SyntaxReference> {
let s = s.strip_prefix("\u{feff}").unwrap_or(s); // Strip UTF-8 BOM
let cache = self.first_line_cache();
for &(ref reg, i) in cache.regexes.iter().rev() {
if reg.search(s, 0, s.len(), None) {
Expand Down Expand Up @@ -1401,6 +1402,16 @@ mod tests {
assert_prototype_only_on(&["main"], &rebuilt, &rebuilt.syntaxes()[0]);
}

#[test]
fn find_syntax_set_from_line_with_bom() {
// Regression test for #529
let syntax_set = SyntaxSet::load_defaults_newlines();
let syntax_ref = syntax_set
.find_syntax_by_first_line("\u{feff}<?xml version=\"1.0\"?>")
.unwrap();
assert_eq!(syntax_ref.name, "XML");
}

fn assert_ops_contain(ops: &[(usize, ScopeStackOp)], expected: &(usize, ScopeStackOp)) {
assert!(
ops.contains(expected),
Expand Down