git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1645, implement new indentation rules for types
0d104d9Phil Freeman
src/Language/PureScript/Pretty/Types.hs+12−6
@@ -121,21 +121,27 @@ constrained = mkPattern match
121 match _ = Nothing
121 match _ = Nothing
122
122
123matchTypeAtom :: Pattern () Type Box
123matchTypeAtom :: Pattern () Type Box
124−matchTypeAtom = typeLiterals <+> fmap ((`before` text ")") . (text "(" <>)) matchType
124+matchTypeAtom = typeLiterals <+> fmap ((`before` (text ")")) . (text "(" <>)) matchType
125
125
126matchType :: Pattern () Type Box
126matchType :: Pattern () Type Box
127matchType = buildPrettyPrinter operators matchTypeAtom
127matchType = buildPrettyPrinter operators matchTypeAtom
128 where
128 where
129 operators :: OperatorTable () Type Box
129 operators :: OperatorTable () Type Box
130 operators =
130 operators =
131− OperatorTable [ [ AssocL typeApp $ \f x -> f `beforeWithSpace` x ]
131+ OperatorTable [ [ AssocL typeApp $ \f x -> keepSingleLinesOr (moveRight 2) f x ]
132− , [ AssocR appliedFunction $ \arg ret -> (arg <> text " ") `before` (text "-> " <> ret)
132+ , [ AssocR appliedFunction $ \arg ret -> keepSingleLinesOr id arg (text "-> " <> ret) ]
133− ]
134 , [ Wrap constrained $ \deps ty -> constraintsAsBox deps ty ]
133 , [ Wrap constrained $ \deps ty -> constraintsAsBox deps ty ]
135− , [ Wrap forall_ $ \idents ty -> text ("forall " ++ unwords idents ++ ". ") <> ty ]
134+ , [ Wrap forall_ $ \idents ty -> keepSingleLinesOr (moveRight 2) (text ("forall " ++ unwords idents ++ ".")) ty ]
136− , [ Wrap kinded $ \k ty -> ty `before` text (" :: " ++ prettyPrintKind k) ]
135+ , [ Wrap kinded $ \k ty -> keepSingleLinesOr (moveRight 2) ty (text (":: " ++ prettyPrintKind k)) ]
137 ]
136 ]
138
137
138+ -- If both boxes span a single line, keep them on the same line, or else
139+ -- use the specified function to modify the second box, then combine vertically.
140+ keepSingleLinesOr :: (Box -> Box) -> Box -> Box -> Box
141+ keepSingleLinesOr f b1 b2
142+ | rows b1 > 1 || rows b2 > 1 = vcat left [ b1, f b2 ]
143+ | otherwise = hcat top [ b1, text " ", b2]
144+
139forall_ :: Pattern () Type ([String], Type)
145forall_ :: Pattern () Type ([String], Type)
140forall_ = mkPattern match
146forall_ = mkPattern match
141 where
147 where
src/Language/PureScript/Pretty/Values.hs+1
@@ -160,6 +160,7 @@ prettyPrintBinderAtom (ArrayBinder bs) =
160 ++ " ]"
160 ++ " ]"
161prettyPrintBinderAtom (NamedBinder ident binder) = showIdent ident ++ "@" ++ prettyPrintBinder binder
161prettyPrintBinderAtom (NamedBinder ident binder) = showIdent ident ++ "@" ++ prettyPrintBinder binder
162prettyPrintBinderAtom (PositionedBinder _ _ binder) = prettyPrintBinderAtom binder
162prettyPrintBinderAtom (PositionedBinder _ _ binder) = prettyPrintBinderAtom binder
163+prettyPrintBinderAtom (TypedBinder _ binder) = prettyPrintBinderAtom binder
163prettyPrintBinderAtom b = parens (prettyPrintBinder b)
164prettyPrintBinderAtom b = parens (prettyPrintBinder b)
164
165
165-- |
166-- |