{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Hledger.Data.Errors (
makeAccountTagErrorExcerpt,
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.Posting (postingStripPrices)
import Hledger.Data.Types
import Hledger.Utils
import Data.Maybe
import Safe (headMay)
import Hledger.Data.Posting (isVirtual)
makeAccountTagErrorExcerpt :: (AccountName, AccountDeclarationInfo) -> TagName -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeAccountTagErrorExcerpt :: (Text, AccountDeclarationInfo)
-> Text -> (FilePath, Int, Maybe (Int, Maybe Int), Text)
makeAccountTagErrorExcerpt (Text
a, AccountDeclarationInfo
adi) Text
_t = (FilePath
f, Int
l, forall {a}. Maybe a
merrcols, Text
ex)
where
(SourcePos FilePath
f Pos
pos Pos
_) = AccountDeclarationInfo -> SourcePos
adisourcepos AccountDeclarationInfo
adi
l :: Int
l = Pos -> Int
unPos Pos
pos
txt :: Text
txt = (Text, AccountDeclarationInfo) -> Text
showAccountDirective (Text
a, AccountDeclarationInfo
adi) 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")
ex :: Text
ex = Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTagErrorExcerpt Int
l forall {a}. Maybe a
merrcols Text
txt
merrcols :: Maybe a
merrcols = forall {a}. Maybe a
Nothing
showAccountDirective :: (Text, AccountDeclarationInfo) -> Text
showAccountDirective (Text
a, AccountDeclarationInfo{Int
[Tag]
Text
SourcePos
adideclarationorder :: AccountDeclarationInfo -> Int
aditags :: AccountDeclarationInfo -> [Tag]
adicomment :: AccountDeclarationInfo -> Text
adisourcepos :: SourcePos
adideclarationorder :: Int
aditags :: [Tag]
adicomment :: Text
adisourcepos :: AccountDeclarationInfo -> SourcePos
..}) =
Text
"account " forall a. Semigroup a => a -> a -> a
<> Text
a
forall a. Semigroup a => a -> a -> a
<> (if Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null Text
adicomment then Text
" ; " forall a. Semigroup a => a -> a -> a
<> Text
adicomment else Text
"")
decorateTagErrorExcerpt :: Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTagErrorExcerpt :: Int -> Maybe (Int, Maybe Int) -> Text -> Text
decorateTagErrorExcerpt 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
_showAccountDirective :: a
_showAccountDirective = forall a. HasCallStack => a
undefined
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)
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
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
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
mpindex :: Maybe Int
mpindex = (Posting -> Bool) -> Transaction -> Maybe Int
transactionFindPostingIndex ((forall a. Eq a => a -> a -> Bool
==Posting
p)forall b c a. (b -> c) -> (a -> b) -> a -> c
.Posting -> Posting
postingStripPrices) Transaction
t
errrelline :: Int
errrelline = case Maybe Int
mpindex of
Maybe Int
Nothing -> Int
0
Just Int
pindex ->
Text -> Int
commentExtraLines (Transaction -> Text
tcomment Transaction
t) forall a. Num a => a -> a -> a
+
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map Posting -> Int
postingLines forall a b. (a -> b) -> a -> b
$ forall a. Int -> [a] -> [a]
take Int
pindex forall a b. (a -> b) -> a -> b
$ Transaction -> [Posting]
tpostings Transaction
t)
where
postingLines :: Posting -> Int
postingLines Posting
p' = Int
1 forall a. Num a => a -> a -> a
+ Text -> Int
commentExtraLines (Posting -> Text
pcomment Posting
p')
commentExtraLines :: Text -> Int
commentExtraLines Text
c = forall a. Ord a => a -> a -> a
max Int
0 (forall (t :: * -> *) a. Foldable t => t a -> Int
length (Text -> [Text]
T.lines Text
c) forall a. Num a => a -> a -> a
- Int
1)
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
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
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
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
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
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
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
(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))
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
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')