@@ -3,7 +3,9 @@ module Format (format) where
33import Prelude
44import Data.Array as Array
55import Data.Array.NonEmpty as NE
6+import Data.Array.NonEmpty.Internal (NonEmptyArray)
67import Data.Foldable (fold, foldMap)
8+import Data.FoldableWithIndex (foldMapWithIndex)
79import Data.Maybe (Maybe(..), maybe)
810import Data.Monoid.Additive (Additive(..))
911import Data.Newtype (ala)
@@ -696,7 +698,8 @@ formatWhere settings@{ indentation } indent' (CST.Where { expr: expr', bindings:
696698 (wherePrefix <> newline <> indent)
697699 <> formatSourceToken settings indent blank where''
698700 <> whereSuffix
699− <> formatLetBindings letBindings'
701+ <> (newline <> indent)
702+ <> (formatLetBindings settings indent) letBindings'
700703 where
701704 wherePrefix = if precedingEmptyLines where''.leadingComments > 1 then newline else blank
702705
@@ -706,23 +709,6 @@ formatWhere settings@{ indentation } indent' (CST.Where { expr: expr', bindings:
706709 else
707710 blank
708711
709− formatLetBindings letBindings' =
710− foldMap
711− formatLetBindingWithFollowing
712− ( NE.zip letBindings'
713− (NE.snoc' (Just <$> NE.tail letBindings') Nothing)
714− )
715−
716− formatLetBindingWithFollowing (current /\ maybeFollowing) = formatLetBinding settings indent (newline <> indent) suffix current
717− where
718− suffix = case maybeFollowing of
719− Nothing → blank -- no newline on last entry
720− Just followingLetBinding →
721− if isPrecededByBlankLines followingLetBinding then
722− newline
723− else
724− blank
725−
726712formatLetBinding ∷
727713 Settings →
728714 Indent →
@@ -1104,10 +1090,8 @@ formatLetIn lines settings@{ indentation } indent' letIn' = case letIn' of
11041090 MultipleLines → (newline <> indent') /\ (indent' <> indentation) /\ (newline <> indent' <> indentation)
11051091 SingleLine → space /\ indent' /\ space
11061092 formatSourceToken settings indent' blank let'
1107− <> foldMap
1108− (formatLetBinding settings indent prefix newline)
1109− (NE.init letBindings)
1110− <> formatLetBinding settings indent prefix blank (NE.last letBindings)
1093+ <> prefix
1094+ <> (formatLetBindings settings indent letBindings)
11111095 <> inPrefix
11121096 <> formatSourceToken settings indent' blank in'
11131097 <> prefix
@@ -1188,13 +1172,11 @@ formatDoBlock ∷
11881172 Indent →
11891173 CST.DoBlock Void →
11901174 String
1191−formatDoBlock lines settings@{ indentation } indent' doBlock' = case doBlock' of
1175+formatDoBlock lines settings@{ indentation } indent' = case _ of
11921176 { keyword: do', statements: doStatements } → do
11931177 let
1194− indentTrick = indent' <> indentation
1195−
11961178 indent /\ prefix = case lines of
1197− MultipleLines → indentTrick /\ (newline <> indentTrick)
1179+ MultipleLines → (indent' <> indentation) /\ (newline <> indent' <> indentation)
11981180 SingleLine → indent' /\ space
11991181 formatSourceToken settings indent' blank do'
12001182 <> foldMap
@@ -1216,21 +1198,40 @@ formatDoStatement settings@{ indentation } indent' doStatement' = case doStateme
12161198 <> formatExprPrefix (singleOrMultiline doStatement') settings indent' expr'
12171199 CST.DoDiscard expr' → do
12181200 formatExpr settings indent' expr'
1219− CST.DoLet let' letBindings → do
1220− let
1221− indent = indent' <> indentation
1222− formatSourceToken settings indent' blank let'
1223− <> foldMap
1224− (formatLetBinding settings indent (newline <> indent) newline)
1225− (NE.init letBindings)
1226− <> formatLetBinding
1227− settings
1228− indent
1229− (newline <> indent)
1230− blank
1231− (NE.last letBindings)
1201+ CST.DoLet let' letBindings'' → do
1202+ case lines of
1203+ MultipleLines →
1204+ formatSourceToken settings indent' blank let'
1205+ <> (newline <> indent)
1206+ <> (formatLetBindings settings indent) letBindings''
1207+ SingleLine →
1208+ formatSourceToken settings indent' blank let'
1209+ <> formatLetBinding settings space space blank (NE.head letBindings'')
1210+ where
1211+ indent = indent' <> indentation
1212+ lines = singleOrMultiline doStatement'
12321213 CST.DoError e → absurd e
12331214
1215+formatLetBindings ∷ Settings → Indent → NonEmptyArray (CST.LetBinding Void) → String
1216+formatLetBindings settings indent letBindings =
1217+ foldMapWithIndex
1218+ formatLetBindingWithFollowing
1219+ ( NE.zip letBindings
1220+ (NE.snoc' (Just <$> NE.tail letBindings) Nothing)
1221+ )
1222+ where
1223+ formatLetBindingWithFollowing idx (current /\ maybeFollowing) = do
1224+ formatLetBinding settings indent prefix suffix current
1225+ where
1226+ prefix = if idx == 0 then blank else (newline <> indent)
1227+ suffix = case maybeFollowing of
1228+ Nothing → blank -- no newline on last entry
1229+ Just followingLetBinding →
1230+ if isPrecededByBlankLines followingLetBinding then
1231+ newline
1232+ else
1233+ blank
1234+
12341235formatCaseOf ∷
12351236 Lines →
12361237 Settings →