Skip to content

Commit

Permalink
Merge pull request #473 from pcapriotti/topic/bump-pretty-printer
Browse files Browse the repository at this point in the history
Bump pretty printer and stop relying on its internal modules.
  • Loading branch information
HuwCampbell authored May 23, 2023
2 parents 08fc3c8 + 79f3a63 commit 2b3b129
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

- Widen bounds for `ansi-wl-pprint`. This supports the use of `prettyprinter`
in a non-breaking way, as the `ansi-wl-pprint > 1.0` support the newer
library.

- Export `helpIndent` from `Options.Applicative`.

- Export completion script generators from `Options.Applicative.BashCompletion`.
Expand Down
2 changes: 1 addition & 1 deletion optparse-applicative.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ library
build-depends: base == 4.*
, transformers >= 0.2 && < 0.7
, transformers-compat >= 0.3 && < 0.8
, ansi-wl-pprint >= 0.6.8 && < 0.7
, ansi-wl-pprint >= 0.6.8 && < 1.1

if flag(process)
build-depends: process >= 1.0 && < 1.7
Expand Down
4 changes: 3 additions & 1 deletion src/Options/Applicative/BashCompletion.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
-- | You don't need to import this module to enable bash completion.
--
-- See
-- <http://github.com/pcapriotti/optparse-applicative/wiki/Bash-Completion the wiki>
-- for more information on bash completion.
module Options.Applicative.BashCompletion
( bashCompletionParser,
( bashCompletionParser,

bashCompletionScript,
fishCompletionScript,
zshCompletionScript,
Expand Down
1 change: 1 addition & 0 deletions src/Options/Applicative/Help/Chunk.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Options.Applicative.Help.Chunk
( Chunk(..)
, chunked
Expand Down
3 changes: 2 additions & 1 deletion src/Options/Applicative/Help/Core.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Options.Applicative.Help.Core (
cmdDesc,
briefDesc,
Expand All @@ -24,7 +25,7 @@ import Control.Monad (guard)
import Data.Function (on)
import Data.List (sort, intersperse, groupBy)
import Data.Foldable (any, foldl')
import Data.Maybe (maybeToList, catMaybes, fromMaybe)
import Data.Maybe (catMaybes, fromMaybe)
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (mempty)
#endif
Expand Down
30 changes: 21 additions & 9 deletions src/Options/Applicative/Help/Pretty.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
module Options.Applicative.Help.Pretty
( module Text.PrettyPrint.ANSI.Leijen
, Doc
, indent
, renderPretty
, displayS
, (.$.)
, groupOrNestLine
, altSep
, hangAtIfOver
) where

import Control.Applicative
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup ((<>))
#endif

import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>), columns)
import Text.PrettyPrint.ANSI.Leijen.Internal (Doc (..), flatten)
import Text.PrettyPrint.ANSI.Leijen hiding (Doc, (<$>), (<>), columns, indent, renderPretty, displayS)
import qualified Text.PrettyPrint.ANSI.Leijen as PP

import Prelude

type Doc = PP.Doc

indent :: Int -> PP.Doc -> PP.Doc
indent = PP.indent

renderPretty :: Float -> Int -> PP.Doc -> SimpleDoc
renderPretty = PP.renderPretty

displayS :: SimpleDoc -> ShowS
displayS = PP.displayS

(.$.) :: Doc -> Doc -> Doc
(.$.) = (PP.<$>)

Expand All @@ -38,8 +52,8 @@ ifAtRoot =
-- start of our nesting level.
ifElseAtRoot :: (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
ifElseAtRoot f g doc =
Nesting $ \i ->
Column $ \j ->
nesting $ \i ->
column $ \j ->
if i == j
then f doc
else g doc
Expand All @@ -52,9 +66,7 @@ ifElseAtRoot f g doc =
-- group.
groupOrNestLine :: Doc -> Doc
groupOrNestLine =
Union
<$> flatten
<*> ifNotAtRoot (line <>) . nest 2
group . ifNotAtRoot (linebreak <>) . nest 2


-- | Separate items in an alternative with a pipe.
Expand Down Expand Up @@ -85,7 +97,7 @@ altSep x y =
-- the starting column, and it won't be indented more.
hangAtIfOver :: Int -> Int -> Doc -> Doc
hangAtIfOver i j d =
Column $ \k ->
column $ \k ->
if k <= j then
align d
else
Expand Down
7 changes: 3 additions & 4 deletions tests/test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -949,12 +949,11 @@ prop_long_command_line_flow = once $
---

deriving instance Arbitrary a => Arbitrary (Chunk a)
deriving instance Eq SimpleDoc
deriving instance Show SimpleDoc


equalDocs :: Float -> Int -> Doc -> Doc -> Property
equalDocs f w d1 d2 = Doc.renderPretty f w d1
=== Doc.renderPretty f w d2
equalDocs f w d1 d2 = Doc.displayS (Doc.renderPretty f w d1) ""
=== Doc.displayS (Doc.renderPretty f w d2) ""

prop_listToChunk_1 :: [String] -> Property
prop_listToChunk_1 xs = isEmpty (listToChunk xs) === null xs
Expand Down

0 comments on commit 2b3b129

Please sign in to comment.