-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathUtil.hs
44 lines (39 loc) · 1.86 KB
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Development.IDE.Plugin.CodeAction.Util where
import Data.Data (Data)
import Data.Time.Clock.POSIX (POSIXTime,
getCurrentTime,
utcTimeToPOSIXSeconds)
import qualified Data.Unique as U
import Debug.Trace
import Development.IDE.GHC.Compat.ExactPrint as GHC
import Development.IDE.GHC.Dump (showAstDataHtml)
import GHC.Stack
import GHC.Utils.Outputable
import System.Environment.Blank (getEnvDefault)
import System.IO.Unsafe
import Text.Printf
--------------------------------------------------------------------------------
-- Tracing exactprint terms
-- Should in `Development.IDE.GHC.Orphans`,
-- leave it here to prevent cyclic module dependency
{-# NOINLINE timestamp #-}
timestamp :: POSIXTime
timestamp = utcTimeToPOSIXSeconds $ unsafePerformIO getCurrentTime
debugAST :: Bool
debugAST = unsafePerformIO (getEnvDefault "GHCIDE_DEBUG_AST" "0") == "1"
-- | Prints an 'Outputable' value to stderr and to an HTML file for further inspection
traceAst :: (Data a, ExactPrint a, HasCallStack) => String -> a -> a
traceAst lbl x
| debugAST = trace doTrace x
| otherwise = x
where
renderDump = renderWithContext defaultSDocContext{sdocStyle = defaultDumpStyle, sdocPprDebug = True}
htmlDump = showAstDataHtml x
doTrace = unsafePerformIO $ do
u <- U.newUnique
let htmlDumpFileName = printf "/tmp/hls/%s-%s-%d.html" (show timestamp) lbl (U.hashUnique u)
writeFile htmlDumpFileName $ renderDump htmlDump
return $ unlines
[prettyCallStack callStack ++ ":"
, exactPrint x
, "file://" ++ htmlDumpFileName]