{-|
Helpers for making error messages.
-}

{-# LANGUAGE OverloadedStrings #-}

module Hledger.Data.Errors (
  makeTransactionErrorExcerpt,
  makePostingErrorExcerpt,
  makePostingAccountErrorExcerpt,
  makeBalanceAssertionErrorExcerpt,
  transactionFindPostingIndex,
)
where

import Data.Function ((&))
import Data.List (find)
import Data.Text (Text)
import qualified Data.Text as T

import Hledger.Data.Transaction (showTransaction)
import Hledger.Data.Types
import Hledger.Utils
import Data.Maybe
import Safe (headMay)
import Hledger.Data.Posting (isVirtual)

-- | Given a problem transaction and a function calculating the best
-- column(s) for marking the error region:
-- render it as a megaparsec-style excerpt, showing the original line number
-- on the transaction line, and a column(s) marker.
-- Returns the file path, line number, column(s) if known,
-- and the rendered excerpt, or as much of these as is possible.
-- A limitation: columns will be accurate for the rendered error message but not for the original journal data.
makeTransactionErrorExcerpt :: Transaction -> (Transaction -> Maybe (Int, Maybe Int)) -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeTransactionErrorExcerpt :: Transaction
-> (Transaction -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeTransactionErrorExcerpt Transaction
t Transaction -> Maybe (Int, Maybe Int)
findtxnerrorcolumns = (FilePath
f, Int
tl, Maybe (Int, Maybe Int)
merrcols, Text
ex)
  -- XXX findtxnerrorcolumns is awkward, I don't think this is the final form
  where
    (SourcePos FilePath
f Pos
tpos Pos
_) = forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ Transaction -> (SourcePos, SourcePos)
tsourcepos Transaction
t
    tl :: Int
tl = Pos -> Int
unPos Pos
tpos
    txntxt :: Text
txntxt = Transaction -> Text
showTransaction Transaction
t forall a b. a -> (a -> b) -> b
& Text -> Text
textChomp forall a b. a -> (a -> b) -> b
& (forall a. Semigroup a => a -> a -> a
<>Text
"\n")
    merrcols :: Maybe (Int, Maybe Int)
merrcols = Transaction -> Maybe (Int, Maybe Int)
findtxnerrorcolumns Transaction
t
    ex :: Text
ex = Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTransactionErrorExcerpt Int
tl Maybe (Int, Maybe Int)
merrcols Text
txntxt

-- | Add megaparsec-style left margin, line number, and optional column marker(s).
decorateTransactionErrorExcerpt :: Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTransactionErrorExcerpt :: Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTransactionErrorExcerpt Int
l Maybe (Int, Maybe Int)
mcols Text
txt =
  [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ [Text]
ls' forall a. Semigroup a => a -> a -> a
<> [Text]
colmarkerline forall a. Semigroup a => a -> a -> a
<> forall a b. (a -> b) -> [a] -> [b]
map (Text
lineprefixforall a. Semigroup a => a -> a -> a
<>) [Text]
ms
  where
    ([Text]
ls,[Text]
ms) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
1 forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
txt
    ls' :: [Text]
ls' = forall a b. (a -> b) -> [a] -> [b]
map ((FilePath -> Text
T.pack (forall a. Show a => a -> FilePath
show Int
l) forall a. Semigroup a => a -> a -> a
<> Text
" | ") forall a. Semigroup a => a -> a -> a
<>) [Text]
ls
    colmarkerline :: [Text]
colmarkerline =
      [Text
lineprefix forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate (Int
colforall a. Num a => a -> a -> a
-Int
1) Text
" " forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate Int
regionw Text
"^"
      | Just (Int
col, Maybe Int
mendcol) <- [Maybe (Int, Maybe Int)
mcols]
      , let regionw :: Int
regionw = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
1 (forall a. Num a => a -> a -> a
subtract Int
col) Maybe Int
mendcol forall a. Num a => a -> a -> a
+ Int
1
      ]
    lineprefix :: Text
lineprefix = Int -> Text -> Text
T.replicate Int
marginw Text
" " forall a. Semigroup a => a -> a -> a
<> Text
"| "
      where  marginw :: Int
marginw = forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Show a => a -> FilePath
show Int
l) forall a. Num a => a -> a -> a
+ Int
1

-- | Given a problem posting and a function calculating the best
-- column(s) for marking the error region:
-- look up error info from the parent transaction, and render the transaction
-- as a megaparsec-style excerpt, showing the original line number
-- on the problem posting's line, and a column indicator.
-- Returns the file path, line number, column(s) if known,
-- and the rendered excerpt, or as much of these as is possible.
-- A limitation: columns will be accurate for the rendered error message but not for the original journal data.
makePostingErrorExcerpt :: Posting -> (Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)) -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingErrorExcerpt :: Posting
-> (Posting -> Transaction -> Text -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingErrorExcerpt Posting
p Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
findpostingerrorcolumns =
  case Posting -> Maybe Transaction
ptransaction Posting
p of
    Maybe Transaction
Nothing -> (FilePath
"-", Int
0, forall a. Maybe a
Nothing, Text
"")
    Just Transaction
t  -> (FilePath
f, Int
errabsline, Maybe (Int, Maybe Int)
merrcols, Text
ex)
      where
        (SourcePos FilePath
f Pos
tl Pos
_) = forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ Transaction -> (SourcePos, SourcePos)
tsourcepos Transaction
t
        tcommentlines :: Int
tcommentlines = forall a. Ord a => a -> a -> a
max Int
0 (forall (t :: * -> *) a. Foldable t => t a -> Int
length (Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ Transaction -> Text
tcomment Transaction
t) forall a. Num a => a -> a -> a
- Int
1)
        mpindex :: Maybe Int
mpindex = (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex (forall a. Eq a => a -> a -> Bool
==Posting
p) Transaction
t
        errrelline :: Int
errrelline = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 (Int
tcommentlinesforall a. Num a => a -> a -> a
+) Maybe Int
mpindex   -- XXX doesn't count posting coment lines
        errabsline :: Int
errabsline = Pos -> Int
unPos Pos
tl forall a. Num a => a -> a -> a
+ Int
errrelline
        txntxt :: Text
txntxt = Transaction -> Text
showTransaction Transaction
t forall a b. a -> (a -> b) -> b
& Text -> Text
textChomp forall a b. a -> (a -> b) -> b
& (forall a. Semigroup a => a -> a -> a
<>Text
"\n")
        merrcols :: Maybe (Int, Maybe Int)
merrcols = Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
findpostingerrorcolumns Posting
p Transaction
t Text
txntxt
        ex :: Text
ex = Int -> Int -> Maybe (Int, Maybe Int) -> Text -> Text
decoratePostingErrorExcerpt Int
errabsline Int
errrelline Maybe (Int, Maybe Int)
merrcols Text
txntxt

-- | Add megaparsec-style left margin, line number, and optional column marker(s).
decoratePostingErrorExcerpt :: Int -> Int -> Maybe (Int, Maybe Int) -> Text -> Text
decoratePostingErrorExcerpt :: Int -> Int -> Maybe (Int, Maybe Int) -> Text -> Text
decoratePostingErrorExcerpt Int
absline Int
relline Maybe (Int, Maybe Int)
mcols Text
txt =
  [Text] -> Text
T.unlines forall a b. (a -> b) -> a -> b
$ [Text]
js' forall a. Semigroup a => a -> a -> a
<> [Text]
ks' forall a. Semigroup a => a -> a -> a
<> [Text]
colmarkerline forall a. Semigroup a => a -> a -> a
<> [Text]
ms'
  where
    ([Text]
ls,[Text]
ms) = forall a. Int -> [a] -> ([a], [a])
splitAt (Int
rellineforall a. Num a => a -> a -> a
+Int
1) forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
txt
    ([Text]
js,[Text]
ks) = forall a. Int -> [a] -> ([a], [a])
splitAt (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
ls forall a. Num a => a -> a -> a
- Int
1) [Text]
ls
    ([Text]
js',[Text]
ks') = case [Text]
ks of
      [Text
k] -> (forall a b. (a -> b) -> [a] -> [b]
map (Text
lineprefixforall a. Semigroup a => a -> a -> a
<>) [Text]
js, [FilePath -> Text
T.pack (forall a. Show a => a -> FilePath
show Int
absline) forall a. Semigroup a => a -> a -> a
<> Text
" | " forall a. Semigroup a => a -> a -> a
<> Text
k])
      [Text]
_   -> ([], [])
    ms' :: [Text]
ms' = forall a b. (a -> b) -> [a] -> [b]
map (Text
lineprefixforall a. Semigroup a => a -> a -> a
<>) [Text]
ms
    colmarkerline :: [Text]
colmarkerline =
      [Text
lineprefix forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate (Int
colforall a. Num a => a -> a -> a
-Int
1) Text
" " forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.replicate Int
regionw Text
"^"
      | Just (Int
col, Maybe Int
mendcol) <- [Maybe (Int, Maybe Int)
mcols]
      , let regionw :: Int
regionw = Int
1 forall a. Num a => a -> a -> a
+ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 (forall a. Num a => a -> a -> a
subtract Int
col) Maybe Int
mendcol
      ]
    lineprefix :: Text
lineprefix = Int -> Text -> Text
T.replicate Int
marginw Text
" " forall a. Semigroup a => a -> a -> a
<> Text
"| "
      where  marginw :: Int
marginw = forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Show a => a -> FilePath
show Int
absline) forall a. Num a => a -> a -> a
+ Int
1

-- | Find the 1-based index of the first posting in this transaction
-- satisfying the given predicate.
transactionFindPostingIndex :: (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex :: (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex Posting -> Bool
ppredicate = 
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Posting -> Bool
ppredicateforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] forall b c a. (b -> c) -> (a -> b) -> a -> c
. Transaction -> [Posting]
tpostings

-- | From the given posting, make an error excerpt showing the transaction with
-- this posting's account part highlighted.
makePostingAccountErrorExcerpt :: Posting -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingAccountErrorExcerpt :: Posting -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingAccountErrorExcerpt Posting
p = Posting
-> (Posting -> Transaction -> Text -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingErrorExcerpt Posting
p forall {p} {p}. Posting -> p -> p -> Maybe (Int, Maybe Int)
finderrcols
  where
    -- Calculate columns suitable for highlighting the synthetic excerpt.
    finderrcols :: Posting -> p -> p -> Maybe (Int, Maybe Int)
finderrcols Posting
p' p
_ p
_ = forall a. a -> Maybe a
Just (Int
col, forall a. a -> Maybe a
Just Int
col2)
      where
        col :: Int
col = Int
5 forall a. Num a => a -> a -> a
+ if Posting -> Bool
isVirtual Posting
p' then Int
1 else Int
0
        col2 :: Int
col2 = Int
col forall a. Num a => a -> a -> a
+ Text -> Int
T.length (Posting -> Text
paccount Posting
p') forall a. Num a => a -> a -> a
- Int
1

-- | From the given posting, make an error excerpt showing the transaction with
-- the balance assertion highlighted.
makeBalanceAssertionErrorExcerpt :: Posting -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeBalanceAssertionErrorExcerpt :: Posting -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeBalanceAssertionErrorExcerpt Posting
p = Posting
-> (Posting -> Transaction -> Text -> Maybe (Int, Maybe Int))
-> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makePostingErrorExcerpt Posting
p Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
finderrcols
  where
    finderrcols :: Posting -> Transaction -> Text -> Maybe (Int, Maybe Int)
finderrcols Posting
p' Transaction
t Text
trendered = forall a. a -> Maybe a
Just (Int
col, forall a. a -> Maybe a
Just Int
col2)
      where
        -- Analyse the rendering to find the columns to highlight.
        tlines :: Int
tlines = forall a. Show a => FilePath -> a -> a
dbg5 FilePath
"tlines" forall a b. (a -> b) -> a -> b
$ forall a. Ord a => a -> a -> a
max Int
1 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ Transaction -> Text
tcomment Transaction
t  -- transaction comment can generate extra lines
        (Int
col, Int
col2) =
          let def :: (Int, Int)
def = (Int
5, forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (forall a b. (a -> b) -> [a] -> [b]
map Text -> Int
T.length forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
trendered))  -- fallback: underline whole posting. Shouldn't happen.
          in
            case (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex (forall a. Eq a => a -> a -> Bool
==Posting
p') Transaction
t of
              Maybe Int
Nothing  -> (Int, Int)
def
              Just Int
idx -> forall a. a -> Maybe a -> a
fromMaybe (Int, Int)
def forall a b. (a -> b) -> a -> b
$ do
                let
                  beforeps :: [Posting]
beforeps = forall a. Int -> [a] -> [a]
take (Int
idxforall a. Num a => a -> a -> a
-Int
1) forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t
                  beforepslines :: Int
beforepslines = forall a. Show a => FilePath -> a -> a
dbg5 FilePath
"beforepslines" forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a. Ord a => a -> a -> a
max Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
T.lines forall b c a. (b -> c) -> (a -> b) -> a -> c
. Posting -> Text
pcomment) [Posting]
beforeps   -- posting comment can generate extra lines (assume only one commodity shown)
                Text
assertionline <- forall a. Show a => FilePath -> a -> a
dbg5 FilePath
"assertionline" forall a b. (a -> b) -> a -> b
$ forall a. [a] -> Maybe a
headMay forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
drop (Int
tlines forall a. Num a => a -> a -> a
+ Int
beforepslines) forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines Text
trendered
                let
                  col2' :: Int
col2' = Text -> Int
T.length Text
assertionline
                  l :: FilePath
l = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall a. Eq a => a -> a -> Bool
/= Char
'=') forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse forall a b. (a -> b) -> a -> b
$ Text -> FilePath
T.unpack Text
assertionline
                  l' :: FilePath
l' = forall a. (a -> Bool) -> [a] -> [a]
dropWhile (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char
'=',Char
'*']) FilePath
l
                  col' :: Int
col' = forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
l' forall a. Num a => a -> a -> a
+ Int
1
                forall (m :: * -> *) a. Monad m => a -> m a
return (Int
col', Int
col2')