1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
--------------------------------------------------------------------------------
{-# LANGUAGE TypeFamilies #-}
module Language.Haskell.Stylish.Step.SimpleAlign
( Config (..)
, defaultConfig
, step
) where
--------------------------------------------------------------------------------
import Control.Monad (guard)
import Data.List (foldl', foldl1')
import Data.Maybe (fromMaybe)
import qualified GHC.Hs as Hs
import qualified SrcLoc as S
--------------------------------------------------------------------------------
import Language.Haskell.Stylish.Align
import Language.Haskell.Stylish.Editor
import Language.Haskell.Stylish.Module
import Language.Haskell.Stylish.Step
import Language.Haskell.Stylish.Util
--------------------------------------------------------------------------------
data Config = Config
{ cCases :: !Bool
, cTopLevelPatterns :: !Bool
, cRecords :: !Bool
} deriving (Show)
--------------------------------------------------------------------------------
defaultConfig :: Config
defaultConfig = Config
{ cCases = True
, cTopLevelPatterns = True
, cRecords = True
}
--------------------------------------------------------------------------------
type Record = [S.Located (Hs.ConDeclField Hs.GhcPs)]
--------------------------------------------------------------------------------
records :: S.Located (Hs.HsModule Hs.GhcPs) -> [Record]
records modu = do
let decls = map S.unLoc (Hs.hsmodDecls (S.unLoc modu))
tyClDecls = [ tyClDecl | Hs.TyClD _ tyClDecl <- decls ]
dataDecls = [ d | d@(Hs.DataDecl _ _ _ _ _) <- tyClDecls ]
dataDefns = map Hs.tcdDataDefn dataDecls
d@Hs.ConDeclH98 {} <- concatMap getConDecls dataDefns
case Hs.con_args d of
Hs.RecCon rec -> [S.unLoc rec]
_ -> []
where
getConDecls :: Hs.HsDataDefn Hs.GhcPs -> [Hs.ConDecl Hs.GhcPs]
getConDecls d@Hs.HsDataDefn {} = map S.unLoc $ Hs.dd_cons d
getConDecls (Hs.XHsDataDefn x) = Hs.noExtCon x
--------------------------------------------------------------------------------
recordToAlignable :: Record -> [Alignable S.RealSrcSpan]
recordToAlignable = fromMaybe [] . traverse fieldDeclToAlignable
--------------------------------------------------------------------------------
fieldDeclToAlignable
:: S.Located (Hs.ConDeclField Hs.GhcPs) -> Maybe (Alignable S.RealSrcSpan)
fieldDeclToAlignable (S.L _ (Hs.XConDeclField x)) = Hs.noExtCon x
fieldDeclToAlignable (S.L matchLoc (Hs.ConDeclField _ names ty _)) = do
matchPos <- toRealSrcSpan matchLoc
leftPos <- toRealSrcSpan $ S.getLoc $ last names
tyPos <- toRealSrcSpan $ S.getLoc ty
Just $ Alignable
{ aContainer = matchPos
, aLeft = leftPos
, aRight = tyPos
, aRightLead = length ":: "
}
--------------------------------------------------------------------------------
matchGroupToAlignable
:: Config
-> Hs.MatchGroup Hs.GhcPs (Hs.LHsExpr Hs.GhcPs)
-> [Alignable S.RealSrcSpan]
matchGroupToAlignable _conf (Hs.XMatchGroup x) = Hs.noExtCon x
matchGroupToAlignable conf (Hs.MG _ alts _) =
fromMaybe [] $ traverse (matchToAlignable conf) (S.unLoc alts)
--------------------------------------------------------------------------------
matchToAlignable
:: Config
-> S.Located (Hs.Match Hs.GhcPs (Hs.LHsExpr Hs.GhcPs))
-> Maybe (Alignable S.RealSrcSpan)
matchToAlignable conf (S.L matchLoc m@(Hs.Match _ Hs.CaseAlt pats@(_ : _) grhss)) = do
let patsLocs = map S.getLoc pats
pat = last patsLocs
guards = getGuards m
guardsLocs = map S.getLoc guards
left = foldl' S.combineSrcSpans pat guardsLocs
guard $ cCases conf
body <- rhsBody grhss
matchPos <- toRealSrcSpan matchLoc
leftPos <- toRealSrcSpan left
rightPos <- toRealSrcSpan $ S.getLoc body
Just $ Alignable
{ aContainer = matchPos
, aLeft = leftPos
, aRight = rightPos
, aRightLead = length "-> "
}
matchToAlignable conf (S.L matchLoc (Hs.Match _ (Hs.FunRhs name _ _) pats@(_ : _) grhss)) = do
guard $ cTopLevelPatterns conf
body <- unguardedRhsBody grhss
let patsLocs = map S.getLoc pats
nameLoc = S.getLoc name
left = last (nameLoc : patsLocs)
bodyLoc = S.getLoc body
matchPos <- toRealSrcSpan matchLoc
leftPos <- toRealSrcSpan left
bodyPos <- toRealSrcSpan bodyLoc
Just $ Alignable
{ aContainer = matchPos
, aLeft = leftPos
, aRight = bodyPos
, aRightLead = length "= "
}
matchToAlignable _conf (S.L _ (Hs.XMatch x)) = Hs.noExtCon x
matchToAlignable _conf (S.L _ (Hs.Match _ _ _ _)) = Nothing
--------------------------------------------------------------------------------
multiWayIfToAlignable
:: Config
-> Hs.LHsExpr Hs.GhcPs
-> [Alignable S.RealSrcSpan]
multiWayIfToAlignable conf (S.L _ (Hs.HsMultiIf _ grhss)) =
fromMaybe [] $ traverse (grhsToAlignable conf) grhss
multiWayIfToAlignable _conf _ = []
--------------------------------------------------------------------------------
grhsToAlignable
:: Config
-> S.Located (Hs.GRHS Hs.GhcPs (Hs.LHsExpr Hs.GhcPs))
-> Maybe (Alignable S.RealSrcSpan)
grhsToAlignable conf (S.L grhsloc (Hs.GRHS _ guards@(_ : _) body)) = do
guard $ cCases conf
let guardsLocs = map S.getLoc guards
bodyLoc = S.getLoc body
left = foldl1' S.combineSrcSpans guardsLocs
matchPos <- toRealSrcSpan grhsloc
leftPos <- toRealSrcSpan left
bodyPos <- toRealSrcSpan bodyLoc
Just $ Alignable
{ aContainer = matchPos
, aLeft = leftPos
, aRight = bodyPos
, aRightLead = length "-> "
}
grhsToAlignable _conf (S.L _ (Hs.XGRHS x)) = Hs.noExtCon x
grhsToAlignable _conf (S.L _ _) = Nothing
--------------------------------------------------------------------------------
step :: Maybe Int -> Config -> Step
step maxColumns config = makeStep "Cases" $ \ls module' ->
let changes
:: (S.Located (Hs.HsModule Hs.GhcPs) -> [a])
-> (a -> [Alignable S.RealSrcSpan])
-> [Change String]
changes search toAlign = concat $
map (align maxColumns) . map toAlign $ search (parsedModule module')
configured :: [Change String]
configured = concat $
[changes records recordToAlignable | cRecords config] ++
[changes everything (matchGroupToAlignable config)] ++
[changes everything (multiWayIfToAlignable config)] in
applyChanges configured ls
|