From 20f9c009ee18e18ca5c4d4db440f4e565757cbc4 Mon Sep 17 00:00:00 2001
From: Lennart Spitzner <hexagoxel@hexagoxel.de>
Date: Sun, 11 Mar 2018 22:42:47 +0100
Subject: [PATCH] Stop hanging indent for IEThingWith

plus minor refactors/cleanups

this is more in line with IndentPolicyLeft and imo also looks
nicer in general
---
 src-literatetests/10-tests.blt                | 40 +++++++++-------
 src-literatetests/tests-context-free.blt      | 37 +++++++-------
 .../Haskell/Brittany/Internal/Layouters/IE.hs | 48 ++++++++++++-------
 .../Brittany/Internal/Layouters/Import.hs     | 22 +++++++--
 4 files changed, 90 insertions(+), 57 deletions(-)

diff --git a/src-literatetests/10-tests.blt b/src-literatetests/10-tests.blt
index 3a5941c..802a6fc 100644
--- a/src-literatetests/10-tests.blt
+++ b/src-literatetests/10-tests.blt
@@ -787,27 +787,33 @@ import           Test                                     ( -- comment
                                                           )
 
 #test long-bindings
-import           Test                                     ( longbindingNameThatoverflowsColum )
-import           Test                                     ( Long( List
-                                                                , Of
-                                                                , Things
-                                                                ) )
+import           Test                                     ( longbindingNameThatoverflowsColum
+                                                          )
+import           Test                                     ( Long
+                                                            ( List
+                                                            , Of
+                                                            , Things
+                                                            )
+                                                          )
 
 #test things-with-with-comments
-import           Test                                     ( Thing( -- Comments
-                                                                 )
+import           Test                                     ( Thing
+                                                            ( -- Comments
+                                                            )
                                                           )
-import           Test                                     ( Thing( Item
-                                                                 -- and Comment
-                                                                 )
+import           Test                                     ( Thing
+                                                            ( Item
+                                                            -- and Comment
+                                                            )
                                                           )
-import           Test                                     ( Thing( With
-                                                                 -- Comments
-                                                                 , and
-                                                                 -- also
-                                                                 , items
-                                                                 -- !
-                                                                 )
+import           Test                                     ( Thing
+                                                            ( With
+                                                            -- Comments
+                                                            , and
+                                                            -- also
+                                                            , items
+                                                            -- !
+                                                            )
                                                           )
 #test prefer-dense-empty-list
 import           VeryLongModuleNameThatCouldEvenCauseAnEmptyBindingListToExpandIntoMultipleLine
diff --git a/src-literatetests/tests-context-free.blt b/src-literatetests/tests-context-free.blt
index d1a27b3..8ab4d7e 100644
--- a/src-literatetests/tests-context-free.blt
+++ b/src-literatetests/tests-context-free.blt
@@ -792,27 +792,30 @@ import Test (Long(List, Of, Things))
 
 #test things-with-with-comments
 import Test
-  ( Thing( With
-         -- Comments
-         , and
-         -- also
-         , items
-         -- !
-         )
+  ( Thing
+    ( With
+    -- Comments
+    , and
+    -- also
+    , items
+    -- !
+    )
   )
 import Test
-  ( Thing( Item
-         -- and Comment
-         )
+  ( Thing
+    ( Item
+    -- and Comment
+    )
   )
 import Test
-  ( Thing( With
-         -- Comments
-         , and
-         -- also
-         , items
-         -- !
-         )
+  ( Thing
+    ( With
+    -- Comments
+    , and
+    -- also
+    , items
+    -- !
+    )
   )
 
 #test prefer-dense-empty-list
diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs b/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
index ebf9b36..126d519 100644
--- a/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
+++ b/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
@@ -47,10 +47,16 @@ layoutIE lie@(L _ ie) = docWrapNode lie $ case ie of
   IEThingWith _ _ ns _ -> do
     hasComments <- hasAnyCommentsBelow lie
     docAltFilter
-      [(not hasComments, docSeq $  [ien, docLit $ Text.pack "("]
+      [ ( not hasComments
+        , docSeq
+        $  [ien, docLit $ Text.pack "("]
         ++ intersperse docCommaSep (map nameDoc ns)
-        ++ [docParenR])
-      ,(otherwise, docSeq [ien, layoutItems (splitFirstLast ns)])
+        ++ [docParenR]
+        )
+      , (otherwise
+        , docAddBaseY BrIndentRegular
+        $ docPar ien (layoutItems (splitFirstLast ns))
+        )
       ]
    where
     nameDoc = (docLit =<<) . lrdrNameToTextAnn . prepareName
@@ -113,21 +119,27 @@ layoutAnnAndSepLLIEs llies@(L _ lies) = do
 -- )
 layoutLLIEs :: Located [LIE RdrName] -> ToBriDocM BriDocNumbered
 layoutLLIEs llies = do
-  ieDs <- layoutAnnAndSepLLIEs llies
+  ieDs        <- layoutAnnAndSepLLIEs llies
   hasComments <- hasAnyCommentsBelow llies
   case ieDs of
     [] -> docAltFilter
-            [ (not hasComments, docLit $ Text.pack "()")
-            , ( hasComments
-              , docPar
-                  (docSeq [docParenLSep, docWrapNodeRest llies docEmpty])
-                  docParenR
-              )
-            ]
-    (ieDsH:ieDsT) ->
-      docAltFilter
-        [ (not hasComments, docSeq $ docLit (Text.pack "("):ieDs ++ [docParenR])
-        , (otherwise, docPar (docSetBaseY $ docSeq [docParenLSep, ieDsH]) $
-            docLines $ ieDsT
-            ++ [docParenR])
-        ]
+      [ (not hasComments, docLit $ Text.pack "()")
+      , ( hasComments
+        , docPar (docSeq [docParenLSep, docWrapNodeRest llies docEmpty])
+                 docParenR
+        )
+      ]
+    (ieDsH:ieDsT) -> docAltFilter
+      [ ( not hasComments
+        , docSeq
+        $  [docLit (Text.pack "(")]
+        ++ (docForceSingleline <$> ieDs)
+        ++ [docParenR]
+        )
+      , ( otherwise
+        , docPar (docSetBaseY $ docSeq [docParenLSep, ieDsH])
+        $  docLines
+        $  ieDsT
+        ++ [docParenR]
+        )
+      ]
diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
index 5fa05a2..613f2d2 100644
--- a/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
+++ b/src/Language/Haskell/Brittany/Internal/Layouters/Import.hs
@@ -95,9 +95,22 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
                   docParenR
                 else docSeq [hidDoc, docParenLSep, docSeparator, docParenR]
                 -- ..[hiding].( b )
-              [ieD] -> if hasComments
-                then docPar (docSeq [hidDoc, docParenLSep, ieD]) docParenR
-                else docSeq [hidDoc, docParenLSep, ieD, docSeparator, docParenR]
+              [ieD] -> docAltFilter
+                [ ( not hasComments
+                  , docSeq
+                    [ hidDoc
+                    , docParenLSep
+                    , docForceSingleline $ ieD
+                    , docSeparator
+                    , docParenR
+                    ]
+                  )
+                , ( otherwise
+                  , docPar
+                    (docSeq [hidDoc, docParenLSep, docNonBottomSpacing ieD])
+                    docParenR
+                  )
+                ]
                 -- ..[hiding].( b
                 --            , b'
                 --            )
@@ -114,8 +127,7 @@ layoutImport limportD@(L _ importD) = docWrapNode limportD $ case importD of
     then
       let asDoc = maybe docEmpty makeAsDoc masT
       in docAlt
-        [ docForceSingleline $
-            docSeq [importHead, asDoc, docSetBaseY $ bindingsD]
+        [ docForceSingleline $ docSeq [importHead, asDoc, bindingsD]
         , docAddBaseY BrIndentRegular $
             docPar (docSeq [importHead, asDoc]) bindingsD
         ]