Don't including trailing newlines in comment nodes

This makes constructing hunks harder to reason about.

This change doesn't affect output, but helps when debugging, as it
makes multiline atoms much less common.
pull/513/head
Wilfred Hughes 2023-04-30 09:51:39 +07:00
parent b5cc0787f4
commit 87f19f5e10
1 changed files with 43 additions and 1 deletions

@ -240,7 +240,7 @@ impl<'a> Syntax<'a> {
pub fn new_atom(
arena: &'a Arena<Syntax<'a>>,
position: Vec<SingleLineSpan>,
mut position: Vec<SingleLineSpan>,
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.