module Elm.Print.Common
( showDoc
, wrapParens
, arrow
, mkQualified
, typeWithVarsDoc
) where
import Data.Text (Text)
import Data.Text.Prettyprint.Doc (Doc, concatWith, lparen, pretty, rparen, surround, (<+>))
import qualified Data.Text as T
showDoc :: Doc ann -> Text
showDoc = T.pack . show
wrapParens :: Doc ann -> Doc ann
wrapParens = wordsDoc . T.words . showDoc
where
wordsDoc :: [Text] -> Doc ann
wordsDoc = \case
[] -> ""
[x] -> pretty x
xs -> lparen <> pretty (T.unwords xs) <> rparen
arrow :: Doc ann
arrow = "->"
mkQualified :: Text -> Doc ann
mkQualified = pretty . ("T." <>)
typeWithVarsDoc
:: Text
-> [Text]
-> Doc ann
typeWithVarsDoc (mkQualified -> qTypeName) = \case
[] -> qTypeName
vars -> qTypeName <+> typeVarsDoc vars
where
typeVarsDoc :: [Text] -> Doc ann
typeVarsDoc = concatWith (surround " ") . map pretty