format properly multiline type annotations

This commit is contained in:
Sébastien Besnier 2020-11-25 15:15:32 +01:00
parent 4ed8d3eb79
commit 7361f62902
3 changed files with 50 additions and 10 deletions

View file

@ -37,10 +37,6 @@ pub enum Newlines {
No, No,
} }
pub fn fmt_annotation<'a>(buf: &mut String<'a>, annotation: &'a TypeAnnotation<'a>, indent: u16) {
annotation.format(buf, indent);
}
pub trait Formattable<'a> { pub trait Formattable<'a> {
fn is_multiline(&self) -> bool; fn is_multiline(&self) -> bool;
@ -313,8 +309,18 @@ impl<'a> Formattable<'a> for TypeAnnotation<'a> {
rhs.value.format(buf, indent); rhs.value.format(buf, indent);
} }
SpaceBefore(ann, _spaces) | SpaceAfter(ann, _spaces) => { SpaceBefore(ann, spaces) => {
ann.format_with_options(buf, parens, newlines, indent) newline(buf, indent + INDENT);
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent + INDENT);
ann.format_with_options(buf, parens, newlines, indent + INDENT)
}
SpaceAfter(ann, spaces) => {
ann.format_with_options(buf, parens, newlines, indent);
fmt_comments_only(buf, spaces.iter(), NewlineAt::Bottom, indent);
// seems like a lot of spaceAfter are not constructible
// since most of the time we use "SpaceBefore".
// Is this specific case really unreachable?
unreachable!("cannot have space after type annotation");
} }
Malformed(raw) => buf.push_str(raw), Malformed(raw) => buf.push_str(raw),

View file

@ -36,7 +36,11 @@ impl<'a> Formattable<'a> for Def<'a> {
match self { match self {
Annotation(loc_pattern, loc_annotation) => { Annotation(loc_pattern, loc_annotation) => {
loc_pattern.format(buf, indent); loc_pattern.format(buf, indent);
buf.push_str(" : "); if loc_annotation.is_multiline() {
buf.push_str(" :");
} else {
buf.push_str(" : ");
}
loc_annotation.format(buf, indent); loc_annotation.format(buf, indent);
} }
Alias { name, vars, ann } => { Alias { name, vars, ann } => {

View file

@ -770,7 +770,7 @@ mod test_fmt {
), ),
indoc!( indoc!(
r#" r#"
f : f :
{ {
y : Int, y : Int,
x : Int, x : Int,
@ -782,8 +782,30 @@ mod test_fmt {
} }
#[test] #[test]
fn comments_in_record_annotation() { fn multiline_type_definition() {
expr_formats_same( expr_formats_same(indoc!(
r#"
f :
Int
f"#
));
}
#[test]
fn multiline_empty_record_type_definition() {
expr_formats_same(indoc!(
r#"
f :
{}
f"#
));
}
#[test]
fn type_definition_comment_after_colon() {
expr_formats_to(
indoc!( indoc!(
r#" r#"
f : # comment f : # comment
@ -791,6 +813,14 @@ mod test_fmt {
f"# f"#
), ),
indoc!(
r#"
f :
# comment
{}
f"#
),
); );
} }