module HaddockUtils where

import GhcPrelude

import GHC.Hs
import SrcLoc

import Control.Monad

-- -----------------------------------------------------------------------------
-- Adding documentation to record fields (used in parsing).

addFieldDoc :: LConDeclField a -> Maybe LHsDocString -> LConDeclField a
addFieldDoc :: LConDeclField a -> Maybe LHsDocString -> LConDeclField a
addFieldDoc (L SrcSpan
l ConDeclField a
fld) Maybe LHsDocString
doc
  = SrcSpan -> ConDeclField a -> LConDeclField a
forall l e. l -> e -> GenLocated l e
L SrcSpan
l (ConDeclField a
fld { cd_fld_doc :: Maybe LHsDocString
cd_fld_doc = ConDeclField a -> Maybe LHsDocString
forall pass. ConDeclField pass -> Maybe LHsDocString
cd_fld_doc ConDeclField a
fld Maybe LHsDocString -> Maybe LHsDocString -> Maybe LHsDocString
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Maybe LHsDocString
doc })

addFieldDocs :: [LConDeclField a] -> Maybe LHsDocString -> [LConDeclField a]
addFieldDocs :: [LConDeclField a] -> Maybe LHsDocString -> [LConDeclField a]
addFieldDocs [] Maybe LHsDocString
_ = []
addFieldDocs (LConDeclField a
x:[LConDeclField a]
xs) Maybe LHsDocString
doc = LConDeclField a -> Maybe LHsDocString -> LConDeclField a
forall a. LConDeclField a -> Maybe LHsDocString -> LConDeclField a
addFieldDoc LConDeclField a
x Maybe LHsDocString
doc LConDeclField a -> [LConDeclField a] -> [LConDeclField a]
forall a. a -> [a] -> [a]
: [LConDeclField a]
xs


addConDoc :: LConDecl a -> Maybe LHsDocString -> LConDecl a
addConDoc :: LConDecl a -> Maybe LHsDocString -> LConDecl a
addConDoc LConDecl a
decl    Maybe LHsDocString
Nothing = LConDecl a
decl
addConDoc (L SrcSpan
p ConDecl a
c) Maybe LHsDocString
doc     = SrcSpan -> ConDecl a -> LConDecl a
forall l e. l -> e -> GenLocated l e
L SrcSpan
p ( ConDecl a
c { con_doc :: Maybe LHsDocString
con_doc = ConDecl a -> Maybe LHsDocString
forall pass. ConDecl pass -> Maybe LHsDocString
con_doc ConDecl a
c Maybe LHsDocString -> Maybe LHsDocString -> Maybe LHsDocString
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Maybe LHsDocString
doc } )

addConDocs :: [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
addConDocs :: [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
addConDocs [] Maybe LHsDocString
_ = []
addConDocs [LConDecl a
x] Maybe LHsDocString
doc = [LConDecl a -> Maybe LHsDocString -> LConDecl a
forall a. LConDecl a -> Maybe LHsDocString -> LConDecl a
addConDoc LConDecl a
x Maybe LHsDocString
doc]
addConDocs (LConDecl a
x:[LConDecl a]
xs) Maybe LHsDocString
doc = LConDecl a
x LConDecl a -> [LConDecl a] -> [LConDecl a]
forall a. a -> [a] -> [a]
: [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
forall a. [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
addConDocs [LConDecl a]
xs Maybe LHsDocString
doc

addConDocFirst :: [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
addConDocFirst :: [LConDecl a] -> Maybe LHsDocString -> [LConDecl a]
addConDocFirst [] Maybe LHsDocString
_ = []
addConDocFirst (LConDecl a
x:[LConDecl a]
xs) Maybe LHsDocString
doc = LConDecl a -> Maybe LHsDocString -> LConDecl a
forall a. LConDecl a -> Maybe LHsDocString -> LConDecl a
addConDoc LConDecl a
x Maybe LHsDocString
doc LConDecl a -> [LConDecl a] -> [LConDecl a]
forall a. a -> [a] -> [a]
: [LConDecl a]
xs