Skip to content

Commit

Permalink
Add RecordConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
toku-sa-n committed Mar 15, 2024
1 parent a388c14 commit 65ba306
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/HIndent/Ast/Declaration/Data/Body.hs
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ViewPatterns #-}

module HIndent.Ast.Declaration.Data.Body
( DataBody
, mkDataBody
) where

import Control.Monad
import Data.Maybe
import qualified GHC.Types.SrcLoc as GHC
import HIndent.Applicative
import HIndent.Ast.Declaration.Data.GADT.Constructor
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
import Control.Monad
import Data.Maybe
import qualified GHC.Types.SrcLoc as GHC
import HIndent.Applicative
import HIndent.Ast.Declaration.Data.GADT.Constructor
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

newtype RecordConstructor =
RecordConstructor (GHC.LConDecl GHC.GhcPs)

instance CommentExtraction RecordConstructor where
nodeComments RecordConstructor {} = NodeComments [] [] []

instance Pretty RecordConstructor where
pretty' (RecordConstructor x) = pretty x

mkRecordConstructor :: GHC.LConDecl GHC.GhcPs -> RecordConstructor
mkRecordConstructor = RecordConstructor

data DataBody
= GADT
{ kind :: Maybe (WithComments Type)
{ kind :: Maybe (WithComments Type)
, constructors :: [WithComments GADTConstructor]
}
| Record
{ dd_cons :: [GHC.LConDecl GHC.GhcPs]
{ dd_cons :: [RecordConstructor]
, dd_derivs :: GHC.HsDeriving GHC.GhcPs
}

instance CommentExtraction DataBody where
nodeComments GADT {} = NodeComments [] [] []
nodeComments GADT {} = NodeComments [] [] []
nodeComments Record {} = NodeComments [] [] []

instance Pretty DataBody where
Expand All @@ -42,7 +54,7 @@ instance Pretty DataBody where
pretty' Record {..} = do
case dd_cons of
[] -> indentedBlock derivingsAfterNewline
[x@(GHC.L _ GHC.ConDeclH98 {con_args = GHC.RecCon {}})] -> do
[RecordConstructor (x@(GHC.L _ GHC.ConDeclH98 {con_args = GHC.RecCon {}}))] -> do

Check warning on line 57 in src/HIndent/Ast/Declaration/Data/Body.hs

View workflow job for this annotation

GitHub Actions / hlint

Warning in module HIndent.Ast.Declaration.Data.Body: Redundant bracket ▫︎ Found: "(x@(GHC.L _ GHC.ConDeclH98 {con_args = GHC.RecCon {}}))" ▫︎ Perhaps: "x@(GHC.L _ GHC.ConDeclH98 {con_args = GHC.RecCon {}})"
string " = "
pretty x
unless (null dd_derivs) $ space |=> printDerivings
Expand All @@ -67,24 +79,24 @@ mkDataBody defn@GHC.HsDataDefn {..} =
if isGADT defn
then GADT
{ constructors =
fromMaybe (error "Some constructors are not GADT ones.")
$ mapM (traverse mkGADTConstructor . fromGenLocated)
$ getConDecls defn
fromMaybe (error "Some constructors are not GADT ones.") $
mapM (traverse mkGADTConstructor . fromGenLocated) $
getConDecls defn
, ..
}
else Record {dd_cons = getConDecls defn, ..}
else Record {dd_cons = fmap mkRecordConstructor $ getConDecls defn, ..}

Check warning on line 87 in src/HIndent/Ast/Declaration/Data/Body.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in mkDataBody in module HIndent.Ast.Declaration.Data.Body: Use <$> ▫︎ Found: "fmap mkRecordConstructor $ getConDecls defn" ▫︎ Perhaps: "mkRecordConstructor <$> getConDecls defn"
where
kind = fmap mkType . fromGenLocated <$> dd_kindSig

isGADT :: GHC.HsDataDefn GHC.GhcPs -> Bool
isGADT (getConDecls -> (GHC.L _ GHC.ConDeclGADT {}:_)) = True
isGADT _ = False
isGADT _ = False

getConDecls :: GHC.HsDataDefn GHC.GhcPs -> [GHC.LConDecl GHC.GhcPs]
#if MIN_VERSION_ghc_lib_parser(9, 6, 0)
getConDecls GHC.HsDataDefn {..} =
case dd_cons of
GHC.NewTypeCon x -> [x]
GHC.NewTypeCon x -> [x]
GHC.DataTypeCons _ xs -> xs
#else
getConDecls GHC.HsDataDefn {..} = dd_cons
Expand Down

0 comments on commit 65ba306

Please sign in to comment.