@@ -25,12 +25,18 @@ step' indentSize ls (module', allComments) = applyChanges changes ls
25
25
datas' = datas $ fmap linesFromSrcSpan module'
26
26
changes = datas' >>= maybeToList . changeDecl allComments indentSize
27
27
28
- findComment :: LineBlock -> [Comment ] -> Maybe Comment
29
- findComment lb = find commentOnLine
28
+ findCommentOnLine :: LineBlock -> [Comment ] -> Maybe Comment
29
+ findCommentOnLine lb = find commentOnLine
30
30
where
31
31
commentOnLine (Comment _ (H. SrcSpan _ start _ end _) _) =
32
32
blockStart lb == start && blockEnd lb == end
33
33
34
+ findCommentBelowLine :: LineBlock -> [Comment ] -> Maybe Comment
35
+ findCommentBelowLine lb = find commentOnLine
36
+ where
37
+ commentOnLine (Comment _ (H. SrcSpan _ start _ end _) _) =
38
+ blockStart lb == start - 1 && blockEnd lb == end - 1
39
+
34
40
commentsWithin :: LineBlock -> [Comment ] -> [Comment ]
35
41
commentsWithin lb = filter within
36
42
where
@@ -39,9 +45,8 @@ commentsWithin lb = filter within
39
45
40
46
changeDecl :: [Comment ] -> Int -> H. Decl LineBlock -> Maybe ChangeLine
41
47
changeDecl _ _ (H. DataDecl _ (H. DataType _) Nothing _ [] _) = Nothing
42
- changeDecl allComments indentSize (H. DataDecl block (H. DataType _) Nothing dhead decls derivings)
43
- | null $ commentsWithin block allComments = Just $ change block (const $ concat newLines)
44
- | otherwise = Nothing
48
+ changeDecl allComments indentSize (H. DataDecl block (H. DataType _) Nothing dhead decls derivings) =
49
+ Just $ change block (const $ concat newLines)
45
50
where
46
51
newLines = fmap constructors zipped ++ [fmap (indented . H. prettyPrint) derivings]
47
52
zipped = zip decls ([1 .. ] :: [Int ])
@@ -53,14 +58,17 @@ changeDecl _ _ _ = Nothing
53
58
54
59
processConstructor :: [Comment ] -> String -> Int -> H. QualConDecl LineBlock -> [String ]
55
60
processConstructor allComments init indentSize (H. QualConDecl _ _ _ (H. RecDecl _ dname fields)) = do
56
- init <> H. prettyPrint dname : n1 : ns ++ [indented " }" ]
61
+ init <> H. prettyPrint dname : n1 ++ ns ++ [indented " }" ]
57
62
where
58
63
n1 = processName " { " ( extractField $ head fields)
59
- ns = fmap (processName " , " . extractField) (tail fields)
60
- processName prefix (fnames, _type, Nothing ) =
61
- indented prefix <> intercalate " , " (fmap H. prettyPrint fnames) <> " :: " <> H. prettyPrint _type
62
- processName prefix (fnames, _type, (Just (Comment _ _ c))) =
63
- indented prefix <> intercalate " , " (fmap H. prettyPrint fnames) <> " :: " <> H. prettyPrint _type <> " --" <> c
64
- extractField (H. FieldDecl lb names _type) = (names, _type, findComment lb allComments)
64
+ ns = tail fields >>= (processName " , " . extractField)
65
+ processName prefix (fnames, _type, lineComment, commentBelowLine) =
66
+ [indented prefix <> intercalate " , " (fmap H. prettyPrint fnames) <> " :: " <> H. prettyPrint _type <> addLineComment lineComment] ++ addCommentBelow commentBelowLine
67
+ addLineComment (Just (Comment _ _ c)) = " --" <> c
68
+ addLineComment Nothing = " "
69
+ addCommentBelow Nothing = []
70
+ addCommentBelow (Just (Comment _ _ c)) = [indented " --" <> c]
71
+ extractField (H. FieldDecl lb names _type) =
72
+ (names, _type, findCommentOnLine lb allComments, findCommentBelowLine lb allComments)
65
73
indented = indent indentSize
66
74
processConstructor _ init _ decl = [init <> trimLeft (H. prettyPrint decl)]
0 commit comments