Skip to content

Commit

Permalink
Implement OverlapMode (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
toku-sa-n authored Jun 7, 2024
1 parent 1f6cfc2 commit f07e0f8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
1 change: 1 addition & 0 deletions hindent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ library
HIndent.Ast.Declaration.Foreign.CallingConvention
HIndent.Ast.Declaration.Foreign.Safety
HIndent.Ast.Declaration.Instance.Class
HIndent.Ast.Declaration.Instance.Class.OverlapMode
HIndent.Ast.Declaration.Instance.Family.Data
HIndent.Ast.Declaration.Instance.Family.Type
HIndent.Ast.Declaration.Rule
Expand Down
9 changes: 6 additions & 3 deletions src/HIndent/Ast/Declaration/Instance/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module HIndent.Ast.Declaration.Instance.Class

import Control.Monad
import qualified GHC.Data.Bag as GHC
import qualified GHC.Types.Basic as GHc
import HIndent.Applicative
import HIndent.Ast.Declaration.Instance.Class.OverlapMode
import HIndent.Ast.NodeComments
import HIndent.Ast.WithComments
import qualified HIndent.GhcLibParserWrapper.GHC.Hs as GHC
import {-# SOURCE #-} HIndent.Pretty
import HIndent.Pretty.Combinators
Expand All @@ -18,7 +19,7 @@ import HIndent.Pretty.SigBindFamily
import HIndent.Pretty.Types

data ClassInstance = ClassInstance
{ cid_overlap_mode :: Maybe (GHC.XRec GHC.GhcPs GHc.OverlapMode)
{ overlapMode :: Maybe (WithComments OverlapMode)
, cid_sigs :: [GHC.LSig GHC.GhcPs]
, cid_binds :: GHC.LHsBinds GHC.GhcPs
, cid_tyfam_insts :: [GHC.LTyFamInstDecl GHC.GhcPs]
Expand All @@ -32,7 +33,7 @@ instance CommentExtraction ClassInstance where
instance Pretty ClassInstance where
pretty' (ClassInstance {..}) = do
string "instance " |=> do
whenJust cid_overlap_mode $ \x -> do
whenJust overlapMode $ \x -> do
pretty x
space
pretty (fmap HsSigTypeInsideInstDecl cid_poly_ty)
Expand All @@ -52,4 +53,6 @@ instance Pretty ClassInstance where
mkClassInstance :: GHC.InstDecl GHC.GhcPs -> Maybe ClassInstance
mkClassInstance GHC.ClsInstD {cid_inst = GHC.ClsInstDecl {..}} =
Just $ ClassInstance {..}
where
overlapMode = fmap (fmap mkOverlapMode . fromGenLocated) cid_overlap_mode
mkClassInstance _ = Nothing
36 changes: 36 additions & 0 deletions src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module HIndent.Ast.Declaration.Instance.Class.OverlapMode
( OverlapMode
, mkOverlapMode
) where

import qualified GHC.Types.Basic as GHC
import HIndent.Ast.NodeComments
import {-# SOURCE #-} HIndent.Pretty
import HIndent.Pretty.Combinators.String
import HIndent.Pretty.NodeComments

data OverlapMode
= Overlappable
| Overlapping
| Overlaps
| Incoherent

instance CommentExtraction OverlapMode where
nodeComments Overlappable = NodeComments [] [] []
nodeComments Overlapping = NodeComments [] [] []
nodeComments Overlaps = NodeComments [] [] []
nodeComments Incoherent = NodeComments [] [] []

instance Pretty OverlapMode where
pretty' Overlappable = string "{-# OVERLAPPABLE #-}"
pretty' Overlapping = string "{-# OVERLAPPING #-}"
pretty' Overlaps = string "{-# OVERLAPS #-}"
pretty' Incoherent = string "{-# INCOHERENT #-}"

mkOverlapMode :: GHC.OverlapMode -> OverlapMode
mkOverlapMode GHC.NoOverlap {} =

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest, 9.8.1)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest, 9.6.4)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest, 9.4.8)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (windows-latest, 9.4.8)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (windows-latest, 9.8.1)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (windows-latest, nightly)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (windows-latest, nightly)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (macos-13, 9.4.8)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (windows-latest, 9.6.4)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (macos-13, 9.8.1)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (macos-13, 9.6.4)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (macos-latest, nightly)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (macos-latest, nightly)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest, nightly)

Pattern match(es) are non-exhaustive

Check warning on line 31 in src/HIndent/Ast/Declaration/Instance/Class/OverlapMode.hs

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest, nightly)

Pattern match(es) are non-exhaustive
error "This AST node should never appear in the tree"
mkOverlapMode GHC.Overlappable {} = Overlappable
mkOverlapMode GHC.Overlapping {} = Overlapping
mkOverlapMode GHC.Overlaps {} = Overlaps
mkOverlapMode GHC.Incoherent {} = Incoherent
28 changes: 0 additions & 28 deletions src/HIndent/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import qualified GHC.Types.Name as GHC
import qualified GHC.Types.Name.Reader as GHC
import qualified GHC.Types.SourceText as GHC
import qualified GHC.Types.SrcLoc as GHC
import HIndent.Applicative
import HIndent.Ast.Declaration
import HIndent.Ast.Declaration.Bind
import HIndent.Ast.Declaration.Data.Body
Expand Down Expand Up @@ -134,26 +133,6 @@ class CommentExtraction a =>
instance (CommentExtraction l, Pretty e) => Pretty (GHC.GenLocated l e) where
pretty' (GHC.L _ e) = pretty e

instance Pretty (GHC.ClsInstDecl GHC.GhcPs) where
pretty' GHC.ClsInstDecl {..} = do
string "instance " |=> do
whenJust cid_overlap_mode $ \x -> do
pretty x
space
pretty (fmap HsSigTypeInsideInstDecl cid_poly_ty)
|=> unless (null sigsAndMethods) (string " where")
unless (null sigsAndMethods) $ do
newline
indentedBlock $ lined $ fmap pretty sigsAndMethods
where
sigsAndMethods =
SBF.mkSortedLSigBindFamilyList
cid_sigs
(GHC.bagToList cid_binds)
[]
cid_tyfam_insts
cid_datafam_insts

instance Pretty
(GHC.MatchGroup
GHC.GhcPs
Expand Down Expand Up @@ -1215,13 +1194,6 @@ instance Pretty (GHC.DerivClauseTys GHC.GhcPs) where
pretty' (GHC.DctSingle _ ty) = parens $ pretty ty
pretty' (GHC.DctMulti _ ts) = hvTuple $ fmap pretty ts

instance Pretty GHC.OverlapMode where
pretty' GHC.NoOverlap {} = notUsedInParsedStage
pretty' GHC.Overlappable {} = string "{-# OVERLAPPABLE #-}"
pretty' GHC.Overlapping {} = string "{-# OVERLAPPING #-}"
pretty' GHC.Overlaps {} = string "{-# OVERLAPS #-}"
pretty' GHC.Incoherent {} = string "{-# INCOHERENT #-}"

instance Pretty GHC.StringLiteral where
pretty' = output

Expand Down
3 changes: 0 additions & 3 deletions src/HIndent/Pretty.hs-boot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module HIndent.Pretty
) where

import Data.Void
import qualified GHC.Types.Basic as GHC
import qualified GHC.Types.Name.Reader as GHC
import qualified GHC.Types.SourceText as GHC
import qualified GHC.Types.SrcLoc as GHC
Expand Down Expand Up @@ -47,8 +46,6 @@ instance Pretty SigBindFamily

instance Pretty InfixOp

instance Pretty GHC.OverlapMode

instance Pretty HsSigType'

instance Pretty
Expand Down

0 comments on commit f07e0f8

Please sign in to comment.