diff --git a/src/parse/syntax.rs b/src/parse/syntax.rs index b59394466..ef512e0d7 100644 --- a/src/parse/syntax.rs +++ b/src/parse/syntax.rs @@ -240,7 +240,7 @@ impl<'a> Syntax<'a> { pub fn new_atom( arena: &'a Arena>, - position: Vec, + mut position: Vec, mut content: &str, kind: AtomKind, ) -> &'a Syntax<'a> { @@ -250,6 +250,11 @@ impl<'a> Syntax<'a> { content = &content[..content.len() - 1]; } + if kind == AtomKind::Comment && content.ends_with('\n') { + position.pop(); + content = &content[..content.len() - 1]; + } + arena.alloc(Atom { info: SyntaxInfo::default(), position, @@ -1000,6 +1005,43 @@ mod tests { } } + #[test] + fn test_new_atom_truncates_trailing_newline() { + let arena = Arena::new(); + let position = vec![ + SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 8, + }, + SingleLineSpan { + line: 1.into(), + start_col: 0, + end_col: 1, + }, + ]; + let content = ";; hello\n"; + + let atom = Syntax::new_atom(&arena, position, content, AtomKind::Comment); + + match atom { + List { .. } => unreachable!(), + Atom { + position, content, .. + } => { + assert_eq!(content, ";; hello"); + assert_eq!( + *position, + vec![SingleLineSpan { + line: 0.into(), + start_col: 0, + end_col: 8, + }] + ); + } + } + } + /// 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.