Skip to content

Add break_newtypes flag. (#342) #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions data/stylish-haskell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ steps:
# # Default: false
# break_enums: false
#
# # Whether or not to break newtype types before `=` sign
# #
# # Default: true
# break_newtypes: true
#
# # Whether or not to break single constructor data types before `=` sign
# #
# # Default: true
Expand Down
1 change: 1 addition & 0 deletions lib/Language/Haskell/Stylish/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ parseRecords c o = Data.step
<*> (o A..: "field_comment")
<*> (o A..: "deriving")
<*> (o A..:? "break_enums" A..!= False)
<*> (o A..:? "break_newtypes" A..!= True)
<*> (o A..:? "break_single_constructors" A..!= True)
<*> (o A..: "via" >>= parseIndent)
<*> (o A..:? "curried_context" A..!= False)
Expand Down
5 changes: 4 additions & 1 deletion lib/Language/Haskell/Stylish/Step/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ data Config = Config
-- ^ Indent before @deriving@ lines (measured from column 0)
, cBreakEnums :: !Bool
-- ^ Break enums by newlines and follow the above rules
, cBreakNewtypes :: !Bool
-- ^ Break newtypes by newlines and follow the above rules
, cBreakSingleConstructors :: !Bool
-- ^ Break single constructors when enabled, e.g. @Indent 2@ will not cause newline after @=@
, cVia :: !Indent
Expand All @@ -91,6 +93,7 @@ defaultConfig = Config
, cFieldComment = 2
, cDeriving = 4
, cBreakEnums = True
, cBreakNewtypes = True
, cBreakSingleConstructors = False
, cVia = Indent 4
, cSortDeriving = True
Expand Down Expand Up @@ -150,7 +153,7 @@ formatDataDecl cfg@Config{..} m ldecl@(L declPos decl) =
putEolComment declPos
newline >> spaces x
pure True
(_, _) | not (isNewtype decl) && singleConstructor decl && not cBreakSingleConstructors ->
(_, _) | if isNewtype decl then not cBreakNewtypes else singleConstructor decl && not cBreakSingleConstructors ->
False <$ space
(Indent x, _)
| isEnum decl && not cBreakEnums -> False <$ space
Expand Down
12 changes: 6 additions & 6 deletions tests/Language/Haskell/Stylish/Step/Data/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,19 +1292,19 @@ case58 = expected @=? testStep (step sameIndentStyle) input
expected = input

sameSameStyle :: Config
sameSameStyle = Config SameLine SameLine 2 2 False True SameLine False True NoMaxColumns
sameSameStyle = Config SameLine SameLine 2 2 False True True SameLine False True NoMaxColumns

sameIndentStyle :: Config
sameIndentStyle = Config SameLine (Indent 2) 2 2 False True SameLine False True NoMaxColumns
sameIndentStyle = Config SameLine (Indent 2) 2 2 False True True SameLine False True NoMaxColumns

indentSameStyle :: Config
indentSameStyle = Config (Indent 2) SameLine 2 2 False True SameLine False True NoMaxColumns
indentSameStyle = Config (Indent 2) SameLine 2 2 False True True SameLine False True NoMaxColumns

indentIndentStyle :: Config
indentIndentStyle = Config (Indent 2) (Indent 2) 2 2 False True SameLine False True NoMaxColumns
indentIndentStyle = Config (Indent 2) (Indent 2) 2 2 False True True SameLine False True NoMaxColumns

indentIndentStyle4 :: Config
indentIndentStyle4 = Config (Indent 4) (Indent 4) 4 4 False True SameLine False True NoMaxColumns
indentIndentStyle4 = Config (Indent 4) (Indent 4) 4 4 False True True SameLine False True NoMaxColumns

sameSameNoSortStyle :: Config
sameSameNoSortStyle = Config SameLine SameLine 2 2 False True SameLine False False NoMaxColumns
sameSameNoSortStyle = Config SameLine SameLine 2 2 False True True SameLine False False NoMaxColumns