1
1
{-# LANGUAGE LambdaCase #-}
2
2
{-# LANGUAGE OverloadedLists #-}
3
+ {-# LANGUAGE OverloadedStrings #-}
3
4
{-# LANGUAGE TypeApplications #-}
4
5
{-# OPTIONS_GHC -Werror=incomplete-patterns #-}
5
6
{-# OPTIONS_GHC -Wno-error=unused-matches #-}
@@ -21,11 +22,16 @@ import Data.Maybe (catMaybes, maybeToList)
21
22
import Data.Maybe qualified as Maybe
22
23
import Data.Name (Name )
23
24
import Data.Semigroup (sconcat )
25
+ import Data.Text qualified as Text
26
+ import Data.Text.Encoding (encodeUtf8Builder )
24
27
import Data.Utf8 qualified as Utf8
28
+ import Gren.Int qualified as GI
29
+ import Gren.String qualified as GS
25
30
import Parse.Primitives qualified as P
26
31
import Reporting.Annotation qualified as A
27
32
import Text.PrettyPrint.Avh4.Block (Block )
28
33
import Text.PrettyPrint.Avh4.Block qualified as Block
34
+ import Text.Printf qualified
29
35
30
36
toByteStringBuilder :: Src. Module -> B. Builder
31
37
toByteStringBuilder module_ =
@@ -202,7 +208,7 @@ formatCommentBlockNonEmpty =
202
208
spaceOrStack . fmap formatComment
203
209
204
210
formatModule :: 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) =
206
212
Block. stack $
207
213
NonEmpty. fromList $
208
214
catMaybes
@@ -279,10 +285,21 @@ formatModule (Src.Module moduleName exports docs imports values unions aliases b
279
285
Nothing -> Nothing
280
286
Just some ->
281
287
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
+ ]
286
303
287
304
formatTopLevelCommentBlock :: NonEmpty Src. Comment -> Block
288
305
formatTopLevelCommentBlock comments =
@@ -352,23 +369,30 @@ formatExposing commentsAfterKeyword commentsAfterListing = \case
352
369
formatExposed :: Src. Exposed -> Block
353
370
formatExposed = \ case
354
371
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 " (..)"
356
374
Src. Operator _ name -> Block. line $ Block. char7 ' (' <> utf8 name <> Block. char7 ' )'
357
375
358
376
formatImport :: ([Src. Comment ], Src. Import ) -> Block
359
377
formatImport (commentsBefore, Src. Import name alias exposing exposingComments comments) =
360
378
let (SC. ImportComments commentsAfterKeyword commentsAfterName) = comments
361
- in spaceOrIndent $
379
+ in Block. stack $
362
380
NonEmpty. fromList $
363
381
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
+ ]
372
396
]
373
397
374
398
formatImportAlias :: (Name , SC. ImportAliasComments ) -> Block
@@ -536,13 +560,14 @@ formatExpr = \case
536
560
Src. Chr char ->
537
561
NoExpressionParens $
538
562
formatString StringStyleChar char
539
- Src. Str string ->
563
+ Src. Str string GS. SingleLineString ->
540
564
NoExpressionParens $
541
565
formatString StringStyleSingleQuoted string
542
- Src. Int int ->
566
+ Src. Str string GS. MultilineString ->
543
567
NoExpressionParens $
544
- Block. line $
545
- Block. string7 (show int)
568
+ formatString StringStyleTripleQuoted string
569
+ Src. Int int intFormat ->
570
+ NoExpressionParens $ formatInt intFormat int
546
571
Src. Float float ->
547
572
NoExpressionParens $
548
573
Block. line $
@@ -770,6 +795,16 @@ formatExpr = \case
770
795
exprParensNone $
771
796
formatExpr (A. toValue expr)
772
797
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
+
773
808
parensComments :: [Src. Comment ] -> [Src. Comment ] -> Block -> Block
774
809
parensComments [] [] inner = inner
775
810
parensComments commentsBefore commentsAfter inner =
@@ -1005,10 +1040,8 @@ formatPattern = \case
1005
1040
Src. PStr string ->
1006
1041
NoPatternParens $
1007
1042
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
1012
1045
1013
1046
formatPatternConstructorArg :: ([Src. Comment ], Src. Pattern ) -> PatternBlock
1014
1047
formatPatternConstructorArg (commentsBefore, pat) =
@@ -1028,7 +1061,13 @@ formatString style str =
1028
1061
StringStyleSingleQuoted ->
1029
1062
stringBox (Block. char7 ' "' )
1030
1063
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
+ ]
1032
1071
where
1033
1072
stringBox :: Block. Line -> Block
1034
1073
stringBox quotes =
0 commit comments