11{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE OverloadedLists #-}
3+ {-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE TypeApplications #-}
45{-# OPTIONS_GHC -Werror=incomplete-patterns #-}
56{-# OPTIONS_GHC -Wno-error=unused-matches #-}
@@ -21,11 +22,16 @@ import Data.Maybe (catMaybes, maybeToList)
2122import Data.Maybe qualified as Maybe
2223import Data.Name (Name )
2324import Data.Semigroup (sconcat )
25+ import Data.Text qualified as Text
26+ import Data.Text.Encoding (encodeUtf8Builder )
2427import Data.Utf8 qualified as Utf8
28+ import Gren.Int qualified as GI
29+ import Gren.String qualified as GS
2530import Parse.Primitives qualified as P
2631import Reporting.Annotation qualified as A
2732import Text.PrettyPrint.Avh4.Block (Block )
2833import Text.PrettyPrint.Avh4.Block qualified as Block
34+ import Text.Printf qualified
2935
3036toByteStringBuilder :: Src. Module -> B. Builder
3137toByteStringBuilder module_ =
@@ -202,7 +208,7 @@ formatCommentBlockNonEmpty =
202208 spaceOrStack . fmap formatComment
203209
204210formatModule :: Src. Module -> Block
205- formatModule (Src. Module moduleName exports docs imports values unions aliases binops topLevelComments comments effects) =
211+ formatModule (Src. Module moduleName exports docs imports values unions aliases (commentsBeforeBinops, binops) topLevelComments comments effects) =
206212 Block. stack $
207213 NonEmpty. fromList $
208214 catMaybes
@@ -279,10 +285,21 @@ formatModule (Src.Module moduleName exports docs imports values unions aliases b
279285 Nothing -> Nothing
280286 Just some ->
281287 Just $
282- Block. stack
283- [ Block. blankLine,
284- Block. stack $ fmap (formatInfix . A. toValue) some
285- ]
288+ Block. stack $
289+ NonEmpty. fromList $
290+ mconcat
291+ [ case formatCommentBlock commentsBeforeBinops of
292+ Just comments_ ->
293+ [ Block. blankLine,
294+ Block. blankLine,
295+ comments_,
296+ Block. blankLine
297+ ]
298+ Nothing -> [] ,
299+ [ Block. blankLine,
300+ Block. stack $ fmap (formatInfix . A. toValue) some
301+ ]
302+ ]
286303
287304formatTopLevelCommentBlock :: NonEmpty Src. Comment -> Block
288305formatTopLevelCommentBlock comments =
@@ -352,23 +369,30 @@ formatExposing commentsAfterKeyword commentsAfterListing = \case
352369formatExposed :: Src. Exposed -> Block
353370formatExposed = \ case
354371 Src. Lower name -> Block. line $ utf8 $ A. toValue name
355- Src. Upper name privacy -> Block. line $ utf8 $ A. toValue name
372+ Src. Upper name Src. Private -> Block. line $ utf8 (A. toValue name)
373+ Src. Upper name (Src. Public _) -> Block. line $ utf8 (A. toValue name) <> Block. string7 " (..)"
356374 Src. Operator _ name -> Block. line $ Block. char7 ' (' <> utf8 name <> Block. char7 ' )'
357375
358376formatImport :: ([Src. Comment ], Src. Import ) -> Block
359377formatImport (commentsBefore, Src. Import name alias exposing exposingComments comments) =
360378 let (SC. ImportComments commentsAfterKeyword commentsAfterName) = comments
361- in spaceOrIndent $
379+ in Block. stack $
362380 NonEmpty. fromList $
363381 catMaybes
364- [ Just $ Block. line $ Block. string7 " import" ,
365- Just $ withCommentsBefore commentsAfterKeyword $ Block. line $ utf8 $ A. toValue name,
366- (spaceOrStack . fmap formatComment) <$> NonEmpty. nonEmpty commentsAfterName,
367- fmap formatImportAlias alias,
368- formatExposing
369- (maybe [] SC. _afterExposing exposingComments)
370- (maybe [] SC. _afterExposingListing exposingComments)
371- exposing
382+ [ fmap (\ b -> Block. stack [Block. blankLine, b]) $ formatCommentBlock commentsBefore,
383+ Just $
384+ spaceOrIndent $
385+ NonEmpty. fromList $
386+ catMaybes
387+ [ Just $ Block. line $ Block. string7 " import" ,
388+ Just $ withCommentsBefore commentsAfterKeyword $ Block. line $ utf8 $ A. toValue name,
389+ (spaceOrStack . fmap formatComment) <$> NonEmpty. nonEmpty commentsAfterName,
390+ fmap formatImportAlias alias,
391+ formatExposing
392+ (maybe [] SC. _afterExposing exposingComments)
393+ (maybe [] SC. _afterExposingListing exposingComments)
394+ exposing
395+ ]
372396 ]
373397
374398formatImportAlias :: (Name , SC. ImportAliasComments ) -> Block
@@ -536,13 +560,14 @@ formatExpr = \case
536560 Src. Chr char ->
537561 NoExpressionParens $
538562 formatString StringStyleChar char
539- Src. Str string ->
563+ Src. Str string GS. SingleLineString ->
540564 NoExpressionParens $
541565 formatString StringStyleSingleQuoted string
542- Src. Int int ->
566+ Src. Str string GS. MultilineString ->
543567 NoExpressionParens $
544- Block. line $
545- Block. string7 (show int)
568+ formatString StringStyleTripleQuoted string
569+ Src. Int int intFormat ->
570+ NoExpressionParens $ formatInt intFormat int
546571 Src. Float float ->
547572 NoExpressionParens $
548573 Block. line $
@@ -770,6 +795,16 @@ formatExpr = \case
770795 exprParensNone $
771796 formatExpr (A. toValue expr)
772797
798+ formatInt :: GI. IntFormat -> Int -> Block
799+ formatInt intFormat int =
800+ case intFormat of
801+ GI. DecimalInt ->
802+ Block. line $
803+ Block. string7 (show int)
804+ GI. HexInt ->
805+ Block. line $
806+ Block. string7 (Text.Printf. printf " 0x%X" int)
807+
773808parensComments :: [Src. Comment ] -> [Src. Comment ] -> Block -> Block
774809parensComments [] [] inner = inner
775810parensComments commentsBefore commentsAfter inner =
@@ -1005,10 +1040,8 @@ formatPattern = \case
10051040 Src. PStr string ->
10061041 NoPatternParens $
10071042 formatString StringStyleSingleQuoted string
1008- Src. PInt int ->
1009- NoPatternParens $
1010- Block. line $
1011- Block. string7 (show int)
1043+ Src. PInt int intFormat ->
1044+ NoPatternParens $ formatInt intFormat int
10121045
10131046formatPatternConstructorArg :: ([Src. Comment ], Src. Pattern ) -> PatternBlock
10141047formatPatternConstructorArg (commentsBefore, pat) =
@@ -1028,7 +1061,13 @@ formatString style str =
10281061 StringStyleSingleQuoted ->
10291062 stringBox (Block. char7 ' "' )
10301063 StringStyleTripleQuoted ->
1031- stringBox (Block. string7 " \"\"\" " )
1064+ Block. stack $
1065+ NonEmpty. fromList $
1066+ mconcat
1067+ [ [Block. line (Block. string7 " \"\"\" " )],
1068+ fmap (Block. line . Block. lineFromBuilder . encodeUtf8Builder) $ Text. splitOn " \\ n" $ (Utf8. toText str),
1069+ [Block. line (Block. string7 " \"\"\" " )]
1070+ ]
10321071 where
10331072 stringBox :: Block. Line -> Block
10341073 stringBox quotes =
0 commit comments