Skip to content

Conversation

@georgefst
Copy link
Collaborator

Ended up writing a fix for #39, to help with a script I was writing this afternoon.

Not quite sure about the details of the API yet.

Tests expected to fail right now, though fixes should be trivial. And new tests will be added anyway.

Copy/paste error from 'defaultPostProcess'.
-- only thing we will be doing with it is turning it /back/ into a string
-- at some stage, so we might as well cut out the middle man and store it
-- directly like this.
| CustomExpr Style !String
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This opens up a lot of flexibility. But it feels slightly ugly, since we will never actually parse a CustomExpr. We could formalise that fact with GADTs and DataKinds, but it's possibly unwarranted complexity.

Copy link
Owner

@cdepillabout cdepillabout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good to me!

@georgefst
Copy link
Collaborator Author

georgefst commented Dec 8, 2024

I'm spitballing here, and I'm not sure where it falls on a spectrum anywhere from making this redundant to being completely orthogonal, but anyway, if I ever return to this, I think we should look in to integrating syb somehow to make it easy to override show output for certain types:

import Data.Data
import Data.Generics.Aliases
import Text.Pretty.Simple

gshowWith :: (Data a, Typeable b) => (b -> String) -> a -> String
gshowWith f x = gshowsWith f x ""
gshowsWith :: (Data a, Typeable b) => (b -> String) -> a -> ShowS
gshowsWith f =
    ( \t ->
        showChar '('
            . (showString . showConstr . toConstr $ t)
            . (foldr (.) id . gmapQ ((showChar ' ' .) . gshowsWith f) $ t)
            . showChar ')'
    )
        `extQ` (shows :: String -> ShowS)
        -- only here do we modify the definition of `gshows`
        `extQ` ((++) . f)

data Example = Example Int String Word | Rec () Example deriving (Data, Typeable)
example :: Example
example = Rec () $ Example 42 "Hello" 15

main :: IO ()
main =
    pPrintString $
        gshowWith
            (\(i :: Int) -> "<int: " ++ show i ++ ">")
            example
( Rec ( () )
    ( Example <int: 42> "Hello" ( 15 ) )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants