Skip to content

Commit 53f6d6c

Browse files
committed
Implement test case 'wrap signature if it does not fit max column length'
1 parent 0bd9851 commit 53f6d6c

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed
Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
{-# LANGUAGE BlockArguments #-}
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE TypeApplications #-}
4+
{-# LANGUAGE RecordWildCards #-}
45
module Language.Haskell.Stylish.Step.Signature where
56

67
import RdrName (RdrName)
78
import SrcLoc (GenLocated (..), Located)
8-
import GHC.Hs.Decls (HsDecl(..))
9-
import GHC.Hs.Binds (Sig(..))
9+
import GHC.Hs.Decls
10+
import GHC.Hs.Binds
11+
import GHC.Hs.Types
1012
import GHC.Hs.Extension (GhcPs)
1113

1214
--------------------------------------------------------------------------------
1315
import Language.Haskell.Stylish.Block
1416
import Language.Haskell.Stylish.Step
1517
import Language.Haskell.Stylish.Module
16-
import Language.Haskell.Stylish.Editor (change)
17-
import Language.Haskell.Stylish.GHC (getStartLineUnsafe, getEndLineUnsafe)
18+
import Language.Haskell.Stylish.Editor (change, noop)
19+
import Language.Haskell.Stylish.GHC (getStartLineUnsafe, getEndLineUnsafe, getEndColumnUnsafe)
1820
import Language.Haskell.Stylish.Editor (Change, applyChanges)
21+
import Language.Haskell.Stylish.Printer
1922

2023
-- TODO unify with type alias from Data.hs
2124
type ChangeLine = Change String
2225

26+
data MaxColumns
27+
= MaxColumns !Int
28+
| NoMaxColumns
29+
deriving (Show, Eq)
30+
31+
fits :: Int -> MaxColumns -> Bool
32+
fits _ NoMaxColumns = True
33+
fits v (MaxColumns limit) = v <= limit
34+
2335
data Config = Config
24-
{ cMaxColumns :: Int
36+
{ cMaxColumns :: MaxColumns
2537
}
2638

2739
step :: Config -> Step
@@ -32,15 +44,40 @@ changes cfg m = fmap (formatSignatureDecl cfg m) (topLevelFunctionSignatures m)
3244

3345
topLevelFunctionSignatures :: Module -> [Located SignatureDecl]
3446
topLevelFunctionSignatures = queryModule @(Located (HsDecl GhcPs)) \case
35-
L pos (SigD _ (TypeSig _ [name] _)) -> [L pos $ MkSignatureDecl name]
47+
L pos (SigD _ (TypeSig _ [name] (HsWC _ (HsIB _ (L _ funTy@(HsFunTy _ _ _ )))))) ->
48+
[L pos $ MkSignatureDecl name (listParameters funTy)]
3649
_ -> []
3750

51+
listParameters :: HsType GhcPs -> [Located RdrName]
52+
listParameters (HsFunTy _ (L _ arg2) (L _ arg3)) = listParameters arg2 <> listParameters arg3
53+
listParameters (HsTyVar _ _promotionFlag name) = [name]
54+
listParameters _ = []
55+
3856
data SignatureDecl = MkSignatureDecl
3957
{ sigName :: Located RdrName
58+
, sigParameters :: [Located RdrName]
4059
}
4160

4261
formatSignatureDecl :: Config -> Module -> Located SignatureDecl -> ChangeLine
43-
formatSignatureDecl _cfg _m ldecl = change originalDeclBlock id
62+
formatSignatureDecl Config{..} m ldecl@(L _declPos decl) = do
63+
let block = originalDeclBlock
64+
declLength = getEndColumnUnsafe ldecl
65+
if fits declLength cMaxColumns then
66+
noop block
67+
else
68+
change block (const printDecl)
69+
4470
where
4571
originalDeclBlock =
4672
Block (getStartLineUnsafe ldecl) (getEndLineUnsafe ldecl)
73+
74+
printerConfig = PrinterConfig
75+
{ columns = case cMaxColumns of
76+
NoMaxColumns -> Nothing
77+
MaxColumns n -> Just n
78+
}
79+
80+
printDecl = runPrinter_ printerConfig [] m do
81+
(putRdrName $ sigName decl) >> space >> putText "::" >> newline
82+
spaces 5 >> (putRdrName $ head $ sigParameters decl) >> newline
83+
traverse (\para -> spaces 2 >> putText "->" >> space >> (putRdrName para) >> newline) (tail $ sigParameters decl)

tests/Language/Haskell/Stylish/Step/Signature/Tests.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Test.HUnit (Assertion, (@=?))
1313
tests :: Test
1414
tests = testGroup "Language.Haskell.Stylish.Step.Signature.Tests"
1515
[ testCase "do not wrap signature if it fits max column length" case00
16-
-- , testCase "wrap signature if it does not fit max column length" case01
16+
, testCase "wrap signature if it does not fit max column length" case01
1717
-- , testCase "how it behaves when there is a list of constraints" case02
1818
-- , testCase "how it behaves when there is a explicit forall" case03
1919
-- , testCase "how it behaves when there is a explicit forall" case04
@@ -22,7 +22,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Signature.Tests"
2222

2323
config :: Int -> Config
2424
config cMaxColumns = Config
25-
{ cMaxColumns = cMaxColumns
25+
{ cMaxColumns = MaxColumns cMaxColumns
2626
}
2727

2828
case00 :: Assertion

0 commit comments

Comments
 (0)