From 3708838b6a4a7618afbadb5182d81ff2fa9ad5e8 Mon Sep 17 00:00:00 2001
From: sniperrifle2004 <sniperrifle2004+github@gmail.com>
Date: Fri, 22 Dec 2017 06:58:39 +0100
Subject: [PATCH] Also handle comments inside ThingWith

---
 src-literatetests/10-tests.blt                | 22 ++++++++++++-
 src-literatetests/tests-context-free.blt      | 22 +++++++++++++
 .../Haskell/Brittany/Internal/Layouters/IE.hs | 32 +++++++++++++++----
 3 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/src-literatetests/10-tests.blt b/src-literatetests/10-tests.blt
index 758efe0..7164f77 100644
--- a/src-literatetests/10-tests.blt
+++ b/src-literatetests/10-tests.blt
@@ -765,7 +765,27 @@ import           Test                                     ( -- comment
 
 #test long-bindings
 import           Test                                     ( longbindingNameThatoverflowsColum )
-import           Test                                     ( Long(List, Of, Things) )
+import           Test                                     ( Long( List
+                                                                , Of
+                                                                , Things
+                                                                ) )
+
+#test things-with-with-comments
+import           Test                                     ( Thing( -- Comments
+                                                                 )
+                                                          )
+import           Test                                     ( Thing( Item
+                                                                 -- and Comment
+                                                                 )
+                                                          )
+import           Test                                     ( Thing( With
+                                                                 -- Comments
+                                                                 , and
+                                                                 -- also
+                                                                 , items
+                                                                 -- !
+                                                                 )
+                                                          )
 
 #test preamble full-preamble
 {-# LANGUAGE BangPatterns #-}
diff --git a/src-literatetests/tests-context-free.blt b/src-literatetests/tests-context-free.blt
index a48890a..2795da8 100644
--- a/src-literatetests/tests-context-free.blt
+++ b/src-literatetests/tests-context-free.blt
@@ -771,6 +771,28 @@ import Test ( -- comment
 import Test (longbindingNameThatoverflowsColum)
 import Test (Long(List, Of, Things))
 
+#test things-with-with-comments
+import Test ( Thing( With
+                   -- Comments
+                   , and
+                   -- also
+                   , items
+                   -- !
+                   )
+            )
+import Test ( Thing( Item
+                   -- and Comment
+                   )
+            )
+import Test ( Thing( With
+                   -- Comments
+                   , and
+                   -- also
+                   , items
+                   -- !
+                   )
+            )
+
 #test preamble full-preamble
 {-# LANGUAGE BangPatterns #-}
 
diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs b/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
index 262108e..85a4ef8 100644
--- a/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
+++ b/src/Language/Haskell/Brittany/Internal/Layouters/IE.hs
@@ -44,14 +44,32 @@ layoutIE lie@(L _ ie) = docWrapNode lie $ case ie of
   IEThingAbs _                     -> ien
   IEThingAll _                     -> docSeq [ien, docLit $ Text.pack "(..)"]
   IEThingWith _ (IEWildcard _) _ _ -> docSeq [ien, docLit $ Text.pack "(..)"]
-  IEThingWith _ _ ns fs ->
-    docSeq
-      $  [ien, docLit $ Text.pack "("]
-      ++ intersperse docCommaSep (map nameDoc ns ++ map prepareFL fs)
-      ++ [docLit $ Text.pack ")"]
+  IEThingWith _ _ ns _ -> do
+    hasComments <- hasAnyCommentsBelow lie
+    docAltFilter
+      [(not hasComments, docSeq $  [ien, docLit $ Text.pack "("]
+        ++ intersperse docCommaSep (map nameDoc ns)
+        ++ [docParenR])
+      ,(otherwise, docSeq [ien, layoutItems (splitFirstLast ns)])
+      ]
    where
     nameDoc = (docLit =<<) . lrdrNameToTextAnn . prepareName
-    prepareFL = docLit . Text.pack . FastString.unpackFS . flLabel . unLoc
+    layoutItem n = docSeq [docCommaSep, docWrapNode n $ nameDoc n]
+    layoutItems FirstLastEmpty =
+      docSetBaseY $
+        docLines [docSeq [docParenLSep, docWrapNodeRest lie docEmpty]
+                 ,docParenR
+                 ]
+    layoutItems (FirstLastSingleton n) =
+      docSetBaseY $ docLines
+        [docSeq [docParenLSep, docWrapNodeRest lie $ nameDoc n], docParenR]
+    layoutItems (FirstLast n1 nMs nN)  =
+      docSetBaseY $ docLines $
+         [docSeq [docParenLSep, docWrapNode n1 $ nameDoc n1]]
+         ++ map layoutItem nMs
+         ++ [ docSeq [docCommaSep, docWrapNodeRest lie $ nameDoc nN]
+            , docParenR
+            ]
   IEModuleContents n -> docSeq
     [ docLit $ Text.pack "module"
     , docSeparator
@@ -101,7 +119,7 @@ layoutLLIEs llies = do
     [] -> docAltFilter
             [ (not hasComments, docLit $ Text.pack "()")
             , (otherwise, docPar (docSeq [docParenLSep, docWrapNode llies docEmpty])
-                       $ docLines [docParenR])
+                       docParenR)
             ]
     (ieDsH:ieDsT) ->
       docAltFilter