Fix stack overflow for very large docs

Based on the origin PR here: https://github.com/haskell/pretty/pull/9