Skip to content

Commit

Permalink
Layout aligned elements more consistently
Browse files Browse the repository at this point in the history
Fixes #83.
  • Loading branch information
sjakobi authored and quchen committed Nov 5, 2019
1 parent 47d7d09 commit a006aff
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion prettyprinter/src/Data/Text/Prettyprint/Doc/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,15 @@ data WhitespaceStrippingState
deriving Typeable


-- | Test whether a docstream starts with a linebreak, ignoring any annotations.
startsWithLine :: SimpleDocStream ann -> Bool
startsWithLine sds = case sds of
SLine{} -> True
SAnnPush _ s -> startsWithLine s
SAnnPop s -> startsWithLine s
_ -> False


-- $
-- >>> import qualified Data.Text.IO as T
-- >>> doc = "lorem" <> hardline <> hardline <> pretty "ipsum"
Expand Down Expand Up @@ -1785,7 +1794,16 @@ layoutWadlerLeijen
| fits pWidth minNestingLevel availableWidth x = x
| otherwise = y
where
minNestingLevel = min lineIndent currentColumn
minNestingLevel =
-- See https://github.com/quchen/prettyprinter/issues/83.
if startsWithLine y
-- y might be a (more compact) hanging layout. Let's check x
-- thoroughly with the smaller lineIndent.
then lineIndent
-- y definitely isn't a hanging layout. Let's allow the first
-- line of x to be checked on its own and format it consistently
-- with subsequent lines with the same indentation.
else currentColumn
ribbonWidth = case pWidth of
AvailablePerLine lineLength ribbonFraction ->
(Just . max 0 . min lineLength . round)
Expand Down

0 comments on commit a006aff

Please sign in to comment.