From 7e45ab338f3b409c40738f862c578491a5e95d53 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 24 Apr 2022 18:29:01 -1000 Subject: [PATCH] imp: check: show column ranges properly (#1436) --- hledger-lib/Hledger/Read/Common.hs | 31 +++++++++++++++++----------- hledger/test/errors/accounts.test | 2 +- hledger/test/errors/commodities.test | 2 +- hledger/test/errors/payees.test | 2 +- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 9002d9b09..971eb8a84 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -366,14 +366,16 @@ journalCheckPayeesDeclared j = mapM_ checkpayee (jtxns j) checkpayee t | payee `elem` journalPayeesDeclared j = Right () | otherwise = Left $ - printf "%s:%d:%d:\n%sundeclared payee \"%s\"\n" f l (maybe 0 ((+1).fst) mcols) ex payee + printf "%s:%d:%d-%d:\n%sundeclared payee \"%s\"\n" f l col col2 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 finderrcols t = Just (col, Just col2) where - col = T.length (showTransactionLineFirstPart t) + 1 - col2 = col + T.length (transactionPayee t) + col = T.length (showTransactionLineFirstPart t) + 2 + col2 = col + T.length (transactionPayee t) - 1 -- | Check that all the journal's postings are to accounts declared with -- account directives, returning an error message otherwise. @@ -383,13 +385,15 @@ journalCheckAccountsDeclared j = mapM_ checkacct (journalPostings j) checkacct p@Posting{paccount=a} | a `elem` journalAccountNamesDeclared j = Right () | otherwise = Left $ - printf "%s:%d:%d:\n%sundeclared account \"%s\"\n" f l (maybe 0 ((+1).fst) mcols) ex a + printf "%s:%d:%d-%d:\n%sundeclared account \"%s\"\n" f l col col2 ex a where (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols + col = maybe 0 fst mcols + col2 = maybe 0 (fromMaybe 0 . snd) mcols finderrcols p _ _ = Just (col, Just col2) where - col = 4 + if isVirtual p then 1 else 0 - col2 = col + T.length a + col = 5 + if isVirtual p then 1 else 0 + col2 = col + T.length a - 1 -- | Check that all the commodities used in this journal's postings have been declared -- by commodity directives, returning an error message otherwise. @@ -399,9 +403,12 @@ journalCheckCommoditiesDeclared j = mapM_ checkcommodities (journalPostings j) checkcommodities p = case findundeclaredcomm p of Nothing -> Right () - Just (c, _) -> - Left $ printf "%s:%d:\n%sundeclared commodity \"%s\"\n" f l ex c - where (f,l,_,ex) = makePostingErrorExcerpt p finderrcols + Just (comm, _) -> + Left $ printf "%s:%d:%d-%d:\n%sundeclared commodity \"%s\"\n" f l col col2 ex comm + where + (f,l,mcols,ex) = makePostingErrorExcerpt p finderrcols + col = maybe 0 fst mcols + col2 = maybe 0 (fromMaybe 0 . snd) mcols where -- Find the first undeclared commodity symbol in this posting's amount -- or balance assertion amount, if any. The boolean will be true if @@ -444,7 +451,7 @@ journalCheckCommoditiesDeclared j = mapM_ checkcommodities (journalPostings j) errrelline = 1 + tcommentlines + pindex -- XXX doesn't count posting coment lines errline = fromMaybe "" (T.lines txntxt `atMay` (errrelline-1)) acctend = 4 + T.length (paccount p) + if isVirtual p then 2 else 0 - amtstart = acctend + (T.length $ T.takeWhile isSpace $ T.drop acctend errline) + amtstart = acctend + (T.length $ T.takeWhile isSpace $ T.drop acctend errline) + 1 amtend = amtstart + (T.length $ T.stripEnd $ T.takeWhile (/=';') $ T.drop amtstart errline) -- | Given a problem transaction and a function calculating the best @@ -512,9 +519,9 @@ decoratePostingErrorExcerpt absline relline mcols txt = _ -> ([], []) ms' = map (lineprefix<>) ms colmarkerline = - [lineprefix <> T.replicate col " " <> T.replicate regionw "^" + [lineprefix <> T.replicate (col-1) " " <> T.replicate regionw "^" | Just (col, mendcol) <- [mcols] - , let regionw = maybe 1 (subtract col) mendcol + , let regionw = 1 + maybe 0 (subtract col) mendcol ] lineprefix = T.replicate marginw " " <> "| " where marginw = length (show absline) + 1 diff --git a/hledger/test/errors/accounts.test b/hledger/test/errors/accounts.test index 21eba1453..e082d78c0 100644 --- a/hledger/test/errors/accounts.test +++ b/hledger/test/errors/accounts.test @@ -1,6 +1,6 @@ $$$ hledger check accounts -f accounts.j >>>2 -hledger: Error: /Users/simon/src/hledger/hledger/test/errors/accounts.j:4:6: +hledger: Error: /Users/simon/src/hledger/hledger/test/errors/accounts.j:4:6-6: | 2022-01-01 4 | (a) 1 | ^ diff --git a/hledger/test/errors/commodities.test b/hledger/test/errors/commodities.test index 01b44fc8f..508ff9a53 100644 --- a/hledger/test/errors/commodities.test +++ b/hledger/test/errors/commodities.test @@ -1,6 +1,6 @@ $$$ hledger check commodities -f commodities.j >>>2 -hledger: Error: /Users/simon/src/hledger/hledger/test/errors/commodities.j:6: +hledger: Error: /Users/simon/src/hledger/hledger/test/errors/commodities.j:6:21-23: | 2022-01-01 6 | (a) A 1 | ^^^ diff --git a/hledger/test/errors/payees.test b/hledger/test/errors/payees.test index c4ba30092..0904cf655 100644 --- a/hledger/test/errors/payees.test +++ b/hledger/test/errors/payees.test @@ -1,6 +1,6 @@ $$$ hledger check payees -f payees.j >>>2 -hledger: Error: /Users/simon/src/hledger/hledger/test/errors/payees.j:6:12: +hledger: Error: /Users/simon/src/hledger/hledger/test/errors/payees.j:6:12-12: 6 | 2022-01-01 p | ^ | (a) A 1