packages feed

tilia-0.2.0.0: src/Tilia/Render/Class.hs

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}

-- | Classes, instances and families.
module Tilia.Render.Class
  ( classDecl,
    clsInstDecl,
    tyFamInstDecl,
    dataFamInstDecl,
    standaloneDerivDecl,
    famDecl,
    roleAnnot,
  )
where

import Data.Choice (fromBool)
import Data.Foldable (fold)
import Data.Maybe (isJust, isNothing)
import GHC.Builtin.Types (cTupleTyConName, isCTupleTyConName)
import GHC.Core.Coercion.Axiom (Role (..))
import GHC.Hs
import GHC.Types.Fixity (LexicalFixity (..))
import GHC.Types.Name.Reader (RdrName (..))
import GHC.Types.SrcLoc (GenLocated (..), getLoc, unLoc)
import Prelude hiding (break)

import Tilia.Doc.Combinators
import Tilia.Render.Context
import Tilia.Render.Data (dataDecl)
import Tilia.Render.Name
import Tilia.Render.Pragma
import Tilia.Render.Type
import Tilia.Span (Span, endOf)
import Tilia.Span.Ghc

-- | A type class declaration.
classDecl ::
  -- | The context.
  Ctx ->
  -- | Where its keywords were.
  AnnClassDecl ->
  -- | Its superclasses.
  Maybe (LHsContext GhcPs) ->
  -- | Its name.
  LocatedN RdrName ->
  -- | Its type variables.
  LHsQTyVars GhcPs ->
  -- | Was its head written infix?
  LexicalFixity ->
  -- | Its functional dependencies.
  [LHsFunDep GhcPs] ->
  -- | The signatures and pragmas in its body.
  [LSig GhcPs] ->
  -- | The default definitions of its methods.
  LHsBinds GhcPs ->
  -- | Its associated families.
  [LFamilyDecl GhcPs] ->
  -- | The defaults of those families.
  [LTyFamDefltDecl GhcPs] ->
  -- | The Haddocks among its members.
  [LDocDecl GhcPs] ->
  Doc
classDecl
  ctx
  anns
  ctxt
  tyCon
  HsQTvs{..}
  fixity
  fdeps
  sigs
  binds
  families
  defaults
  docs =
    layoutFrom ctx (wholeHeadSpan <> spansOf members) $
      keyword <> layoutFrom ctx wholeHeadSpan head' <> body
    where
      keyword = refAt (spanOf (acd_class anns)) (txt "class")
      headSpan = spanOf tyCon <> spansOf hsq_explicit
      whereSpan = spanOf (acd_where anns)
      wholeHeadSpan = foldMap spanOf ctxt <> headSpan <> spansOf fdeps

      head' =
        break
          <> indent
            1
            (ptr keyword)
            ( foldMap (leftContext ctx headSpan) ctxt
                <> ( if null fdeps
                       then classHead
                       else classHead >+> funDeps ctx fdeps
                   )
                <> includeWhen
                  (isJust whereSpan)
                  (break <> keywordAt whereSpan "where")
            )

      classHead
        | isCTuple (unLoc tyCon) (length hsq_explicit) =
            layoutFrom ctx headSpan
              . layoutFrom ctx (spansOf hsq_explicit)
              $ parens
                tupleParens
                ( insideBrackets
                    (spanOf tyCon)
                    ( commaSep
                        (ctxCommaStyle ctx)
                        (fmap (align . at_ ctx (tyVarBndr ctx)) hsq_explicit)
                    )
                )
        | otherwise =
            typeHead
              ctx
              0
              (outerBracketSpans (acd_openp anns) (acd_closep anns))
              fixity
              tyCon
              spanOf
              (at_ ctx (tyVarBndr ctx))
              hsq_explicit

      tupleParens = case getLoc tyCon of
        EpAnn{anns = NameAnnCommas{nann_adornment = NameParens o c}} ->
          (spanOf o, spanOf c)
        _ -> (Nothing, Nothing)

      body =
        includeUnless (null members && isNothing whereSpan)
          . indent 1 (ptr keyword)
          $ break <> knotDecls (ctxKnot ctx) ctx Associated members

      members =
        inSourceOrder
          [ fmap (fmap (SigD NoExtField)) sigs,
            fmap (fmap (ValD NoExtField)) binds,
            fmap (fmap (TyClD NoExtField . FamDecl NoExtField)) families,
            fmap (fmap (InstD NoExtField . TyFamInstD NoExtField)) defaults,
            fmap (fmap (DocD NoExtField)) docs
          ]

-- | Is this the constraint tuple of the given arity?
isCTuple :: RdrName -> Int -> Bool
isCTuple (Exact n) arity = isCTupleTyConName n && n == cTupleTyConName arity
isCTuple _ _ = False

-- | The functional dependencies of a class.
funDeps :: Ctx -> [LHsFunDep GhcPs] -> Doc
funDeps _ [] = mempty
funDeps ctx fdeps =
  break
    <> commaSepAfter
      (ctxCommaStyle ctx)
      "|"
      (fmap (align . at_ ctx (funDep ctx)) fdeps)

-- | One functional dependency of a class, as in @a b -> c@.
funDep :: Ctx -> FunDep GhcPs -> Doc
funDep ctx (FunDep _ before after) =
  sepBy space (fmap (name ctx) before)
    <> space
    <> txt (reserved ctx "->")
    <> space
    <> sepBy space (fmap (name ctx) after)

-- | A class instance.
clsInstDecl ::
  -- | The context.
  Ctx ->
  -- | The instance.
  ClsInstDecl GhcPs ->
  Doc
clsInstDecl ctx ClsInstDecl{cid_ext = (warning, anns, _), ..} =
  keyword <> layoutFrom ctx headSpan head' <> body
  where
    keyword = refAt (spanOf (acid_instance anns)) (txt "instance")
    headSpan = foldMap spanOf warning <> typeSpan
    typeSpan = spanOf cid_poly_ty
    whereSpan = spanOf (acid_where anns)

    head' =
      break
        <> indent
          1
          (ptr keyword)
          ( foldMap
              (\w -> at ctx w (warningTxt ctx (warningSquares w)) <> break)
              warning
              <> foldMap (<> break) (overlapMode ctx cid_overlap_mode)
              <> ( maybe id located typeSpan $
                     layoutFrom
                       ctx
                       typeSpan
                       (hsSigTypeBody ctx (unLoc cid_poly_ty))
                       <> includeWhen
                         (isJust whereSpan)
                         (break <> keywordAt whereSpan "where")
                 )
          )

    body =
      includeUnless (null members && isNothing whereSpan)
        . indent 1 (ptr keyword)
        $ break <> knotDecls (ctxKnot ctx) ctx Associated members

    members =
      inSourceOrder
        [ fmap (fmap (SigD NoExtField)) cid_sigs,
          fmap (fmap (ValD NoExtField)) cid_binds,
          fmap (fmap (InstD NoExtField . TyFamInstD NoExtField)) cid_tyfam_insts,
          fmap (fmap (InstD NoExtField . DataFamInstD NoExtField)) cid_datafam_insts
        ]

-- | A standalone @deriving@ declaration.
standaloneDerivDecl ::
  -- | The context.
  Ctx ->
  -- | The declaration.
  DerivDecl GhcPs ->
  Doc
standaloneDerivDecl
  ctx
  DerivDecl{deriv_ext = (warning, (derivTok, instTok)), ..} =
    keyword <> space <> strategy
    where
      keyword = refAt (spanOf derivTok) (txt "deriving")
      instanceWord = keywordAt (spanOf instTok) "instance"
      instanceHead from =
        break
          <> indent
            1
            (ptr from)
            ( foldMap
                (\w -> at ctx w (warningTxt ctx (warningSquares w)) <> break)
                warning
                <> foldMap (<> break) (overlapMode ctx deriv_overlap_mode)
                <> hsSigType ctx (hswc_body deriv_type)
            )

      strategy = case deriv_strategy of
        Nothing -> indent 1 (ptr keyword) instanceWord <> instanceHead keyword
        Just (L _ s) -> case s of
          StockStrategy tok -> named tok "stock"
          AnyclassStrategy tok -> named tok "anyclass"
          NewtypeStrategy tok -> named tok "newtype"
          ViaStrategy (XViaStrategyPs tok sigTy) ->
            indent 1 (ptr keyword) (keywordAt (spanOf tok) "via")
              <> break
              <> indent 1 (ptr keyword) (hsSigType ctx sigTy)
              <> break
              <> indent
                1
                (ptr keyword)
                (ref instanceWord <> instanceHead instanceWord)
        where
          named tok kw =
            indent
              1
              (ptr keyword)
              (keywordAt (spanOf tok) kw <> space <> instanceWord)
              <> instanceHead keyword

-- | A type family instance.
tyFamInstDecl :: Ctx -> FamilyStyle -> TyFamInstDecl GhcPs -> Doc
tyFamInstDecl ctx style TyFamInstDecl{..} =
  txt keyword >+> break <> tyFamInstEqn ctx tfid_eqn
  where
    keyword = case style of
      Associated -> "type"
      Free -> "type instance"

-- | A data family instance.
dataFamInstDecl :: Ctx -> FamilyStyle -> DataFamInstDecl GhcPs -> Doc
dataFamInstDecl ctx style (DataFamInstDecl FamEqn{..}) =
  dataDecl
    ctx
    (fromBool (style == Free))
    feqn_tycon
    feqn_pats
    typeArgSpan
    (typeArgument ctx)
    feqn_fixity
    (let (opens, closes, _) = feqn_ext in outerBracketSpans opens closes)
    outerBinders
    feqn_rhs
  where
    -- @data instance forall k (a :: k). D a = …@ binds its variables ahead
    -- of the head, exactly as a type family instance does.
    outerBinders =
      outerBndrs
        ctx
        break
        (spanOf feqn_tycon <> foldMap typeArgSpan feqn_pats)
        feqn_bndrs

-- | A @data family@ or @type family@ declaration.
famDecl ::
  -- | The context.
  Ctx ->
  -- | Does it stand on its own or in a class?
  FamilyStyle ->
  -- | The declaration.
  FamilyDecl GhcPs ->
  Doc
famDecl ctx style FamilyDecl{fdTyVars = HsQTvs{..}, ..} =
  keywords <> head' <> equations
  where
    keywords =
      refAt
        ( spanOf (afd_type fdExt)
            <> spanOf (afd_data fdExt)
            <> spanOf (afd_family fdExt)
        )
        (txt (keyword <> familyWord))
    (keyword, closedEqns) = case fdInfo of
      DataFamily -> ("data", Nothing)
      OpenTypeFamily -> ("type", Nothing)
      ClosedTypeFamily eqs -> ("type", Just eqs)
    familyWord = case style of
      Associated -> ""
      Free -> " family"

    headSpan = spanOf fdLName <> spansOf hsq_explicit
    headAndSigSpan = spanOf fdResultSig <> headSpan

    head' =
      indent 1 (ptr keywords) . layoutFrom ctx headAndSigSpan $
        break
          <> ( if isNothing resultSig && isNothing fdInjectivityAnn
                 then familyHead
                 else
                   familyHead
                     >+> space
                       <> fold resultSig
                       <> foldMap
                         ((space <>) . at_ ctx (injectivityAnn ctx))
                         fdInjectivityAnn
             )
          <> foldMap (const (break <> txt "where")) closedEqns

    familyHead =
      typeHead
        ctx
        0
        (outerBracketSpans (afd_openp fdExt) (afd_closep fdExt))
        fdFixity
        fdLName
        spanOf
        (at_ ctx (tyVarBndr ctx))
        hsq_explicit

    resultSig = familyResultSig ctx headSpan fdResultSig

    equations = case closedEqns of
      Nothing -> mempty
      Just eqs ->
        case eqs of
          Nothing -> space <> txt ".."
          Just given ->
            includeUnless (null given) $
              newline
                <> indent
                  1
                  (ptr keywords)
                  ( sepBy
                      newline
                      (fmap (at_ ctx (tyFamInstEqn ctx)) given)
                  )

-- | What the head of a family says of its result, a kind or a variable
-- naming it, if anything.
familyResultSig ::
  -- | The context.
  Ctx ->
  -- | The head.
  Maybe Span ->
  -- | What it says.
  LFamilyResultSig GhcPs ->
  Maybe Doc
familyResultSig ctx headSpan (L _ sig) = case sig of
  NoSig NoExtField -> Nothing
  KindSig NoExtField k ->
    Just
      ( joinedByArrow ctx break headSpan (spanOf k) (reserved ctx "::")
          <> hsType ctx k
      )
  TyVarSig NoExtField bndr ->
    Just (txt "=" <> break <> at ctx bndr (tyVarBndr ctx))

-- | The injectivity annotation of a type family, as in @| r -> a@.
injectivityAnn :: Ctx -> InjectivityAnn GhcPs -> Doc
injectivityAnn ctx (InjectivityAnn _ from to) =
  leadingBar
    <> name ctx from
    <> space
    <> txt (reserved ctx "->")
    <> space
    <> sepBy space (fmap (name ctx) to)

-- | One equation of a type family.
tyFamInstEqn ::
  -- | The context.
  Ctx ->
  -- | The equation.
  TyFamInstEqn GhcPs ->
  Doc
tyFamInstEqn ctx FamEqn{..} =
  if hasBinders then binders >+> lhs >+> rhs else binders <> (lhs >+> rhs)
  where
    binders = outerBndrs ctx break headSpan feqn_bndrs
    headSpan = spanOf feqn_tycon <> foldMap typeArgSpan feqn_pats
    hasBinders = case feqn_bndrs of
      HsOuterExplicit _ (_ : _) -> True
      _ -> False

    lhs =
      typeHead
        ctx
        1
        (let (opens, closes, _) = feqn_ext in outerBracketSpans opens closes)
        feqn_fixity
        feqn_tycon
        typeArgSpan
        (typeArgument ctx)
        feqn_pats

    rhs =
      trailing (txt "=")
        <> layoutFrom
          ctx
          (fmap endOf headSpan <> spanOf feqn_rhs)
          (break <> hsType ctx feqn_rhs)

-- | A @type role@ declaration.
roleAnnot ::
  -- | The context.
  Ctx ->
  -- | The declaration.
  RoleAnnotDecl GhcPs ->
  Doc
roleAnnot ctx (RoleAnnotDecl (typeTok, roleTok) tyCon roles) =
  keywordAt (spanOf typeTok) "type"
    >+> space
      <> keywordAt (spanOf roleTok) "role"
      <> break
      <> ( name ctx tyCon
             >+> break
               <> align (sepBy break (fmap (align . at_ ctx role) roles))
         )
  where
    role = maybe (txt "_") $ \case
      Nominal -> txt "nominal"
      Representational -> txt "representational"
      Phantom -> txt "phantom"