All if comment test cases work

This commit is contained in:
Chad Stearns 2020-01-06 03:06:23 -05:00
parent a2566d85ff
commit 81f4f232f7
3 changed files with 217 additions and 70 deletions

View File

@ -1,9 +1,10 @@
use crate::fmt::def::fmt_def;
use crate::fmt::pattern::fmt_pattern;
use crate::fmt::spaces::{add_spaces, fmt_comments_only, fmt_spaces, newline, INDENT};
use crate::fmt::spaces::{add_spaces, fmt_comments_only, fmt_spaces, newline, INDENT, fmt_if_spaces, is_comment};
use crate::parse::ast::{AssignedField, Base, CommentOrNewline, Expr, Pattern};
use crate::region::Located;
use bumpalo::collections::{String, Vec};
use crate::parse::expr;
pub fn fmt_expr<'a>(
buf: &mut String<'a>,
@ -431,28 +432,82 @@ fn fmt_if<'a>(
indent
};
buf.push_str("if");
if is_multiline_condition {
buf.push_str("if");
newline(buf, return_indent);
fmt_expr(buf, &loc_condition.value, return_indent, false, false);
newline(buf, indent);
buf.push_str("then");
dbg!(&loc_condition.value);
match &loc_condition.value {
Expr::SpaceBefore(expr_below, spaces_below) => {
fmt_if_spaces(buf, spaces_below.iter(), return_indent);
newline(buf, return_indent);
match &expr_below {
Expr::SpaceAfter(expr_above, spaces_above) => {
fmt_expr(buf, &expr_above, return_indent, false, false);
fmt_if_spaces(buf, spaces_above.iter(), return_indent);
newline(buf, indent);
}
_ => {
fmt_expr(buf, &expr_below, return_indent, false, false);
}
}
}
_ => {
fmt_expr(buf, &loc_condition.value, return_indent, false, false);
},
}
} else {
buf.push_str("if ");
buf.push(' ');
fmt_expr(buf, &loc_condition.value, indent, false, true);
buf.push_str(" then");
buf.push(' ');
}
buf.push_str("then");
if is_multiline {
newline(buf, return_indent);
match &loc_then.value {
Expr::SpaceBefore(expr_below, spaces_below) => {
let any_comments_below = spaces_below.iter().any(is_comment);
if !any_comments_below {
newline(buf, return_indent);
}
fmt_if_spaces(buf, spaces_below.iter(), return_indent);
if any_comments_below {
newline(buf, return_indent);
}
match &expr_below {
Expr::SpaceAfter(expr_above, spaces_above) => {
fmt_expr(buf, &expr_above, return_indent, false, false);
fmt_if_spaces(buf, spaces_above.iter(), return_indent);
newline(buf, indent);
}
_ => {
fmt_expr(buf, &expr_below, return_indent, false, false);
}
}
}
_ => {
fmt_expr(buf, &loc_condition.value, return_indent, false, false);
},
}
} else {
buf.push_str(" ");
fmt_expr(buf, &loc_then.value, return_indent, false, false);
}
fmt_expr(buf, &loc_then.value, return_indent, false, false);
if is_multiline {
newline(buf, indent);
buf.push_str("else");
newline(buf, return_indent);
} else {

View File

@ -53,6 +53,43 @@ where
}
}
pub fn fmt_if_spaces<'a, I>(buf: &mut String<'a>, spaces: I, indent: u16)
where
I: Iterator<Item = &'a CommentOrNewline<'a>>,
{
use self::CommentOrNewline::*;
let mut iter = spaces.peekable();
let mut encountered_comment = false;
while let Some(space) = iter.next() {
dbg!(space);
match space {
Newline => {}
LineComment(comment) => {
buf.push('#');
buf.push_str(comment);
}
}
match iter.peek() {
None => {},
Some(next_space) => {
match next_space {
Newline => {},
LineComment(_) => {
newline(buf, indent);
},
}
},
}
}
}
/// Like format_spaces, but remove newlines and keep only comments.
pub fn fmt_comments_only<'a, I>(buf: &mut String<'a>, spaces: I, indent: u16)
where
@ -76,3 +113,15 @@ fn fmt_comment<'a>(buf: &mut String<'a>, comment: &'a str, indent: u16) {
newline(buf, indent);
}
pub fn is_comment<'a>(space: &'a CommentOrNewline<'a>) -> bool {
match space {
CommentOrNewline::Newline => {
false
},
CommentOrNewline::LineComment(_) => {
true
},
}
}

View File

@ -819,71 +819,114 @@ mod test_format {
);
}
// #[test]
// fn if_removes_newlines() {
// expr_formats_to(
// indoc!(
// r#"
// if
//
// # You never know!
// isPrime 8
//
// # Top Comment
//
// # Bottom Comment
//
//
// then
//
// # A
//
// # B
//
// nothing
//
// # B again
//
// else
//
// # C
// # D
//
// # E
// # F
//
// just (div 1 8)
// "#
// ),
// indoc!(
// r#"
// if
// # You never know!
// isPrime 8
#[test]
fn if_removes_newlines_from_else() {
expr_formats_to(
indoc!(
r#"
if
isPrime 8
then
nothing
else
# C
# D
// # Top Comment
# E
# F
// # Bottom Comment
// then
// # A
just (div 1 8)
"#
),
indoc!(
r#"
if
isPrime 8
then
nothing
else
# C
# D
# E
# F
just (div 1 8)
"#
),
);
}
// # B
#[test]
fn if_removes_newlines_from_then() {
expr_formats_to(
indoc!(
r#"
if
isPrime 9
then
# EE
# FF
// nothing
nothing
// # B again
//
// else
// # C
// # D
# GG
else
just (div 1 9)
"#
),
indoc!(
r#"
if
isPrime 9
then
# EE
# FF
nothing
# GG
else
just (div 1 9)
"#
),
);
}
#[test]
fn if_removes_newlines_from_condition() {
expr_formats_to(
indoc!(
r#"
if
# Is
# It
isPrime 10
# Prime?
then
nothing
else
just (div 1 10)
"#
),
indoc!(
r#"
if
# Is
# It
isPrime 10
# Prime?
then
nothing
else
just (div 1 10)
"#
),
);
}
// # E
// # F
// just (div 1 8)
// "#
// ),
// );
// }
#[test]
fn multi_line_if() {
expr_formats_to(