fpco / th-utilities

Collection of useful functions for use with Template Haskell
MIT License
10 stars 6 forks source link

Add 'dumpSplices' #5

Closed neongreen closed 7 years ago

neongreen commented 7 years ago

I often use this utility for debugging TH code or understanding libraries that use TH (persistent, lens, etc):


-- | Print splices generated by a TH splice (the printing will happen
-- during compilation, as a GHC warning). Useful for debugging.
--
-- For instance, you can dump splices generated with 'makeLenses' by
-- replacing a top-level invocation of 'makeLenses' in your code with:
--
-- @dumpSplices $ makeLenses ''Foo@
--
dumpSplices :: DecsQ -> DecsQ
dumpSplices x = do
  ds <- x
  let code = lines (pprint ds)
  reportWarning ("\n" ++ unlines (map ("    " ++) code))
  return ds

However, it doesn't have a home and so I end up copying it from project to project. Perhaps it could be adopted by th-utilities? If so, I'll make a PR.

neongreen commented 7 years ago

(I know about -ddump-splices, of course, but my function lets you dump a specific splice instead of everything in the module, which makes a big difference when you have e.g. dozens of makeLenses in your code. Also, :set -ddump-splices breaks haskell-mode.)

mgsloan commented 7 years ago

Sure, this would be a quite sensible addition to the TH.Utilities module, a PR would be appreciated!