Remove trailing whitespace from the output of the formatter

This commit is contained in:
Joshua Warner 2021-12-18 16:38:00 -08:00
parent c7b826e3d4
commit 48c207bb06
8 changed files with 149 additions and 107 deletions

View file

@ -13,6 +13,7 @@ use bumpalo::{collections::String, Bump};
pub struct Buf<'a> {
text: String<'a>,
spaces_to_flush: usize,
beginning_of_line: bool,
}
@ -20,6 +21,7 @@ impl<'a> Buf<'a> {
pub fn new_in(arena: &'a Bump) -> Buf<'a> {
Buf {
text: String::new_in(arena),
spaces_to_flush: 0,
beginning_of_line: true,
}
}
@ -43,18 +45,49 @@ impl<'a> Buf<'a> {
pub fn push(&mut self, ch: char) {
debug_assert!(!self.beginning_of_line);
debug_assert!(ch != '\n');
debug_assert!(ch != '\n' && ch != ' ');
self.flush_spaces();
self.text.push(ch);
}
pub fn push_str_allow_spaces(&mut self, s: &str) {
debug_assert!(!self.beginning_of_line);
debug_assert!(!s.contains('\n'));
self.flush_spaces();
self.text.push_str(s);
}
pub fn push_str(&mut self, s: &str) {
debug_assert!(!self.beginning_of_line);
debug_assert!(!s.contains('\n'));
debug_assert!(!s.contains('\n') && !s.ends_with(' '));
if !s.is_empty() {
self.flush_spaces();
}
self.text.push_str(s);
}
pub fn spaces(&mut self, count: usize) {
self.spaces_to_flush += count;
}
pub fn newline(&mut self) {
self.spaces_to_flush = 0;
self.text.push('\n');
self.beginning_of_line = true;
}
fn flush_spaces(&mut self) {
if self.spaces_to_flush > 0 {
for _ in 0..self.spaces_to_flush {
self.text.push(' ');
}
self.spaces_to_flush = 0;
}
}
}