Skip to content

Commit a27fcb1

Browse files
Don't indent closing paren where possible
1 parent eab8ff0 commit a27fcb1

File tree

6 files changed

+74
-32
lines changed

6 files changed

+74
-32
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased
2+
3+
* Don't indent closing parentheses for tuples where possible
4+
15
## Ormolu 0.7.5.0
26

37
* Switched to `ghc-lib-parser-9.10`, with the following new syntactic features/behaviors:

data/examples/declaration/value/function/arrow/proc-applications-out.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bar f x =
99
( y,
1010
z,
1111
w
12-
)
12+
)
1313
->
1414
f -- The value
1515
-<

data/examples/declaration/value/function/arrow/proc-do-complex-out.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ foo
99
( (a, b),
1010
(c, d),
1111
(e, f)
12-
)
12+
)
1313
-> do
1414
-- Begin do
1515
(x, y) <- -- GHC parser fails if layed out over multiple lines
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
f
2+
( a,
3+
b,
4+
c
5+
) = True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
f
2+
( a,
3+
b,
4+
c
5+
) = True

src/Ormolu/Printer/Meat/Declaration/Value.hs

+58-30
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@ p_match' placer render style isInfix multAnn strictness m_pats GRHSs {..} = do
207207
patSpans = combineSrcSpans' (getLocA <$> ne_pats)
208208
indentBody = not (isOneLineSpan combinedSpans)
209209
switchLayout [combinedSpans] $ do
210-
let stdCase = sep breakpoint (located' p_pat) m_pats
210+
let stdCase = sep breakpoint (located' p_pat') m_pats
211211
case style of
212212
Function name ->
213213
p_infixDefHelper
214214
isInfix
215215
indentBody
216216
(p_rdrName name)
217-
(located' p_pat <$> m_pats)
217+
(located' p_pat' <$> m_pats)
218218
PatternBind -> stdCase
219219
Case -> stdCase
220220
Lambda -> do
@@ -228,12 +228,12 @@ p_match' placer render style isInfix multAnn strictness m_pats GRHSs {..} = do
228228
when needsSpace space
229229
sitcc stdCase
230230
LambdaCase -> do
231-
located' p_pat head_pat
231+
located' p_pat' head_pat
232232
unless (null tail_pats) $ do
233233
breakpoint
234234
-- When we have multiple patterns (with `\cases`) across multiple
235235
-- lines, we have to indent all but the first pattern.
236-
inci $ sep breakpoint (located' p_pat) tail_pats
236+
inci $ sep breakpoint (located' p_pat') tail_pats
237237
return indentBody
238238
let -- Calculate position of end of patterns. This is useful when we decide
239239
-- about putting certain constructions in hanging positions.
@@ -293,6 +293,15 @@ p_match' placer render style isInfix multAnn strictness m_pats GRHSs {..} = do
293293
switchLayout [patGrhssSpan] $
294294
placeHanging placement p_body
295295
inci p_where
296+
where
297+
p_pat' =
298+
p_pat $
299+
case style of
300+
Function {} -> PatternCtxFunction
301+
PatternBind {} -> PatternCtxTopLevel
302+
Case {} -> PatternCtxCase
303+
Lambda {} -> PatternCtxFunction
304+
LambdaCase {} -> PatternCtxCase
296305

297306
p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R ()
298307
p_grhs = p_grhs' Normal exprPlacement p_hsExpr
@@ -433,7 +442,7 @@ p_stmt' ::
433442
p_stmt' placer render = \case
434443
LastStmt _ body _ _ -> located body render
435444
BindStmt _ p f@(getLocA -> l) -> do
436-
located p p_pat
445+
located p (p_pat PatternCtxDoBlock)
437446
space
438447
txt "<-"
439448
let loc = getLocA p
@@ -855,7 +864,7 @@ p_hsExpr' isApp s = \case
855864
txt "proc"
856865
located p $ \x -> do
857866
breakpoint
858-
inci (p_pat x)
867+
inci (p_pat PatternCtxFunction x)
859868
breakpoint
860869
txt "->"
861870
placeHanging (cmdTopPlacement (unLoc e)) $
@@ -887,17 +896,17 @@ p_patSynBind PSB {..} = do
887896
switchLayout pattern_def_spans $ do
888897
txt "<-"
889898
breakpoint
890-
located psb_def p_pat
899+
located psb_def (p_pat PatternCtxPatSynonym)
891900
ImplicitBidirectional ->
892901
switchLayout pattern_def_spans $ do
893902
equals
894903
breakpoint
895-
located psb_def p_pat
904+
located psb_def (p_pat PatternCtxPatSynonym)
896905
ExplicitBidirectional mgroup -> do
897906
switchLayout pattern_def_spans $ do
898907
txt "<-"
899908
breakpoint
900-
located psb_def p_pat
909+
located psb_def (p_pat PatternCtxPatSynonym)
901910
breakpoint
902911
txt "where"
903912
breakpoint
@@ -1049,64 +1058,81 @@ p_let render localBinds e = sitcc $ do
10491058
space
10501059
sitcc (located e render)
10511060

1052-
p_pat :: Pat GhcPs -> R ()
1053-
p_pat = \case
1061+
data PatternContext
1062+
= PatternCtxFunction
1063+
| PatternCtxCase
1064+
| PatternCtxDoBlock
1065+
| PatternCtxQuasi
1066+
| PatternCtxTopLevel
1067+
| PatternCtxPatSynonym
1068+
1069+
p_pat :: PatternContext -> Pat GhcPs -> R ()
1070+
p_pat ctx = \case
10541071
WildPat _ -> txt "_"
10551072
VarPat _ name -> p_rdrName name
10561073
LazyPat _ pat -> do
10571074
txt "~"
1058-
located pat p_pat
1075+
located pat p_pat'
10591076
AsPat _ name pat -> do
10601077
p_rdrName name
10611078
txt "@"
1062-
located pat p_pat
1079+
located pat p_pat'
10631080
ParPat _ pat ->
1064-
located pat (parens S . p_pat)
1081+
located pat (parens S . p_pat')
10651082
BangPat _ pat -> do
10661083
txt "!"
1067-
located pat p_pat
1084+
located pat p_pat'
10681085
ListPat _ pats ->
1069-
brackets S $ sep commaDel (located' p_pat) pats
1086+
brackets S $ sep commaDel (located' p_pat') pats
10701087
TuplePat _ pats boxing -> do
1088+
let parenStyle =
1089+
-- don't indent closing paren if possible, because it's
1090+
-- prettier. but closing paren must be indented in certain
1091+
-- scenarios or it's a syntax error.
1092+
case ctx of
1093+
PatternCtxCase -> S
1094+
PatternCtxTopLevel -> S
1095+
PatternCtxDoBlock -> S
1096+
_ -> N
10711097
let parens' =
10721098
case boxing of
1073-
Boxed -> parens S
1074-
Unboxed -> parensHash S
1075-
parens' $ sep commaDel (sitcc . located' p_pat) pats
1099+
Boxed -> parens
1100+
Unboxed -> parensHash
1101+
parens' parenStyle $ sep commaDel (sitcc . located' p_pat') pats
10761102
SumPat _ pat tag arity ->
1077-
p_unboxedSum S tag arity (located pat p_pat)
1103+
p_unboxedSum S tag arity (located pat p_pat')
10781104
ConPat _ pat details ->
10791105
case details of
10801106
PrefixCon tys xs -> sitcc $ do
10811107
p_rdrName pat
10821108
unless (null tys && null xs) breakpoint
10831109
inci . sitcc $
1084-
sep breakpoint (sitcc . either p_hsConPatTyArg (located' p_pat)) $
1110+
sep breakpoint (sitcc . either p_hsConPatTyArg (located' p_pat')) $
10851111
(Left <$> tys) <> (Right <$> xs)
10861112
RecCon (HsRecFields fields dotdot) -> do
10871113
p_rdrName pat
10881114
breakpoint
10891115
let f = \case
10901116
Nothing -> txt ".."
1091-
Just x -> located x p_pat_hsFieldBind
1117+
Just x -> located x (p_pat_hsFieldBind ctx)
10921118
inci . braces N . sep commaDel f $
10931119
case dotdot of
10941120
Nothing -> Just <$> fields
10951121
Just (L _ (RecFieldsDotDot n)) -> (Just <$> take n fields) ++ [Nothing]
10961122
InfixCon l r -> do
10971123
switchLayout [getLocA l, getLocA r] $ do
1098-
located l p_pat
1124+
located l p_pat'
10991125
breakpoint
11001126
inci $ do
11011127
p_rdrName pat
11021128
space
1103-
located r p_pat
1129+
located r p_pat'
11041130
ViewPat _ expr pat -> sitcc $ do
11051131
located expr p_hsExpr
11061132
space
11071133
txt "->"
11081134
breakpoint
1109-
inci (located pat p_pat)
1135+
inci (located pat p_pat')
11101136
SplicePat _ splice -> p_hsUntypedSplice DollarSplice splice
11111137
LitPat _ p -> atom p
11121138
NPat _ v (isJust -> isNegated) _ -> do
@@ -1123,28 +1149,30 @@ p_pat = \case
11231149
space
11241150
located k (atom . ol_val)
11251151
SigPat _ pat HsPS {..} -> do
1126-
located pat p_pat
1152+
located pat p_pat'
11271153
p_typeAscription (lhsTypeToSigType hsps_body)
11281154
EmbTyPat _ (HsTP _ ty) -> do
11291155
txt "type"
11301156
space
11311157
located ty p_hsType
11321158
InvisPat _ tyPat -> p_tyPat tyPat
1159+
where
1160+
p_pat' = p_pat ctx
11331161

11341162
p_tyPat :: HsTyPat GhcPs -> R ()
11351163
p_tyPat (HsTP _ ty) = txt "@" *> located ty p_hsType
11361164

11371165
p_hsConPatTyArg :: HsConPatTyArg GhcPs -> R ()
11381166
p_hsConPatTyArg (HsConPatTyArg _ patSigTy) = p_tyPat patSigTy
11391167

1140-
p_pat_hsFieldBind :: HsRecField GhcPs (LPat GhcPs) -> R ()
1141-
p_pat_hsFieldBind HsFieldBind {..} = do
1168+
p_pat_hsFieldBind :: PatternContext -> HsRecField GhcPs (LPat GhcPs) -> R ()
1169+
p_pat_hsFieldBind ctx HsFieldBind {..} = do
11421170
located hfbLHS p_fieldOcc
11431171
unless hfbPun $ do
11441172
space
11451173
equals
11461174
breakpoint
1147-
inci (located hfbRHS p_pat)
1175+
inci (located hfbRHS (p_pat ctx))
11481176

11491177
p_unboxedSum :: BracketStyle -> ConTag -> Arity -> R () -> R ()
11501178
p_unboxedSum s tag arity m = do
@@ -1197,7 +1225,7 @@ p_hsQuote anns = \case
11971225
| any (isJust . matchAddEpAnn AnnOpenEQ) anns = ""
11981226
| otherwise = "e"
11991227
quote name (located expr p_hsExpr)
1200-
PatBr _ pat -> located pat (quote "p" . p_pat)
1228+
PatBr _ pat -> located pat (quote "p" . p_pat PatternCtxQuasi)
12011229
DecBrL _ decls -> quote "d" (handleStarIsType decls (p_hsDecls Free decls))
12021230
DecBrG _ _ -> notImplemented "DecBrG" -- result of renamer
12031231
TypBr _ ty -> quote "t" (located ty (handleStarIsType ty . p_hsType))

0 commit comments

Comments
 (0)