{-# LANGUAGE RecordWildCards #-}

module HIndent.Ast.Declaration.Class.NameAndTypeVariables
  ( NameAndTypeVariables
  , mkNameAndTypeVariables
  ) where

import qualified GHC.Types.Fixity as GHC
import HIndent.Ast.Name.Infix
import HIndent.Ast.Name.Prefix
import HIndent.Ast.NodeComments
import HIndent.Ast.Type.Variable
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 NameAndTypeVariables
  = Prefix
      { NameAndTypeVariables -> WithComments PrefixName
pName :: WithComments PrefixName -- Using `name` in both `Prefix` and `Infix` causes a type conflict.
      , NameAndTypeVariables -> [WithComments TypeVariable]
typeVariables :: [WithComments TypeVariable]
      }
  | Infix
      { NameAndTypeVariables -> WithComments TypeVariable
left :: WithComments TypeVariable
      , NameAndTypeVariables -> WithComments InfixName
iName :: WithComments InfixName
      , NameAndTypeVariables -> WithComments TypeVariable
right :: WithComments TypeVariable
      , NameAndTypeVariables -> [WithComments TypeVariable]
remains :: [WithComments TypeVariable]
      }

instance CommentExtraction NameAndTypeVariables where
  nodeComments :: NameAndTypeVariables -> NodeComments
nodeComments Prefix {} = [LEpaComment] -> [LEpaComment] -> [LEpaComment] -> NodeComments
NodeComments [] [] []
  nodeComments Infix {} = [LEpaComment] -> [LEpaComment] -> [LEpaComment] -> NodeComments
NodeComments [] [] []

instance Pretty NameAndTypeVariables where
  pretty' :: NameAndTypeVariables -> Printer ()
pretty' Prefix {[WithComments TypeVariable]
WithComments PrefixName
pName :: NameAndTypeVariables -> WithComments PrefixName
typeVariables :: NameAndTypeVariables -> [WithComments TypeVariable]
pName :: WithComments PrefixName
typeVariables :: [WithComments TypeVariable]
..} = [Printer ()] -> Printer ()
spaced ([Printer ()] -> Printer ()) -> [Printer ()] -> Printer ()
forall a b. (a -> b) -> a -> b
$ WithComments PrefixName -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments PrefixName
pName Printer () -> [Printer ()] -> [Printer ()]
forall a. a -> [a] -> [a]
: (WithComments TypeVariable -> Printer ())
-> [WithComments TypeVariable] -> [Printer ()]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WithComments TypeVariable -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty [WithComments TypeVariable]
typeVariables
  pretty' Infix {[WithComments TypeVariable]
WithComments TypeVariable
WithComments InfixName
left :: NameAndTypeVariables -> WithComments TypeVariable
iName :: NameAndTypeVariables -> WithComments InfixName
right :: NameAndTypeVariables -> WithComments TypeVariable
remains :: NameAndTypeVariables -> [WithComments TypeVariable]
left :: WithComments TypeVariable
iName :: WithComments InfixName
right :: WithComments TypeVariable
remains :: [WithComments TypeVariable]
..} = do
    Printer () -> Printer ()
forall a. Printer a -> Printer a
parens (Printer () -> Printer ()) -> Printer () -> Printer ()
forall a b. (a -> b) -> a -> b
$ [Printer ()] -> Printer ()
spaced [WithComments TypeVariable -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments TypeVariable
left, WithComments InfixName -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments InfixName
iName, WithComments TypeVariable -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty WithComments TypeVariable
right]
    [Printer ()] -> Printer ()
spacePrefixed ([Printer ()] -> Printer ()) -> [Printer ()] -> Printer ()
forall a b. (a -> b) -> a -> b
$ (WithComments TypeVariable -> Printer ())
-> [WithComments TypeVariable] -> [Printer ()]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap WithComments TypeVariable -> Printer ()
forall a. Pretty a => a -> Printer ()
pretty [WithComments TypeVariable]
remains

mkNameAndTypeVariables :: GHC.TyClDecl GHC.GhcPs -> Maybe NameAndTypeVariables
mkNameAndTypeVariables :: TyClDecl GhcPs -> Maybe NameAndTypeVariables
mkNameAndTypeVariables GHC.ClassDecl {tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
tcdFixity = LexicalFixity
GHC.Prefix, [LSig GhcPs]
[LDocDecl GhcPs]
[LTyFamDefltDecl GhcPs]
[LFamilyDecl GhcPs]
[LHsFunDep GhcPs]
Maybe (LHsContext GhcPs)
XClassDecl GhcPs
LIdP GhcPs
LHsBinds GhcPs
LHsQTyVars GhcPs
tcdCExt :: XClassDecl GhcPs
tcdCtxt :: Maybe (LHsContext GhcPs)
tcdLName :: LIdP GhcPs
tcdTyVars :: LHsQTyVars GhcPs
tcdFDs :: [LHsFunDep GhcPs]
tcdSigs :: [LSig GhcPs]
tcdMeths :: LHsBinds GhcPs
tcdATs :: [LFamilyDecl GhcPs]
tcdATDefs :: [LTyFamDefltDecl GhcPs]
tcdDocs :: [LDocDecl GhcPs]
tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdDocs :: forall pass. TyClDecl pass -> [LDocDecl pass]
tcdATDefs :: forall pass. TyClDecl pass -> [LTyFamDefltDecl pass]
tcdATs :: forall pass. TyClDecl pass -> [LFamilyDecl pass]
tcdMeths :: forall pass. TyClDecl pass -> LHsBinds pass
tcdSigs :: forall pass. TyClDecl pass -> [LSig pass]
tcdFDs :: forall pass. TyClDecl pass -> [LHsFunDep pass]
tcdCtxt :: forall pass. TyClDecl pass -> Maybe (LHsContext pass)
tcdCExt :: forall pass. TyClDecl pass -> XClassDecl pass
..} =
  NameAndTypeVariables -> Maybe NameAndTypeVariables
forall a. a -> Maybe a
Just Prefix {[WithComments TypeVariable]
WithComments PrefixName
pName :: WithComments PrefixName
typeVariables :: [WithComments TypeVariable]
pName :: WithComments PrefixName
typeVariables :: [WithComments TypeVariable]
..}
  where
    pName :: WithComments PrefixName
pName = GenLocated SrcSpanAnnN PrefixName -> WithComments PrefixName
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated (GenLocated SrcSpanAnnN PrefixName -> WithComments PrefixName)
-> GenLocated SrcSpanAnnN PrefixName -> WithComments PrefixName
forall a b. (a -> b) -> a -> b
$ (RdrName -> PrefixName)
-> GenLocated SrcSpanAnnN RdrName
-> GenLocated SrcSpanAnnN PrefixName
forall a b.
(a -> b) -> GenLocated SrcSpanAnnN a -> GenLocated SrcSpanAnnN b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap RdrName -> PrefixName
mkPrefixName LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
tcdLName
    typeVariables :: [WithComments TypeVariable]
typeVariables =
      (HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall a b. (a -> b) -> WithComments a -> WithComments b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable
forall a. HsTyVarBndr a GhcPs -> TypeVariable
mkTypeVariable (WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> WithComments TypeVariable)
-> (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
    -> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs))
-> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> WithComments TypeVariable)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
-> [WithComments TypeVariable]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LHsQTyVars GhcPs -> [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
forall pass.
LHsQTyVars pass -> [LHsTyVarBndr (HsBndrVis pass) pass]
GHC.hsq_explicit LHsQTyVars GhcPs
tcdTyVars
mkNameAndTypeVariables GHC.ClassDecl { tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
tcdFixity = LexicalFixity
GHC.Infix
                                     , tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
tcdTyVars = GHC.HsQTvs {hsq_explicit :: forall pass.
LHsQTyVars pass -> [LHsTyVarBndr (HsBndrVis pass) pass]
hsq_explicit = LHsTyVarBndr (HsBndrVis GhcPs) GhcPs
h:LHsTyVarBndr (HsBndrVis GhcPs) GhcPs
t:[LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
xs}
                                     , [LSig GhcPs]
[LDocDecl GhcPs]
[LTyFamDefltDecl GhcPs]
[LFamilyDecl GhcPs]
[LHsFunDep GhcPs]
Maybe (LHsContext GhcPs)
XClassDecl GhcPs
LIdP GhcPs
LHsBinds GhcPs
tcdLName :: forall pass. TyClDecl pass -> LIdP pass
tcdDocs :: forall pass. TyClDecl pass -> [LDocDecl pass]
tcdATDefs :: forall pass. TyClDecl pass -> [LTyFamDefltDecl pass]
tcdATs :: forall pass. TyClDecl pass -> [LFamilyDecl pass]
tcdMeths :: forall pass. TyClDecl pass -> LHsBinds pass
tcdSigs :: forall pass. TyClDecl pass -> [LSig pass]
tcdFDs :: forall pass. TyClDecl pass -> [LHsFunDep pass]
tcdCtxt :: forall pass. TyClDecl pass -> Maybe (LHsContext pass)
tcdCExt :: forall pass. TyClDecl pass -> XClassDecl pass
tcdCExt :: XClassDecl GhcPs
tcdCtxt :: Maybe (LHsContext GhcPs)
tcdLName :: LIdP GhcPs
tcdFDs :: [LHsFunDep GhcPs]
tcdSigs :: [LSig GhcPs]
tcdMeths :: LHsBinds GhcPs
tcdATs :: [LFamilyDecl GhcPs]
tcdATDefs :: [LTyFamDefltDecl GhcPs]
tcdDocs :: [LDocDecl GhcPs]
..
                                     } = NameAndTypeVariables -> Maybe NameAndTypeVariables
forall a. a -> Maybe a
Just Infix {[WithComments TypeVariable]
WithComments TypeVariable
WithComments InfixName
left :: WithComments TypeVariable
iName :: WithComments InfixName
right :: WithComments TypeVariable
remains :: [WithComments TypeVariable]
left :: WithComments TypeVariable
iName :: WithComments InfixName
right :: WithComments TypeVariable
remains :: [WithComments TypeVariable]
..}
  where
    left :: WithComments TypeVariable
left = HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable
forall a. HsTyVarBndr a GhcPs -> TypeVariable
mkTypeVariable (HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated LHsTyVarBndr (HsBndrVis GhcPs) GhcPs
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
h
    iName :: WithComments InfixName
iName = GenLocated SrcSpanAnnN InfixName -> WithComments InfixName
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated (GenLocated SrcSpanAnnN InfixName -> WithComments InfixName)
-> GenLocated SrcSpanAnnN InfixName -> WithComments InfixName
forall a b. (a -> b) -> a -> b
$ (RdrName -> InfixName)
-> GenLocated SrcSpanAnnN RdrName
-> GenLocated SrcSpanAnnN InfixName
forall a b.
(a -> b) -> GenLocated SrcSpanAnnN a -> GenLocated SrcSpanAnnN b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap RdrName -> InfixName
mkInfixName LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
tcdLName
    right :: WithComments TypeVariable
right = HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable
forall a. HsTyVarBndr a GhcPs -> TypeVariable
mkTypeVariable (HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated LHsTyVarBndr (HsBndrVis GhcPs) GhcPs
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
t
    remains :: [WithComments TypeVariable]
remains = (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> WithComments TypeVariable)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
-> [WithComments TypeVariable]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall a b. (a -> b) -> WithComments a -> WithComments b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> TypeVariable
forall a. HsTyVarBndr a GhcPs -> TypeVariable
mkTypeVariable (WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> WithComments TypeVariable)
-> (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
    -> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs))
-> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments TypeVariable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> WithComments (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
forall l a. CommentExtraction l => GenLocated l a -> WithComments a
fromGenLocated) [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
xs
mkNameAndTypeVariables TyClDecl GhcPs
_ = Maybe NameAndTypeVariables
forall a. Maybe a
Nothing