From 89092d994cd4ea19db4f6a728a6b42b2ade10ad5 Mon Sep 17 00:00:00 2001
From: Lennart Spitzner <hexagoxel@hexagoxel.de>
Date: Thu, 20 Apr 2023 21:19:00 +0000
Subject: [PATCH] Minor comment updates and cleanup

---
 .../Brittany/Internal/ToBriDoc/Decl.hs        | 73 ++++++++++++++-----
 .../Brittany/Internal/ToBriDoc/Expr.hs        |  6 +-
 .../Brittany/Internal/ToBriDoc/OpTree.hs      |  4 +-
 .../Brittany/Internal/ToBriDoc/Stmt.hs        |  2 +-
 4 files changed, 61 insertions(+), 24 deletions(-)

diff --git a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs
index 7f177f0..28b8b02 100644
--- a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs
+++ b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Decl.hs
@@ -260,7 +260,7 @@ layoutPatternBind funId binderDoc lmatch@(L _ match) = do
       let t' = fixPatternBindIdentifier match t
       docLit t'
     _ -> pure Nothing
-  patDoc <- docHandleComms lmatch $ case (mIdDoc, patDocs) of
+  patDoc <- docHandleComms lmatch $ case (mIdDoc, patDocs) of -- TODO92 we use lmatch twice here!
     (Just idDoc, p1 : p2 : pr) | isInfix -> if null pr
       then docCols
         ColPatternsFuncInfix
@@ -360,8 +360,7 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
               , docSeparator
               , docForceSingleline $ return w
               ]
-        , -- docMoveToKWDP annKeyWhere AnnWhere False -- TODO92
-          docEnsureIndent whereIndent
+        , docEnsureIndent whereIndent
         $ docLines
             [ wrapBinds $ wrapWhere $ docLit $ Text.pack "where"
             , docEnsureIndent whereIndent
@@ -372,7 +371,6 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
         ]
       Just (wrapWhere, ws) ->
         fmap (pure . pure)
-          -- $ docMoveToKWDP annKeyWhere AnnWhere False -- TODO92
           $ docEnsureIndent whereIndent
           $ docLines
               [ wrapBinds $ wrapWhere $ docLit $ Text.pack "where"
@@ -420,8 +418,12 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
         [(grhsEpAnn, guards, body)] -> do
           let grhsHasComms = hasAnyCommentsBelow grhsEpAnn
           let guardPart = docHandleComms grhsEpAnn $ singleLineGuardsDoc guards
+          -- func x | null x = x + a + 2 where a = 1
+          -- or
+          -- func x | null x = x + a + b where
+          --   a = 1
+          --   b = 2
           forM_ wherePart $ \wherePart' ->
-            -- one-line solution
             addAlternativeCond (not hasComments && not grhsHasComms) $ docCols
               (ColBindingLine alignmentToken)
               [ docSeq (patPartInline ++ [guardPart])
@@ -431,7 +433,18 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
                 , wherePart'
                 ]
               ]
-          -- one-line solution + where in next line(s)
+
+          -- any below have this pattern:
+          -- …
+          --   where a = 1
+          -- or
+          -- …
+          --  where
+          --   a = 1
+          --   b = 1
+
+          -- func x | null x = do
+          --   stmt x
           addAlternativeCond (not $ maybe False startsWithComments grhsEpAnn)
             $ docLines
             $ [ docCols
@@ -450,7 +463,8 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
                   ]
               ]
             ++ wherePartMultiLine
-          -- two-line solution + where in next line(s)
+          -- func x | null x =
+          --   x + a + 2
           addAlternative
             $ docLines
             $ [ docForceSingleline
@@ -460,8 +474,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
               $ return body
               ]
             ++ wherePartMultiLine
-          -- pattern and exactly one clause in single line, body as par;
-          -- where in following lines
+          -- func x | null x
+          --   = do
+          --       stmt x
+          --       log "abc"
           addAlternative
             $ docLines
             $ [ docCols
@@ -480,7 +496,10 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
              --   , docAddBaseY BrIndentRegular $ return body
              --   ]
             ++ wherePartMultiLine
-          -- pattern and exactly one clause in single line, body in new line.
+          -- func x | null x =
+          --   do
+          --     stmt1
+          --     stmt2 x
           addAlternative
             $ docLines
             $ [ docSeq (patPartInline ++ [guardPart, return binderDoc])
@@ -496,8 +515,8 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
       case mPatDoc of
         Just patDoc | multipleClauses, indentPolicy == IndentPolicyFree ->
           -- multiple clauses added in-paragraph, each in a single line
-          -- example: foo | bar = baz
-          --              | lll = asd
+          -- func x | null x    = baz
+          --        | otherwise = asd
           addAlternative
             $ docLines
             $ [ docSeq
@@ -524,7 +543,9 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
               ]
             ++ wherePartMultiLine
         _ -> return ()
-      -- multiple clauses, each in a separate, single line
+      -- func x y
+      --   | null x, null y = a + b
+      --   | otherwise = a - b
       addAlternative
         $ docLines
         $ [ docAddBaseY BrIndentRegular
@@ -550,8 +571,12 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
                     ]
           ]
         ++ wherePartMultiLine
-      -- multiple clauses, each with the guard(s) in a single line, body
-      -- as a paragraph
+      -- func x y
+      --   | null x, null y = do
+      --       stmt x
+      --       stmt y
+      --   | otherwise -> do
+      --       abort
       addAlternativeCond (not hasComments)
         $ docLines
         $ [ docAddBaseY BrIndentRegular
@@ -572,8 +597,13 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
                 ]
           ]
         ++ wherePartMultiLine
-      -- multiple clauses, each with the guard(s) in a single line, body
-      -- in a new line as a paragraph
+      -- func x y
+      --   | null x, null y
+      --   = do
+      --     stmt x
+      --     stmt y
+      --   | otherwise
+      --   = abort
       addAlternative
         $ docLines
         $ [ docAddBaseY BrIndentRegular
@@ -593,7 +623,14 @@ layoutPatternBindFinal alignmentToken binderDoc mPatDoc clauses (wrapBinds, mWhe
               ]
           ]
         ++ wherePartMultiLine
-      -- conservative approach: everything starts on the left.
+      -- func x y
+      --   | null x
+      --   , null y
+      --   = do
+      --       stmt x
+      --       stmt y
+      --   | otherwise
+      --   = abort
       addAlternative
         $ docLines
         $ [ docAddBaseY BrIndentRegular
diff --git a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Expr.hs b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Expr.hs
index 1c2c60a..cd4f322 100644
--- a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Expr.hs
+++ b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Expr.hs
@@ -672,8 +672,7 @@ layoutExpr lexpr@(L _ expr) = do
             IndentPolicyLeft     -> docLines noHangingBinds
             IndentPolicyMultiple -> docLines noHangingBinds
             IndentPolicyFree     -> docLines
-              [ -- TODO92 docNodeAnnKW lexpr (Just AnnLet) $
-                docSeq
+              [ docSeq
                 [ appSep $ letDoc
                 , wrapBinds $ docSetBaseAndIndent $ docLines $ pure <$> bindDocs
                 ]
@@ -683,8 +682,7 @@ layoutExpr lexpr@(L _ expr) = do
                 ]
               ]
           addAlternative $ docLines
-            [ -- TODO92 docNodeAnnKW lexpr (Just AnnLet) $
-              docAddBaseY BrIndentRegular $ docPar
+            [ docAddBaseY BrIndentRegular $ docPar
               (letDoc)
               (wrapBinds $ docSetBaseAndIndent $ docLines $ pure <$> bindDocs)
             , docAddBaseY BrIndentRegular
diff --git a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/OpTree.hs b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/OpTree.hs
index 27f3087..0863aec 100644
--- a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/OpTree.hs
+++ b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/OpTree.hs
@@ -96,7 +96,9 @@ gatherOpTreeT hasParen hasComms commWrap locOpen locClose opExprList = \case
       commWrap
       locOpen
       locClose
-      ((docLit $ printRdrNameWithAnns op1, callLayouter layout_type r1) : opExprList)
+      ( (docLit $ printRdrNameWithAnns op1, callLayouter layout_type r1)
+      : opExprList
+      )
       l1
   (L _ (HsParTy epAnn inner)) -> do
     let AnnParen _ spanOpen spanClose = anns epAnn
diff --git a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Stmt.hs b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Stmt.hs
index 4083199..e5219b2 100644
--- a/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Stmt.hs
+++ b/source/library/Language/Haskell/Brittany/Internal/ToBriDoc/Stmt.hs
@@ -69,7 +69,7 @@ layoutStmt lstmt@(L _ stmt) = do
               in wrapBinds $ f $ return bindDoc
             ]
           , -- let
-              --   bind = expr
+            --   bind = expr
             docAddBaseY BrIndentRegular $ docPar
             (docLit $ Text.pack "let")
             (wrapBinds $ docSetBaseAndIndent $ return bindDoc)