@@ -1705,7 +1705,7 @@ layoutPretty = layoutWadlerLeijen
17051705-- >>> let hr = pipe <> pretty (replicate (26-2) '-') <> pipe
17061706-- >>> let go layouter x = (T.putStrLn . renderStrict . layouter (LayoutOptions (AvailablePerLine 26 1))) (vsep [hr, x, hr])
17071707--
1708- -- If we render this using @ 'layoutPretty'@ with a page width of 26 characters
1708+ -- If we render this using 'layoutPretty' with a page width of 26 characters
17091709-- per line, all the @fun@ calls fit into the first line so they will be put
17101710-- there,
17111711--
@@ -1730,22 +1730,38 @@ layoutPretty = layoutWadlerLeijen
17301730-- , ghijklm ])))))
17311731-- |------------------------|
17321732--
1733- -- The key difference between @'layoutPretty'@ and @'layoutSmart'@ is that the
1734- -- latter will check the potential document up to the end of the current
1735- -- indentation level, instead of just having one element lookahead.
1733+ -- The key difference between 'layoutPretty' and 'layoutSmart' is that the
1734+ -- latter will check the potential document until it encounters a line with the
1735+ -- same indentation or less than the start of the document. Any line encountered
1736+ -- earlier is assumed to belong to the same syntactic structure.
1737+ -- 'layoutPretty' checks only the first line.
1738+ --
1739+ -- Consider for example the question of whether the @A@s fit into the document
1740+ -- below:
1741+ --
1742+ -- > 1 A
1743+ -- > 2 A
1744+ -- > 3 A
1745+ -- > 4 B
1746+ -- > 5 B
1747+ --
1748+ -- 'layoutPretty' will check only line 1, ignoring whether e.g. line 2 might
1749+ -- already be too wide.
1750+ -- By contrast, 'layoutSmart' stops only once it reaches line 4, where the @B@
1751+ -- has the same indentation as the first @A@.
17361752layoutSmart
17371753 :: LayoutOptions
17381754 -> Doc ann
17391755 -> SimpleDocStream ann
17401756layoutSmart = layoutWadlerLeijen (FittingPredicate fits)
17411757 where
1742- -- Search with more lookahead: assuming that nesting roughly corresponds to
1743- -- syntactic depth, @fits@ checks that not only the current line fits, but
1744- -- the entire syntactic structure being formatted at this level of
1745- -- indentation fits. If we were to remove the second case for @SLine@, we
1746- -- would check that not only the current structure fits, but also the rest
1747- -- of the document, which would be slightly more intelligent but would have
1748- -- exponential runtime (and is prohibitively expensive in practice) .
1758+ -- Why doesn't layoutSmart simply check the entire document?
1759+ --
1760+ -- 1. That would be very expensive.
1761+ -- 2. In that case the layout of a particular part of a document would
1762+ -- depend on the fit of completely unrelated parts of the same document.
1763+ -- See https://github.com/quchen/prettyprinter/issues/83 for a related
1764+ -- bug .
17491765 fits :: PageWidth
17501766 -> Int -- ^ Minimum nesting level to fit in
17511767 -> Int -- ^ Width in which to fit the first line
0 commit comments