@@ -45,17 +45,25 @@ changes cfg m = fmap (formatSignatureDecl cfg m) (topLevelFunctionSignatures m)
45
45
topLevelFunctionSignatures :: Module -> [Located SignatureDecl ]
46
46
topLevelFunctionSignatures = queryModule @ (Located (HsDecl GhcPs )) \ case
47
47
L pos (SigD _ (TypeSig _ [name] (HsWC _ (HsIB _ (L _ funTy@ (HsFunTy _ _ _ )))))) ->
48
- [L pos $ MkSignatureDecl name (listParameters funTy)]
48
+ [L pos $ MkSignatureDecl name (listParameters funTy) [] ]
49
+ L pos (SigD _ (TypeSig _ [name] (HsWC _ (HsIB _ (L _ (HsQualTy _ (L _ contexts) (L _ funTy))))))) ->
50
+ [L pos $ MkSignatureDecl name (listParameters funTy) (contexts >>= listContexts)]
49
51
_ -> []
50
52
51
53
listParameters :: HsType GhcPs -> [Located RdrName ]
52
54
listParameters (HsFunTy _ (L _ arg2) (L _ arg3)) = listParameters arg2 <> listParameters arg3
53
55
listParameters (HsTyVar _ _promotionFlag name) = [name]
54
56
listParameters _ = []
55
57
58
+ listContexts :: Located (HsType GhcPs ) -> [Located RdrName ]
59
+ listContexts (L _ (HsTyVar _ _ name)) = [name]
60
+ listContexts (L _ (HsAppTy _ arg1 arg2)) = listContexts arg1 <> listContexts arg2
61
+ listContexts _ = []
62
+
56
63
data SignatureDecl = MkSignatureDecl
57
64
{ sigName :: Located RdrName
58
65
, sigParameters :: [Located RdrName ]
66
+ , sigConstraints :: [Located RdrName ]
59
67
}
60
68
61
69
formatSignatureDecl :: Config -> Module -> Located SignatureDecl -> ChangeLine
@@ -74,20 +82,54 @@ printDecl Config{..} m MkSignatureDecl{..} = runPrinter_ printerConfig [] m do
74
82
printRemainingLines
75
83
where
76
84
85
+ ----------------------------------------------------------------------------------------
86
+
77
87
printFirstLine =
78
88
putRdrName sigName >> space >> putText " ::" >> newline
79
89
90
+ ----------------------------------------------------------------------------------------
91
+
80
92
printSecondLine =
81
- spaces 5 >> (putRdrName $ head sigParameters) >> newline
93
+ if hasConstraints then printConstraints
94
+ else printFirstParameter
95
+
96
+ printConstraints =
97
+ spaces 5 >> putText " ("
98
+ >> (traverse (\ ctr -> printConstraint ctr >> putText " , " ) (init groupConstraints))
99
+ >> (printConstraint $ last groupConstraints)
100
+ >> putText " )" >> newline
101
+
102
+ groupConstraints = zip (dropEvery sigConstraints 2 ) (dropEvery (tail sigConstraints) 2 )
103
+
104
+ printConstraint (tc, tp) = putRdrName tc >> space >> putRdrName tp
105
+
106
+ printFirstParameter =
107
+ spaces 5 >> (putRdrName $ head sigParameters) >> newline
108
+
109
+ ----------------------------------------------------------------------------------------
82
110
83
111
printRemainingLines =
84
- traverse printRemainingLine (tail sigParameters)
112
+ if hasConstraints then
113
+ printRemainingLine " =>" (head sigParameters)
114
+ >> traverse (printRemainingLine " ->" ) (tail sigParameters)
115
+ else
116
+ traverse (printRemainingLine " ->" ) (tail sigParameters)
117
+
118
+ printRemainingLine prefix parameter =
119
+ spaces 2 >> putText prefix >> space >> (putRdrName parameter) >> newline
85
120
86
- printRemainingLine parameter =
87
- spaces 2 >> putText " ->" >> space >> (putRdrName parameter) >> newline
121
+ ----------------------------------------------------------------------------------------
88
122
89
123
printerConfig = PrinterConfig
90
124
{ columns = case cMaxColumns of
91
125
NoMaxColumns -> Nothing
92
126
MaxColumns n -> Just n
93
127
}
128
+
129
+ hasConstraints = not $ null sigConstraints
130
+
131
+ -- 99 problems :)
132
+ dropEvery :: [a ] -> Int -> [a ]
133
+ dropEvery xs n
134
+ | length xs < n = xs
135
+ | otherwise = take (n- 1 ) xs ++ dropEvery (drop n xs) n
0 commit comments