1 {-# LANGUAGE DeriveFunctor #-}
2 {-# LANGUAGE MultiParamTypeClasses #-}
4 module Distribution
.FieldGrammar
.Pretty
9 import Distribution
.CabalSpecVersion
10 import Distribution
.Compat
.Lens
11 import Distribution
.Compat
.Newtype
12 import Distribution
.Compat
.Prelude
13 import Distribution
.Fields
.Field
(FieldName
)
14 import Distribution
.Fields
.Pretty
(PrettyField
(..))
15 import Distribution
.Pretty
(Pretty
(..), showFreeText
, showFreeTextV3
)
16 import Distribution
.Utils
.Generic
(toUTF8BS
)
17 import Text
.PrettyPrint
(Doc
)
18 import qualified Text
.PrettyPrint
as PP
21 import Distribution
.FieldGrammar
.Class
23 newtype PrettyFieldGrammar s a
= PrettyFG
24 { fieldGrammarPretty
:: CabalSpecVersion
-> s
-> [PrettyField
()]
28 instance Applicative
(PrettyFieldGrammar s
) where
29 pure _
= PrettyFG
(\_ _
-> mempty
)
30 PrettyFG f
<*> PrettyFG x
= PrettyFG
(\v s
-> f v s
<> x v s
)
32 -- | We can use 'PrettyFieldGrammar' to pp print the @s@.
34 -- /Note:/ there is not trailing @($+$ text "")@.
35 prettyFieldGrammar
:: CabalSpecVersion
-> PrettyFieldGrammar s a
-> s
-> [PrettyField
()]
36 prettyFieldGrammar
= flip fieldGrammarPretty
38 instance FieldGrammar Pretty PrettyFieldGrammar
where
39 blurFieldGrammar f
(PrettyFG pp
) = PrettyFG
(\v -> pp v
. aview f
)
41 uniqueFieldAla fn _pack l
= PrettyFG
$ \_v s
->
42 ppField fn
(pretty
(pack
' _pack
(aview l s
)))
44 booleanFieldDef fn l def
= PrettyFG pp
48 |
otherwise = ppField fn
(PP
.text
(show b
))
52 optionalFieldAla fn _pack l
= PrettyFG pp
54 pp v s
= case aview l s
of
56 Just a
-> ppField fn
(prettyVersioned v
(pack
' _pack a
))
58 optionalFieldDefAla fn _pack l def
= PrettyFG pp
62 |
otherwise = ppField fn
(prettyVersioned v
(pack
' _pack x
))
66 freeTextField fn l
= PrettyFG pp
68 pp v s
= maybe mempty
(ppField fn
. showFT
) (aview l s
)
71 | v
>= CabalSpecV3_0
= showFreeTextV3
72 |
otherwise = showFreeText
74 -- it's ok to just show, as showFreeText of empty string is empty.
75 freeTextFieldDef fn l
= PrettyFG pp
77 pp v s
= ppField fn
(showFT
(aview l s
))
80 | v
>= CabalSpecV3_0
= showFreeTextV3
81 |
otherwise = showFreeText
83 freeTextFieldDefST
= defaultFreeTextFieldDefST
85 monoidalFieldAla fn _pack l
= PrettyFG pp
87 pp v s
= ppField fn
(prettyVersioned v
(pack
' _pack
(aview l s
)))
89 prefixedFields _fnPfx l
= PrettyFG
(\_
-> pp
. aview l
)
92 -- always print the field, even its Doc is empty.
93 -- i.e. don't use ppField
94 [ PrettyField
() (toUTF8BS n
) $ PP
.vcat
$ map PP
.text
$ lines s
96 -- fnPfx `isPrefixOf` n
99 knownField _
= pure
()
100 deprecatedSince _ _ x
= x
102 -- TODO: as PrettyFieldGrammar isn't aware of cabal-version: we output the field
103 -- this doesn't affect roundtrip as `removedIn` fields cannot be parsed
104 -- so invalid documents can be only manually constructed.
106 availableSince _ _
= id
107 hiddenField _
= PrettyFG
(\_
-> mempty
)
109 ppField
:: FieldName
-> Doc
-> [PrettyField
()]
110 ppField name fielddoc
111 | PP
.isEmpty fielddoc
= []
112 |
otherwise = [PrettyField
() name fielddoc
]