Fix list comprehension comment placement issue

pull/3/head
Lennart Spitzner 2016-08-18 00:59:12 +02:00
parent b58a8b0146
commit e67855fd98
3 changed files with 32 additions and 6 deletions

View File

@ -639,3 +639,12 @@ regressionTests = do
utargets'' = "foo" utargets'' = "foo"
return () return ()
|] |]
it "list comprehension comment placement" $ do
roundTripEqual $
[text|
func =
[ (thing, take 10 alts) --TODO: select best ones
| (thing, _got, alts@(_:_)) <- nosuchFooThing
, gast <- award
]
|]

View File

@ -43,6 +43,7 @@ module Language.Haskell.Brittany.LayoutBasics
, docEmpty , docEmpty
, docLit , docLit
, docAlt , docAlt
, docAltFilter
, docLines , docLines
, docCols , docCols
, docSeq , docSeq
@ -910,6 +911,9 @@ docExt x anns shouldAddComment = allocateNode $ BDFExternal
docAlt :: [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered docAlt :: [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docAlt l = allocateNode . BDFAlt =<< sequence l docAlt l = allocateNode . BDFAlt =<< sequence l
docAltFilter :: [(Bool, ToBriDocM BriDocNumbered)] -> ToBriDocM BriDocNumbered
docAltFilter = docAlt . map snd . filter fst
docSeq :: [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered docSeq :: [ToBriDocM BriDocNumbered] -> ToBriDocM BriDocNumbered
docSeq l = allocateNode . BDFSeq =<< sequence l docSeq l = allocateNode . BDFSeq =<< sequence l

View File

@ -424,18 +424,31 @@ layoutExpr lexpr@(L _ expr) = docWrapNode lexpr $ case expr of
; MonadComp -> True ; MonadComp -> True
; _ -> False } -> do ; _ -> False } -> do
stmtDocs <- docSharedWrapper layoutStmt `mapM` stmts stmtDocs <- docSharedWrapper layoutStmt `mapM` stmts
docAlt hasComments <- hasAnyCommentsBelow lexpr
[ docSeq docAltFilter
[ appSep $ docLit $ Text.pack "[" [ (,) (not hasComments)
, appSep $ docForceSingleline $ List.last stmtDocs $ docSeq
[ docNodeAnnKW lexpr Nothing
$ appSep
$ docLit
$ Text.pack "["
, docNodeAnnKW lexpr (Just AnnOpenS)
$ appSep
$ docForceSingleline
$ List.last stmtDocs
, appSep $ docLit $ Text.pack "|" , appSep $ docLit $ Text.pack "|"
, docSeq $ List.intersperse docCommaSep , docSeq $ List.intersperse docCommaSep
$ fmap docForceSingleline $ List.init stmtDocs $ fmap docForceSingleline $ List.init stmtDocs
, docLit $ Text.pack " ]" , docLit $ Text.pack " ]"
] ]
, let , (,) True
$ let
start = docCols ColListComp start = docCols ColListComp
[appSep $ docLit $ Text.pack "[", List.last stmtDocs] [ docNodeAnnKW lexpr Nothing
$ appSep $ docLit $ Text.pack "["
, docNodeAnnKW lexpr (Just AnnOpenS)
$ List.last stmtDocs
]
(s1:sM) = List.init stmtDocs (s1:sM) = List.init stmtDocs
line1 = docCols ColListComp line1 = docCols ColListComp
[appSep $ docLit $ Text.pack "|", s1] [appSep $ docLit $ Text.pack "|", s1]