From d1736bd9df1554020aa2a5b1175ef70d9658cda5 Mon Sep 17 00:00:00 2001
From: Patrick Augusto <ptkato.irl@gmail.com>
Date: Sat, 23 Oct 2021 16:39:25 -0300
Subject: [PATCH] Make cabal init conform with --no-comments flag for commented
 fields

---
 .../src/Distribution/Client/Init/Format.hs    | 26 +++++++++----------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/cabal-install/src/Distribution/Client/Init/Format.hs b/cabal-install/src/Distribution/Client/Init/Format.hs
index 18b3cae5db..a442f5303f 100644
--- a/cabal-install/src/Distribution/Client/Init/Format.hs
+++ b/cabal-install/src/Distribution/Client/Init/Format.hs
@@ -64,18 +64,9 @@ fieldD
     -> WriteOpts
     -> PrettyField FieldAnnotation
 fieldD fieldName fieldContents fieldComments includeField opts
-    | fieldContents == empty =
-      -- If there is no content, optionally produce a commented out field.
-      fieldSEmptyContents $ CommentBefore fieldComments
-    | otherwise =
-        -- If the "--no-comments" or "--minimal" flag is set, strip comments.
-        let comments
-              | isMinimal = []
-              | hasNoComments = []
-              | otherwise = fieldComments
-
-        -- If the "--minimal" flag is set, strip comments.
-        in fieldSWithContents comments
+      -- If the "--no-comments" or "--minimal" flag is set, strip comments.
+    | hasNoComments || isMinimal = contents NoComment
+    | otherwise                  = contents $ commentPositionFor fieldName fieldComments
   where
     commentPositionFor fn
       | fn == "cabal-version" = CommentAfter
@@ -84,6 +75,11 @@ fieldD fieldName fieldContents fieldComments includeField opts
     isMinimal = _optMinimal opts
     hasNoComments = _optNoComments opts
 
+    contents
+        -- If there is no content, optionally produce a commented out field.
+      | fieldContents == empty = fieldSEmptyContents
+      | otherwise              = fieldSWithContents
+
     fieldSEmptyContents cs
       | not includeField || isMinimal = PrettyEmpty
       | otherwise = PrettyField
@@ -92,7 +88,7 @@ fieldD fieldName fieldContents fieldComments includeField opts
         empty
 
     fieldSWithContents cs =
-      PrettyField (withComments . commentPositionFor fieldName $ map ("-- " ++) cs) fieldName fieldContents
+      PrettyField (withComments cs) fieldName fieldContents
 
 
 -- | A field annotation instructing the pretty printer to comment out the field
@@ -104,7 +100,9 @@ commentedOutWithComments NoComment = FieldAnnotation True NoComment
 
 -- | A field annotation with the specified comment lines.
 withComments :: CommentPosition -> FieldAnnotation
-withComments = FieldAnnotation False
+withComments (CommentBefore cs) = FieldAnnotation False . CommentBefore $ map ("-- " ++) cs
+withComments (CommentAfter  cs) = FieldAnnotation False . CommentAfter  $ map ("-- " ++) cs
+withComments NoComment = FieldAnnotation False NoComment
 
 -- | A field annotation with no comments.
 annNoComments :: FieldAnnotation
-- 
GitLab