diff --git a/lib/pinc_format/Formatter.ml b/lib/pinc_format/Formatter.ml
index 3632273..12cb402 100644
--- a/lib/pinc_format/Formatter.ml
+++ b/lib/pinc_format/Formatter.ml
@@ -1,6 +1,8 @@
open PPrint
module Parsetree = Pinc_Parser.Parsetree
+let[@inline] ( ^-^ ) x y = x ^^ ifflat empty hardline ^^ y
+
let rec format_comma_separated_attributes ?force_break_at format = function
| [] -> empty
| lst ->
@@ -246,7 +248,8 @@ and format_binary_expression left op right =
| Parsetree.Operators.Binary.DOT_ACCESS -> l ^^ dot ^^ r
| Parsetree.Operators.Binary.BRACKET_ACCESS -> l ^^ brackets r
| Parsetree.Operators.Binary.FUNCTION_CALL -> l ^^ parens r
- | Parsetree.Operators.Binary.PIPE -> l ^^ space ^^ bar ^^ rangle ^^ space ^^ r
+ | Parsetree.Operators.Binary.PIPE ->
+ l ^^ break 1 ^^ ifflat empty (twice space) ^^ bar ^^ rangle ^^ space ^^ r
| Parsetree.Operators.Binary.ARRAY_ADD -> l ^^ space ^^ langle ^^ minus ^^ space ^^ r
| Parsetree.Operators.Binary.MERGE -> l ^^ space ^^ twice at ^^ space ^^ r
| Parsetree.Operators.Binary.RANGE -> l ^^ twice dot ^^ r
@@ -275,7 +278,7 @@ and format_for_in ~index ~iterator ~reverse ~iterable ~body =
and format_conditional ~condition ~consequent ~alternate =
string "if"
^^ space
- ^^ parens (format_expression condition)
+ ^^ group (nest 2 (lparen ^-^ format_expression condition ^-^ rparen))
^^ space
^^ format_expression consequent
^^ optional
@@ -391,7 +394,9 @@ and format_template_node
| P_FragmentTemplateNode fragement_children ->
format_fragment_template_node ~fragement_children
| P_ExpressionTemplateNode template_expression_node_expression ->
- braces (format_expression template_expression_node_expression)
+ group
+ (nest 2 (lbrace ^-^ format_expression template_expression_node_expression)
+ ^-^ rbrace)
| P_TextTemplateNode "\n" when last ->
had_newline := true;
empty
diff --git a/test/array/run.t b/test/array/run.t
index 9a3264d..d75ccf7 100644
--- a/test/array/run.t
+++ b/test/array/run.t
@@ -72,7 +72,8 @@
let nested_array = new_array <- (merged_array <- merged_record_2);
let index = 8;
- let partitioned = merged_array_2 |> Array.partition(fn (item) -> { item % 2 == 0 });
+ let partitioned = merged_array_2
+ |> Array.partition(fn (item) -> { item % 2 == 0 });
let even = partitioned[0];
let odd = partitioned[1];
@@ -99,34 +100,36 @@
{merged_array_2 |> Array.keep(fn (item) -> { item % 2 == 0 })}
- {if (merged_array_2 |> Array.find(fn (item) -> { item == 3 })) {
- "FOUND"
- } else {
- "NOT FOUND"
- }}
+ {if (merged_array_2 |> Array.find(fn (item) -> { item == 3 })) { "FOUND" } else { "NOT FOUND" }}
- {if (merged_array_2 |> Array.find(fn (item) -> { item == -10 })) {
- "FOUND"
- } else {
- "NOT FOUND"
- }}
+ {
+ if (merged_array_2 |> Array.find(fn (item) -> { item == -10 })) {
+ "FOUND"
+ } else {
+ "NOT FOUND"
+ }
+ }
{even |> Array.every(fn (item) -> { item % 2 == 0 })}
{merged_array_2 |> Array.some(fn (item) -> { item % 2 == 0 })}
{merged_array_2 |> Array.every(fn (item) -> { item % 2 == 0 })}
{merged_array_2 |> Array.some(fn (item) -> { item < 0 })}
- {Array.make(6) |> Array.map(fn (item) -> item + 1) |> Array.reduce(
- 0,
- fn (acc, item) -> item + acc,
- )}
+ {
+ Array.make(6)
+ |> Array.map(fn (item) -> item + 1)
+ |> Array.reduce(0, fn (acc, item) -> item + acc)
+ }
{merged_array_2 |> Array.take_until(fn (item) -> item == 5)}
{1...10 |> Array.slice(2, 5)}
- {["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] |> Array.chunk(3) |> Array.map(
- fn (chunk) -> Array.join(chunk, ","),
- ) |> Array.map(fn (item) ->
{item}
)}
+ {
+ ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
+ |> Array.chunk(3)
+ |> Array.map(fn (chunk) -> Array.join(chunk, ","))
+ |> Array.map(fn (item) -> {item}
)
+ }
{[5, 1, 2, 3, 6, 3, 4, 7] |> Array.sort(fn (a, b) -> a - b)}
diff --git a/test/component/run.t b/test/component/run.t
index e3ad1db..b365420 100644
--- a/test/component/run.t
+++ b/test/component/run.t
@@ -62,13 +62,13 @@
let matrix = #Array(of: #Array(of: #Int));
- {for (row in matrix) {
-
- {for (col in row) {
- - {col}
- }}
-
- }}
+ {
+ for (row in matrix) {
+
+ {for (col in row) { - {col}
}}
+
+ }
+ }
}
@@ -77,7 +77,8 @@
component HeadlineSecondary {
let class? = #String;
- let tag? = #String :: fn (value) -> value |> Base.Fn.default("h3");
+ let tag? = #String :: fn (value) -> value
+ |> Base.Fn.default("h3");
let data = #Record(
of: {
diff --git a/test/format/run.t b/test/format/run.t
index 51ab8ba..990a6ca 100644
--- a/test/format/run.t
+++ b/test/format/run.t
@@ -20,13 +20,13 @@
>
- {for (fruit in fruits) {
- - {fruit}
- }}
+ {for (fruit in fruits) { - {fruit}
}}
- {"Aliqua in elit sunt in anim culpa nostrud elit.
+ {
+ "Aliqua in elit sunt in anim culpa nostrud elit.
Elit voluptate nisi laborum est eu esse non exercitation minim.
- Nisi ea enim minim quis."}
+ Nisi ea enim minim quis."
+ }
>
}
@@ -80,10 +80,12 @@
>
- {for (fruit in reverse fruits) {
- let prefix = "fruit:";
- - {prefix} {fruit}
- }}
+ {
+ for (fruit in reverse fruits) {
+ let prefix = "fruit:";
+ - {prefix} {fruit}
+ }
+ }
{/* A Template Comment */}
diff --git a/test/loop/run.t b/test/loop/run.t
index 2fb93f0..3f67e0a 100644
--- a/test/loop/run.t
+++ b/test/loop/run.t
@@ -92,129 +92,93 @@
EXCLUSIVE RANGES
0..10
- {for (i in 0..10) {
- {i}
- }}
+ {for (i in 0..10) { {i}
}}
0..0
- {for (i in 0..0) {
- {i}
- }}
+ {for (i in 0..0) { {i}
}}
0..1
- {for (i in 0..1) {
- {i}
- }}
+ {for (i in 0..1) { {i}
}}
0..-1
- {for (i in 0..-1) {
- {i}
- }}
+ {for (i in 0..-1) { {i}
}}
-10..10
- {for (i in -10..10) {
- {i}
- }}
+ {for (i in -10..10) { {i}
}}
10..0
- {for (i in 10..0) {
- {i}
- }}
+ {for (i in 10..0) { {i}
}}
0..10 reverse
- {for (i in reverse 0..10) {
- {i}
- }}
+ {for (i in reverse 0..10) { {i}
}}
INCLUSIVE RANGES
0...10
- {for (i in 0...10) {
- {i}
- }}
+ {for (i in 0...10) { {i}
}}
0...0
- {for (i in 0...0) {
- {i}
- }}
+ {for (i in 0...0) { {i}
}}
0...1
- {for (i in 0...1) {
- {i}
- }}
+ {for (i in 0...1) { {i}
}}
0...-1
- {for (i in 0...-1) {
- {i}
- }}
+ {for (i in 0...-1) { {i}
}}
-10...10
- {for (i in -10...10) {
- {i}
- }}
+ {for (i in -10...10) { {i}
}}
10...0
- {for (i in 10...0) {
- {i}
- }}
+ {for (i in 10...0) { {i}
}}
0...10 reverse
- {for (i in reverse 0...10) {
- {i}
- }}
+ {for (i in reverse 0...10) { {i}
}}
ITERABLES
array
- {for (i in array) {
- {i}
- }}
+ {for (i in array) { {i}
}}
array reverse
- {for (i in reverse array) {
- {i}
- }}
+ {for (i in reverse array) { {i}
}}
string
- {for (c in "String!") {
- {c}
- }}
+ {for (c in "String!") { {c}
}}
string reverse
- {for (c in reverse "Sträinèg!") {
- {c}
- }}
+ {for (c in reverse "Sträinèg!") { {c}
}}
string array
- {for (index, s in string_array) {
- {index} {s}
- }}
+ {for (index, s in string_array) { {index} {s}
}}
null value
- {for (s in null_value) {
- {s}
- }}
+ {for (s in null_value) { {s}
}}
BREAK / CONTINUE
- {for (c in "String!") {
- if (c == 'i') {
- continue;
- };
-
- c
- }}
-
- {for (i in 0..5) {
- if (i == 3) {
- break;
- };
-
- i
- }}
+ {
+ for (c in "String!") {
+ if (c == 'i') {
+ continue;
+ };
+
+ c
+ }
+ }
+
+ {
+ for (i in 0..5) {
+ if (i == 3) {
+ break;
+ };
+
+ i
+ }
+ }
}
diff --git a/test/record/run.t b/test/record/run.t
index 71d76e6..7cabfcb 100644
--- a/test/record/run.t
+++ b/test/record/run.t
@@ -48,9 +48,7 @@
{record_nested.d.f}
{record_nested.d.h.y}
- {for (el in record_nested.d.g) {
- el ** 2
- }}
+ {for (el in record_nested.d.g) { el ** 2 }}
}
diff --git a/test/slot/run.t b/test/slot/run.t
index c43d187..3164dcd 100644
--- a/test/slot/run.t
+++ b/test/slot/run.t
@@ -65,13 +65,9 @@
}
/>
- {if (record.a > 9) {
- Yaaay!
- }}
+ {if (record.a > 9) { Yaaay! }}
- {for (i in 0..record.a) {
- Yaaay! {i}
- }}
+ {for (i in 0..record.a) { Yaaay! {i} }}
}
@@ -93,9 +89,11 @@
let restricted = #Slot(max: 1, constraints: [SubComponent]);
let multi = #Slot :: fn (els) -> for (el in els) {
- {el @@ {
- class: "item",
- }}
+ {
+ el @@ {
+ class: "item",
+ }
+ }
};
let slot_nothing = #Slot(key: "nothing", constraints: []);
diff --git a/test/template/run.t b/test/template/run.t
index 28dc2aa..56d290d 100644
--- a/test/template/run.t
+++ b/test/template/run.t
@@ -52,15 +52,17 @@
- {if (intro) {
- let foobar = "foo";
- if (foobar) {
-
- } else {
- let barfoo = "3";
-
+ {
+ if (intro) {
+ let foobar = "foo";
+ if (foobar) {
+
+ } else {
+ let barfoo = "3";
+
+ }
}
- }}
+ }
{content}
diff --git a/test/test/run.t b/test/test/run.t
index c0a726d..ea1cfae 100644
--- a/test/test/run.t
+++ b/test/test/run.t
@@ -22,9 +22,7 @@
>
- {for (fruit in fruits) {
- - {fruit}
- }}
+ {for (fruit in fruits) { - {fruit}
}}
Aliqua in elit sunt in anim culpa nostrud elit.