From 281d7a2f81698b265ce7ff8d559af2eeea8df484 Mon Sep 17 00:00:00 2001 From: Sergey Vinokurov Date: Tue, 18 Sep 2018 00:25:18 +0100 Subject: [PATCH] Lay out unboxed tuples with spaces This avoids clashes with names like foo# --- src-literatetests/15-regressions.blt | 10 +++++++ .../Brittany/Internal/Layouters/Expr.hs | 7 +++-- .../Brittany/Internal/Layouters/Pattern.hs | 30 +++++++++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src-literatetests/15-regressions.blt b/src-literatetests/15-regressions.blt index 080c15e..cb4ac5d 100644 --- a/src-literatetests/15-regressions.blt +++ b/src-literatetests/15-regressions.blt @@ -650,3 +650,13 @@ jaicyhHumzo btrKpeyiFej mava = do ) Xcde{} -> (s, Pioemav) pure imomue + +#test unboxed-tuple and vanilla names +{-# LANGUAGE UnboxedTuples #-} +spanKey = case foo of + (# bar, baz #) -> (# baz, bar #) + +#test unboxed-tuple and hashed name +{-# LANGUAGE MagicHash, UnboxedTuples #-} +spanKey = case foo of + (# bar#, baz# #) -> (# baz# +# bar#, bar# #) diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs index 92bcceb..96b739c 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Expr.hs @@ -371,8 +371,11 @@ layoutExpr lexpr@(L _ expr) = do $ \(arg, exprM) -> docWrapNode arg $ maybe docEmpty layoutExpr exprM hasComments <- hasAnyCommentsBelow lexpr let (openLit, closeLit) = case boxity of - Boxed -> (docLit $ Text.pack "(", docLit $ Text.pack ")") - Unboxed -> (docLit $ Text.pack "(#", docLit $ Text.pack "#)") + Boxed -> (docLit $ Text.pack "(", docLit $ Text.pack ")") + Unboxed -> + ( docSeq [docLit $ Text.pack "(#", docSeparator] + , docSeq [docSeparator, docLit $ Text.pack "#)"] + ) case splitFirstLast argDocs of FirstLastEmpty -> docSeq [ openLit diff --git a/src/Language/Haskell/Brittany/Internal/Layouters/Pattern.hs b/src/Language/Haskell/Brittany/Internal/Layouters/Pattern.hs index c65b357..6c95e0b 100644 --- a/src/Language/Haskell/Brittany/Internal/Layouters/Pattern.hs +++ b/src/Language/Haskell/Brittany/Internal/Layouters/Pattern.hs @@ -140,8 +140,8 @@ layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of -- (nestedpat1, nestedpat2, nestedpat3) -> expr -- (#nestedpat1, nestedpat2, nestedpat3#) -> expr case boxity of - Boxed -> wrapPatListy args "(" ")" - Unboxed -> wrapPatListy args "(#" "#)" + Boxed -> wrapPatListy args "(" ")" False + Unboxed -> wrapPatListy args "(#" "#)" True AsPat asName asPat -> do -- bind@nestedpat -> expr wrapPatPrepend asPat (docLit $ lrdrNameToText asName <> Text.pack "@") @@ -172,7 +172,7 @@ layoutPat lpat@(L _ pat) = docWrapNode lpat $ case pat of ListPat elems _ _ -> -- [] -> expr1 -- [nestedpat1, nestedpat2, nestedpat3] -> expr2 - wrapPatListy elems "[" "]" + wrapPatListy elems "[" "]" False BangPat pat1 -> do -- !nestedpat -> expr wrapPatPrepend pat1 (docLit $ Text.pack "!") @@ -213,17 +213,23 @@ wrapPatListy :: [Located (Pat GhcPs)] -> String -> String + -> Bool -> ToBriDocM (Seq BriDocNumbered) -wrapPatListy elems start end = do +wrapPatListy elems start end padSeparators = do elemDocs <- Seq.fromList elems `forM` (layoutPat >=> colsWrapPat) - sDoc <- docLit $ Text.pack start - eDoc <- docLit $ Text.pack end case Seq.viewl elemDocs of Seq.EmptyL -> fmap Seq.singleton $ docLit $ Text.pack $ start ++ end x1 Seq.:< rest -> do - rest' <- rest `forM` \bd -> docSeq - [ docLit $ Text.pack "," - , docSeparator - , return bd - ] - return $ (sDoc Seq.<| x1 Seq.<| rest') Seq.|> eDoc + sDoc <- docLit $ Text.pack start + eDoc <- docLit $ Text.pack end + let sDoc' | padSeparators = docSeq [return sDoc, docSeparator] + | otherwise = return sDoc + eDoc' | padSeparators = docSeq [docSeparator, return eDoc] + | otherwise = return eDoc + sDoc'' <- sDoc' + eDoc'' <- eDoc' + rest' <- rest `forM` \bd -> docSeq + [ docCommaSep + , return bd + ] + return $ (sDoc'' Seq.<| x1 Seq.<| rest') Seq.|> eDoc''