From 71157dbd4e34191441c67dc0c472be4c6f0bb499 Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Fri, 1 Dec 2017 19:10:26 +0100 Subject: [PATCH] feat(format): Format do expressions --- format/src/pretty_print.rs | 18 +++++++++++++++++- format/tests/pretty_print.rs | 10 ++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/format/src/pretty_print.rs b/format/src/pretty_print.rs index 46d6b05905..fd4e78dea6 100644 --- a/format/src/pretty_print.rs +++ b/format/src/pretty_print.rs @@ -3,7 +3,7 @@ use std::{iter, ops}; use itertools::{Either, Itertools}; use pretty::{Arena, DocAllocator, DocBuilder}; -use base::ast::{Expr, Pattern, SpannedExpr, SpannedPattern, ValueBinding}; +use base::ast::{Do, Expr, Pattern, SpannedExpr, SpannedPattern, ValueBinding}; use base::kind::Kind; use base::pos::{self, BytePos, HasSpan, Span, Spanned}; use base::source; @@ -288,6 +288,22 @@ impl<'a: 'e, 'e> Printer<'a, 'e> { self.pretty_expr_(binds.last().unwrap().alias.span.end, body) ].group() } + Expr::Do(Do { + ref id, + ref bound, + ref body, + .. + }) => chain![arena; + chain![arena; + "do", + self.space_before(id.span.start), + id.value.name.as_ref(), + self.space_after(id.span.end), + "=", + self.hang(arena.nil(), bound).group() + ].group(), + self.pretty_expr_(bound.span.end, body) + ], Expr::Error(_) => arena.text(""), }; comments.append(doc) diff --git a/format/tests/pretty_print.rs b/format/tests/pretty_print.rs index 97fb3ecced..9d17f8d120 100644 --- a/format/tests/pretty_print.rs +++ b/format/tests/pretty_print.rs @@ -300,3 +300,13 @@ let semigroup = "#; assert_diff!(&format_expr(expr).unwrap(), expr, " ", 0); } + +#[test] +fn do_expression() { + let expr = r#" +do /* x1 */ x /* x2 */ = Some 1 +// test +test abc 1232 "" +"#; + assert_diff!(&format_expr(expr).unwrap(), expr, " ", 0); +}