tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
956 stars 83 forks source link

Implement Term as pure operation #985

Closed brandonchinn178 closed 1 year ago

brandonchinn178 commented 1 year ago

Implement Term as an AST builder + interpreter, so that tests and ormolu-live (and Fourmolu) don't have to use hacky work-arounds to print exceptions.

The monad instance is a bit sus, but no one should really be using <- in any Term blocks. If someone does, it'll just blow up in tests. We can get rid of the monad weirdness by just writing everything as

prettyOrmoluException :: OrmoluException -> PrettyText
prettyOrmoluException = \case
  OrmoluParsingFailed s e ->
    [ WithBold [putOutputable s]
    , ...
    ]

type PrettyText = [PrettyNode]
data PrettyNode
  = OutputText Text
  | WithColor Color PrettyText
  | WithBold PrettyText

hPutPretty :: ColorMode -> Handle -> PrettyText -> IO ()
getPretty :: PrettyText -> Text

But the do-notation does look a bit nicer, so I didn't want to rewrite all of that for now.