{-# LANGUAGE RecordWildCards #-}

module HIndent.Ast.Declaration.TypeSynonym
  ( TypeSynonym
  , mkTypeSynonym
  ) where

import HIndent.Ast.Declaration.TypeSynonym.Lhs
import HIndent.Ast.NodeComments
import HIndent.Ast.Type
import HIndent.Ast.WithComments
import qualified HIndent.GhcLibParserWrapper.GHC.Hs as GHC
import {-# SOURCE #-} HIndent.Pretty
import HIndent.Pretty.Combinators
import HIndent.Pretty.NodeComments

data TypeSynonym = TypeSynonym
  { TypeSynonym -> TypeSynonymLhs
lhs :: TypeSynonymLhs
  , TypeSynonym -> WithComments Type
rhs :: WithComments Type
  }

instance CommentExtraction TypeSynonym where
  nodeComments :: TypeSynonym -> NodeComments
nodeComments TypeSynonym {} = [LEpaComment] -> [LEpaComment] -> [LEpaComment] -> NodeComments
NodeComments [] [] []

instance Pretty TypeSynonym where
  pretty' :: TypeSynonym -> Printer ()
pretty' TypeSynonym {WithComments Type
TypeSynonymLhs
lhs :: TypeSynonym -> TypeSynonymLhs
rhs :: TypeSynonym -> WithComments Type
lhs :: TypeSynonymLhs
rhs :: WithComments Type
..} = do
    HasCallStack => String -> Printer ()
String -> Printer ()
string String
"type "
    TypeSynonymLhs -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty TypeSynonymLhs
lhs
    Printer ()
hor Printer () -> Printer () -> Printer ()
forall a. Printer a -> Printer a -> Printer a
<-|> Printer ()
ver
    where
      hor :: Printer ()
hor = HasCallStack => String -> Printer ()
String -> Printer ()
string String
" = " Printer () -> Printer () -> Printer ()
forall a b. Printer a -> Printer b -> Printer b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> WithComments Type -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments Type
rhs
      ver :: Printer ()
ver = Printer ()
newline Printer () -> Printer () -> Printer ()
forall a b. Printer a -> Printer b -> Printer b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Printer () -> Printer ()
forall a. Printer a -> Printer a
indentedBlock (HasCallStack => String -> Printer ()
String -> Printer ()
string String
"= " Printer () -> Printer () -> Printer ()
forall a. Printer () -> Printer a -> Printer a
|=> WithComments Type -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments Type
rhs)

mkTypeSynonym :: GHC.TyClDecl GHC.GhcPs -> TypeSynonym
mkTypeSynonym :: TyClDecl GhcPs -> TypeSynonym
mkTypeSynonym synonym :: TyClDecl GhcPs
synonym@GHC.SynDecl {XSynDecl GhcPs
LIdP GhcPs
LHsType GhcPs
LexicalFixity
LHsQTyVars GhcPs
tcdSExt :: XSynDecl GhcPs
tcdLName :: LIdP GhcPs
tcdTyVars :: LHsQTyVars GhcPs
tcdFixity :: LexicalFixity
tcdRhs :: LHsType GhcPs
tcdRhs :: forall pass. TyClDecl pass -> LHsType pass
tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdSExt :: forall pass. TyClDecl pass -> XSynDecl pass
..} = TypeSynonym {WithComments Type
TypeSynonymLhs
lhs :: TypeSynonymLhs
rhs :: WithComments Type
lhs :: TypeSynonymLhs
rhs :: WithComments Type
..}
  where
    lhs :: TypeSynonymLhs
lhs = TyClDecl GhcPs -> TypeSynonymLhs
mkTypeSynonymLhs TyClDecl GhcPs
synonym
    rhs :: WithComments Type
rhs = HsType GhcPs -> Type
mkType (HsType GhcPs -> Type)
-> WithComments (HsType GhcPs) -> WithComments Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated SrcSpanAnnA (HsType GhcPs)
-> WithComments (HsType GhcPs)
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
tcdRhs
mkTypeSynonym TyClDecl GhcPs
_ = String -> TypeSynonym
forall a. HasCallStack => String -> a
error String
"Not a type synonym."