Skip to content

Commit 5eb4902

Browse files
Fix records with comments (#257)
* Format records where comments are in the same line as the field name * Fix records format, records with comments will now be formatted * Fix formatting of comments below Co-authored-by: Łukasz Gołębiewski <[email protected]>
1 parent 8065c3c commit 5eb4902

File tree

2 files changed

+64
-33
lines changed
  • lib/Language/Haskell/Stylish/Step
  • tests/Language/Haskell/Stylish/Step/Data

2 files changed

+64
-33
lines changed

lib/Language/Haskell/Stylish/Step/Data.hs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ step' indentSize ls (module', allComments) = applyChanges changes ls
2525
datas' = datas $ fmap linesFromSrcSpan module'
2626
changes = datas' >>= maybeToList . changeDecl allComments indentSize
2727

28-
findComment :: LineBlock -> [Comment] -> Maybe Comment
29-
findComment lb = find commentOnLine
28+
findCommentOnLine :: LineBlock -> [Comment] -> Maybe Comment
29+
findCommentOnLine lb = find commentOnLine
3030
where
3131
commentOnLine (Comment _ (H.SrcSpan _ start _ end _) _) =
3232
blockStart lb == start && blockEnd lb == end
3333

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+
3440
commentsWithin :: LineBlock -> [Comment] -> [Comment]
3541
commentsWithin lb = filter within
3642
where
@@ -39,9 +45,8 @@ commentsWithin lb = filter within
3945

4046
changeDecl :: [Comment] -> Int -> H.Decl LineBlock -> Maybe ChangeLine
4147
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)
4550
where
4651
newLines = fmap constructors zipped ++ [fmap (indented . H.prettyPrint) derivings]
4752
zipped = zip decls ([1..] ::[Int])
@@ -53,14 +58,17 @@ changeDecl _ _ _ = Nothing
5358

5459
processConstructor :: [Comment] -> String -> Int -> H.QualConDecl LineBlock -> [String]
5560
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 "}"]
5762
where
5863
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)
6573
indented = indent indentSize
6674
processConstructor _ init _ decl = [init <> trimLeft (H.prettyPrint decl)]

tests/Language/Haskell/Stylish/Step/Data/Tests.hs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests"
2929
, testCase "case 16" case16
3030
, testCase "case 17" case17
3131
, testCase "case 18" case18
32+
, testCase "case 19" case19
3233
]
3334

3435
case00 :: Assertion
@@ -287,6 +288,26 @@ case14 = expected @=? testStep (step 2) input
287288

288289
case15 :: Assertion
289290
case15 = expected @=? testStep (step 2) input
291+
where
292+
input = unlines
293+
[ "module Herp where"
294+
, ""
295+
, "data Foo a = Foo"
296+
, " { a :: a, -- comment"
297+
, " a2 :: String"
298+
, " }"
299+
]
300+
expected = unlines
301+
[ "module Herp where"
302+
, ""
303+
, "data Foo a = Foo"
304+
, " { a :: a -- comment"
305+
, " , a2 :: String"
306+
, " }"
307+
]
308+
309+
case16 :: Assertion
310+
case16 = expected @=? testStep (step 2) input
290311
where
291312
input = unlines
292313
[ "module Herp where"
@@ -298,71 +319,73 @@ case15 = expected @=? testStep (step 2) input
298319
expected = unlines
299320
[ "module Herp where"
300321
, ""
301-
, "data Foo = Foo {"
302-
, " a :: Int -- ^ comment"
322+
, "data Foo = Foo"
323+
, " { a :: Int -- ^ comment"
303324
, " }"
304325
]
305326

306-
case16 :: Assertion
307-
case16 = expected @=? testStep (step 2) input
327+
case17 :: Assertion
328+
case17 = expected @=? testStep (step 2) input
308329
where
309330
input = unlines
310331
[ "module Herp where"
311332
, ""
312333
, "data Foo a = Foo"
313334
, " { a :: a,"
314-
, "-- ^ comment"
335+
, "-- comment"
315336
, " a2 :: String"
316337
, " }"
317338
]
318339
expected = unlines
319340
[ "module Herp where"
320341
, ""
321342
, "data Foo a = Foo"
322-
, " { a :: a,"
323-
, "-- ^ comment"
324-
, " a2 :: String"
343+
, " { a :: a"
344+
, " -- comment"
345+
, " , a2 :: String"
325346
, " }"
326347
]
327348

328-
case17 :: Assertion
329-
case17 = expected @=? testStep (step 2) input
349+
case18 :: Assertion
350+
case18 = expected @=? testStep (step 2) input
330351
where
331352
input = unlines
332353
[ "module Herp where"
333354
, ""
334355
, "data Foo a = Foo"
335-
, " { a :: a, -- comment"
356+
, " { a :: a,"
357+
, "-- ^ comment"
336358
, " a2 :: String"
337359
, " }"
338360
]
339361
expected = unlines
340362
[ "module Herp where"
341363
, ""
342364
, "data Foo a = Foo"
343-
, " { a :: a, -- comment"
344-
, " a2 :: String"
365+
, " { a :: a"
366+
, " -- ^ comment"
367+
, " , a2 :: String"
345368
, " }"
346369
]
347370

348-
case18 :: Assertion
349-
case18 = expected @=? testStep (step 2) input
371+
case19 :: Assertion
372+
case19 = expected @=? testStep (step 2) input
350373
where
351374
input = unlines
352375
[ "module Herp where"
353376
, ""
354377
, "data Foo a = Foo"
355-
, " { a :: a,"
356-
, "-- comment "
357-
, " a2 :: String"
378+
, " { firstName, lastName :: String,"
379+
, "-- ^ names"
380+
, " age :: Int"
358381
, " }"
359382
]
360383
expected = unlines
361384
[ "module Herp where"
362385
, ""
363386
, "data Foo a = Foo"
364-
, " { a :: a,"
365-
, "-- comment "
366-
, " a2 :: String"
387+
, " { firstName, lastName :: String"
388+
, " -- ^ names"
389+
, " , age :: Int"
367390
, " }"
368391
]

0 commit comments

Comments
 (0)