Skip to content

Commit 743610b

Browse files
authored
Add a language pragma style "vertical_compact". (#280)
Add a language pragma style "vertical_compact". Closes #278.
1 parent 5f66720 commit 743610b

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

data/stylish-haskell.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ steps:
212212
# - compact: A more compact style.
213213
#
214214
# - compact_line: Similar to compact, but wrap each line with
215-
# `{-#LANGUAGE #-}'.
215+
# `{-# LANGUAGE #-}'.
216+
#
217+
# - vertical_compact: Similar to vertical, but use only one language pragma.
216218
#
217219
# Default: vertical.
218220
style: vertical

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ parseLanguagePragmas config o = LanguagePragmas.step
267267
<*> mkLanguage o
268268
where
269269
styles =
270-
[ ("vertical", LanguagePragmas.Vertical)
271-
, ("compact", LanguagePragmas.Compact)
272-
, ("compact_line", LanguagePragmas.CompactLine)
270+
[ ("vertical", LanguagePragmas.Vertical)
271+
, ("compact", LanguagePragmas.Compact)
272+
, ("compact_line", LanguagePragmas.CompactLine)
273+
, ("vertical_compact", LanguagePragmas.VerticalCompact)
273274
]
274275

275276

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ data Style
2424
= Vertical
2525
| Compact
2626
| CompactLine
27+
| VerticalCompact
2728
deriving (Eq, Show)
2829

2930

@@ -73,6 +74,16 @@ compactLinePragmas lg columns align pragmas' = map (wrapLanguage . pad) prags
7374
map (++ ",") (init pragmas') ++ [last pragmas']
7475

7576

77+
--------------------------------------------------------------------------------
78+
verticalCompactPragmas :: String -> [String] -> Lines
79+
verticalCompactPragmas lg pragmas' =
80+
[ "{-# " <> lg
81+
, " " <> head pragmas'
82+
]
83+
<> [ " , " <> pragma | pragma <- tail pragmas']
84+
<> [ " #-}"]
85+
86+
7687
--------------------------------------------------------------------------------
7788
truncateComma :: String -> String
7889
truncateComma "" = ""
@@ -83,9 +94,10 @@ truncateComma xs
8394

8495
--------------------------------------------------------------------------------
8596
prettyPragmas :: String -> Maybe Int -> Int -> Bool -> Style -> [String] -> Lines
86-
prettyPragmas lp _ longest align Vertical = verticalPragmas lp longest align
87-
prettyPragmas lp cols _ _ Compact = compactPragmas lp cols
88-
prettyPragmas lp cols _ align CompactLine = compactLinePragmas lp cols align
97+
prettyPragmas lp _ longest align Vertical = verticalPragmas lp longest align
98+
prettyPragmas lp cols _ _ Compact = compactPragmas lp cols
99+
prettyPragmas lp cols _ align CompactLine = compactLinePragmas lp cols align
100+
prettyPragmas lp _ _ _ VerticalCompact = verticalCompactPragmas lp
89101

90102

91103
--------------------------------------------------------------------------------

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.LanguagePragmas.Tests"
3030
, testCase "case 10" case10
3131
, testCase "case 11" case11
3232
, testCase "case 12" case12
33+
, testCase "case 13" case13
3334
]
3435

3536
lANG :: String
@@ -236,3 +237,25 @@ case12 = expected @=? testStep (step Nothing Compact False False "language") inp
236237
[ "{-# language NoImplicitPrelude, OverloadedStrings, ScopedTypeVariables, TemplateHaskell, ViewPatterns #-}"
237238
, "module Main where"
238239
]
240+
241+
--------------------------------------------------------------------------------
242+
case13 :: Assertion
243+
case13 = expected @=? testStep (step Nothing VerticalCompact False False "language") input
244+
where
245+
input = unlines
246+
[ "{-# LANGUAGE ViewPatterns, OverloadedStrings #-}"
247+
, "{-# LANGUAGE TemplateHaskell, ViewPatterns #-}"
248+
, "{-# LANGUAGE ScopedTypeVariables, NoImplicitPrelude #-}"
249+
, "module Main where"
250+
]
251+
252+
expected = unlines
253+
[ "{-# language"
254+
, " NoImplicitPrelude"
255+
, " , OverloadedStrings"
256+
, " , ScopedTypeVariables"
257+
, " , TemplateHaskell"
258+
, " , ViewPatterns"
259+
, " #-}"
260+
, "module Main where"
261+
]

0 commit comments

Comments
 (0)