git.iko.soy/rowtype-yoga/pose
on main help sign in
Better where and let
commit551c925564c349c6da57b6641d5221ba5482c7c6 authorMark Eibes <mark.eibes@gmail.com> dateSun 4 Jul 2021, 21:26 (5 years ago) parent791b214
6 files changed+68−40
packages/core/.prettierignore+3 packages/core/src/Format.purs+40−39 packages/core/testfiles/golden/DoAndIfThenElse.purs+9 packages/core/testfiles/golden/Let.purs+2−1 packages/core/testfiles/original/DoAndIfThenElse.purs+11 packages/core/testfiles/original/Let.purs+3
packages/core/.prettierignorenew file+3
@@ -0,0 +1,3 @@
1+testfiles
2+.spago
3+output
packages/core/src/Format.purs+40−39
@@ -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 →
packages/core/testfiles/golden/DoAndIfThenElse.purs+9
@@ -21,3 +21,12 @@ qux = do
2121 y <- pure 0
2222 pure (x + y)
2323 else do 1
24+
25+something = do
26+ let inline = 12
27+ let
28+ noSpaceAfterMe = "a"
29+ heinz = 12
30+
31+ keinz = 8
32+ pure 2
packages/core/testfiles/golden/Let.purs+2−1
@@ -6,8 +6,9 @@ foo =
66 let
77 x :: Int
88 x = 1
9−
109 y = 3
10+
11+ z = 8
1112 in
1213 x
1314
packages/core/testfiles/original/DoAndIfThenElse.purs+11
@@ -7,6 +7,7 @@ bar = do if true then 0 else
77
88baz = do if true then do 0 else do
99 x <- pure 1
10+
1011 y <- pure 0
1112 pure (x + y)
1213
@@ -15,3 +16,13 @@ qux = do if true then do
1516 y <- pure 0
1617 pure (x + y)
1718 else do 1
19+
20+something = do
21+ let inline = 12
22+ let
23+ noSpaceAfterMe = "a"
24+ heinz = 12
25+
26+
27+ keinz = 8
28+ pure 2
packages/core/testfiles/original/Let.purs+3
@@ -6,11 +6,14 @@ foo =
66 let x :: Int
77 x = 1
88 y = 3
9+
10+ z = 8
911 in x
1012
1113bar = do
1214 x <- pure 1
1315 let y = 3
16+
1417 z :: Int
1518 z = 4
1619 pure (x + y + z)