on master help sign in
Make function arguments look more consistent
3abadaeUtku Demir
data/examples/declaration/rewrite-rule/basic1-out.hs+2−1
@@ -10,7 +10,8 @@
10
10
11{-# RULES
11{-# RULES
12"map/map"
12"map/map"
13− map f
13+ map
14+ f
14 (map g xs) =
15 (map g xs) =
15 map
16 map
16 (f . g)
17 (f . g)
data/examples/declaration/rewrite-rule/phase-out.hs+4−2
@@ -1,6 +1,7 @@
1{-# RULES
1{-# RULES
2"map/map" [2]
2"map/map" [2]
3− map f
3+ map
4+ f
4 (map g xs) =
5 (map g xs) =
5 map
6 map
6 (f . g)
7 (f . g)
@@ -9,7 +10,8 @@
9
10
10{-# RULES
11{-# RULES
11"map/map" [1] forall x y z.
12"map/map" [1] forall x y z.
12− map f
13+ map
14+ f
13 (map g xs) =
15 (map g xs) =
14 map
16 map
15 (f . g)
17 (f . g)
data/examples/declaration/value/function/application-out.hsnew file+17
@@ -0,0 +1,17 @@
1+foo =
2+ f1
3+ p1
4+ p2
5+ p3
6+
7+foo' =
8+ f2
9+ p1
10+ p2
11+ p3
12+
13+foo'' =
14+ f3
15+ p1
16+ p2
17+ p3
data/examples/declaration/value/function/application.hsnew file+12
@@ -0,0 +1,12 @@
1+foo =
2+ f1
3+ p1
4+ p2 p3
5+
6+foo' = f2 p1
7+ p2
8+ p3
9+
10+foo'' =
11+ f3 p1 p2
12+ p3
data/examples/declaration/value/function/block-arguments-out.hs+2
@@ -25,3 +25,5 @@ f7 = foo \x -> y
25
25
26f8 = foo \x ->
26f8 = foo \x ->
27 y
27 y
28+
29+f9 = foo do { bar } baz
data/examples/declaration/value/function/block-arguments.hs+2
@@ -21,3 +21,5 @@ f7 = foo \x -> y
21
21
22f8 = foo \x ->
22f8 = foo \x ->
23 y
23 y
24+
25+f9 = foo do { bar } baz
data/examples/declaration/value/function/static-pointers-out.hs+2−1
@@ -14,6 +14,7 @@ bar =
14baz :: StaticPtr Bool
14baz :: StaticPtr Bool
15baz =
15baz =
16 static
16 static
17− ( fun 1
17+ ( fun
18+ 1
18 2
19 2
19 )
20 )
src/Ormolu/Exception.hs+3−1
@@ -51,7 +51,9 @@ instance Exception OrmoluException where
51 )
51 )
52 ++ ["Please, consider reporting the bug."]
52 ++ ["Please, consider reporting the bug."]
53 OrmoluNonIdempotentOutput loc left right ->
53 OrmoluNonIdempotentOutput loc left right ->
54+ showParsingErr
54− showParsingErr "Formatting is not idempotent:" loc
55+ "Formatting is not idempotent:"
56+ loc
55 ["before: " ++ show left, "after: " ++ show right]
57 ["before: " ++ show left, "after: " ++ show right]
56 ++ "Please, consider reporting the bug.\n"
58 ++ "Please, consider reporting the bug.\n"
57
59
src/Ormolu/Parser/CommentStream.hs+2−1
@@ -62,7 +62,8 @@ mkCommentStream extraComments pstate =
62 mapMaybe toRealSpan $
62 mapMaybe toRealSpan $
63 extraComments
63 extraComments
64 ++ mapMaybe (liftMaybe . fmap unAnnotationComment) (GHC.comment_q pstate)
64 ++ mapMaybe (liftMaybe . fmap unAnnotationComment) (GHC.comment_q pstate)
65+ ++ concatMap
65− ++ concatMap (mapMaybe (liftMaybe . fmap unAnnotationComment) . snd)
66+ (mapMaybe (liftMaybe . fmap unAnnotationComment) . snd)
66 (GHC.annotations_comments pstate)
67 (GHC.annotations_comments pstate)
67
68
68-- | Test whether a 'Comment' looks like a Haddock following a definition,
69-- | Test whether a 'Comment' looks like a Haddock following a definition,
src/Ormolu/Printer.hs+2−1
@@ -21,7 +21,8 @@ printModule ::
21 -- | Resulting rendition
21 -- | Resulting rendition
22 Text
22 Text
23printModule ParseResult {..} =
23printModule ParseResult {..} =
24+ runR
24− runR (p_hsModule prExtensions prParsedSource)
25+ (p_hsModule prExtensions prParsedSource)
25 (mkSpanStream prParsedSource)
26 (mkSpanStream prParsedSource)
26 prCommentStream
27 prCommentStream
27 prAnns
28 prAnns
src/Ormolu/Printer/Meat/Declaration/Value.hs+39−18
@@ -19,7 +19,7 @@ import Data.Bool (bool)
19import Data.Char (isPunctuation, isSymbol)
19import Data.Char (isPunctuation, isSymbol)
20import Data.Data hiding (Infix, Prefix)
20import Data.Data hiding (Infix, Prefix)
21import Data.List (intersperse, sortOn)
21import Data.List (intersperse, sortOn)
22−import Data.List.NonEmpty (NonEmpty ((:|)))
22+import Data.List.NonEmpty ((<|), NonEmpty ((:|)))
23import qualified Data.List.NonEmpty as NE
23import qualified Data.List.NonEmpty as NE
24import Data.Text (Text)
24import Data.Text (Text)
25import qualified Data.Text as Text
25import qualified Data.Text as Text
@@ -237,7 +237,8 @@ p_match' placer render style isInfix strictness m_pats m_grhss = do
237 combineSrcSpans' $
237 combineSrcSpans' $
238 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
238 getGRHSSpan . unLoc <$> NE.fromList grhssGRHSs
239 patGrhssSpan =
239 patGrhssSpan =
240+ maybe
240− maybe grhssSpan
241+ grhssSpan
241 (combineSrcSpans grhssSpan . srcLocSpan)
242 (combineSrcSpans grhssSpan . srcLocSpan)
242 endOfPats
243 endOfPats
243 placement =
244 placement =
@@ -525,25 +526,44 @@ p_hsExpr' s = \case
525 breakpoint
526 breakpoint
526 inci (p_matchGroup LambdaCase mgroup)
527 inci (p_matchGroup LambdaCase mgroup)
527 HsApp NoExt f x -> do
528 HsApp NoExt f x -> do
528− -- We hang only the last function argument. In order to do this, we only
529+ let -- In order to format function applications with multiple parameters
529− -- call 'placeHanging' on the topmost 'HsApp', and then use
530+ -- nicer, traverse the AST to gather the function and all the
530− -- 'p_withoutHanging' for the descendants.
531+ -- parameters together.
531− let p_withoutHanging (HsApp NoExt f' x') = do
532+ gatherArgs f' knownArgs =
532 case f' of
533 case f' of
533− L _ (HsApp _ _ _) -> located f' p_withoutHanging
534+ L _ (HsApp _ l r) -> gatherArgs l (r <| knownArgs)
534− _ -> located f' (p_hsExpr' s)
535+ _ -> (f', knownArgs)
535− breakpoint
536+ (func, args) = gatherArgs f (x :| [])
536− inci $ located x' p_hsExpr
537+ -- We need to handle the last argument specially if it is a
537− p_withoutHanging e = p_hsExpr e
538+ -- hanging construct, so separate it from the rest.
538− -- Only use the hanging placement if the function spans a single
539+ (initp, lastp) = (NE.init args, NE.last args)
540+ initSpan = combineSrcSpans' $ getLoc f :| map getLoc initp
541+ -- Hang the last argument only if the initial arguments spans one
539 -- line.
542 -- line.
540 placement =
543 placement =
541− if isOneLineSpan (getLoc f)
544+ if isOneLineSpan initSpan
542− then exprPlacement (unLoc x)
545+ then exprPlacement (unLoc lastp)
543 else Normal
546 else Normal
547+ -- If the last argument is not hanging, just separate every argument as
548+ -- usual. If it is hanging, print the initial arguments and hang the
549+ -- last one. Also, use braces around the every argument except the last
550+ -- one.
551+ case placement of
552+ Normal -> do
544− useBraces (located f p_withoutHanging)
553+ useBraces $ do
554+ located func (p_hsExpr' s)
555+ breakpoint
556+ inci $ sep breakpoint (located' p_hsExpr) initp
557+ inci $ do
558+ unless (null initp) breakpoint
559+ located lastp p_hsExpr
560+ Hanging -> do
561+ useBraces . switchLayout [initSpan] $ do
562+ located func (p_hsExpr' s)
563+ breakpoint
564+ sep breakpoint (located' p_hsExpr) initp
545− placeHanging placement $
565+ placeHanging placement $
546− located x p_hsExpr
566+ located lastp p_hsExpr
547 HsAppType a e -> do
567 HsAppType a e -> do
548 located e p_hsExpr
568 located e p_hsExpr
549 breakpoint
569 breakpoint
@@ -1089,7 +1109,8 @@ p_stringLit src =
1089 zipPrevNext :: [a] -> [(Maybe a, a, Maybe a)]
1109 zipPrevNext :: [a] -> [(Maybe a, a, Maybe a)]
1090 zipPrevNext xs =
1110 zipPrevNext xs =
1091 let z =
1111 let z =
1112+ zip
1092− zip (zip (Nothing : map Just xs) xs)
1113+ (zip (Nothing : map Just xs) xs)
1093 (map Just (tail xs) ++ repeat Nothing)
1114 (map Just (tail xs) ++ repeat Nothing)
1094 in map (\((p, x), n) -> (p, x, n)) z
1115 in map (\((p, x), n) -> (p, x, n)) z
1095 orig (_, x, _) = x
1116 orig (_, x, _) = x
src/Ormolu/Printer/Meat/Type.hs+2−1
@@ -191,7 +191,8 @@ p_conDeclField :: ConDeclField GhcPs -> R ()
191p_conDeclField ConDeclField {..} = do
191p_conDeclField ConDeclField {..} = do
192 mapM_ (p_hsDocString Pipe True) cd_fld_doc
192 mapM_ (p_hsDocString Pipe True) cd_fld_doc
193 sitcc $
193 sitcc $
194+ sep
194− sep (comma >> breakpoint)
195+ (comma >> breakpoint)
195 (located' (p_rdrName . rdrNameFieldOcc))
196 (located' (p_rdrName . rdrNameFieldOcc))
196 cd_fld_names
197 cd_fld_names
197 space
198 space