Skip to content

Commit 84ff4e5

Browse files
Make sorting deriving list optional (#316)
* Make sorting deriving list optional Not everyone wants their typeclasses sorted. * Remove redundant code Co-authored-by: Łukasz Gołębiewski <lukasz.golebiewski@gmail.com>
1 parent 062310c commit 84ff4e5

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

data/stylish-haskell.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ steps:
5858
#
5959
# # How many spaces to insert before "via" clause counted from indentation of deriving clause
6060
# # Possible values:
61-
# # - "same_line" -- "{" and first field goes on the same line as the data constructor.
62-
# # - "indent N" -- insert a new line and N spaces from the beginning of the data constructor
61+
# # - "same_line" -- "via" part goes on the same line as "deriving" keyword.
62+
# # - "indent N" -- insert a new line and N spaces from the beginning of "deriving" keyword.
6363
# via: "indent 2"
6464
#
65+
# # Sort typeclass names in the "deriving" list alphabetically.
66+
# sort_deriving: true
67+
#
6568
# # Wheter or not to break enums onto several lines
6669
# #
6770
# # Default: false

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ parseRecords c o = Data.step
221221
<*> (o A..:? "break_single_constructors" A..!= True)
222222
<*> (o A..: "via" >>= parseIndent)
223223
<*> (o A..:? "curried_context" A..!= False)
224+
<*> (o A..:? "sort_deriving" A..!= True)
224225
<*> pure configMaxColumns)
225226
where
226227
configMaxColumns =

lib/Language/Haskell/Stylish/Step/Data.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ data Config = Config
7171
-- ^ Indentation between @via@ clause and start of deriving column start
7272
, cCurriedContext :: !Bool
7373
-- ^ If true, use curried context. E.g: @allValues :: Enum a => Bounded a => Proxy a -> [a]@
74+
, cSortDeriving :: !Bool
75+
-- ^ If true, will sort type classes in a @deriving@ list.
7476
, cMaxColumns :: !MaxColumns
7577
} deriving (Show)
7678

@@ -266,7 +268,7 @@ putDeriving Config{..} (L pos clause) = do
266268
= clause
267269
& deriv_clause_tys
268270
& unLocated
269-
& sortBy compareOutputable
271+
& (if cSortDeriving then sortBy compareOutputable else id)
270272
& fmap hsib_body
271273

272274
headTy =

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests"
6565
, testCase "case 52" case52
6666
, testCase "case 53" case53
6767
, testCase "case 54" case54
68+
, testCase "case 55" case55
6869
]
6970

7071
case00 :: Assertion
@@ -1200,17 +1201,29 @@ case54 = expected @=? testStep (step indentIndentStyle { cMaxColumns = MaxColumn
12001201
, " deriving newtype (Applicative, Functor, Monad)"
12011202
]
12021203

1204+
case55 :: Assertion
1205+
case55 = expected @=? testStep (step sameSameNoSortStyle) input
1206+
where
1207+
input = unlines
1208+
[ "data Foo = Foo deriving (Z, Y, X, Bar, Abcd)"
1209+
]
1210+
1211+
expected = input
1212+
12031213
sameSameStyle :: Config
1204-
sameSameStyle = Config SameLine SameLine 2 2 False True SameLine False NoMaxColumns
1214+
sameSameStyle = Config SameLine SameLine 2 2 False True SameLine False True NoMaxColumns
12051215

12061216
sameIndentStyle :: Config
1207-
sameIndentStyle = Config SameLine (Indent 2) 2 2 False True SameLine False NoMaxColumns
1217+
sameIndentStyle = Config SameLine (Indent 2) 2 2 False True SameLine False True NoMaxColumns
12081218

12091219
indentSameStyle :: Config
1210-
indentSameStyle = Config (Indent 2) SameLine 2 2 False True SameLine False NoMaxColumns
1220+
indentSameStyle = Config (Indent 2) SameLine 2 2 False True SameLine False True NoMaxColumns
12111221

12121222
indentIndentStyle :: Config
1213-
indentIndentStyle = Config (Indent 2) (Indent 2) 2 2 False True SameLine False NoMaxColumns
1223+
indentIndentStyle = Config (Indent 2) (Indent 2) 2 2 False True SameLine False True NoMaxColumns
12141224

12151225
indentIndentStyle4 :: Config
1216-
indentIndentStyle4 = Config (Indent 4) (Indent 4) 4 4 False True SameLine False NoMaxColumns
1226+
indentIndentStyle4 = Config (Indent 4) (Indent 4) 4 4 False True SameLine False True NoMaxColumns
1227+
1228+
sameSameNoSortStyle :: Config
1229+
sameSameNoSortStyle = Config SameLine SameLine 2 2 False True SameLine False False NoMaxColumns

0 commit comments

Comments
 (0)