This commit is contained in:
Folkert 2020-07-10 22:32:18 +02:00
parent 36ccafd8ab
commit 9aa05fdc4c
3 changed files with 225 additions and 111 deletions

View file

@ -1,50 +1,85 @@
use crate::annotation::{fmt_annotation, Parens};
use crate::expr::{fmt_expr, is_multiline_expr};
use crate::annotation::{fmt_annotation, Formattable, Newlines, Parens};
use crate::expr::fmt_expr;
use crate::pattern::fmt_pattern;
use crate::spaces::{fmt_spaces, newline, INDENT};
use crate::spaces::{fmt_spaces, is_comment, newline, INDENT};
use bumpalo::collections::String;
use roc_parse::ast::{Def, Expr, Pattern, TypeAnnotation};
use roc_parse::ast::{Def, Expr, Pattern};
pub fn fmt_def<'a>(buf: &mut String<'a>, def: &'a Def<'a>, indent: u16) {
use roc_parse::ast::Def::*;
/// A Located formattable value is also formattable
impl<'a> Formattable<'a> for Def<'a> {
fn is_multiline(&self) -> bool {
use roc_parse::ast::Def::*;
match def {
Annotation(loc_pattern, loc_annotation) => {
fmt_type_annotation(buf, &loc_pattern.value, &loc_annotation.value, indent);
}
Alias { name, vars, ann } => {
buf.push_str(name.value);
match self {
Alias { ann, .. } => ann.is_multiline(),
Annotation(loc_pattern, loc_annotation) => {
loc_pattern.is_multiline() || loc_annotation.is_multiline()
}
Body(loc_pattern, loc_expr) => loc_pattern.is_multiline() || loc_expr.is_multiline(),
if vars.is_empty() {
buf.push(' ');
} else {
for var in *vars {
buf.push(' ');
fmt_pattern(buf, &var.value, indent, Parens::NotNeeded);
}
TypedBody(_loc_pattern, _loc_annotation, _loc_expr) => {
unreachable!("annotations and bodies have not yet been merged into TypedBody");
}
buf.push_str(" : ");
fmt_annotation(buf, &ann.value, indent);
SpaceBefore(sub_def, spaces) | SpaceAfter(sub_def, spaces) => {
spaces.iter().any(|s| is_comment(s)) || sub_def.is_multiline()
}
Nested(def) => def.is_multiline(),
}
Body(loc_pattern, loc_expr) => {
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
}
TypedBody(_loc_pattern, _loc_annotation, _loc_expr) => {
unreachable!("annotations and bodies have not yet been merged into TypedBody");
}
SpaceBefore(sub_def, spaces) => {
fmt_spaces(buf, spaces.iter(), indent);
fmt_def(buf, sub_def, indent);
}
SpaceAfter(sub_def, spaces) => {
fmt_def(buf, sub_def, indent);
fmt_spaces(buf, spaces.iter(), indent);
}
Nested(def) => fmt_def(buf, def, indent),
}
fn format_with_options(
&self,
buf: &mut String<'a>,
_parens: Parens,
_newlines: Newlines,
indent: u16,
) {
use roc_parse::ast::Def::*;
match self {
Annotation(loc_pattern, loc_annotation) => {
loc_pattern.format(buf, indent);
buf.push_str(" : ");
loc_annotation.format(buf, indent);
}
Alias { name, vars, ann } => {
buf.push_str(name.value);
if vars.is_empty() {
buf.push(' ');
} else {
for var in *vars {
buf.push(' ');
fmt_pattern(buf, &var.value, indent, Parens::NotNeeded);
}
}
buf.push_str(" : ");
ann.format(buf, indent)
}
Body(loc_pattern, loc_expr) => {
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
}
TypedBody(_loc_pattern, _loc_annotation, _loc_expr) => {
unreachable!("annotations and bodies have not yet been merged into TypedBody");
}
SpaceBefore(sub_def, spaces) => {
fmt_spaces(buf, spaces.iter(), indent);
sub_def.format(buf, indent);
}
SpaceAfter(sub_def, spaces) => {
sub_def.format(buf, indent);
fmt_spaces(buf, spaces.iter(), indent);
}
Nested(def) => def.format(buf, indent),
}
}
}
pub fn fmt_def<'a>(buf: &mut String<'a>, def: &Def<'a>, indent: u16) {
def.format(buf, indent);
}
pub fn fmt_body<'a>(
@ -55,7 +90,7 @@ pub fn fmt_body<'a>(
) {
fmt_pattern(buf, pattern, indent, Parens::InApply);
buf.push_str(" =");
if is_multiline_expr(body) {
if body.is_multiline() {
match body {
Expr::Record { .. } | Expr::List(_) => {
newline(buf, indent + INDENT);
@ -71,14 +106,3 @@ pub fn fmt_body<'a>(
fmt_expr(buf, body, indent, false, true);
}
}
pub fn fmt_type_annotation<'a>(
buf: &mut String<'a>,
pattern: &'a Pattern<'a>,
annotation: &'a TypeAnnotation<'a>,
indent: u16,
) {
fmt_pattern(buf, pattern, indent, Parens::NotNeeded);
buf.push_str(" : ");
fmt_annotation(buf, annotation, indent);
}