Add a workaround for parsers leaving trailing \r in comments

Fixes #364
pull/369/head
Wilfred Hughes 2022-09-10 12:02:22 +07:00
parent 19ad2fdb61
commit 98e42b4362
1 changed files with 23 additions and 1 deletions

@ -241,9 +241,15 @@ impl<'a> Syntax<'a> {
pub fn new_atom(
arena: &'a Arena<Syntax<'a>>,
position: Vec<SingleLineSpan>,
content: &str,
mut content: &str,
kind: AtomKind,
) -> &'a Syntax<'a> {
// If a parser hasn't cleaned up \r on CRLF files with
// comments, discard it.
if content.ends_with("\r") {
content = &content[..content.len() - 1];
}
arena.alloc(Atom {
info: SyntaxInfo::default(),
position,
@ -940,6 +946,22 @@ mod tests {
assert_ne!(comment, atom);
}
#[test]
fn test_new_atom_truncates_carriage_return() {
let arena = Arena::new();
let position = vec![];
let content = "foo\r";
let atom = Syntax::new_atom(&arena, position, content, AtomKind::Comment);
match atom {
List { .. } => unreachable!(),
Atom { content, .. } => {
assert_eq!(content, "foo");
}
}
}
/// Ignore the syntax highighting kind when comparing
/// atoms. Sometimes changing delimiter wrapping can change
/// whether a parser thinks that a node is e.g. a type.