@@ -353,7 +353,14 @@ p_hsCmdTop = \case
353353 HsCmdTop NoExtField cmd -> located cmd p_hsCmd
354354 XCmdTop x -> noExtCon x
355355
356−withSpacing :: Data a => (a -> R ()) -> Located a -> R ()
356+-- | Render an expression preserving blank lines between such consecutive
357+-- expressions found in the original source code.
358+withSpacing ::
359+ -- | Rendering function
360+ (a -> R ()) ->
361+ -- | Entity to render
362+ Located a ->
363+ R ()
357364withSpacing f l = located l $ \x -> do
358365 case getLoc l of
359366 UnhelpfulSpan _ -> f x
@@ -464,30 +471,24 @@ gatherStmtBlock (XParStmtBlock x) = noExtCon x
464471p_hsLocalBinds :: HsLocalBindsLR GhcPs GhcPs -> R ()
465472p_hsLocalBinds = \case
466473 HsValBinds NoExtField (ValBinds NoExtField bag lsigs) -> do
467− let ssStart =
468− either
469− (srcSpanStart . getLoc)
470− (srcSpanStart . getLoc)
471− items =
472− (Left <$> bagToList bag) ++ (Right <$> lsigs)
473− p_item (Left x) = located x p_valDecl
474− p_item (Right x) = located x p_sigDecl
475474 -- When in a single-line layout, there is a chance that the inner
476475 -- elements will also contain semicolons and they will confuse the
477476 -- parser. so we request braces around every element except the last.
478477 br <- layoutToBraces <$> getLayout
479− sitcc $
480− sepSemi
481− ( \(p, i) ->
482− ( case p of
483− SinglePos -> id
484− FirstPos -> br
485− MiddlePos -> br
486− LastPos -> id
487− )
488− (p_item i)
489− )
490− (attachRelativePos $ sortOn ssStart items)
478+ let items =
479+ let injectLeft (L l x) = L l (Left x)
480+ injectRight (L l x) = L l (Right x)
481+ in (injectLeft <$> bagToList bag) ++ (injectRight <$> lsigs)
482+ positionToBracing = \case
483+ SinglePos -> id
484+ FirstPos -> br
485+ MiddlePos -> br
486+ LastPos -> id
487+ p_item' (p, item) =
488+ positionToBracing p $
489+ withSpacing (either p_valDecl p_sigDecl) item
490+ binds = sortOn (srcSpanStart . getLoc) items
491+ sitcc $ sepSemi p_item' (attachRelativePos binds)
491492 HsValBinds NoExtField _ -> notImplemented "HsValBinds"
492493 HsIPBinds NoExtField (IPBinds NoExtField xs) ->
493494 -- Second argument of IPBind is always Left before type-checking.