Skip to content

Commit 1a86995

Browse files
chris-martinjaspervdj
authored andcommitted
Allow setting "columns: null" to disable all wrapping
1 parent ba5456a commit 1a86995

File tree

12 files changed

+282
-94
lines changed

12 files changed

+282
-94
lines changed

data/stylish-haskell.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ steps:
223223
# - squash: {}
224224

225225
# A common setting is the number of columns (parts of) code will be wrapped
226-
# to. Different steps take this into account. Default: 80.
226+
# to. Different steps take this into account.
227+
#
228+
# Set this to null to disable all line wrapping.
229+
#
230+
# Default: 80.
227231
columns: 80
228232

229233
# By default, line endings are converted according to the OS. You can override

lib/Language/Haskell/Stylish.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ import Paths_stylish_haskell (version)
4040

4141

4242
--------------------------------------------------------------------------------
43-
simpleAlign :: Int -- ^ Columns
43+
simpleAlign :: Maybe Int -- ^ Columns
4444
-> SimpleAlign.Config
4545
-> Step
4646
simpleAlign = SimpleAlign.step
4747

4848

4949
--------------------------------------------------------------------------------
50-
imports :: Int -- ^ columns
50+
imports :: Maybe Int -- ^ columns
5151
-> Imports.Options
5252
-> Step
5353
imports = Imports.step
5454

5555

5656
--------------------------------------------------------------------------------
57-
languagePragmas :: Int -- ^ columns
57+
languagePragmas :: Maybe Int -- ^ columns
5858
-> LanguagePragmas.Style
5959
-> Bool -- ^ Pad to same length in vertical mode?
6060
-> Bool -- ^ remove redundant?

lib/Language/Haskell/Stylish/Align.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,21 @@ data Alignable a = Alignable
5555
--------------------------------------------------------------------------------
5656
-- | Create changes that perform the alignment.
5757
align
58-
:: Int -- ^ Max columns
58+
:: Maybe Int -- ^ Max columns
5959
-> [Alignable H.SrcSpan] -- ^ Alignables
6060
-> [Change String] -- ^ Changes performing the alignment.
6161
align _ [] = []
6262
align maxColumns alignment
6363
-- Do not make any change if we would go past the maximum number of columns.
64-
| longestLeft + longestRight > maxColumns = []
65-
| not (fixable alignment) = []
66-
| otherwise = map align' alignment
64+
| exceedsColumns (longestLeft + longestRight) = []
65+
| not (fixable alignment) = []
66+
| otherwise = map align' alignment
6767
where
68+
exceedsColumns i = case maxColumns of
69+
Nothing -> False -- No number exceeds a maximum column count of
70+
-- Nothing, because there is no limit to exceed.
71+
Just c -> i > c
72+
6873
-- The longest thing in the left column.
6974
longestLeft = maximum $ map (H.srcSpanEndColumn . aLeft) alignment
7075

lib/Language/Haskell/Stylish/Config.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Extensions = [String]
5252
--------------------------------------------------------------------------------
5353
data Config = Config
5454
{ configSteps :: [Step]
55-
, configColumns :: Int
55+
, configColumns :: Maybe Int
5656
, configLanguageExtensions :: [String]
5757
, configNewline :: IO.Newline
5858
, configCabal :: Bool
@@ -119,7 +119,7 @@ parseConfig (A.Object o) = do
119119
-- First load the config without the actual steps
120120
config <- Config
121121
<$> pure []
122-
<*> (o A..:? "columns" A..!= 80)
122+
<*> (o A..:! "columns" A..!= Just 80)
123123
<*> (o A..:? "language_extensions" A..!= [])
124124
<*> (o A..:? "newline" >>= parseEnum newlines IO.nativeNewline)
125125
<*> (o A..:? "cabal" A..!= True)
@@ -253,7 +253,7 @@ mkLanguage :: A.Object -> A.Parser String
253253
mkLanguage o = do
254254
lang <- o A..:? "language_prefix"
255255
maybe (pure "LANGUAGE") validate lang
256-
where
256+
where
257257
validate :: String -> A.Parser String
258258
validate s
259259
| fmap toLower s == "language" = pure s

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ prettyImportSpec separate = prettyImportSpec'
258258

259259
--------------------------------------------------------------------------------
260260
prettyImport :: (Ord l, Show l) =>
261-
Int -> Options -> Bool -> Bool -> Int -> H.ImportDecl l -> [String]
261+
Maybe Int -> Options -> Bool -> Bool -> Int -> H.ImportDecl l -> [String]
262262
prettyImport columns Options{..} padQualified padName longest imp
263263
| (void `fmap` H.importSpecs imp) == emptyImportSpec = emptyWrap
264264
| otherwise = case longListAlign of
@@ -277,7 +277,7 @@ prettyImport columns Options{..} padQualified padName longest imp
277277
longListWrapper shortWrap longWrap
278278
| listAlign == NewLine
279279
|| length shortWrap > 1
280-
|| length (head shortWrap) > columns
280+
|| exceedsColumns (length (head shortWrap))
281281
= longWrap
282282
| otherwise = shortWrap
283283

@@ -292,22 +292,22 @@ prettyImport columns Options{..} padQualified padName longest imp
292292
. withLast (++ (maybeSpace ++ ")"))
293293

294294
inlineWrapper = case listAlign of
295-
NewLine -> (paddedNoSpecBase :) . wrapRest columns listPadding'
296-
WithModuleName -> wrap columns paddedBase (withModuleNameBaseLength + 4)
297-
WithAlias -> wrap columns paddedBase (inlineBaseLength + 1)
295+
NewLine -> (paddedNoSpecBase :) . wrapRestMaybe columns listPadding'
296+
WithModuleName -> wrapMaybe columns paddedBase (withModuleNameBaseLength + 4)
297+
WithAlias -> wrapMaybe columns paddedBase (inlineBaseLength + 1)
298298
-- Add 1 extra space to ensure same padding as in original code.
299299
AfterAlias -> withTail ((' ' : maybeSpace) ++)
300-
. wrap columns paddedBase (afterAliasBaseLength + 1)
300+
. wrapMaybe columns paddedBase (afterAliasBaseLength + 1)
301301

302-
inlineWithBreakWrap = paddedNoSpecBase : wrapRest columns listPadding'
302+
inlineWithBreakWrap = paddedNoSpecBase : wrapRestMaybe columns listPadding'
303303
( mapSpecs
304304
$ withInit (++ ",")
305305
. withHead (("(" ++ maybeSpace) ++)
306306
. withLast (++ (maybeSpace ++ ")")))
307307

308308
inlineToMultilineWrap
309309
| length inlineWithBreakWrap > 2
310-
|| any ((> columns) . length) (tail inlineWithBreakWrap)
310+
|| any (exceedsColumns . length) (tail inlineWithBreakWrap)
311311
= multilineWrap
312312
| otherwise = inlineWithBreakWrap
313313

@@ -389,9 +389,14 @@ prettyImport columns Options{..} padQualified padName longest imp
389389
True -> " "
390390
False -> ""
391391

392+
exceedsColumns i = case columns of
393+
Nothing -> False -- No number exceeds a maximum column count of
394+
-- Nothing, because there is no limit to exceed.
395+
Just c -> i > c
396+
392397

393398
--------------------------------------------------------------------------------
394-
prettyImportGroup :: Int -> Options -> Bool -> Int
399+
prettyImportGroup :: Maybe Int -> Options -> Bool -> Int
395400
-> [H.ImportDecl LineBlock]
396401
-> Lines
397402
prettyImportGroup columns align fileAlign longest imps =
@@ -415,12 +420,12 @@ prettyImportGroup columns align fileAlign longest imps =
415420

416421

417422
--------------------------------------------------------------------------------
418-
step :: Int -> Options -> Step
423+
step :: Maybe Int -> Options -> Step
419424
step columns = makeStep "Imports" . step' columns
420425

421426

422427
--------------------------------------------------------------------------------
423-
step' :: Int -> Options -> Lines -> Module -> Lines
428+
step' :: Maybe Int -> Options -> Lines -> Module -> Lines
424429
step' columns align ls (module', _) = applyChanges
425430
[ change block $ const $
426431
prettyImportGroup columns align fileAlign longest importGroup

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,23 @@ verticalPragmas lg longest align pragmas' =
5353

5454

5555
--------------------------------------------------------------------------------
56-
compactPragmas :: String -> Int -> [String] -> Lines
57-
compactPragmas lg columns pragmas' = wrap columns ("{-# " ++ lg) 13 $
56+
compactPragmas :: String -> Maybe Int -> [String] -> Lines
57+
compactPragmas lg columns pragmas' = wrapMaybe columns ("{-# " ++ lg) 13 $
5858
map (++ ",") (init pragmas') ++ [last pragmas' ++ " #-}"]
5959

6060

6161
--------------------------------------------------------------------------------
62-
compactLinePragmas :: String -> Int -> Bool -> [String] -> Lines
62+
compactLinePragmas :: String -> Maybe Int -> Bool -> [String] -> Lines
6363
compactLinePragmas _ _ _ [] = []
6464
compactLinePragmas lg columns align pragmas' = map (wrapLanguage . pad) prags
6565
where
6666
wrapLanguage ps = "{-# " ++ lg ++ ps ++ " #-}"
67-
maxWidth = columns - 16
67+
maxWidth = fmap (\c -> c - 16) columns
6868
longest = maximum $ map length prags
6969
pad
7070
| align = padRight longest
7171
| otherwise = id
72-
prags = map truncateComma $ wrap maxWidth "" 1 $
72+
prags = map truncateComma $ wrapMaybe maxWidth "" 1 $
7373
map (++ ",") (init pragmas') ++ [last pragmas']
7474

7575

@@ -82,7 +82,7 @@ truncateComma xs
8282

8383

8484
--------------------------------------------------------------------------------
85-
prettyPragmas :: String -> Int -> Int -> Bool -> Style -> [String] -> Lines
85+
prettyPragmas :: String -> Maybe Int -> Int -> Bool -> Style -> [String] -> Lines
8686
prettyPragmas lp _ longest align Vertical = verticalPragmas lp longest align
8787
prettyPragmas lp cols _ _ Compact = compactPragmas lp cols
8888
prettyPragmas lp cols _ align CompactLine = compactLinePragmas lp cols align
@@ -105,12 +105,12 @@ filterRedundant isRedundant' = snd . foldr filterRedundant' (S.empty, [])
105105
known' = xs' `S.union` known
106106

107107
--------------------------------------------------------------------------------
108-
step :: Int -> Style -> Bool -> Bool -> String -> Step
108+
step :: Maybe Int -> Style -> Bool -> Bool -> String -> Step
109109
step = ((((makeStep "LanguagePragmas" .) .) .) .) . step'
110110

111111

112112
--------------------------------------------------------------------------------
113-
step' :: Int -> Style -> Bool -> Bool -> String -> Lines -> Module -> Lines
113+
step' :: Maybe Int -> Style -> Bool -> Bool -> String -> Lines -> Module -> Lines
114114
step' columns style align removeRedundant lngPrefix ls (module', _)
115115
| null pragmas' = ls
116116
| otherwise = applyChanges changes ls

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fieldDeclToAlignable (H.FieldDecl ann names ty) = Just $ Alignable
108108

109109

110110
--------------------------------------------------------------------------------
111-
step :: Int -> Config -> Step
111+
step :: Maybe Int -> Config -> Step
112112
step maxColumns config = makeStep "Cases" $ \ls (module', _) ->
113113
let module'' = fmap H.srcInfoSpan module'
114114
changes search toAlign =

lib/Language/Haskell/Stylish/Util.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module Language.Haskell.Stylish.Util
1010
, trimRight
1111
, wrap
1212
, wrapRest
13+
, wrapMaybe
14+
, wrapRestMaybe
1315

1416
, withHead
1517
, withInit
@@ -98,6 +100,27 @@ wrap maxWidth leading ind = wrap' leading
98100
((length ss + length str) >= maxWidth && ind + length str <= maxWidth)
99101

100102

103+
--------------------------------------------------------------------------------
104+
wrapMaybe :: Maybe Int -- ^ Maximum line width (maybe)
105+
-> String -- ^ Leading string
106+
-> Int -- ^ Indentation
107+
-> [String] -- ^ Strings to add/wrap
108+
-> Lines -- ^ Resulting lines
109+
wrapMaybe (Just maxWidth) = wrap maxWidth
110+
wrapMaybe Nothing = noWrap
111+
112+
113+
--------------------------------------------------------------------------------
114+
noWrap :: String -- ^ Leading string
115+
-> Int -- ^ Indentation
116+
-> [String] -- ^ Strings to add
117+
-> Lines -- ^ Resulting lines
118+
noWrap leading _ind = noWrap' leading
119+
where
120+
noWrap' ss [] = [ss]
121+
noWrap' ss (str:strs) = noWrap' (ss ++ " " ++ str) strs
122+
123+
101124
--------------------------------------------------------------------------------
102125
wrapRest :: Int
103126
-> Int
@@ -116,6 +139,29 @@ wrapRest maxWidth ind = reverse . wrapRest' [] ""
116139
overflows ss str = (length ss + length str + 1) >= maxWidth
117140

118141

142+
--------------------------------------------------------------------------------
143+
wrapRestMaybe :: Maybe Int
144+
-> Int
145+
-> [String]
146+
-> Lines
147+
wrapRestMaybe (Just maxWidth) = wrapRest maxWidth
148+
wrapRestMaybe Nothing = noWrapRest
149+
150+
151+
--------------------------------------------------------------------------------
152+
noWrapRest :: Int
153+
-> [String]
154+
-> Lines
155+
noWrapRest ind = reverse . noWrapRest' [] ""
156+
where
157+
noWrapRest' ls ss []
158+
| null ss = ls
159+
| otherwise = ss:ls
160+
noWrapRest' ls ss (str:strs)
161+
| null ss = noWrapRest' ls (indent ind str) strs
162+
| otherwise = noWrapRest' ls (ss ++ " " ++ str) strs
163+
164+
119165
--------------------------------------------------------------------------------
120166
withHead :: (a -> a) -> [a] -> [a]
121167
withHead _ [] = []

0 commit comments

Comments
 (0)