module Elm.Print.Common
( showDoc
, wrapParens
, arrow
, mkQualified
, typeWithVarsDoc
, qualifiedTypeWithVarsDoc
) where
import Data.Text (Text)
import Data.Text.Prettyprint.Doc (Doc, concatWith, parens, pretty, surround, (<+>))
import qualified Data.Text as T
showDoc :: Doc ann -> Text
showDoc = T.pack . show
wrapParens :: Doc ann -> Doc ann
wrapParens doc = case T.words $ showDoc doc of
[] -> doc
[_] -> doc
_ -> parens doc
arrow :: Doc ann
arrow = "->"
mkQualified :: Text -> Doc ann
mkQualified = pretty . ("T." <>)
typeWithVarsDoc
:: Bool
-> Text
-> [Text]
-> Doc ann
typeWithVarsDoc isQualified typeName = \case
[] -> tName
vars -> tName <+> typeVarsDoc vars
where
typeVarsDoc :: [Text] -> Doc ann
typeVarsDoc = concatWith (surround " ") . map pretty
tName :: Doc ann
tName =
if isQualified
then mkQualified typeName
else pretty typeName
qualifiedTypeWithVarsDoc
:: Text
-> [Text]
-> Doc ann
qualifiedTypeWithVarsDoc = typeWithVarsDoc True