Pretty.hs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module Data.SCargot.Pretty where
  2. -- | A 'LayoutOptions' value describes how to pretty-print a 'SExpr'.
  3. -- It describes how to print atoms, what horizontal space to fit
  4. -- it into, and other related options.
  5. --
  6. -- The 'swingIndent' value might require a big of explanation: in
  7. -- pretty-printing s-expressions, you have the option of whether
  8. -- to 'swing' expressions which get pushed to subsequent lines
  9. -- to the left, or to align them along the right. e.g. the
  10. -- s-expression @(foo a b)@ could use a non-swing indent as
  11. --
  12. -- > (foo arg-one
  13. -- > arg-two)
  14. --
  15. -- or a swing indent as
  16. --
  17. -- > (foo arg-one
  18. -- > arg-two)
  19. --
  20. -- often, in formatting Lisp code, control structures will
  21. -- swing subsequent expressions, as in
  22. --
  23. -- > (define (factorial n)
  24. -- > (if (= n 0)
  25. -- > 1
  26. -- > (* n (fact (- n 1)))))
  27. --
  28. -- but most functions will _not_ swing:
  29. --
  30. -- > (call-my-func arg-number-one
  31. -- > arg-number-two
  32. -- > arg-number-three)
  33. --
  34. -- The 'swingIndent' field lets you choose whether or not to
  35. -- swing subsequent s-expressions based on the atom in the car
  36. -- position of a list. You can default to always swinging subsequent
  37. -- expressions with @const True@ and never with @const False@, or
  38. -- choose based on some more advanced criteria. _If_ a swing happens,
  39. -- subsequent lines are indented based on the 'indentAmount' variable;
  40. -- otherwise, subsequent lines are indented based on the size of the
  41. -- @car@ of the list.
  42. data LayoutOptions a = LayoutOptions
  43. { atomPrinter :: a -> Text -- ^ How to serialize a given atom to 'Text'.
  44. , swingIndent :: a -> Bool -- ^ Whether or not to swing
  45. , indentAmount :: Int -- ^ How much to indent after a swing
  46. , maxWidth :: Maybe Int -- ^ The maximum width (if any)
  47. }
  48. basicPrint :: (a -> Text) -> LayoutOptions a
  49. basicPrint f = LayoutOptions
  50. { atomPrinter = f
  51. , swingIndent = const False
  52. , maxWidth = Nothing
  53. }
  54. prettyPrintSExpr :: LayoutOptions a -> SExpr a -> Text