From d07bf4afbb192f9d28c6349cd24d6e32f7d9de96 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 12 Jul 2022 14:40:49 +0100 Subject: [PATCH] fix: errors: omit wrong column numbers in a number of error messages accounts, commodities, payees, ordereddates, uniqueleafnames The column numbers were accurate for the rendered excerpt but not for the actual data. --- hledger-lib/Hledger/Data/Errors.hs | 2 ++ hledger-lib/Hledger/Data/JournalChecks.hs | 30 +++++++++++-------- .../Data/JournalChecks/Ordereddates.hs | 9 ++---- .../Data/JournalChecks/Uniqueleafnames.hs | 9 ++---- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/hledger-lib/Hledger/Data/Errors.hs b/hledger-lib/Hledger/Data/Errors.hs index eb871e6f9..c398b2544 100644 --- a/hledger-lib/Hledger/Data/Errors.hs +++ b/hledger-lib/Hledger/Data/Errors.hs @@ -26,6 +26,7 @@ import Hledger.Utils -- 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 t findtxnerrorcolumns = (f, tl, merrcols, ex) -- XXX findtxnerrorcolumns is awkward, I don't think this is the final form @@ -58,6 +59,7 @@ decorateTransactionErrorExcerpt l mcols txt = -- 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 p findpostingerrorcolumns = case ptransaction p of diff --git a/hledger-lib/Hledger/Data/JournalChecks.hs b/hledger-lib/Hledger/Data/JournalChecks.hs index 7f2d0d918..f94213da0 100644 --- a/hledger-lib/Hledger/Data/JournalChecks.hs +++ b/hledger-lib/Hledger/Data/JournalChecks.hs @@ -41,11 +41,12 @@ journalCheckAccounts j = mapM_ checkacct (journalPostings j) checkacct p@Posting{paccount=a} | a `elem` journalAccountNamesDeclared j = Right () | otherwise = Left $ - printf "%s:%d:%d-%d:\n%sundeclared account \"%s\"\n" f l col col2 ex a + printf "%s:%d:\n%sundeclared account \"%s\"\n" f l ex a where - (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols - col = maybe 0 fst mcols - col2 = maybe 0 (fromMaybe 0 . snd) mcols + (f,l,_mcols,ex) = makePostingErrorExcerpt p finderrcols + -- Calculate columns suitable for highlighting the excerpt. + -- We won't show these in the main error line as they aren't + -- accurate for the actual data. finderrcols p _ _ = Just (col, Just col2) where col = 5 + if isVirtual p then 1 else 0 @@ -60,11 +61,9 @@ journalCheckCommodities j = mapM_ checkcommodities (journalPostings j) case findundeclaredcomm p of Nothing -> Right () Just (comm, _) -> - Left $ printf "%s:%d:%d-%d:\n%sundeclared commodity \"%s\"\n" f l col col2 ex comm + Left $ printf "%s:%d:\n%sundeclared commodity \"%s\"\n" f l ex comm where - (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols - col = maybe 0 fst mcols - col2 = maybe 0 (fromMaybe 0 . snd) mcols + (f,l,_mcols,ex) = makePostingErrorExcerpt p finderrcols where -- Find the first undeclared commodity symbol in this posting's amount -- or balance assertion amount, if any. The boolean will be true if @@ -83,6 +82,10 @@ journalCheckCommodities j = mapM_ checkcommodities (journalPostings j) assertioncomms = [acommodity a | Just a <- [baamount <$> pbalanceassertion]] findundeclared = find (`M.notMember` jcommodities j) + -- Calculate columns suitable for highlighting the excerpt. + -- We won't show these in the main error line as they aren't + -- accurate for the actual data. + -- Find the best position for an error column marker when this posting -- is rendered by showTransaction. -- Reliably locating a problem commodity symbol in showTransaction output @@ -119,13 +122,14 @@ journalCheckPayees j = mapM_ checkpayee (jtxns j) checkpayee t | payee `elem` journalPayeesDeclared j = Right () | otherwise = Left $ - printf "%s:%d:%d-%d:\n%sundeclared payee \"%s\"\n" f l col col2 ex payee + printf "%s:%d:\n%sundeclared payee \"%s\"\n" f l ex payee where payee = transactionPayee t - (f,l,mcols,ex) = makeTransactionErrorExcerpt t finderrcols - col = maybe 0 fst mcols - col2 = maybe 0 (fromMaybe 0 . snd) mcols + (f,l,_mcols,ex) = makeTransactionErrorExcerpt t finderrcols + -- Calculate columns suitable for highlighting the excerpt. + -- We won't show these in the main error line as they aren't + -- accurate for the actual data. finderrcols t = Just (col, Just col2) where - col = T.length (showTransactionLineFirstPart t) + 2 + col = T.length (showTransactionLineFirstPart t) + 2 col2 = col + T.length (transactionPayee t) - 1 diff --git a/hledger-lib/Hledger/Data/JournalChecks/Ordereddates.hs b/hledger-lib/Hledger/Data/JournalChecks/Ordereddates.hs index 08da33954..86b0c71d8 100755 --- a/hledger-lib/Hledger/Data/JournalChecks/Ordereddates.hs +++ b/hledger-lib/Hledger/Data/JournalChecks/Ordereddates.hs @@ -6,7 +6,6 @@ where import Control.Monad (forM) import Data.List (groupBy) import Text.Printf (printf) -import Data.Maybe (fromMaybe) import Hledger.Data.Errors (makeTransactionErrorExcerpt) import Hledger.Data.Transaction (transactionFile, transactionDateOrDate2) @@ -26,12 +25,10 @@ journalCheckOrdereddates whichdate j = do FoldAcc{fa_previous=Nothing} -> Right () FoldAcc{fa_error=Nothing} -> Right () FoldAcc{fa_error=Just t, fa_previous=Just tprev} -> Left $ printf - "%s:%d:%d-%d:\n%stransaction date%s is out of order with previous transaction date %s" - f l col col2 ex datenum tprevdate + "%s:%d:\n%stransaction date%s is out of order with previous transaction date %s" + f l ex datenum tprevdate where - (f,l,mcols,ex) = makeTransactionErrorExcerpt t finderrcols - col = maybe 0 fst mcols - col2 = maybe 0 (fromMaybe 0 . snd) mcols + (f,l,_mcols,ex) = makeTransactionErrorExcerpt t finderrcols finderrcols _t = Just (1, Just 10) datenum = if whichdate==SecondaryDate then "2" else "" tprevdate = show $ getdate tprev diff --git a/hledger-lib/Hledger/Data/JournalChecks/Uniqueleafnames.hs b/hledger-lib/Hledger/Data/JournalChecks/Uniqueleafnames.hs index 545349654..2435c54f2 100755 --- a/hledger-lib/Hledger/Data/JournalChecks/Uniqueleafnames.hs +++ b/hledger-lib/Hledger/Data/JournalChecks/Uniqueleafnames.hs @@ -11,7 +11,6 @@ import Data.List (groupBy, sortBy) import Data.Text (Text) import qualified Data.Text as T import Text.Printf (printf) -import Data.Maybe (fromMaybe) import Hledger.Data.AccountName (accountLeafName) import Hledger.Data.Errors (makePostingErrorExcerpt) @@ -48,13 +47,11 @@ checkposting leafandfullnames p@Posting{paccount=a} = case [lf | lf@(_,fs) <- leafandfullnames, a `elem` fs] of [] -> Right () (leaf,fulls):_ -> Left $ printf - "%s:%d:%d-%d:\n%saccount leaf name \"%s\" is not unique\nit is used in account names: %s" - f l col col2 ex leaf accts + "%s:%d:\n%saccount leaf name \"%s\" is not unique\nit is used in account names: %s" + f l ex leaf accts where -- t = fromMaybe nulltransaction ptransaction -- XXX sloppy - col = maybe 0 fst mcols - col2 = maybe 0 (fromMaybe 0 . snd) mcols - (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols + (f,l,_mcols,ex) = makePostingErrorExcerpt p finderrcols where finderrcols p _ _ = Just (col, Just col2) where