From 9751073ed7c18fe299c585de86980dddb721792e Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Thu, 15 Oct 2020 21:12:32 +0200 Subject: [PATCH] Keep seq after formatting --- book/src/syntax-and-semantics.md | 12 ++++---- examples/marshalling.rs | 8 +++--- format/src/pretty_print.rs | 49 +++++++++++++++++++++----------- format/tests/pretty_print.rs | 7 +++-- 4 files changed, 47 insertions(+), 29 deletions(-) diff --git a/book/src/syntax-and-semantics.md b/book/src/syntax-and-semantics.md index a2a60ee7c3..91f5f212bf 100644 --- a/book/src/syntax-and-semantics.md +++ b/book/src/syntax-and-semantics.md @@ -340,20 +340,22 @@ Sequence expressions work just like `do` expressions, only they do not have a bi ```f#,rust let io @ { ? } = import! std.io -io.print "Hello" -io.print " " +seq io.print "Hello" +seq io.print " " io.println "world!" ``` -For backwards compatibility it is also possible to write (`seq`) before each expression. +The `seq` keyword can also be omitted. ```f#,rust let io @ { ? } = import! std.io -seq io.print "Hello" -seq io.print " " +io.print "Hello" +io.print " " io.println "world!" ``` +(In the future one of these ways are likely to be deprecated with only one way remaining, the formatter will be able to update the code in any case). + ### Indentation If you have been following along this far, you may be think that the syntax so far is pretty limiting. In particular, you wouldn't be wrong in thinking that the `let` and `type` syntax are clunky due to their need to be closed by the `in` keyword. Luckily, Gluon offers a more convenient way of writing bindings by relying on indentation. diff --git a/examples/marshalling.rs b/examples/marshalling.rs index 750dd89695..8f669d11b0 100644 --- a/examples/marshalling.rs +++ b/examples/marshalling.rs @@ -311,8 +311,8 @@ fn marshal_wrapper(thread: &Thread) -> Result<()> { let actual = { name = "Bob", age = 11, data = True } let expected = roundtrip actual - assert (actual.name == expected.name) - assert (actual.age == expected.age) + let _ = assert (actual.name == expected.name) + let _ = assert (actual.age == expected.age) assert (actual.data == expected.data) "#; @@ -369,8 +369,8 @@ fn marshal_userdata(thread: &Thread) -> Result<()> { let { assert } = import! std.test let { WindowHandle, create_hwnd, id, metadata } = import! hwnd let hwnd : WindowHandle = create_hwnd 0 "Window1" - assert (id hwnd == 0) - assert (metadata hwnd == "Window1") + let _ = assert (id hwnd == 0) + let _ = assert (metadata hwnd == "Window1") hwnd "#; diff --git a/format/src/pretty_print.rs b/format/src/pretty_print.rs index e76c367281..62102a069d 100644 --- a/format/src/pretty_print.rs +++ b/format/src/pretty_print.rs @@ -478,29 +478,44 @@ where ref bound, ref body, .. - }) => match id { - Some(pattern) => { - let from = chain![ - arena, - "do", - self.space_before(pattern.span.start()), - self.pretty_pattern(pattern), - self.space_after(pattern.span.end()), - "=" - ]; + }) => { + if self.source.src_slice(expr.span).starts_with("seq") { + let from = arena.text("seq"); chain![ arena, self.hang(from, (self.space_before(bound.span.start()), true), bound), self.pretty_expr_(bound.span.end(), body) ] - } + } else { + match id { + Some(pattern) => { + let from = chain![ + arena, + "do", + self.space_before(pattern.span.start()), + self.pretty_pattern(pattern), + self.space_after(pattern.span.end()), + "=" + ]; + chain![ + arena, + self.hang( + from, + (self.space_before(bound.span.start()), true), + bound + ), + self.pretty_expr_(bound.span.end(), body) + ] + } - None => chain![ - arena, - self.pretty_expr_(bound.span.start(), bound), - self.pretty_expr_(bound.span.end(), body) - ], - }, + None => chain![ + arena, + self.pretty_expr_(bound.span.start(), bound), + self.pretty_expr_(bound.span.end(), body) + ], + } + } + } Expr::MacroExpansion { ref original, .. } => { return self.pretty_expr_(previous_end, original); } diff --git a/format/tests/pretty_print.rs b/format/tests/pretty_print.rs index 21aa9847f7..4606a91a1a 100644 --- a/format/tests/pretty_print.rs +++ b/format/tests/pretty_print.rs @@ -818,7 +818,7 @@ let assert_success : [Show e] fn sequence() { let expr = r#" // a -io.print "Hello" +seq io.print "Hello" // b io.print " " // c @@ -828,11 +828,12 @@ io.println "World" expect![[r#" // a - io.print "Hello" + seq io.print "Hello" // b io.print " " // c io.println "World" // d - "#]].assert_eq(&format_expr(expr).unwrap()); + "#]] + .assert_eq(&format_expr(expr).unwrap()); }