fix formating newlines

This commit is contained in:
Sébastien Besnier 2020-11-25 15:44:58 +01:00
parent 7361f62902
commit aa3ce94f86
3 changed files with 73 additions and 11 deletions

View file

@ -81,13 +81,16 @@ where
} }
macro_rules! format_sequence { macro_rules! format_sequence {
($buf: expr, $indent:expr, $start:expr, $end:expr, $items:expr, $t:ident) => { ($buf: expr, $indent:expr, $start:expr, $end:expr, $items:expr, $final_comments:expr, $newline:expr, $t:ident) => {
// is it a multiline type annotation? let is_multiline =
if $items.iter().any(|item| item.value.is_multiline()) { $items.iter().any(|item| item.value.is_multiline()) || !$final_comments.is_empty();
if is_multiline {
let braces_indent = $indent + INDENT; let braces_indent = $indent + INDENT;
let item_indent = braces_indent + INDENT; let item_indent = braces_indent + INDENT;
if ($newline == Newlines::Yes) {
newline($buf, braces_indent); newline($buf, braces_indent);
}
$buf.push($start); $buf.push($start);
for item in $items.iter() { for item in $items.iter() {
@ -135,10 +138,12 @@ macro_rules! format_sequence {
} }
} }
} }
fmt_comments_only($buf, $final_comments.iter(), NewlineAt::Top, item_indent);
newline($buf, braces_indent); newline($buf, braces_indent);
$buf.push($end); $buf.push($end);
} else { } else {
// is_multiline == false // is_multiline == false
// there is no comment to add
$buf.push($start); $buf.push($start);
let mut iter = $items.iter().peekable(); let mut iter = $items.iter().peekable();
while let Some(item) = iter.next() { while let Some(item) = iter.next() {
@ -281,9 +286,9 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
TagUnion { TagUnion {
tags, tags,
ext, ext,
final_comments: _, final_comments,
} => { } => {
format_sequence!(buf, indent, '[', ']', tags, Tag); format_sequence!(buf, indent, '[', ']', tags, final_comments, newlines, Tag);
if let Some(loc_ext_ann) = *ext { if let Some(loc_ext_ann) = *ext {
loc_ext_ann.value.format(buf, indent); loc_ext_ann.value.format(buf, indent);
@ -293,9 +298,18 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
Record { Record {
fields, fields,
ext, ext,
final_comments: _, final_comments,
} => { } => {
format_sequence!(buf, indent, '{', '}', fields, AssignedField); format_sequence!(
buf,
indent,
'{',
'}',
fields,
final_comments,
newlines,
AssignedField
);
if let Some(loc_ext_ann) = *ext { if let Some(loc_ext_ann) = *ext {
loc_ext_ann.value.format(buf, indent); loc_ext_ann.value.format(buf, indent);
@ -312,7 +326,7 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
SpaceBefore(ann, spaces) => { SpaceBefore(ann, spaces) => {
newline(buf, indent + INDENT); newline(buf, indent + INDENT);
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT); fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT);
ann.format_with_options(buf, parens, newlines, indent + INDENT) ann.format_with_options(buf, parens, Newlines::No, indent + INDENT)
} }
SpaceAfter(ann, spaces) => { SpaceAfter(ann, spaces) => {
ann.format_with_options(buf, parens, newlines, indent); ann.format_with_options(buf, parens, newlines, indent);

View file

@ -38,10 +38,21 @@ impl<'a> Formattable<'a> for Def<'a> {
loc_pattern.format(buf, indent); loc_pattern.format(buf, indent);
if loc_annotation.is_multiline() { if loc_annotation.is_multiline() {
buf.push_str(" :"); buf.push_str(" :");
loc_annotation.format_with_options(
buf,
Parens::NotNeeded,
Newlines::Yes,
indent,
);
} else { } else {
buf.push_str(" : "); buf.push_str(" : ");
loc_annotation.format_with_options(
buf,
Parens::NotNeeded,
Newlines::No,
indent,
);
} }
loc_annotation.format(buf, indent);
} }
Alias { name, vars, ann } => { Alias { name, vars, ann } => {
buf.push_str(name.value); buf.push_str(name.value);

View file

@ -781,6 +781,20 @@ mod test_fmt {
); );
} }
#[test]
fn trailing_comma_in_record_annotation_same() {
expr_formats_same(indoc!(
r#"
f :
{
y : Int,
x : Int,
}
f"#
));
}
#[test] #[test]
fn multiline_type_definition() { fn multiline_type_definition() {
expr_formats_same(indoc!( expr_formats_same(indoc!(
@ -824,6 +838,29 @@ mod test_fmt {
); );
} }
#[test]
fn final_comment_record_type_definition() {
expr_formats_to(
indoc!(
r#"
f :
{ # comment
}
f"#
),
indoc!(
r#"
f :
{
# comment
}
f"#
),
);
}
#[test] #[test]
fn def_closure() { fn def_closure() {
expr_formats_same(indoc!( expr_formats_same(indoc!(