diff --git a/data/examples/declaration/annotation/annotation-out.hs b/data/examples/declaration/annotation/annotation-out.hs index 9aa21b0..19bd3c1 100644 --- a/data/examples/declaration/annotation/annotation-out.hs +++ b/data/examples/declaration/annotation/annotation-out.hs @@ -4,7 +4,7 @@ module ( 5 :: Int - ) + ) #-} {-# ANN foo "hey" #-} diff --git a/data/examples/declaration/class/default-signatures-out.hs b/data/examples/declaration/class/default-signatures-out.hs index ed1bf08..fcb1144 100644 --- a/data/examples/declaration/class/default-signatures-out.hs +++ b/data/examples/declaration/class/default-signatures-out.hs @@ -20,9 +20,9 @@ class Bar a where -- Pointless comment default bar - :: ( Read a - , Semigroup a - ) + :: ( Read a, + Semigroup a + ) => a -> a -> a diff --git a/data/examples/declaration/class/dependency-super-classes-out.hs b/data/examples/declaration/class/dependency-super-classes-out.hs index 73a3ac0..b9f4139 100644 --- a/data/examples/declaration/class/dependency-super-classes-out.hs +++ b/data/examples/declaration/class/dependency-super-classes-out.hs @@ -9,9 +9,9 @@ class (MonadReader r s, MonadWriter w m) => MonadState s m | m -> s where -- | 'MonadParsec' class - ( Stream s -- Token streams - , MonadPlus m -- Potential for failure - ) + ( Stream s, -- Token streams + MonadPlus m -- Potential for failure + ) => MonadParsec e s m | m -> e s where diff --git a/data/examples/declaration/class/functional-dependencies-out.hs b/data/examples/declaration/class/functional-dependencies-out.hs index c0c0ff2..c489d33 100644 --- a/data/examples/declaration/class/functional-dependencies-out.hs +++ b/data/examples/declaration/class/functional-dependencies-out.hs @@ -10,10 +10,10 @@ class Bar a b | a -> b, b -> a where -- | Something else. class Baz a b c d - | a b -> c d -- Foo - , b c -> a d -- Bar - , a c -> b d -- Baz - , a c d -> b - , a b d -> a b c d where + | a b -> c d, -- Foo + b c -> a d, -- Bar + a c -> b d, -- Baz + a c d -> b, + a b d -> a b c d where baz :: a -> b diff --git a/data/examples/declaration/class/poly-kinded-classes-out.hs b/data/examples/declaration/class/poly-kinded-classes-out.hs index 2d6b65f..bb09c4f 100644 --- a/data/examples/declaration/class/poly-kinded-classes-out.hs +++ b/data/examples/declaration/class/poly-kinded-classes-out.hs @@ -6,4 +6,4 @@ class Bar ( a -- Variable :: * -- Star - ) + ) diff --git a/data/examples/declaration/class/single-parameters-out.hs b/data/examples/declaration/class/single-parameters-out.hs index 201b25f..68fe8dd 100644 --- a/data/examples/declaration/class/single-parameters-out.hs +++ b/data/examples/declaration/class/single-parameters-out.hs @@ -13,9 +13,9 @@ class Baz a where -- | Baz baz - :: ( a - , a -- ^ First argument - ) + :: ( a, + a -- ^ First argument + ) -> a -- ^ Second argument -> a -- ^ Return value diff --git a/data/examples/declaration/class/super-classes-out.hs b/data/examples/declaration/class/super-classes-out.hs index ee1ce17..5f84540 100644 --- a/data/examples/declaration/class/super-classes-out.hs +++ b/data/examples/declaration/class/super-classes-out.hs @@ -7,8 +7,8 @@ class => Baz a class - ( Foo a -- Foo? - , Bar a -- Bar? - , Baz a -- Baz - ) + ( Foo a, -- Foo? + Bar a, -- Bar? + Baz a -- Baz + ) => BarBar a diff --git a/data/examples/declaration/class/type-operators-out.hs b/data/examples/declaration/class/type-operators-out.hs index 73f700c..931d13b 100644 --- a/data/examples/declaration/class/type-operators-out.hs +++ b/data/examples/declaration/class/type-operators-out.hs @@ -17,7 +17,7 @@ class class ( f :. g - ) + ) a class a `Pair` b @@ -31,5 +31,5 @@ class (f `Product` g) a class ( f `Sum` g - ) + ) a diff --git a/data/examples/declaration/data/deriving-strategies-out.hs b/data/examples/declaration/data/deriving-strategies-out.hs index d5c4ef2..cfde26b 100644 --- a/data/examples/declaration/data/deriving-strategies-out.hs +++ b/data/examples/declaration/data/deriving-strategies-out.hs @@ -2,9 +2,9 @@ newtype Foo = Foo Int deriving stock (Eq, Show, Generic) deriving anyclass - ( ToJSON - , FromJSON - ) + ( ToJSON, + FromJSON + ) deriving newtype (Num) deriving (Monoid) via (Sum Int) deriving diff --git a/data/examples/declaration/data/gadt/multiple-declaration-out.hs b/data/examples/declaration/data/gadt/multiple-declaration-out.hs index 8e5956a..397bd35 100644 --- a/data/examples/declaration/data/gadt/multiple-declaration-out.hs +++ b/data/examples/declaration/data/gadt/multiple-declaration-out.hs @@ -2,14 +2,14 @@ data GADT0 a where GADT01, GADT02 :: Int -> GADT0 a data GADT1 a where - GADT11 - , GADT12 + GADT11, + GADT12 :: Int -> GADT1 a data GADT2 a where - GADT21 - , GADT21 - , GADT22 + GADT21, + GADT21, + GADT22 :: Int -> GADT2 a diff --git a/data/examples/declaration/data/gadt/record-out.hs b/data/examples/declaration/data/gadt/record-out.hs index 8098083..fdaccf6 100644 --- a/data/examples/declaration/data/gadt/record-out.hs +++ b/data/examples/declaration/data/gadt/record-out.hs @@ -2,10 +2,11 @@ data Foo where Foo :: {fooX :: Int} -> Foo Bar - :: { fooY :: Int - , fooBar, fooBaz :: Bool - , fooFoo - , barBar + :: { fooY :: Int, + fooBar, fooBaz :: Bool, + fooFoo, + barBar, + bazBaz :: Int - } + } -> Foo diff --git a/data/examples/declaration/data/gadt/record.hs b/data/examples/declaration/data/gadt/record.hs index 8cca4df..acdf3cb 100644 --- a/data/examples/declaration/data/gadt/record.hs +++ b/data/examples/declaration/data/gadt/record.hs @@ -5,5 +5,6 @@ data Foo where Bar :: { fooY :: Int , fooBar, fooBaz :: Bool , fooFoo - , barBar :: Int + , barBar + , bazBaz :: Int } -> Foo diff --git a/data/examples/declaration/data/multiline-names-out.hs b/data/examples/declaration/data/multiline-names-out.hs index 0e16de0..77d0249 100644 --- a/data/examples/declaration/data/multiline-names-out.hs +++ b/data/examples/declaration/data/multiline-names-out.hs @@ -11,7 +11,7 @@ data (f :* g) a = f a :* g a data ( f :+ g - ) + ) a = L (f a) | R (g a) @@ -23,7 +23,7 @@ data (f `Product` g) a = f a `Product` g a data ( f `Sum` g - ) + ) a = L' (f a) | R' (g a) diff --git a/data/examples/declaration/data/record-out.hs b/data/examples/declaration/data/record-out.hs index a7d329c..38a11aa 100644 --- a/data/examples/declaration/data/record-out.hs +++ b/data/examples/declaration/data/record-out.hs @@ -1,18 +1,18 @@ -- | Something. data Foo = Foo - { fooX :: Int -- ^ X - , fooY :: Int -- ^ Y - , fooBar, fooBaz :: NonEmpty (Identity Bool) -- ^ BarBaz - , fooGag - , fooGog + { fooX :: Int, -- ^ X + fooY :: Int, -- ^ Y + fooBar, fooBaz :: NonEmpty (Identity Bool), -- ^ BarBaz + fooGag, + fooGog :: NonEmpty ( Indentity Bool - ) + ), -- ^ GagGog - , fooFoo - , barBar + fooFoo, + barBar :: Int -- ^ Huh! - } + } deriving (Eq, Show) diff --git a/data/examples/declaration/default/default-out.hs b/data/examples/declaration/default/default-out.hs index e1dee1c..82d13ed 100644 --- a/data/examples/declaration/default/default-out.hs +++ b/data/examples/declaration/default/default-out.hs @@ -1,7 +1,7 @@ default (Int, Foo, Bar) default - ( Int - , Foo - , Bar - ) + ( Int, + Foo, + Bar + ) diff --git a/data/examples/declaration/instance/contexts-out.hs b/data/examples/declaration/instance/contexts-out.hs index 4eba55a..06552de 100644 --- a/data/examples/declaration/instance/contexts-out.hs +++ b/data/examples/declaration/instance/contexts-out.hs @@ -3,38 +3,41 @@ instance Eq a => Eq [a] where (==) _ _ = False instance - ( Ord a - , Ord b - ) - => Ord (a, b) where + ( Ord a, + Ord b + ) + => Ord (a, b) + where compare _ _ = GT instance (Show a, Show b) => Show - ( a - , b - ) where + ( a, + b + ) + where showsPrec _ _ = showString "" instance - ( Read a -- Foo - , Read b - , Read - ( c - , -- Bar - d - ) - ) + ( Read a, -- Foo + Read b, + Read + ( c, + -- Bar + d + ) + ) => Read - ( a - , -- Baz - b - , ( c -- Quux - , d + ( a, + -- Baz + b, + ( c, -- Quux + d + ) ) - ) where + where readsPrec = undefined diff --git a/data/examples/declaration/instance/data-family-instances-out.hs b/data/examples/declaration/instance/data-family-instances-out.hs index 31f3e3e..bec0fea 100644 --- a/data/examples/declaration/instance/data-family-instances-out.hs +++ b/data/examples/declaration/instance/data-family-instances-out.hs @@ -7,17 +7,17 @@ data instance Foo [Int] = IntListFoo - ( Int - , Int - ) - ( Double - , Double - ) + ( Int, + Int + ) + ( Double, + Double + ) newtype instance Foo [Double] = DoubleListFoo { unDoubleListFoo :: Double - } + } data instance Bar Double a = DoubleBar diff --git a/data/examples/declaration/instance/overlappable-instances-out.hs b/data/examples/declaration/instance/overlappable-instances-out.hs index 8a83a06..2e64a0c 100644 --- a/data/examples/declaration/instance/overlappable-instances-out.hs +++ b/data/examples/declaration/instance/overlappable-instances-out.hs @@ -13,6 +13,7 @@ instance {-# OVERLAPS #-} Eq Double where instance {-# INCOHERENT #-} Ord - Double where + Double + where compare _ _ = GT diff --git a/data/examples/declaration/instance/type-family-instances-out.hs b/data/examples/declaration/instance/type-family-instances-out.hs index c305f2a..d751ff9 100644 --- a/data/examples/declaration/instance/type-family-instances-out.hs +++ b/data/examples/declaration/instance/type-family-instances-out.hs @@ -5,9 +5,9 @@ type instance Foo Int = Int type instance Foo [Int] = - ( Int - , Int - ) + ( Int, + Int + ) type instance Bar Int [Int] Double = (Int, Double) @@ -16,6 +16,6 @@ type instance [Int] [Int] Double = - ( Int - , Double - ) + ( Int, + Double + ) diff --git a/data/examples/declaration/rewrite-rule/type-signature-out.hs b/data/examples/declaration/rewrite-rule/type-signature-out.hs index ade3ebd..01dc12e 100644 --- a/data/examples/declaration/rewrite-rule/type-signature-out.hs +++ b/data/examples/declaration/rewrite-rule/type-signature-out.hs @@ -10,7 +10,7 @@ :: forall b. (a -> b -> b) -> b -> b - ). + ). foldr k z (build g) = g k z #-} diff --git a/data/examples/declaration/signature/complete/complete-out.hs b/data/examples/declaration/signature/complete/complete-out.hs index 5d61198..47320d3 100644 --- a/data/examples/declaration/signature/complete/complete-out.hs +++ b/data/examples/declaration/signature/complete/complete-out.hs @@ -3,8 +3,8 @@ {-# COMPLETE A, B #-} {-# COMPLETE - A - , B - , C + A, + B, + C :: Foo #-} diff --git a/data/examples/declaration/signature/minimal/minimal-out.hs b/data/examples/declaration/signature/minimal/minimal-out.hs index 5d03495..6416153 100644 --- a/data/examples/declaration/signature/minimal/minimal-out.hs +++ b/data/examples/declaration/signature/minimal/minimal-out.hs @@ -5,10 +5,10 @@ class Foo a where {-# MINIMAL a | ( b, c, d - | e - , f - ) - | g + | e, + f + ) + | g #-} (==) :: a -> a -> Bool diff --git a/data/examples/declaration/signature/specialize/specialize-instance-out.hs b/data/examples/declaration/signature/specialize/specialize-instance-out.hs index 709bd20..3f320f1 100644 --- a/data/examples/declaration/signature/specialize/specialize-instance-out.hs +++ b/data/examples/declaration/signature/specialize/specialize-instance-out.hs @@ -12,7 +12,7 @@ instance (Num r, V.Vector v r, Factored m r) => Num (VT v m r) where {-# SPECIALIZE instance ( Factored m Int => Num (VT U.Vector m Int) - ) + ) #-} VT x + VT y = VT $ V.zipWith (+) x y diff --git a/data/examples/declaration/signature/specialize/specialize-out.hs b/data/examples/declaration/signature/specialize/specialize-out.hs index 7381881..5bea8d0 100644 --- a/data/examples/declaration/signature/specialize/specialize-out.hs +++ b/data/examples/declaration/signature/specialize/specialize-out.hs @@ -11,14 +11,14 @@ baz :: Num a => a -> a baz = id {-# SPECIALIZE [~2] baz :: Int - -> Int + -> Int #-} {-# SPECIALIZE fits13Bits :: Int -> Bool, Integer -> Bool #-} {-# SPECIALIZE fits13Bits :: Int - -> Bool - , Integer -> Bool + -> Bool, + Integer -> Bool #-} fits13Bits :: Integral a => a -> Bool fits13Bits x = x >= -4096 && x < 4096 diff --git a/data/examples/declaration/signature/type/arguments-out.hs b/data/examples/declaration/signature/type/arguments-out.hs index b3c7205..5cffbd8 100644 --- a/data/examples/declaration/signature/type/arguments-out.hs +++ b/data/examples/declaration/signature/type/arguments-out.hs @@ -10,6 +10,6 @@ functionName -> AnotherLongDataTypeName -> b -> c - ) + ) -> (c -> d) -> (a, b, c, d) diff --git a/data/examples/declaration/signature/type/context-multi-line-out.hs b/data/examples/declaration/signature/type/context-multi-line-out.hs index 6b61d25..b5a237c 100644 --- a/data/examples/declaration/signature/type/context-multi-line-out.hs +++ b/data/examples/declaration/signature/type/context-multi-line-out.hs @@ -1,8 +1,8 @@ functionName - :: ( C1 - , C2 - , C3 - ) + :: ( C1, + C2, + C3 + ) => a -> b -> c diff --git a/data/examples/declaration/signature/type/long-multiline-applications-out.hs b/data/examples/declaration/signature/type/long-multiline-applications-out.hs index 52446f5..d31d4f6 100644 --- a/data/examples/declaration/signature/type/long-multiline-applications-out.hs +++ b/data/examples/declaration/signature/type/long-multiline-applications-out.hs @@ -10,6 +10,6 @@ functionName -> AnotherLongDataTypeName4 -> b -> c - ) + ) -> (c -> d) -> (a, b, c, d) diff --git a/data/examples/declaration/signature/type/multi-value-out.hs b/data/examples/declaration/signature/type/multi-value-out.hs index e7eea29..d7d8cf1 100644 --- a/data/examples/declaration/signature/type/multi-value-out.hs +++ b/data/examples/declaration/signature/type/multi-value-out.hs @@ -1,6 +1,6 @@ foo, bar :: Int -foo - , bar - , baz +foo, + bar, + baz :: Int diff --git a/data/examples/declaration/splice/typed-splice-out.hs b/data/examples/declaration/splice/typed-splice-out.hs index 76ca321..8fbf0d5 100644 --- a/data/examples/declaration/splice/typed-splice-out.hs +++ b/data/examples/declaration/splice/typed-splice-out.hs @@ -2,6 +2,6 @@ x = $$( foo bar - ) + ) x = $$foo diff --git a/data/examples/declaration/splice/untyped-splice-out.hs b/data/examples/declaration/splice/untyped-splice-out.hs index f7e93e8..6bd9d00 100644 --- a/data/examples/declaration/splice/untyped-splice-out.hs +++ b/data/examples/declaration/splice/untyped-splice-out.hs @@ -5,6 +5,6 @@ x = $(foo bar) x = $( foo bar - ) + ) x = $foo diff --git a/data/examples/declaration/value/function/arithmetic-sequences-out.hs b/data/examples/declaration/value/function/arithmetic-sequences-out.hs index f10d2ce..57c7822 100644 --- a/data/examples/declaration/value/function/arithmetic-sequences-out.hs +++ b/data/examples/declaration/value/function/arithmetic-sequences-out.hs @@ -4,14 +4,14 @@ foo' = [0 .. 5] bar x = [ 0 - .. x - ] + .. x + ] baz x = - [ 1 - , 3 - .. x - ] + [ 1, + 3 + .. x + ] barbaz x = [0, 1 ..] diff --git a/data/examples/declaration/value/function/arrow/expression-forms-out.hs b/data/examples/declaration/value/function/arrow/expression-forms-out.hs index 535e694..70b9020 100644 --- a/data/examples/declaration/value/function/arrow/expression-forms-out.hs +++ b/data/examples/declaration/value/function/arrow/expression-forms-out.hs @@ -6,8 +6,8 @@ bar f g x y = (| test ( f -< x - ) + ) ( g -< y - ) - |) + ) + |) diff --git a/data/examples/declaration/value/function/arrow/proc-applications-out.hs b/data/examples/declaration/value/function/arrow/proc-applications-out.hs index 9fefa29..26011e9 100644 --- a/data/examples/declaration/value/function/arrow/proc-applications-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-applications-out.hs @@ -4,15 +4,15 @@ foo x = proc a -> a -< x bar f x = proc - ( y - , z - , w - ) + ( y, + z, + w + ) -> f -< -- The value - ( x -- Foo - , w -- Bar - , z -- Baz - ) + ( x, -- Foo + w, -- Bar + z -- Baz + ) baz x = proc a -> a -<< x diff --git a/data/examples/declaration/value/function/arrow/proc-cases-out.hs b/data/examples/declaration/value/function/arrow/proc-cases-out.hs index ed23d33..394c18b 100644 --- a/data/examples/declaration/value/function/arrow/proc-cases-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-cases-out.hs @@ -5,9 +5,9 @@ foo f = proc a -> case a of Left b -> f -< b bar f g h j = proc a -> case a of Left - ( (a, b) - , (c, d) - ) -> f (a <> c) -< b <> d + ( (a, b), + (c, d) + ) -> f (a <> c) -< b <> d Right (Left a) -> h -< a diff --git a/data/examples/declaration/value/function/arrow/proc-do-complex-out.hs b/data/examples/declaration/value/function/arrow/proc-do-complex-out.hs index ab9a4c3..fd821db 100644 --- a/data/examples/declaration/value/function/arrow/proc-do-complex-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-do-complex-out.hs @@ -6,37 +6,37 @@ foo h ma = proc - ( (a, b) - , (c, d) - , (e, f) - ) + ( (a, b), + (c, d), + (e, f) + ) -> do -- Begin do (x, y) <- -- GHC parser fails if layed out over multiple lines f -- Call into f - ( a - , c -- Tuple together arguments - ) - ( b - , d - ) -< - ( b + 1 -- Funnel into arrow - , d * b - ) + ( a, + c -- Tuple together arguments + ) + ( b, + d + ) -< + ( b + 1, -- Funnel into arrow + d * b + ) if x `mod` y == 0 -- Basic condition then case e of -- Only left case is relevant Left - ( z - , w - ) -> \u -> -- Procs can have lambdas + ( z, + w + ) -> \u -> -- Procs can have lambdas let v = u ^ -- Actually never used 2 in ( returnA -< -- Just do the calculation (x + y * z) - ) + ) else do let u = x -- Let bindings bind expressions, not commands @@ -47,8 +47,8 @@ foo n -> ( ( h . g -< y -- First actual use of y + ) ) - ) returnA -< () -- Sometimes execute effects if i > 0 @@ -58,4 +58,4 @@ foo ( i + x * y -- Just do the calculation - ) + ) diff --git a/data/examples/declaration/value/function/arrow/proc-do-simple-out.hs b/data/examples/declaration/value/function/arrow/proc-do-simple-out.hs index b8c8c29..701b940 100644 --- a/data/examples/declaration/value/function/arrow/proc-do-simple-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-do-simple-out.hs @@ -28,9 +28,9 @@ bazbaz f g h = proc (a, b, c) -> do h x y -< - ( a - , b - , c - ) + ( a, + b, + c + ) returnA -< (x, y, z) diff --git a/data/examples/declaration/value/function/arrow/proc-forms-out.hs b/data/examples/declaration/value/function/arrow/proc-forms-out.hs index 0bd4330..604d026 100644 --- a/data/examples/declaration/value/function/arrow/proc-forms-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-forms-out.hs @@ -13,13 +13,13 @@ foo2 f g h x = h g -< y x . y z - ) + ) ( h g . h f -< y z . y x - ) - |) + ) + |) bar0 f g x y = proc _ -> f -< x &&& g -< y diff --git a/data/examples/declaration/value/function/arrow/proc-parentheses-out.hs b/data/examples/declaration/value/function/arrow/proc-parentheses-out.hs index c61ebb8..895c9ae 100644 --- a/data/examples/declaration/value/function/arrow/proc-parentheses-out.hs +++ b/data/examples/declaration/value/function/arrow/proc-parentheses-out.hs @@ -4,15 +4,15 @@ foo f = proc a -> (f -< a) bar f g = proc a -> ( ( (f) - ( g - ) - ) -< + ( g + ) + ) -< ( ( ( ( ( ( g - a + a + ) ) ) ) ) ) ) - ) diff --git a/data/examples/declaration/value/function/arrow/recursive-procs-out.hs b/data/examples/declaration/value/function/arrow/recursive-procs-out.hs index bb3bc8f..48ec88b 100644 --- a/data/examples/declaration/value/function/arrow/recursive-procs-out.hs +++ b/data/examples/declaration/value/function/arrow/recursive-procs-out.hs @@ -6,9 +6,9 @@ foo f g = proc (x, y) -> do g x -< y bar -< - ( a - , b - ) + ( a, + b + ) rec p <- f p -< diff --git a/data/examples/declaration/value/function/do-single-multi-out.hs b/data/examples/declaration/value/function/do-single-multi-out.hs new file mode 100644 index 0000000..e9e055c --- /dev/null +++ b/data/examples/declaration/value/function/do-single-multi-out.hs @@ -0,0 +1,4 @@ +foo = do + ( bar + baz + ) diff --git a/data/examples/declaration/value/function/do-single-multi.hs b/data/examples/declaration/value/function/do-single-multi.hs new file mode 100644 index 0000000..4f85692 --- /dev/null +++ b/data/examples/declaration/value/function/do-single-multi.hs @@ -0,0 +1,2 @@ +foo = do (bar + baz) diff --git a/data/examples/declaration/value/function/implicit-params-out.hs b/data/examples/declaration/value/function/implicit-params-out.hs index c455f85..11ac72c 100644 --- a/data/examples/declaration/value/function/implicit-params-out.hs +++ b/data/examples/declaration/value/function/implicit-params-out.hs @@ -7,9 +7,9 @@ sort = sortBy ?cmp sort' :: ( ?cmp - :: a -> a -> Bool - , ?foo :: Int - ) + :: a -> a -> Bool, + ?foo :: Int + ) => [a] -> [a] sort' = sort diff --git a/data/examples/declaration/value/function/list-comprehensions-out.hs b/data/examples/declaration/value/function/list-comprehensions-out.hs index 5256620..ccad67b 100644 --- a/data/examples/declaration/value/function/list-comprehensions-out.hs +++ b/data/examples/declaration/value/function/list-comprehensions-out.hs @@ -5,21 +5,21 @@ bar x y = [(a, b) | a <- x, even a, b <- y, a != b] barbaz x y z w = [ (a, b, c, d) -- Foo | a <- - x -- Bar - , b <- y -- Baz - , any even [a, b] - , c <- + x, -- Bar + b <- y, -- Baz + any even [a, b], + c <- z * z ^ - 2 -- Bar baz - , d <- + 2, -- Bar baz + d <- w + - w -- Baz bar - , all + w, -- Baz bar + all even - [ a - , b - , c - , d - ] - ] + [ a, + b, + c, + d + ] + ] diff --git a/data/examples/declaration/value/function/multiline-arguments-out.hs b/data/examples/declaration/value/function/multiline-arguments-out.hs index 25f86eb..2e92014 100644 --- a/data/examples/declaration/value/function/multiline-arguments-out.hs +++ b/data/examples/declaration/value/function/multiline-arguments-out.hs @@ -4,5 +4,5 @@ foo ( Bar x y - ) + ) z = x diff --git a/data/examples/declaration/value/function/operator-sections-out.hs b/data/examples/declaration/value/function/operator-sections-out.hs index fabdf77..a3053d2 100644 --- a/data/examples/declaration/value/function/operator-sections-out.hs +++ b/data/examples/declaration/value/function/operator-sections-out.hs @@ -5,9 +5,9 @@ bar = (<> "hello") baz = ( 1 * 2 + - ) + ) ( * 3 ^ 5 - ) + ) quux = (,) <$> foo <$> bar diff --git a/data/examples/declaration/value/function/parallel-comprehensions-out.hs b/data/examples/declaration/value/function/parallel-comprehensions-out.hs index 3665952..3574d0b 100644 --- a/data/examples/declaration/value/function/parallel-comprehensions-out.hs +++ b/data/examples/declaration/value/function/parallel-comprehensions-out.hs @@ -3,35 +3,35 @@ foo x y = [(a, b) | a <- x | b <- y] bar x y z w = [(a, b, c, d) | a <- x, b <- y, a `mod` b == 0 | c <- z | d <- w] baz x y z w = - [ ( a - , b - , c - , d - , e - , f - , g - , h - , i - , j - ) + [ ( a, + b, + c, + d, + e, + f, + g, + h, + i, + j + ) | a <- -- Foo 1 - x -- Foo 2 - , b <- -- Bar 1 - y -- Bar 2 - , a `mod` + x, -- Foo 2 + b <- -- Bar 1 + y, -- Bar 2 + a `mod` b == -- Value 0 - | c <- -- Baz 1 - z * -- Baz 2 - z -- Baz 3 - | d <- w -- Other - | e <- x * x -- Foo bar - | f <- -- Foo baz 1 - y + y -- Foo baz 2 - | h <- z + z * w ^ 2 -- Bar foo - | i <- -- Bar bar 1 - a + -- Bar bar 2 - b -- Bar bar 3 - , j <- -- Bar baz 1 - a + b -- Bar baz 2 - ] + | c <- -- Baz 1 + z * -- Baz 2 + z -- Baz 3 + | d <- w -- Other + | e <- x * x -- Foo bar + | f <- -- Foo baz 1 + y + y -- Foo baz 2 + | h <- z + z * w ^ 2 -- Bar foo + | i <- -- Bar bar 1 + a + -- Bar bar 2 + b, -- Bar bar 3 + j <- -- Bar baz 1 + a + b -- Bar baz 2 + ] diff --git a/data/examples/declaration/value/function/pattern/famous-cardano-pattern-out.hs b/data/examples/declaration/value/function/pattern/famous-cardano-pattern-out.hs new file mode 100644 index 0000000..a019acc --- /dev/null +++ b/data/examples/declaration/value/function/pattern/famous-cardano-pattern-out.hs @@ -0,0 +1,8 @@ +( getNodeSettingsR :<|> + getNodeInfoR :<|> + getNextUpdateR :<|> + restartNodeR + ) :<|> + ( getUtxoR :<|> + getConfirmedProposalsR + ) = client nodeV1Api diff --git a/data/examples/declaration/value/function/pattern/famous-cardano-pattern.hs b/data/examples/declaration/value/function/pattern/famous-cardano-pattern.hs new file mode 100644 index 0000000..0b4a8a7 --- /dev/null +++ b/data/examples/declaration/value/function/pattern/famous-cardano-pattern.hs @@ -0,0 +1,7 @@ +( getNodeSettingsR + :<|> getNodeInfoR + :<|> getNextUpdateR + :<|> restartNodeR + ):<|>( getUtxoR + :<|> getConfirmedProposalsR + ) = client nodeV1Api diff --git a/data/examples/declaration/value/function/pattern/multiline-case-pattern-out.hs b/data/examples/declaration/value/function/pattern/multiline-case-pattern-out.hs new file mode 100644 index 0000000..c11508c --- /dev/null +++ b/data/examples/declaration/value/function/pattern/multiline-case-pattern-out.hs @@ -0,0 +1,15 @@ +readerBench doc name = + runPure $ case (getReader name, getWriter name) of + ( Right (TextReader r, rexts), + Right (TextWriter w, wexts) + ) -> undefined + +f xs = case xs of + [ a, + b + ] -> a + b + +g xs = case xs of + ( a : + bs + ) -> a + b diff --git a/data/examples/declaration/value/function/pattern/multiline-case-pattern.hs b/data/examples/declaration/value/function/pattern/multiline-case-pattern.hs new file mode 100644 index 0000000..28aa092 --- /dev/null +++ b/data/examples/declaration/value/function/pattern/multiline-case-pattern.hs @@ -0,0 +1,12 @@ +readerBench doc name = + runPure $ case (getReader name, getWriter name) of + (Right (TextReader r, rexts), + Right (TextWriter w, wexts)) -> undefined + +f xs = case xs of + [ a, + b ] -> a + b + +g xs = case xs of + (a: + bs) -> a + b diff --git a/data/examples/declaration/value/function/pattern/n-plus-k-pattern-out.hs b/data/examples/declaration/value/function/pattern/n-plus-k-pattern-out.hs index c6957cb..fea6ed4 100644 --- a/data/examples/declaration/value/function/pattern/n-plus-k-pattern-out.hs +++ b/data/examples/declaration/value/function/pattern/n-plus-k-pattern-out.hs @@ -7,4 +7,4 @@ multiline :: Int multiline ( n + 1 - ) = n + ) = n diff --git a/data/examples/declaration/value/function/pattern/record-patterns-out.hs b/data/examples/declaration/value/function/pattern/record-patterns-out.hs index eae0918..0d3dc30 100644 --- a/data/examples/declaration/value/function/pattern/record-patterns-out.hs +++ b/data/examples/declaration/value/function/pattern/record-patterns-out.hs @@ -13,7 +13,7 @@ baz Boom {boom = b, ..} = b quux :: Boom -> Int quux Boom - { boom = a - , foom = b - , .. - } = a + b + { boom = a, + foom = b, + .. + } = a + b diff --git a/data/examples/declaration/value/function/pattern/splice-pattern-out.hs b/data/examples/declaration/value/function/pattern/splice-pattern-out.hs index ad59abf..a3df15e 100644 --- a/data/examples/declaration/value/function/pattern/splice-pattern-out.hs +++ b/data/examples/declaration/value/function/pattern/splice-pattern-out.hs @@ -7,7 +7,7 @@ singleLine = case () of multiline = case () of $( x + y - ) -> () + ) -> () $( y "something" - ) -> () + ) -> () diff --git a/data/examples/declaration/value/function/pattern/view-pattern-out.hs b/data/examples/declaration/value/function/pattern/view-pattern-out.hs index 7c0cb85..1b53a8e 100644 --- a/data/examples/declaration/value/function/pattern/view-pattern-out.hs +++ b/data/examples/declaration/value/function/pattern/view-pattern-out.hs @@ -12,4 +12,4 @@ multiline Foo bar baz - ) = True + ) = True diff --git a/data/examples/declaration/value/function/record-constructors-out.hs b/data/examples/declaration/value/function/record-constructors-out.hs index 2a243da..7dc7788 100644 --- a/data/examples/declaration/value/function/record-constructors-out.hs +++ b/data/examples/declaration/value/function/record-constructors-out.hs @@ -1,8 +1,8 @@ foo = Foo {a = 3} bar = Bar - { abc = foo - , def = Foo {a = 10} - } + { abc = foo, + def = Foo {a = 10} + } baz = Baz {} diff --git a/data/examples/declaration/value/function/record-updaters-out.hs b/data/examples/declaration/value/function/record-updaters-out.hs index bc8b177..749d04e 100644 --- a/data/examples/declaration/value/function/record-updaters-out.hs +++ b/data/examples/declaration/value/function/record-updaters-out.hs @@ -2,8 +2,8 @@ foo x = x {a = 3} bar x = x - { abc = foo - , def = Foo {a = 10} - } + { abc = foo, + def = Foo {a = 10} + } baz x = x {} diff --git a/data/examples/declaration/value/function/record-wildcards-out.hs b/data/examples/declaration/value/function/record-wildcards-out.hs index a147837..f2bd379 100644 --- a/data/examples/declaration/value/function/record-wildcards-out.hs +++ b/data/examples/declaration/value/function/record-wildcards-out.hs @@ -4,10 +4,10 @@ foo x y = Foo {x, y} bar x y z = Bar - { x - , y - , z - , .. - } + { x, + y, + z, + .. + } baz = Baz {..} diff --git a/data/examples/declaration/value/function/static-pointers-out.hs b/data/examples/declaration/value/function/static-pointers-out.hs index 1b417af..d5531cb 100644 --- a/data/examples/declaration/value/function/static-pointers-out.hs +++ b/data/examples/declaration/value/function/static-pointers-out.hs @@ -6,14 +6,14 @@ foo = static 5 bar :: StaticPtr [Int] bar = static - [ 1 - , 2 - , 3 - ] + [ 1, + 2, + 3 + ] baz :: StaticPtr Bool baz = static ( fun 1 - 2 - ) + 2 + ) diff --git a/data/examples/declaration/value/function/transform-comprehensions-out.hs b/data/examples/declaration/value/function/transform-comprehensions-out.hs index 2c39996..9edc56c 100644 --- a/data/examples/declaration/value/function/transform-comprehensions-out.hs +++ b/data/examples/declaration/value/function/transform-comprehensions-out.hs @@ -3,56 +3,56 @@ foo xs ys = [(x, y) | x <- xs, y <- ys, then reverse] foo' xs ys = - [ ( x - , y - ) - | x <- xs - , y <- ys - , -- First comment + [ ( x, + y + ) + | x <- xs, + y <- ys, + -- First comment then reverse -- Second comment - ] + ] bar xs ys = [(x, y) | x <- xs, y <- ys, then sortWith by (x + y)] bar' xs ys = - [ ( x - , y - ) - | x <- xs - , y <- ys - , -- First comment + [ ( x, + y + ) + | x <- xs, + y <- ys, + -- First comment then sortWith by ( x + y -- Second comment - ) - ] + ) + ] baz xs ys = [(x, y) | x <- xs, y <- ys, then group using permutations] baz' xs ys = - [ ( x - , y - ) - | x <- xs - , y <- ys - , -- First comment + [ ( x, + y + ) + | x <- xs, + y <- ys, + -- First comment then group using permutations -- Second comment - ] + ] quux xs ys = [(x, y) | x <- xs, y <- ys, then group by (x + y) using groupWith] quux' xs ys = - [ ( x - , y - ) - | x <- xs - , y <- ys - , -- First comment + [ ( x, + y + ) + | x <- xs, + y <- ys, + -- First comment then group by ( x + y - ) + ) -- Second comment using groupWith -- Third comment - ] + ] diff --git a/data/examples/declaration/value/function/tuple-sections-out.hs b/data/examples/declaration/value/function/tuple-sections-out.hs index 6642c5e..f1bf9b8 100644 --- a/data/examples/declaration/value/function/tuple-sections-out.hs +++ b/data/examples/declaration/value/function/tuple-sections-out.hs @@ -5,5 +5,4 @@ foo = (,2) bar = (,5,) baz = - ( ,,5,6,7,,, - ) + (,,5,6,7,,,) diff --git a/data/examples/declaration/value/function/tuples-out.hs b/data/examples/declaration/value/function/tuples-out.hs index 6eac553..6317d82 100644 --- a/data/examples/declaration/value/function/tuples-out.hs +++ b/data/examples/declaration/value/function/tuples-out.hs @@ -1,7 +1,7 @@ foo = (1, 2, 3) bar = - ( 1 - , 2 - , 3 - ) + ( 1, + 2, + 3 + ) diff --git a/data/examples/declaration/value/function/unboxed-sums-out.hs b/data/examples/declaration/value/function/unboxed-sums-out.hs index 272b831..24237f8 100644 --- a/data/examples/declaration/value/function/unboxed-sums-out.hs +++ b/data/examples/declaration/value/function/unboxed-sums-out.hs @@ -6,4 +6,4 @@ bar = (# | | 2 | #) baz = (# | | | 10 | | | | | - #) + #) diff --git a/data/examples/declaration/value/function/unboxed-tuples-out.hs b/data/examples/declaration/value/function/unboxed-tuples-out.hs index 617cfb9..d063b7c 100644 --- a/data/examples/declaration/value/function/unboxed-tuples-out.hs +++ b/data/examples/declaration/value/function/unboxed-tuples-out.hs @@ -3,7 +3,7 @@ foo = (# 1, 2, 3 #) bar = - (# 1 - , 2 - , 3 - #) + (# 1, + 2, + 3 + #) diff --git a/data/examples/declaration/value/other/line-multi-line-out.hs b/data/examples/declaration/value/other/line-multi-line-out.hs index 0f94794..46666cb 100644 --- a/data/examples/declaration/value/other/line-multi-line-out.hs +++ b/data/examples/declaration/value/other/line-multi-line-out.hs @@ -1,6 +1,6 @@ x :: [Int] x = - [ 1 - , 2 - , somethingSomething 3 - ] + [ 1, + 2, + somethingSomething 3 + ] diff --git a/data/examples/declaration/warning/warning-multiline-out.hs b/data/examples/declaration/warning/warning-multiline-out.hs index ce33dc0..798b21a 100644 --- a/data/examples/declaration/warning/warning-multiline-out.hs +++ b/data/examples/declaration/warning/warning-multiline-out.hs @@ -1,9 +1,9 @@ {-# WARNING - test - , foo - [ "These are bad functions" - , "Really bad!" - ] + test, + foo + [ "These are bad functions", + "Really bad!" + ] #-} test :: IO () test = pure () diff --git a/data/examples/import/explicit-imports-out.hs b/data/examples/import/explicit-imports-out.hs index bf03967..f4255c9 100644 --- a/data/examples/import/explicit-imports-out.hs +++ b/data/examples/import/explicit-imports-out.hs @@ -1,12 +1,12 @@ import qualified MegaModule as M - ( (<<<) - , (>>>) - , Either - , Maybe (Just, Nothing) - , MaybeT (..) - , Monad ((>>), (>>=), return) - , MonadBaseControl - , join - , liftIO - , void - ) + ( (<<<), + (>>>), + Either, + Maybe (Just, Nothing), + MaybeT (..), + Monad ((>>), (>>=), return), + MonadBaseControl, + join, + liftIO, + void + ) diff --git a/data/examples/import/explicit-imports-with-comments-out.hs b/data/examples/import/explicit-imports-with-comments-out.hs index a2f7489..d7050ce 100644 --- a/data/examples/import/explicit-imports-with-comments-out.hs +++ b/data/examples/import/explicit-imports-with-comments-out.hs @@ -1,6 +1,6 @@ import qualified MegaModule as M ( -- (1) - (<<<) -- (2) - , (>>>) - , Either -- (3) - ) + (<<<), -- (2) + (>>>), + Either -- (3) + ) diff --git a/data/examples/import/misc-out.hs b/data/examples/import/misc-out.hs index 06e9da6..461edf4 100644 --- a/data/examples/import/misc-out.hs +++ b/data/examples/import/misc-out.hs @@ -1,11 +1,11 @@ import A hiding - ( foobarbazqux - , foobarbazqux - , foobarbazqux - , foobarbazqux - , foobarbazqux - , foobarbazqux - , foobarbazqux - ) + ( foobarbazqux, + foobarbazqux, + foobarbazqux, + foobarbazqux, + foobarbazqux, + foobarbazqux, + foobarbazqux + ) import {-# SOURCE #-} safe qualified Module as M hiding (a, b, c, d, e, f) import Name hiding () diff --git a/data/examples/import/nested-explicit-imports-out.hs b/data/examples/import/nested-explicit-imports-out.hs index 531126b..dbe2d4a 100644 --- a/data/examples/import/nested-explicit-imports-out.hs +++ b/data/examples/import/nested-explicit-imports-out.hs @@ -1,10 +1,10 @@ import qualified MegaModule as M - ( (<<<) - , (>>>) - , Either - , Monad - ( (>>) - , (>>=) - , return - ) - ) + ( (<<<), + (>>>), + Either, + Monad + ( (>>), + (>>=), + return + ) + ) diff --git a/data/examples/module-header/double-dot-with-names-out.hs b/data/examples/module-header/double-dot-with-names-out.hs index d9e01d7..e59cc79 100644 --- a/data/examples/module-header/double-dot-with-names-out.hs +++ b/data/examples/module-header/double-dot-with-names-out.hs @@ -1,10 +1,10 @@ {-# LANGUAGE PatternSynonyms #-} module ExportSyntax - ( A (.., NoA) - , Q (F, ..) - , G (T, .., U) - ) + ( A (.., NoA), + Q (F, ..), + G (T, .., U) + ) where data A = A | B diff --git a/data/examples/module-header/multiline-out.hs b/data/examples/module-header/multiline-out.hs index 40da39d..4d77d61 100644 --- a/data/examples/module-header/multiline-out.hs +++ b/data/examples/module-header/multiline-out.hs @@ -1,6 +1,6 @@ module Foo - ( foo - , bar - , baz - ) + ( foo, + bar, + baz + ) where diff --git a/data/examples/module-header/multiline-with-comments-out.hs b/data/examples/module-header/multiline-with-comments-out.hs index 87c9aa2..15af67e 100644 --- a/data/examples/module-header/multiline-with-comments-out.hs +++ b/data/examples/module-header/multiline-with-comments-out.hs @@ -4,18 +4,18 @@ -- | Header. module My.Module ( -- * Something - foo - , bar - , -- * Another thing - () - , {- some other thing -} foo2 -- yet another - , foo3 -- third one - , baz - , bar2 -- a multiline comment + foo, + bar, + -- * Another thing + (), + {- some other thing -} foo2, -- yet another + foo3, -- third one + baz, + bar2, -- a multiline comment -- the second line - , bar3 - , module Foo.Bar.Baz - ) + bar3, + module Foo.Bar.Baz + ) where -- Wow diff --git a/data/examples/module-header/multiline2-out.hs b/data/examples/module-header/multiline2-out.hs index 40da39d..4d77d61 100644 --- a/data/examples/module-header/multiline2-out.hs +++ b/data/examples/module-header/multiline2-out.hs @@ -1,6 +1,6 @@ module Foo - ( foo - , bar - , baz - ) + ( foo, + bar, + baz + ) where diff --git a/data/examples/module-header/warning-pragma-list-multiline-out.hs b/data/examples/module-header/warning-pragma-list-multiline-out.hs index c10f0f1..01d8937 100644 --- a/data/examples/module-header/warning-pragma-list-multiline-out.hs +++ b/data/examples/module-header/warning-pragma-list-multiline-out.hs @@ -1,11 +1,11 @@ module Test {-# DEPRECATED - [ "This module is deprecated." - , "Please use OtherModule instead." - ] + [ "This module is deprecated.", + "Please use OtherModule instead." + ] #-} - ( foo - , bar - , baz - ) + ( foo, + bar, + baz + ) where diff --git a/data/examples/module-header/warning-pragma-multiline-out.hs b/data/examples/module-header/warning-pragma-multiline-out.hs index 79400d6..263ce9d 100644 --- a/data/examples/module-header/warning-pragma-multiline-out.hs +++ b/data/examples/module-header/warning-pragma-multiline-out.hs @@ -1,8 +1,8 @@ module Test {-# DEPRECATED "This module is unstable" #-} - ( foo - , bar - , baz - ) + ( foo, + bar, + baz + ) where import Blah diff --git a/data/printer/list0-multi.hs b/data/printer/list0-multi.hs index 7a8ebf5..fb461e7 100644 --- a/data/printer/list0-multi.hs +++ b/data/printer/list0-multi.hs @@ -1,4 +1,4 @@ -[ foo -, bar -, baz -] +[ foo, + bar, + baz + ] diff --git a/data/printer/list1-multi.hs b/data/printer/list1-multi.hs index d7513f9..4791323 100644 --- a/data/printer/list1-multi.hs +++ b/data/printer/list1-multi.hs @@ -1,8 +1,8 @@ -[ foo -, bar -, foo +[ foo, + bar, + foo :: ( Int -> Int - ) + ) -> Bool -] + ] diff --git a/data/printer/mhdr-multi.hs b/data/printer/mhdr-multi.hs index c66497e..1335007 100644 --- a/data/printer/mhdr-multi.hs +++ b/data/printer/mhdr-multi.hs @@ -1,7 +1,7 @@ module MyModule - ( R - , runR - , txt - , blah - ) + ( R, + runR, + txt, + blah + ) where diff --git a/data/printer/ssig-multi.hs b/data/printer/ssig-multi.hs index 4adf6b5..f57aab7 100644 --- a/data/printer/ssig-multi.hs +++ b/data/printer/ssig-multi.hs @@ -1,5 +1,5 @@ foo :: ( Int -> Int - ) + ) -> Bool diff --git a/src/Ormolu/Printer/Combinators.hs b/src/Ormolu/Printer/Combinators.hs index 9179fce..e76d09b 100644 --- a/src/Ormolu/Printer/Combinators.hs +++ b/src/Ormolu/Printer/Combinators.hs @@ -17,18 +17,13 @@ module Ormolu.Printer.Combinators , newline , inci , located - , locatedVia , located' , switchLayout , vlayout , breakpoint , breakpoint' -- ** Formatting lists - , velt - , velt' - , withSep - , spaceSep - , newlineSep + , sep -- ** Wrapping , sitcc , line @@ -85,18 +80,7 @@ located => Located a -- ^ Thing to enter -> (a -> R ()) -- ^ How to render inner value -> R () -located loc@(L l _) = locatedVia (Just l) loc - --- | A special version of 'located' that allows to control layout using an --- externally provided span. 'Nothing' means that layout won't be changed. - -locatedVia - :: Data a - => Maybe SrcSpan -- ^ Span that controls layout selection - -> Located a -- ^ Thing to enter - -> (a -> R ()) -- ^ How to render inner value - -> R () -locatedVia ml loc f = do +located loc f = do let withRealLocated (L l a) g = case l of UnhelpfulSpan _ -> return () @@ -109,9 +93,7 @@ locatedVia ml loc f = do if isModule (unLoc loc) then id else withEnclosingSpan orf - setEnclosingSpan $ case ml of - Nothing -> f (unLoc loc) - Just l' -> switchLayout l' (f (unLoc loc)) + setEnclosingSpan $ switchLayout [getLoc loc] (f (unLoc loc)) withRealLocated loc spitFollowingComments -- | A version of 'located' with arguments flipped. @@ -123,19 +105,28 @@ located' -> R () located' = flip located --- | Set layout according to given 'SrcSpan' for a given computation. Use --- this only when you need to set layout based on e.g. combined span of +-- | Set layout according to combination of given 'SrcSpan's for a given. +-- Use this only when you need to set layout based on e.g. combined span of -- several elements when there is no corresponding 'Located' wrapper -- provided by GHC AST. It is relatively rare that this one is needed. +-- +-- Given empty list this function will set layout to single line. switchLayout - :: SrcSpan -- ^ Span that controls layout + :: [SrcSpan] -- ^ Span that controls layout -> R () -- ^ Computation to run with changed layout -> R () -switchLayout spn = enterLayout - (if isOneLineSpan spn - then SingleLine - else MultiLine) +switchLayout spans' = enterLayout (spansLayout spans') + +-- | Which layout combined spans result in? + +spansLayout :: [SrcSpan] -> Layout +spansLayout = \case + [] -> SingleLine + (x:xs) -> + if isOneLineSpan (foldr combineSrcSpans x xs) + then SingleLine + else MultiLine -- | Insert a space if enclosing layout is single-line, or newline if it's -- multiline. @@ -156,56 +147,14 @@ breakpoint' = vlayout (return ()) newline ---------------------------------------------------------------------------- -- Formatting lists --- | Element of variable layout. This means that the sub-components may be --- rendered either on single line or each on its own line depending on --- current layout. --- --- This version does not make subsequent element (second and later) align --- with the first automatically and does not insert spaces between elements --- when layout is single line. +-- | Render a collection of elements inserting a separator between them. -velt :: [R ()] -> R () -velt xs = sequence_ (intersperse breakpoint' (sitcc <$> xs)) - --- | Like 'velt', but all sub-elements start at the same indentation level --- as first element, additionally spaces are inserted when layout is single --- line. - -velt' :: [R ()] -> R () -velt' xs = sitcc $ sequence_ (intersperse breakpoint (sitcc <$> xs)) - --- | Put separator between renderings of items of a list. - -withSep +sep :: R () -- ^ Separator - -> (a -> R ()) -- ^ How to render list items - -> [a] -- ^ List to render - -> [R ()] -- ^ List of printing actions -withSep sep f = \case - [] -> [] - (x:xs) -> - let g a = sep >> f a - in f x : fmap g xs - --- | Render space-separated elements. --- --- > spaceSep f = sequence_ . withSep space f - -spaceSep - :: (a -> R ()) -- ^ How to render list items - -> [a] -- ^ List to render + -> (a -> R ()) -- ^ How to render an element + -> [a] -- ^ Elements to render -> R () -spaceSep f = sequence_ . withSep space f - --- | Render newline-separated elements. --- --- > newlineSep f = sequence_ . withSep newline f - -newlineSep - :: (a -> R ()) -- ^ How to render list items - -> [a] -- ^ List to render - -> R () -newlineSep f = sequence_ . withSep newline f +sep s f xs = sequence_ (intersperse s (f <$> xs)) ---------------------------------------------------------------------------- -- Wrapping @@ -259,6 +208,7 @@ bracketsPar :: R () -> R () bracketsPar m = sitcc $ do txt "[: " m + vlayout (return ()) space txt " :]" -- | Surround given entity by parentheses @(@ and @)@. @@ -275,7 +225,7 @@ parensHash :: R () -> R () parensHash m = sitcc $ do txt "(# " m - breakpoint + vlayout space (newline >> txt " ") txt "#)" -- | Braces as used for pragmas: @{-#@ and @#-}@. @@ -302,15 +252,19 @@ pragma pragmaText body = pragmaBraces $ do -- current layout is multiline. ospaces :: R () -> R () -ospaces m = vlayout m (txt " " >> m >> newline) +ospaces m = vlayout m $ do + space + m + newline + txt " " ---------------------------------------------------------------------------- -- Literals --- | Print @,@ followed by a space. +-- | Print @,@. comma :: R () -comma = txt ", " +comma = txt "," -- | Print single space. diff --git a/src/Ormolu/Printer/Meat/Common.hs b/src/Ormolu/Printer/Meat/Common.hs index 24c3847..99b2f8a 100644 --- a/src/Ormolu/Printer/Meat/Common.hs +++ b/src/Ormolu/Printer/Meat/Common.hs @@ -130,9 +130,9 @@ p_infixDefHelper isInfix inci' name args = inci' p1 unless (null ps) . inci' $ do breakpoint - velt' ps + sitcc (sep breakpoint sitcc ps) (_, ps) -> do name unless (null ps) $ do breakpoint - inci' (velt' args) + inci' $ sitcc (sep breakpoint sitcc args) diff --git a/src/Ormolu/Printer/Meat/Declaration/Class.hs b/src/Ormolu/Printer/Meat/Declaration/Class.hs index 0bcf363..ee47617 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Class.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Class.hs @@ -21,7 +21,6 @@ import Ormolu.Printer.Meat.Common import Ormolu.Printer.Meat.Type import Ormolu.Utils import RdrName (RdrName (..)) -import SrcLoc (Located, combineSrcSpans) import {-# SOURCE #-} Ormolu.Printer.Meat.Declaration p_classDecl @@ -37,13 +36,10 @@ p_classDecl -> R () p_classDecl ctx name tvars fixity fdeps csigs cdefs cats catdefs = do let HsQTvs {..} = tvars - variableSpans = foldr (combineSrcSpans . getLoc) noSrcSpan hsq_explicit - signatureSpans = getLoc name `combineSrcSpans` variableSpans - dependencySpans = foldr (combineSrcSpans . getLoc) noSrcSpan fdeps - combinedSpans = - getLoc ctx `combineSrcSpans` - signatureSpans `combineSrcSpans` - dependencySpans + variableSpans = getLoc <$> hsq_explicit + signatureSpans = getLoc name : variableSpans + dependencySpans = getLoc <$> fdeps + combinedSpans = getLoc ctx : (signatureSpans ++ dependencySpans) txt "class" switchLayout combinedSpans $ do breakpoint @@ -86,13 +82,13 @@ p_classFundeps :: [Located (FunDep (Located RdrName))] -> R () p_classFundeps fdeps = unless (null fdeps) $ do breakpoint txt "| " - velt $ withSep comma (located' p_funDep) fdeps + sitcc $ sep (comma >> breakpoint) (sitcc . located' p_funDep) fdeps p_funDep :: FunDep (Located RdrName) -> R () p_funDep (before, after) = do - spaceSep p_rdrName before + sep space p_rdrName before txt " -> " - spaceSep p_rdrName after + sep space p_rdrName after ---------------------------------------------------------------------------- -- Helpers diff --git a/src/Ormolu/Printer/Meat/Declaration/Data.hs b/src/Ormolu/Printer/Meat/Declaration/Data.hs index fa0062f..8ba48e4 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Data.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Data.hs @@ -10,7 +10,6 @@ module Ormolu.Printer.Meat.Declaration.Data where import Control.Monad -import Data.List.NonEmpty (NonEmpty (..)) import Data.Maybe (isJust) import GHC import Ormolu.Printer.Combinators @@ -28,14 +27,13 @@ p_dataDecl -> HsDataDefn GhcPs -- ^ Data definition -> R () p_dataDecl style name tpats fixity HsDataDefn {..} = do - let combinedSpans = combineSrcSpans' (getLoc name :| (getLoc <$> tpats)) txt $ case dd_ND of NewType -> "newtype" DataType -> "data" txt $ case style of Associated -> mempty Free -> " instance" - switchLayout combinedSpans $ do + switchLayout (getLoc name : fmap getLoc tpats) $ do breakpoint inci $ p_infixDefHelper (isInfix fixity) @@ -54,13 +52,13 @@ p_dataDecl style name tpats fixity HsDataDefn {..} = do then do txt " where" newline - inci $ newlineSep (sitcc . located' p_conDecl) dd_cons - else switchLayout (combineSrcSpans' (getLoc name :| (getLoc <$> dd_cons))) $ + inci . sitcc $ sep newline (sitcc . located' p_conDecl) dd_cons + else switchLayout (getLoc name : (getLoc <$> dd_cons)) $ inci $ do breakpoint txt "= " - let sep = vlayout (txt " | ") (txt "| ") - velt $ withSep sep (sitcc . located' p_conDecl) dd_cons + let s = vlayout (txt " | ") (newline >> txt "| ") + sep s (sitcc . located' p_conDecl) dd_cons newline inci . located dd_derivs $ \xs -> forM_ xs (line . located' p_hsDerivingClause) @@ -73,11 +71,10 @@ p_conDecl = \case [] -> return () (c:cs) -> do p_rdrName c - unless (null cs) $ do - breakpoint' - inci $ do - comma - velt $ withSep comma p_rdrName cs + unless (null cs) . inci $ do + comma + breakpoint + sitcc $ sep (comma >> breakpoint) p_rdrName cs breakpoint inci $ do txt ":: " @@ -85,7 +82,7 @@ p_conDecl = \case forM_ con_mb_cxt p_lhsContext case con_args of PrefixCon xs -> do - velt' (located' p_hsType <$> xs) + sep breakpoint (located' p_hsType) xs unless (null xs) $ do breakpoint txt "-> " @@ -95,7 +92,7 @@ p_conDecl = \case breakpoint txt "-> " InfixCon _ _ -> notImplemented "InfixCon" - locatedVia Nothing con_res_ty p_hsType + p_hsType (unLoc con_res_ty) ConDeclH98 {..} -> do p_forallBndrs con_ex_tvs forM_ con_mb_cxt p_lhsContext @@ -103,7 +100,7 @@ p_conDecl = \case PrefixCon xs -> do p_rdrName con_name unless (null xs) breakpoint - inci $ velt' (located' p_hsType <$> xs) + inci . sitcc $ sep breakpoint (sitcc . located' p_hsType) xs RecCon l -> do p_rdrName con_name breakpoint @@ -124,7 +121,7 @@ p_forallBndrs = \case [] -> return () bndrs -> do txt "forall " - spaceSep (located' p_hsTyVarBndr) bndrs + sep space (located' p_hsTyVarBndr) bndrs txt ". " p_lhsContext @@ -150,12 +147,15 @@ p_hsDerivingClause HsDerivingClause {..} = do txt "deriving" let derivingWhat = located deriv_clause_tys $ \case [] -> txt "()" - xs -> parens . velt $ withSep comma (located' p_hsType . hsib_body) xs + xs -> parens . sitcc $ sep + (comma >> breakpoint) + (sitcc . located' p_hsType . hsib_body) + xs case deriv_clause_strategy of Nothing -> do breakpoint inci derivingWhat - Just l -> locatedVia Nothing l $ \case + Just (L _ a) -> case a of StockStrategy -> do txt " stock" breakpoint diff --git a/src/Ormolu/Printer/Meat/Declaration/Default.hs b/src/Ormolu/Printer/Meat/Declaration/Default.hs index f2eb0f5..ffcf181 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Default.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Default.hs @@ -16,6 +16,6 @@ p_defaultDecl = \case DefaultDecl NoExt ts -> line $ do txt "default" breakpoint - inci . parens . velt $ - withSep comma (located' p_hsType) ts + inci . parens . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_hsType) ts XDefaultDecl {} -> notImplemented "XDefaultDecl" diff --git a/src/Ormolu/Printer/Meat/Declaration/Foreign.hs b/src/Ormolu/Printer/Meat/Declaration/Foreign.hs index 8fc3fd4..6eaef12 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Foreign.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Foreign.hs @@ -35,7 +35,7 @@ p_foreignTypeSig fd = do breakpoint -- Switch into the layout of the signature, to allow us to pull name and -- signature onto a single line. - inci . switchLayout (getLoc . hsib_body $ fd_sig_ty fd) $ do + inci . switchLayout [getLoc . hsib_body $ fd_sig_ty fd] $ do p_rdrName (fd_name fd) p_typeAscription (HsWC NoExt (fd_sig_ty fd)) diff --git a/src/Ormolu/Printer/Meat/Declaration/Instance.hs b/src/Ormolu/Printer/Meat/Declaration/Instance.hs index e8e2819..83c945e 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Instance.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Instance.hs @@ -41,7 +41,7 @@ p_standaloneDerivDecl DerivDecl {..} = do Nothing -> do space instTypes False - Just l -> locatedVia Nothing l $ \case + Just (L _ a) -> case a of StockStrategy -> do txt " stock " instTypes False @@ -72,29 +72,31 @@ p_clsInstDecl = \case inci $ do match_overlap_mode cid_overlap_mode breakpoint p_hsType x + -- GHC's AST does not necessarily store each kind of element in source + -- location order. This happens because different declarations are stored in + -- different lists. Consequently, to get all the declarations in proper + -- order, they need to be manually sorted. + let sigs = (getLoc &&& fmap (SigD NoExt)) <$> cid_sigs + vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cid_binds + tyFamInsts = + ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt) + ) <$> cid_tyfam_insts + dataFamInsts = + ( getLoc &&& fmap (InstD NoExt . DataFamInstD NoExt) + ) <$> cid_datafam_insts + allDecls = + snd <$> + sortBy (comparing fst) (sigs <> vals <> tyFamInsts <> dataFamInsts) + if null allDecls + then newline + else do + switchLayout [getLoc hsib_body] breakpoint + inci $ do + txt "where" + newline -- Ensure line is added after where clause. + newline -- Add newline before first declaration. + p_hsDecls Associated allDecls XHsImplicitBndrs NoExt -> notImplemented "XHsImplicitBndrs" - -- GHC's AST does not necessarily store each kind of element in source - -- location order. This happens because different declarations are stored in - -- different lists. Consequently, to get all the declarations in proper - -- order, they need to be manually sorted. - let sigs = (getLoc &&& fmap (SigD NoExt)) <$> cid_sigs - vals = (getLoc &&& fmap (ValD NoExt)) <$> toList cid_binds - tyFamInsts = - ( getLoc &&& fmap (InstD NoExt . TyFamInstD NoExt) - ) <$> cid_tyfam_insts - dataFamInsts = - ( getLoc &&& fmap (InstD NoExt . DataFamInstD NoExt) - ) <$> cid_datafam_insts - allDecls = - snd <$> - sortBy (comparing fst) (sigs <> vals <> tyFamInsts <> dataFamInsts) - if not (null allDecls) - then do - txt " where" - newline -- Ensure line is added after where clause. - newline -- Add newline before first declaration. - inci (p_hsDecls Associated allDecls) - else newline XClsInstDecl NoExt -> notImplemented "XClsInstDecl" p_tyFamInstDecl :: FamilyStyle -> TyFamInstDecl GhcPs -> R () diff --git a/src/Ormolu/Printer/Meat/Declaration/RoleAnnotation.hs b/src/Ormolu/Printer/Meat/Declaration/RoleAnnotation.hs index 203e6f1..820cec9 100644 --- a/src/Ormolu/Printer/Meat/Declaration/RoleAnnotation.hs +++ b/src/Ormolu/Printer/Meat/Declaration/RoleAnnotation.hs @@ -32,7 +32,7 @@ p_roleAnnot' l_name anns = line $ do breakpoint let p_role' = maybe (txt "_") p_role - inci $ velt' $ (located' p_role') <$> anns + inci . sitcc $ sep breakpoint (sitcc . located' p_role') anns p_role :: Role -> R () p_role = \case diff --git a/src/Ormolu/Printer/Meat/Declaration/Rule.hs b/src/Ormolu/Printer/Meat/Declaration/Rule.hs index 26a247a..9eef9be 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Rule.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Rule.hs @@ -16,13 +16,12 @@ import Ormolu.Printer.Meat.Common import Ormolu.Printer.Meat.Declaration.Signature import Ormolu.Printer.Meat.Declaration.Value import Ormolu.Utils -import qualified Data.List.NonEmpty as NE import qualified Data.Text as T p_ruleDecls :: RuleDecls GhcPs -> R () p_ruleDecls = \case - HsRules NoExt _ xs -> line $ pragma "RULES" $ - velt' $ (located' p_ruleDecl) <$> xs + HsRules NoExt _ xs -> line . pragma "RULES" . sitcc $ + sep breakpoint (sitcc . located' p_ruleDecl) xs XRuleDecls NoExt -> notImplemented "XRuleDecls" p_ruleDecl :: RuleDecl GhcPs -> R () @@ -50,14 +49,14 @@ p_ruleName (_, name) = do txt "\"" p_ruleBndrs :: [LRuleBndr GhcPs] -> R () +p_ruleBndrs [] = return () p_ruleBndrs bndrs = - forM_ (NE.nonEmpty bndrs) $ \bndrs_ne -> - switchLayout (combineSrcSpans' (getLoc <$> bndrs_ne)) $ do - txt "forall" - breakpoint - inci $ do - velt' (located' p_ruleBndr <$> bndrs) - txt "." + switchLayout (getLoc <$> bndrs) $ do + txt "forall" + breakpoint + inci $ do + sitcc $ sep breakpoint (sitcc . located' p_ruleBndr) bndrs + txt "." p_ruleBndr :: RuleBndr GhcPs -> R () p_ruleBndr = \case diff --git a/src/Ormolu/Printer/Meat/Declaration/Signature.hs b/src/Ormolu/Printer/Meat/Declaration/Signature.hs index 28208a7..4c3494f 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Signature.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Signature.hs @@ -49,9 +49,9 @@ p_typeSig (n:ns) hswc = do if null ns then p_typeAscription hswc else inci $ do - vlayout (return ()) newline comma - velt (withSep comma p_rdrName ns) + breakpoint + sep (comma >> breakpoint) p_rdrName ns p_typeAscription hswc p_typeAscription @@ -93,7 +93,7 @@ p_fixSig = \case space atom n space - sequence_ (withSep comma p_rdrName names) + sitcc $ sep (comma >> breakpoint) p_rdrName names XFixitySig NoExt -> notImplemented "XFixitySig" p_inlineSig @@ -125,7 +125,7 @@ p_specSig name ts InlinePragma {..} = pragmaBraces $ do breakpoint inci $ do txt ":: " - velt (withSep comma (located' p_hsType) (hsib_body <$> ts)) + sep (comma >> breakpoint) (located' p_hsType . hsib_body) ts p_activation :: Activation -> R () p_activation = \case @@ -162,12 +162,14 @@ p_booleanFormula -> R () p_booleanFormula = \case Var name -> p_rdrName name - And xs -> velt $ - withSep comma (located' p_booleanFormula) xs - Or xs -> velt $ - withSep (vlayout space (return ()) >> txt "| ") - (located' p_booleanFormula) - xs + And xs -> sitcc $ sep + (comma >> breakpoint) + (located' p_booleanFormula) + xs + Or xs -> sitcc $ sep + (breakpoint >> txt "| ") + (located' p_booleanFormula) + xs Parens l -> located l (parens . p_booleanFormula) p_completeSig @@ -177,7 +179,7 @@ p_completeSig p_completeSig cs' mty = located cs' $ \cs -> pragma "COMPLETE" . inci $ do - velt (withSep comma p_rdrName cs) + sitcc $ sep (comma >> breakpoint) p_rdrName cs forM_ mty $ \ty -> do breakpoint inci $ do diff --git a/src/Ormolu/Printer/Meat/Declaration/Type.hs b/src/Ormolu/Printer/Meat/Declaration/Type.hs index bfcbb05..dd64cdf 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Type.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Type.hs @@ -26,6 +26,6 @@ p_synDecl name tvars t = line $ do p_rdrName name let HsQTvs {..} = tvars unless (null hsq_explicit) space - spaceSep (located' p_hsTyVarBndr) hsq_explicit + sep space (located' p_hsTyVarBndr) hsq_explicit breakpoint inci (txt "= " >> located t p_hsType) diff --git a/src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs b/src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs index bf49e63..2e9206c 100644 --- a/src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs +++ b/src/Ormolu/Printer/Meat/Declaration/TypeFamily.hs @@ -12,7 +12,6 @@ where import BasicTypes (LexicalFixity (..)) import Control.Monad -import Data.List.NonEmpty (NonEmpty (..)) import Data.Maybe (isNothing, isJust) import GHC import Ormolu.Printer.Combinators @@ -31,11 +30,9 @@ p_famDecl style FamilyDecl {..} = do Associated -> mempty Free -> " family" let HsQTvs {..} = fdTyVars - combinedSpans = combineSrcSpans' $ - getLoc fdLName :| fmap getLoc hsq_explicit breakpoint inci $ do - switchLayout combinedSpans $ do + switchLayout (getLoc fdLName : (getLoc <$> hsq_explicit)) $ do p_infixDefHelper (isInfix fdFixity) inci @@ -82,14 +79,12 @@ p_injectivityAnn (InjectivityAnn a bs) = do p_rdrName a space txt "-> " - spaceSep p_rdrName bs + sep space p_rdrName bs p_tyFamInstEqn :: TyFamInstEqn GhcPs -> R () p_tyFamInstEqn HsIB {..} = do let FamEqn {..} = hsib_body - combinedSpans = combineSrcSpans' $ - getLoc feqn_tycon :| fmap getLoc feqn_pats - switchLayout combinedSpans $ p_infixDefHelper + switchLayout (getLoc feqn_tycon : (getLoc <$> feqn_pats)) $ p_infixDefHelper (isInfix feqn_fixity) inci (p_rdrName feqn_tycon) diff --git a/src/Ormolu/Printer/Meat/Declaration/Value.hs b/src/Ormolu/Printer/Meat/Declaration/Value.hs index 1c9f120..def44db 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Value.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Value.hs @@ -85,9 +85,9 @@ p_matchGroup' -> MatchGroup GhcPs (Located body) -> R () p_matchGroup' placer pretty style MG {..} = - locatedVia Nothing mg_alts $ - newlineSep (located' (\m@Match {..} -> - p_match' placer pretty style (isInfixMatch m) (matchStrictness m) m_pats m_grhss)) + sep newline (located' (\m@Match {..} -> + p_match' placer pretty style (isInfixMatch m) (matchStrictness m) m_pats m_grhss)) + (unLoc mg_alts) p_matchGroup' _ _ _ (XMatchGroup NoExt) = notImplemented "XMatchGroup" matchStrictness :: Match id body -> SrcStrictness @@ -137,8 +137,8 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do inci' = if isOneLineSpan combinedSpans then id else inci - switchLayout combinedSpans $ do - let stdCase = velt' (located' p_pat <$> m_pats) + switchLayout [combinedSpans] $ do + let stdCase = sep breakpoint (located' p_pat) m_pats case style of Function name -> p_infixDefHelper @@ -201,12 +201,12 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do if isCase style && hasGuards then RightArrow else EqualSign - newlineSep (located' (p_grhs' pretty groupStyle)) grhssGRHSs + sep newline (located' (p_grhs' pretty groupStyle)) grhssGRHSs let whereLocation = combineSrcSpans patGrhssSpan $ getLoc grhssLocalBinds whereIsEmpty = GHC.isEmptyLocalBindsPR (unLoc grhssLocalBinds) unless (GHC.eqEmptyLocalBinds (unLoc grhssLocalBinds)) . inciLocalBinds - . switchLayout whereLocation $ do + . switchLayout [whereLocation] $ do if whereIsEmpty then newline else breakpoint txt "where" unless whereIsEmpty $ do @@ -214,8 +214,8 @@ p_match' placer pretty style isInfix strictness m_pats m_grhss = do inci (located grhssLocalBinds p_hsLocalBinds) case style of Lambda -> placeHanging placement $ - switchLayout patGrhssSpan p_body - _ -> switchLayout patGrhssSpan $ + switchLayout [patGrhssSpan] p_body + _ -> switchLayout [patGrhssSpan] $ placeHanging placement p_body p_grhs :: GroupStyle -> GRHS GhcPs (LHsExpr GhcPs) -> R () @@ -232,7 +232,7 @@ p_grhs' pretty style (GRHS NoExt guards body) = [] -> p_body xs -> do txt "| " - velt $ withSep comma (located' p_stmt) xs + sitcc (sep (comma >> breakpoint) (sitcc . located' p_stmt) xs) space txt $ case style of EqualSign -> "=" @@ -298,7 +298,7 @@ p_hsCmd = \case HsCmdDo NoExt es -> do txt "do" newline - inci (located es (newlineSep (located' (sitcc . p_stmt' p_hsCmd)))) + inci (located es (sitcc . sep newline (located' (sitcc . p_stmt' p_hsCmd)))) HsCmdWrap {} -> notImplemented "HsCmdWrap" XCmd {} -> notImplemented "XCmd" @@ -368,7 +368,7 @@ p_stmt' pretty = \case inci (p_hsExpr x) RecStmt {..} -> do txt "rec " - sitcc $ newlineSep (located' (p_stmt' pretty)) recS_stmts + sitcc $ sep newline (located' (p_stmt' pretty)) recS_stmts XStmtLR {} -> notImplemented "XStmtLR" gatherStmt :: ExprLStmt GhcPs -> [[ExprLStmt GhcPs]] @@ -393,7 +393,7 @@ p_hsLocalBinds = \case (Left <$> bagToList bag) ++ (Right <$> lsigs) p_item (Left x) = located x p_valDecl' p_item (Right x) = located x p_sigDecl' - newlineSep (sitcc . p_item) (sortOn ssStart items) + sitcc $ sep newline (sitcc . p_item) (sortOn ssStart items) HsValBinds NoExt _ -> notImplemented "HsValBinds" HsIPBinds NoExt _ -> notImplemented "HsIPBinds" EmptyLocalBinds NoExt -> return () @@ -439,7 +439,7 @@ p_hsExpr = \case txt "\\case" newline inci (p_matchGroup LambdaCase mgroup) - HsApp NoExt f x -> do + HsApp NoExt f x -> sitcc $ do located f p_hsExpr breakpoint inci (located x p_hsExpr) @@ -479,9 +479,11 @@ p_hsExpr = \case case boxity of Boxed -> parens Unboxed -> parensHash - parens' $ if isSection - then sequence_ (withSep (txt ",") (located' p_hsTupArg) args) - else velt (withSep comma (located' p_hsTupArg) args) + if isSection + then switchLayout [] . parens' $ + sep comma (located' p_hsTupArg) args + else switchLayout (getLoc <$> args) . parens' . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_hsTupArg) args ExplicitSum NoExt tag arity e -> do let before = tag - 1 after = arity - before - 1 @@ -496,7 +498,7 @@ p_hsExpr = \case unless isFirst space located l p_hsExpr unless isLast space - parensHash $ sequence_ (withSep (txt "|") f (zip args [0..])) + parensHash $ sep (txt "|") f (zip args [0..]) HsCase NoExt e mgroup -> do txt "case " located e p_hsExpr @@ -518,7 +520,7 @@ p_hsExpr = \case inci (p_hsExpr x) HsMultiIf NoExt guards -> do txt "if " - sitcc $ newlineSep (located' (p_grhs RightArrow)) guards + sitcc $ sep newline (located' (p_grhs RightArrow)) guards HsLet NoExt localBinds e -> do txt "let " sitcc (located localBinds p_hsLocalBinds) @@ -529,16 +531,14 @@ p_hsExpr = \case let doBody header = do txt header newline - inci $ located es (newlineSep (located' (sitcc . p_stmt))) - compBody = brackets $ located es $ \xs -> do - let p_parBody = - sequence_ . - intersperse breakpoint . - withSep (txt "| ") p_seqBody - p_seqBody = - sequence_ . - intersperse (vlayout (pure ()) newline) . - withSep comma (located' (sitcc . p_stmt)) + inci $ located es (sep newline (located' (sitcc . p_stmt))) + compBody = brackets $ located es $ \xs -> do + let p_parBody = sitcc . sep + (breakpoint >> txt "| ") + p_seqBody + p_seqBody = sitcc . sep + (comma >> breakpoint) + (located' (sitcc . p_stmt)) stmts = init xs yield = last xs lists = foldr (liftAppend . gatherStmt) [] stmts @@ -557,7 +557,7 @@ p_hsExpr = \case ParStmtCtxt _ -> notImplemented "ParStmtCtxt" TransStmtCtxt _ -> notImplemented "TransStmtCtxt" ExplicitList _ _ xs -> - brackets $ velt (withSep comma (located' p_hsExpr) xs) + brackets . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_hsExpr) xs RecordCon {..} -> do located rcon_con_name atom breakpoint @@ -567,11 +567,12 @@ p_hsExpr = \case case rec_dotdot of Just {} -> [txt ".."] Nothing -> [] - inci $ braces $ velt (withSep comma id (fields <> dotdot)) + inci . braces . sitcc $ sep (comma >> breakpoint) sitcc (fields <> dotdot) RecordUpd {..} -> do located rupd_expr p_hsExpr breakpoint - inci $ braces $ velt (withSep comma (located' p_hsRecField) rupd_flds) + inci . braces . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_hsRecField) rupd_flds ExprWithTySig affix x -> sitcc $ do located x p_hsExpr breakpoint @@ -582,21 +583,21 @@ p_hsExpr = \case located hsib_body p_hsType ArithSeq NoExt _ x -> do case x of - From from -> brackets $ do + From from -> brackets . sitcc $ do located from p_hsExpr breakpoint txt ".." - FromThen from next -> brackets $ do - velt (withSep comma (located' p_hsExpr) [from, next]) + FromThen from next -> brackets . sitcc $ do + sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next] breakpoint txt ".." - FromTo from to -> brackets $ do + FromTo from to -> brackets . sitcc $ do located from p_hsExpr breakpoint txt ".. " located to p_hsExpr - FromThenTo from next to -> brackets $ do - velt (withSep comma (located' p_hsExpr) [from, next]) + FromThenTo from next to -> brackets . sitcc $ do + sitcc $ sep (comma >> breakpoint) (located' p_hsExpr) [from, next] breakpoint txt ".. " located to p_hsExpr @@ -680,9 +681,9 @@ p_patSynBind (XPatSynBind NoExt) = notImplemented "XPatSynBind" p_patSynDetails :: HsPatSynDetails (Located RdrName) -> R () p_patSynDetails = \case PrefixCon xs -> - velt' (p_rdrName <$> xs) + sitcc $ sep breakpoint p_rdrName xs RecCon xs -> - velt' (p_rdrName . recordPatSynPatVar <$> xs) + sitcc $ sep breakpoint (p_rdrName . recordPatSynPatVar) xs InfixCon _ _ -> notImplemented "InfixCon" p_pat :: Pat GhcPs -> R () @@ -702,13 +703,13 @@ p_pat = \case txt "!" located pat p_pat ListPat NoExt pats -> do - brackets $ velt (withSep comma (located' p_pat) pats) + brackets . sitcc $ sep (comma >> breakpoint) (located' p_pat) pats TuplePat NoExt pats boxing -> do let f = case boxing of Boxed -> parens Unboxed -> parensHash - f $ velt (withSep comma (located' p_pat) pats) + f . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_pat) pats SumPat NoExt pat _ _ -> do -- XXX I'm not sure about this one. located pat p_pat @@ -718,14 +719,14 @@ p_pat = \case p_rdrName pat unless (null xs) $ do breakpoint - inci $ velt' (located' p_pat <$> xs) + inci . sitcc $ sep breakpoint (sitcc . located' p_pat) xs RecCon (HsRecFields fields dotdot) -> do p_rdrName pat breakpoint let f = \case Nothing -> txt ".." Just x -> located x p_pat_hsRecField - inci . braces . velt . withSep comma f $ case dotdot of + inci . braces . sitcc . sep (comma >> breakpoint) f $ case dotdot of Nothing -> Just <$> fields Just n -> (Just <$> take n fields) ++ [Nothing] InfixCon x y -> do @@ -899,7 +900,7 @@ exprPlacement = \case RecordCon NoExt _ _ -> Hanging HsProc NoExt (L s _) _ -> -- Indentation breaks if pattern is longer than one line and left hanging. - -- Consequentally, once apply hanging when it is safe. + -- Consequently, only apply hanging when it is safe. if isOneLineSpan s then Hanging else Normal diff --git a/src/Ormolu/Printer/Meat/Declaration/Warning.hs b/src/Ormolu/Printer/Meat/Declaration/Warning.hs index de1202d..ac16ac3 100644 --- a/src/Ormolu/Printer/Meat/Declaration/Warning.hs +++ b/src/Ormolu/Printer/Meat/Declaration/Warning.hs @@ -9,13 +9,11 @@ where import BasicTypes import Data.Foldable -import qualified Data.List.NonEmpty as NE import Data.Text (Text) import GHC import Ormolu.Printer.Combinators import Ormolu.Printer.Meat.Common import Ormolu.Utils -import SrcLoc (combineSrcSpans) p_warnDecls :: WarnDecls GhcPs -> R () p_warnDecls (Warnings NoExt _ warnings) = @@ -30,16 +28,16 @@ p_warnDecl XWarnDecl {} = notImplemented "XWarnDecl" p_moduleWarning :: WarningTxt -> R () p_moduleWarning wtxt = do let (pragmaText, lits) = warningText wtxt - switchLayout (listSpan lits) $ do + switchLayout (getLoc <$> lits) $ do breakpoint inci $ pragma pragmaText (inci $ p_lits lits) p_topLevelWarning :: [Located RdrName] -> WarningTxt -> R () p_topLevelWarning fnames wtxt = do let (pragmaText, lits) = warningText wtxt - switchLayout (combineSrcSpans (listSpan fnames) (listSpan lits)) $ do + switchLayout (fmap getLoc fnames ++ fmap getLoc lits) $ do pragma pragmaText . inci $ do - velt (withSep comma p_rdrName fnames) + sitcc $ sep (comma >> breakpoint) p_rdrName fnames breakpoint p_lits lits @@ -48,10 +46,7 @@ warningText = \case WarningTxt _ lits -> ("WARNING", lits) DeprecatedTxt _ lits -> ("DEPRECATED", lits) -listSpan :: [Located a] -> SrcSpan -listSpan xs = combineSrcSpans' (getLoc <$> NE.fromList xs) - p_lits :: [Located StringLiteral] -> R () p_lits = \case [l] -> atom l - ls -> brackets . velt $ withSep comma atom ls + ls -> brackets . sitcc $ sep (comma >> breakpoint) atom ls diff --git a/src/Ormolu/Printer/Meat/ImportExport.hs b/src/Ormolu/Printer/Meat/ImportExport.hs index a380532..9d3849a 100644 --- a/src/Ormolu/Printer/Meat/ImportExport.hs +++ b/src/Ormolu/Printer/Meat/ImportExport.hs @@ -23,7 +23,7 @@ p_hsmodExports [] = do breakpoint' txt ")" p_hsmodExports xs = - parens . velt $ withSep comma (sitcc . located' p_lie) xs + parens . sitcc $ sep (comma >> breakpoint) (sitcc . located' p_lie) xs p_hsmodImport :: ImportDecl GhcPs -> R () p_hsmodImport ImportDecl {..} = line $ do @@ -52,10 +52,10 @@ p_hsmodImport ImportDecl {..} = line $ do txt " hiding" case ideclHiding of Nothing -> return () - Just (_, l) -> do + Just (_, (L _ a)) -> do breakpoint - inci . locatedVia Nothing l $ - parens . velt . withSep comma (located' p_lie) + inci . parens . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_lie) a p_hsmodImport (XImportDecl NoExt) = notImplemented "XImportDecl" p_lie :: IE GhcPs -> R () @@ -72,7 +72,7 @@ p_lie = \case inci $ do let names :: [R ()] names = located' p_ieWrappedName <$> xs - parens . velt . withSep comma id $ + parens . sitcc . sep (comma >> breakpoint) sitcc $ case w of NoIEWildcard -> names IEWildcard n -> diff --git a/src/Ormolu/Printer/Meat/Module.hs b/src/Ormolu/Printer/Meat/Module.hs index 1341c23..56a5168 100644 --- a/src/Ormolu/Printer/Meat/Module.hs +++ b/src/Ormolu/Printer/Meat/Module.hs @@ -20,18 +20,16 @@ import Ormolu.Printer.Meat.Declaration import Ormolu.Printer.Meat.Declaration.Warning import Ormolu.Printer.Meat.ImportExport import Ormolu.Printer.Meat.LanguagePragma -import SrcLoc (combineSrcSpans) p_hsModule :: Set String -> ParsedSource -> R () -p_hsModule exts loc@(L moduleSpan hsModule) = do +p_hsModule exts (L moduleSpan HsModule {..}) = do -- NOTE If span of exports in multiline, the whole thing is multiline. -- This is especially important because span of module itself always seems -- to have length zero, so it's not reliable for layout selection. - let spn = - case hsmodExports hsModule of - Nothing -> moduleSpan - Just (L exportsSpan _) -> combineSrcSpans moduleSpan exportsSpan - locatedVia (Just spn) loc $ \HsModule {..} -> do + let spans' = case hsmodExports of + Nothing -> [moduleSpan] + Just (L exportsSpan _) -> moduleSpan : [exportsSpan] + switchLayout spans' $ do let hasLangPragmas = not (null exts) hasModuleHeader = isJust hsmodName hasImports = not (null hsmodImports) @@ -49,7 +47,7 @@ p_hsModule exts loc@(L moduleSpan hsModule) = do Nothing -> return () Just hsmodExports' -> do breakpoint - inci (locatedVia Nothing hsmodExports' p_hsmodExports) + inci (p_hsmodExports (unLoc hsmodExports')) breakpoint txt "where" when (hasImports || hasDecls) newline diff --git a/src/Ormolu/Printer/Meat/Type.hs b/src/Ormolu/Printer/Meat/Type.hs index 518730c..b36dac1 100644 --- a/src/Ormolu/Printer/Meat/Type.hs +++ b/src/Ormolu/Printer/Meat/Type.hs @@ -22,16 +22,16 @@ p_hsType :: HsType GhcPs -> R () p_hsType = \case HsForAllTy NoExt bndrs t -> do txt "forall " - spaceSep (located' p_hsTyVarBndr) bndrs + sep space (located' p_hsTyVarBndr) bndrs txt ". " - locatedVia Nothing t p_hsType + p_hsType (unLoc t) HsQualTy NoExt qs t -> do located qs p_hsContext breakpoint txt "=> " case unLoc t of - HsQualTy {} -> locatedVia Nothing t p_hsType - HsFunTy {} -> locatedVia Nothing t p_hsType + HsQualTy {} -> p_hsType (unLoc t) + HsFunTy {} -> p_hsType (unLoc t) _ -> located t p_hsType HsTyVar NoExt p n -> do case p of @@ -60,9 +60,11 @@ p_hsType = \case HsBoxedTuple -> parens HsConstraintTuple -> parens HsBoxedOrConstraintTuple -> parens - in parens' . velt $ withSep comma (located' p_hsType) xs + in parens' . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_hsType) xs HsSumTy NoExt xs -> - parensHash . velt $ withSep (txt "| ") (located' p_hsType) xs + parensHash . sitcc $ + sep (txt "| " >> breakpoint') (sitcc . located' p_hsType) xs HsOpTy NoExt x op y -> do located x p_hsType breakpoint @@ -115,10 +117,10 @@ p_hsType = \case case xs of ((L _ (HsTyVar _ Promoted _)):_) -> space _ -> return () - velt $ withSep comma (located' p_hsType) xs + sitcc $ sep (comma >> breakpoint) (sitcc . located' p_hsType) xs HsExplicitTupleTy NoExt xs -> do txt "'" - parens . velt $ withSep comma (located' p_hsType) xs + parens $ sep (comma >> breakpoint) (located' p_hsType) xs HsTyLit NoExt t -> atom t HsWildCardTy NoExt -> txt "_" XHsType (NHsCoreTy t) -> atom t @@ -127,7 +129,8 @@ p_hsContext :: HsContext GhcPs -> R () p_hsContext = \case [] -> txt "()" [x] -> located x p_hsType - xs -> parens . velt $ withSep comma (located' p_hsType) xs + xs -> parens . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_hsType) xs p_hsTyVarBndr :: HsTyVarBndr GhcPs -> R () p_hsTyVarBndr = \case @@ -142,23 +145,22 @@ p_hsTyVarBndr = \case XTyVarBndr NoExt -> notImplemented "XTyVarBndr" p_conDeclFields :: [LConDeclField GhcPs] -> R () -p_conDeclFields = - braces . velt . withSep comma (sitcc . located' p_conDeclField) +p_conDeclFields xs = braces . sitcc $ + sep (comma >> breakpoint) (sitcc . located' p_conDeclField) xs p_conDeclField :: ConDeclField GhcPs -> R () p_conDeclField ConDeclField {..} = do - sitcc . velt $ withSep - comma + sitcc $ sep (comma >> breakpoint) (located' (p_rdrName . rdrNameFieldOcc)) cd_fld_names breakpoint sitcc . inci $ do txt ":: " - locatedVia Nothing cd_fld_type p_hsType + p_hsType (unLoc cd_fld_type) p_conDeclField (XConDeclField NoExt) = notImplemented "XConDeclField" ---------------------------------------------------------------------------- --- Convertion functions +-- Conversion functions tyVarsToTypes :: LHsQTyVars GhcPs -> [LHsType GhcPs] tyVarsToTypes = \case diff --git a/src/Ormolu/Utils.hs b/src/Ormolu/Utils.hs index fb21939..2b94afb 100644 --- a/src/Ormolu/Utils.hs +++ b/src/Ormolu/Utils.hs @@ -1,5 +1,7 @@ -- | Random utilities used by the code. +{-# LANGUAGE LambdaCase #-} + module Ormolu.Utils ( combineSrcSpans' , isModule diff --git a/tests/Ormolu/Printer/CombinatorsSpec.hs b/tests/Ormolu/Printer/CombinatorsSpec.hs index b46fa17..01e471f 100644 --- a/tests/Ormolu/Printer/CombinatorsSpec.hs +++ b/tests/Ormolu/Printer/CombinatorsSpec.hs @@ -47,27 +47,27 @@ rSimpleSig :: R () rSimpleSig = line rFn rList0 :: R () -rList0 = line . brackets $ - velt +rList0 = line . brackets . sitcc $ + sep (comma >> breakpoint) id [ txt "foo" - , comma >> txt "bar" - , comma >> txt "baz" + , txt "bar" + , txt "baz" ] rList1 :: R () -rList1 = line . brackets $ - velt +rList1 = line . brackets . sitcc $ + sep (comma >> breakpoint) id [ txt "foo" - , comma >> txt "bar" - , comma >> rFn + , txt "bar" + , rFn ] rFn :: R () -rFn = velt' +rFn = sitcc $ sep breakpoint sitcc [ txt "foo" - , inci $ velt' + , inci . sitcc $ sep breakpoint sitcc [ do txt ":: " - parens $ velt' + parens . sitcc $ sep breakpoint sitcc [ txt "Int" , txt "-> " >> txt "Int" ] @@ -80,11 +80,11 @@ rModuleHeader = do line $ do txt "module " txt "MyModule" - line . inci . parens . velt $ + line . inci . parens . sitcc . sep (comma >> breakpoint) sitcc $ [ txt "R" - , comma >> txt "runR" - , comma >> txt "txt" - , comma >> txt "blah" + , txt "runR" + , txt "txt" + , txt "blah" ] line (txt "where") @@ -96,8 +96,10 @@ rModuleHeader = do shouldRender :: R () -> FilePath -> Expectation shouldRender m path = do + let rendered = runR False m mempty mempty emptyAnns + -- T.writeFile path rendered expected <- T.readFile path - runR False m mempty mempty emptyAnns `shouldBe` expected + rendered `shouldBe` expected -- | Render using single-line layout. diff --git a/tests/Ormolu/PrinterSpec.hs b/tests/Ormolu/PrinterSpec.hs index bc5d7b6..4ce4034 100644 --- a/tests/Ormolu/PrinterSpec.hs +++ b/tests/Ormolu/PrinterSpec.hs @@ -32,6 +32,7 @@ checkExample srcPath' = it (fromRelFile srcPath' ++ " works") $ do formatted0 <- ormoluFile defaultConfig (fromRelFile srcPath) -- 3. Check the output against expected output. Thus all tests should -- include two files: input and expected output. + -- T.writeFile (fromRelFile expectedOutputPath) formatted0 expected <- (liftIO . T.readFile . fromRelFile) expectedOutputPath shouldMatch False formatted0 expected -- 4. Check that running the formatter on the output produces the same