{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}

module Network.Bugsnag.StackFrame
  ( attachBugsnagCode
  , currentStackFrame
  ) where

import Prelude

import Data.Bugsnag
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text, pack, unpack)
import Instances.TH.Lift ()
import Language.Haskell.TH.Syntax
import Network.Bugsnag.CodeIndex

-- | Attempt to attach code to a 'StackFrame'
--
-- Looks up the content in the Index by File/LineNumber and, if found, sets it
-- on the record.
attachBugsnagCode :: CodeIndex -> StackFrame -> StackFrame
attachBugsnagCode :: CodeIndex -> StackFrame -> StackFrame
attachBugsnagCode CodeIndex
index StackFrame
sf =
  StackFrame
sf
    { stackFrame_code :: Maybe (HashMap Int Text)
stackFrame_code =
        String -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode
          (Text -> String
unpack forall a b. (a -> b) -> a -> b
$ StackFrame -> Text
stackFrame_file StackFrame
sf)
          (StackFrame -> Int
stackFrame_lineNumber StackFrame
sf)
          CodeIndex
index
    }

findBugsnagCode :: FilePath -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode :: String -> Int -> CodeIndex -> Maybe (HashMap Int Text)
findBugsnagCode String
path Int
n =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (Int, Int) -> CodeIndex -> Maybe [(Int, Text)]
findSourceRange String
path (Int
begin, Int
n forall a. Num a => a -> a -> a
+ Int
3)
 where
  begin :: Int
begin
    | Int
n forall a. Ord a => a -> a -> Bool
< Int
3 = Int
0
    | Bool
otherwise = Int
n forall a. Num a => a -> a -> a
- Int
3

-- | Construct a 'StackFrame' from the point of this splice
--
-- Unfortunately there's no way to know the function, so that must be given:
currentStackFrame :: Q Exp
currentStackFrame :: Q Exp
currentStackFrame = [|locStackFrame $(qLocation >>= liftLoc)|]

-- brittany-disable-next-binding

locStackFrame :: Loc -> Text -> StackFrame
locStackFrame :: Loc -> Text -> StackFrame
locStackFrame (Loc String
path String
_ String
_ (Int
ls, Int
cs) (Int, Int)
_) Text
func =
  StackFrame
defaultStackFrame
    { stackFrame_file :: Text
stackFrame_file = String -> Text
pack String
path
    , stackFrame_lineNumber :: Int
stackFrame_lineNumber = Int
ls
    , stackFrame_columnNumber :: Maybe Int
stackFrame_columnNumber = forall a. a -> Maybe a
Just Int
cs
    , stackFrame_method :: Text
stackFrame_method = Text
func
    , stackFrame_inProject :: Maybe Bool
stackFrame_inProject = forall a. a -> Maybe a
Just Bool
True
    , -- N.B. this assumes we're unlikely to see adoption within libraries, or
      -- that such a thing would even work. If this function's used, it's
      -- assumed to be in end-user code.
      stackFrame_code :: Maybe (HashMap Int Text)
stackFrame_code = forall a. Maybe a
Nothing
    }

-- brittany-disable-next-binding

-- Taken from monad-logger
liftLoc :: Loc -> Q Exp
liftLoc :: Loc -> Q Exp
liftLoc (Loc String
a String
b String
c (Int
d1, Int
d2) (Int
e1, Int
e2)) =
  [|
    Loc
      $(lift a)
      $(lift b)
      $(lift c)
      ($(lift d1), $(lift d2))
      ($(lift e1), $(lift e2))
    |]