From 11684843a09893f09bf80be84086d7040b8e79ad Mon Sep 17 00:00:00 2001 From: Sam Jeeves Date: Sat, 14 Oct 2017 22:10:57 +0200 Subject: [PATCH] Balance assertion errors now show line of failed assertion posting. Fixes #481. --- hledger-lib/Hledger/Data/Journal.hs | 4 +++- hledger-lib/Hledger/Data/Transaction.hs | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index dcd7bbb34..6a115c9ef 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -535,7 +535,9 @@ checkBalanceAssertion p@Posting{ pbalanceassertion = Just ass} amt (case ptransaction p of Nothing -> ":" -- shouldn't happen Just t -> printf " in %s:\nin transaction:\n%s" - (showGenericSourcePos $ tsourcepos t) (chomp $ show t) :: String) + (showGenericSourcePos postingPos) (chomp $ show t) :: String + where postingLine = fromJust $ elemIndex p $ tpostings t -- assume postings are in order + postingPos = increaseSourceLine (1+postingLine) (tsourcepos t)) (showPostingLine p) (showDate $ postingDate p) (T.unpack $ paccount p) -- XXX pack diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 7c75a1dac..acd6454dd 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -42,6 +42,7 @@ module Hledger.Data.Transaction ( sourceFilePath, sourceFirstLine, showGenericSourcePos, + increaseSourceLine, -- * misc. tests_Hledger_Data_Transaction ) @@ -81,6 +82,10 @@ sourceFirstLine = \case GenericSourcePos _ line _ -> line JournalSourcePos _ (line, _) -> line +increaseSourceLine :: Int -> GenericSourcePos -> GenericSourcePos +increaseSourceLine val (GenericSourcePos fp line col) = GenericSourcePos fp (line+val) col +increaseSourceLine val (JournalSourcePos fp (first, _)) = GenericSourcePos fp (first+val) 0 + showGenericSourcePos :: GenericSourcePos -> String showGenericSourcePos = \case GenericSourcePos fp line column -> show fp ++ " (line " ++ show line ++ ", column " ++ show column ++ ")"