keep indent state in fmt Buf

This commit is contained in:
Joshua Warner 2021-12-02 16:28:57 -08:00
parent e872d00b13
commit e3f2f8d9a4
11 changed files with 313 additions and 182 deletions

View file

@ -1,6 +1,8 @@
use bumpalo::collections::String;
use roc_parse::ast::CommentOrNewline;
use crate::Buf;
/// The number of spaces to indent.
pub const INDENT: u16 = 4;
@ -17,7 +19,7 @@ pub fn add_spaces(buf: &mut String<'_>, spaces: u16) {
}
pub fn fmt_default_spaces<'a>(
buf: &mut String<'a>,
buf: &mut Buf<'a>,
spaces: &[CommentOrNewline<'a>],
default: &str,
indent: u16,
@ -29,7 +31,7 @@ pub fn fmt_default_spaces<'a>(
}
}
pub fn fmt_spaces<'a, 'b, I>(buf: &mut String<'a>, spaces: I, indent: u16)
pub fn fmt_spaces<'a, 'b, I>(buf: &mut Buf<'a>, spaces: I, indent: u16)
where
I: Iterator<Item = &'b CommentOrNewline<'b>>,
{
@ -38,19 +40,14 @@ where
// Only ever print two newlines back to back.
// (Two newlines renders as one blank line.)
let mut consecutive_newlines = 0;
let mut iter = spaces.peekable();
let mut encountered_comment = false;
while let Some(space) = iter.next() {
for space in spaces {
match space {
Newline => {
if !encountered_comment && (consecutive_newlines < 2) {
if iter.peek() == Some(&&Newline) && consecutive_newlines < 1 {
buf.push('\n');
} else {
newline(buf, indent);
}
buf.newline();
// Don't bother incrementing it if we're already over the limit.
// There's no upside, and it might eventually overflow,
@ -58,14 +55,16 @@ where
}
}
LineComment(comment) => {
buf.indent(indent);
fmt_comment(buf, comment);
newline(buf, indent);
buf.newline();
encountered_comment = true;
}
DocComment(docs) => {
buf.indent(indent);
fmt_docs(buf, docs);
newline(buf, indent);
buf.newline();
encountered_comment = true;
}
@ -86,7 +85,7 @@ pub enum NewlineAt {
/// at the beginning or at the end of the block
/// in the case of there is some comment in the `spaces` argument.
pub fn fmt_comments_only<'a, 'b, I>(
buf: &mut String<'a>,
buf: &mut Buf<'a>,
spaces: I,
new_line_at: NewlineAt,
indent: u16,
@ -103,26 +102,28 @@ pub fn fmt_comments_only<'a, 'b, I>(
Newline => {}
LineComment(comment) => {
if comment_seen || new_line_at == Top || new_line_at == Both {
newline(buf, indent);
buf.newline();
}
buf.indent(indent);
fmt_comment(buf, comment);
comment_seen = true;
}
DocComment(docs) => {
if comment_seen || new_line_at == Top || new_line_at == Both {
newline(buf, indent);
buf.newline();
}
buf.indent(indent);
fmt_docs(buf, docs);
comment_seen = true;
}
}
}
if comment_seen && (new_line_at == Bottom || new_line_at == Both) {
newline(buf, indent);
buf.newline();
}
}
fn fmt_comment<'a>(buf: &mut String<'a>, comment: &str) {
fn fmt_comment<'a>(buf: &mut Buf<'a>, comment: &str) {
buf.push('#');
if !comment.starts_with(' ') {
buf.push(' ');
@ -130,7 +131,7 @@ fn fmt_comment<'a>(buf: &mut String<'a>, comment: &str) {
buf.push_str(comment);
}
fn fmt_docs<'a>(buf: &mut String<'a>, docs: &str) {
fn fmt_docs<'a>(buf: &mut Buf<'a>, docs: &str) {
buf.push_str("##");
if !docs.starts_with(' ') {
buf.push(' ');