From 4818566c8333afd67ba7fc7f5c0e1f73922b74b0 Mon Sep 17 00:00:00 2001
From: Lennart Spitzner <hexagoxel@hexagoxel.de>
Date: Sun, 28 May 2023 15:47:12 +0200
Subject: [PATCH] Fix end-of-decl comment spacing issue

---
 data/15-regressions.blt                       |  6 ++++++
 .../Brittany/Internal/StepOrchestrate.hs      | 19 +++++++++++--------
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/data/15-regressions.blt b/data/15-regressions.blt
index 4dacfeb..bf6f340 100644
--- a/data/15-regressions.blt
+++ b/data/15-regressions.blt
@@ -1066,3 +1066,9 @@ func = func (abc) (def)
 #expected
 -- brittany { lconfig_operatorParenthesisRefactorMode: PRMMinimize }
 func = func abc def
+
+#test wandering comment at end of datadecl
+data ReformatParenMode
+  = ReformatParenModeKeep  -- don't modify parens at all
+  | ReformatParenModeClean -- remove unnecessary parens
+  | ReformatParenModeAll   -- add superfluous parens everywhere
diff --git a/source/library/Language/Haskell/Brittany/Internal/StepOrchestrate.hs b/source/library/Language/Haskell/Brittany/Internal/StepOrchestrate.hs
index ebc3fef..df2ddb1 100644
--- a/source/library/Language/Haskell/Brittany/Internal/StepOrchestrate.hs
+++ b/source/library/Language/Haskell/Brittany/Internal/StepOrchestrate.hs
@@ -184,15 +184,18 @@ processModule traceFunc conf inlineConf moduleElems = do
 
 commentToDoc :: (Int, EpaCommentTok) -> ToBriDocM BriDocNumbered
 commentToDoc (indent, c) = case c of
-  GHC.EpaDocCommentNext  str -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaDocCommentPrev  str -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaDocCommentNamed str -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaDocSection _ str    -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaDocOptions   str    -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaLineComment  str    -> docLitS (replicate indent ' ' ++ str)
-  GHC.EpaBlockComment str    -> docLitS (replicate indent ' ' ++ str)
+  GHC.EpaDocCommentNext  str -> handle str
+  GHC.EpaDocCommentPrev  str -> handle str
+  GHC.EpaDocCommentNamed str -> handle str
+  GHC.EpaDocSection _ str    -> handle str
+  GHC.EpaDocOptions   str    -> handle str
+  GHC.EpaLineComment  str    -> handle str
+  GHC.EpaBlockComment str    -> handle str
   GHC.EpaEofComment          -> docEmpty
-
+ where
+  handle str = if indent == 0
+    then docLitS str
+    else docSeq [docSeparator, docLitS $ (replicate (indent - 1) ' ') ++ str ]
 
 -- Prints the information associated with the module annotation
 -- This includes the imports