Skip to content

Commit 77b1caa

Browse files
Javranjaspervdj
authored andcommitted
Add a language pragma style "vertical_compact".
1 parent 3d53480 commit 77b1caa

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

data/stylish-haskell.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ steps:
300300
# - compact: A more compact style.
301301
#
302302
# - compact_line: Similar to compact, but wrap each line with
303-
# `{-#LANGUAGE #-}'.
303+
# `{-# LANGUAGE #-}'.
304+
#
305+
# - vertical_compact: Similar to vertical, but use only one language pragma.
304306
#
305307
# Default: vertical.
306308
style: vertical

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ parseLanguagePragmas config o = LanguagePragmas.step
336336
<*> mkLanguage o
337337
where
338338
styles =
339-
[ ("vertical", LanguagePragmas.Vertical)
340-
, ("compact", LanguagePragmas.Compact)
341-
, ("compact_line", LanguagePragmas.CompactLine)
339+
[ ("vertical", LanguagePragmas.Vertical)
340+
, ("compact", LanguagePragmas.Compact)
341+
, ("compact_line", LanguagePragmas.CompactLine)
342+
, ("vertical_compact", LanguagePragmas.VerticalCompact)
342343
]
343344

344345

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ data Style
3737
= Vertical
3838
| Compact
3939
| CompactLine
40+
| VerticalCompact
4041
deriving (Eq, Show)
4142

4243

@@ -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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,25 @@ case13 = assertSnippet
227227
, "{-# LANGUAGE DeriveFunctor #-}"
228228
, "main = let !x = 1 + 1 in print x"
229229
]
230+
231+
--------------------------------------------------------------------------------
232+
case13 :: Assertion
233+
case13 = expected @=? testStep (step Nothing VerticalCompact False False "language") input
234+
where
235+
input = unlines
236+
[ "{-# LANGUAGE ViewPatterns, OverloadedStrings #-}"
237+
, "{-# LANGUAGE TemplateHaskell, ViewPatterns #-}"
238+
, "{-# LANGUAGE ScopedTypeVariables, NoImplicitPrelude #-}"
239+
, "module Main where"
240+
]
241+
242+
expected = unlines
243+
[ "{-# language"
244+
, " NoImplicitPrelude"
245+
, " , OverloadedStrings"
246+
, " , ScopedTypeVariables"
247+
, " , TemplateHaskell"
248+
, " , ViewPatterns"
249+
, " #-}"
250+
, "module Main where"
251+
]

0 commit comments

Comments
 (0)