diff --git a/hledger-lib/Hledger/Data/Balancing.hs b/hledger-lib/Hledger/Data/Balancing.hs index 3cdc4d686..dc8e045ff 100644 --- a/hledger-lib/Hledger/Data/Balancing.hs +++ b/hledger-lib/Hledger/Data/Balancing.hs @@ -169,7 +169,7 @@ transactionBalanceError t errs = annotateErrorWithTransaction :: Transaction -> String -> String annotateErrorWithTransaction t s = - unlines [ showSourcePosPair $ tsourcepos t, s + unlines [ sourcePosPairPretty $ tsourcepos t, s , T.unpack . T.stripEnd $ showTransaction t ] @@ -586,7 +586,7 @@ checkBalanceAssertionOneCommodityB p@Posting{paccount=assertedacct} assertedamt (case ptransaction p of Nothing -> "?" -- shouldn't happen Just t -> printf "%s\ntransaction:\n%s" - (showSourcePos pos) + (sourcePosPretty pos) (textChomp $ showTransaction t) :: String where diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 30355e127..f32061d7e 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -374,7 +374,7 @@ journalCheckPayeesDeclared j = mapM_ checkpayee (jtxns j) | otherwise = Left $ printf "undeclared payee \"%s\"\nat: %s\n\n%s" (T.unpack p) - (showSourcePosPair $ tsourcepos t) + (sourcePosPairPretty $ tsourcepos t) (linesPrepend2 "> " " " . (<>"\n") . textChomp $ showTransaction t) where p = transactionPayee t @@ -392,7 +392,7 @@ journalCheckAccountsDeclared j = mapM_ checkacct (journalPostings j) ++ case ptransaction of Nothing -> "" Just t -> printf "in transaction at: %s\n\n%s" - (showSourcePosPair $ tsourcepos t) + (sourcePosPairPretty $ tsourcepos t) (linesPrepend " " . (<>"\n") . textChomp $ showTransaction t) where as = journalAccountNamesDeclared j @@ -411,7 +411,7 @@ journalCheckCommoditiesDeclared j = ++ case ptransaction of Nothing -> "" Just t -> printf "in transaction at: %s\n\n%s" - (showSourcePosPair $ tsourcepos t) + (sourcePosPairPretty $ tsourcepos t) (linesPrepend " " . (<>"\n") . textChomp $ showTransaction t) where mfirstundeclaredcomm = diff --git a/hledger-lib/Hledger/Utils/Parse.hs b/hledger-lib/Hledger/Utils/Parse.hs index 230c5d957..e242ed70d 100644 --- a/hledger-lib/Hledger/Utils/Parse.hs +++ b/hledger-lib/Hledger/Utils/Parse.hs @@ -6,14 +6,13 @@ module Hledger.Utils.Parse ( SimpleTextParser, TextParser, + -- * SourcePos SourcePos(..), mkPos, unPos, initialPos, - - -- * SourcePos - showSourcePosPair, - showSourcePos, + sourcePosPretty, + sourcePosPairPretty, choice', choiceInState, @@ -63,16 +62,12 @@ type SimpleTextParser = Parsec CustomErr Text -- XXX an "a" argument breaks the -- | A parser of text that runs in some monad. type TextParser m a = ParsecT CustomErr Text m a --- | Render source position in human-readable form. -showSourcePos :: SourcePos -> String -showSourcePos (SourcePos fp l c) = - show fp ++ " (line " ++ show (unPos l) ++ ", column " ++ show (unPos c) ++ ")" - --- | Render a pair of source position in human-readable form. -showSourcePosPair :: (SourcePos, SourcePos) -> String -showSourcePosPair (SourcePos fp l1 _, SourcePos _ l2 c2) = - show fp ++ " (lines " ++ show (unPos l1) ++ "-" ++ show l2' ++ ")" - where l2' = if unPos c2 == 1 then unPos l2 - 1 else unPos l2 -- might be at end of file withat last new-line +-- | Render a pair of source positions in human-readable form, only displaying the range of lines. +sourcePosPairPretty :: (SourcePos, SourcePos) -> String +sourcePosPairPretty (SourcePos fp l1 _, SourcePos _ l2 c2) = + fp ++ ":" ++ show (unPos l1) ++ "-" ++ show l2' + where + l2' = if unPos c2 == 1 then unPos l2 - 1 else unPos l2 -- might be at end of file with a final new line -- | Backtracking choice, use this when alternatives share a prefix. -- Consumes no input if all choices fail. diff --git a/hledger/Hledger/Cli/Commands/Check/Ordereddates.hs b/hledger/Hledger/Cli/Commands/Check/Ordereddates.hs index 11e4677de..117fb4780 100755 --- a/hledger/Hledger/Cli/Commands/Check/Ordereddates.hs +++ b/hledger/Hledger/Cli/Commands/Check/Ordereddates.hs @@ -30,7 +30,7 @@ journalCheckOrdereddates CliOpts{reportspec_=rspec} j = do let datestr = if date2_ ropts then "2" else "" uniquestr = if checkunique then " and/or not unique" else "" - positionstr = showSourcePosPair $ tsourcepos error + positionstr = sourcePosPairPretty $ tsourcepos error txn1str = T.unpack . linesPrepend (T.pack " ") $ showTransaction previous txn2str = T.unpack . linesPrepend2 (T.pack "> ") (T.pack " ") $ showTransaction error Left $ diff --git a/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs b/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs index 523244717..8e66a3641 100755 --- a/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs +++ b/hledger/Hledger/Cli/Commands/Check/Uniqueleafnames.hs @@ -49,5 +49,5 @@ checkposting leafandfullnames Posting{paccount,ptransaction} = Nothing -> "" Just t -> printf "\nseen in \"%s\" in transaction at: %s\n\n%s" paccount - (showSourcePosPair $ tsourcepos t) + (sourcePosPairPretty $ tsourcepos t) (linesPrepend "> " . (<>"\n") . textChomp $ showTransaction t) :: String) diff --git a/hledger/test/journal/balance-assertions.test b/hledger/test/journal/balance-assertions.test index 9c01e56f1..1db0dfbd9 100755 --- a/hledger/test/journal/balance-assertions.test +++ b/hledger/test/journal/balance-assertions.test @@ -56,7 +56,7 @@ $ hledger -f - stats b $-1 = $-3 $ hledger -f - stats ->2 /balance assertion.*line 11, column 12/ +>2 /balance assertion.*11:12/ >=1 # 4. should also work without commodity symbols @@ -314,7 +314,7 @@ $ hledger -f - stats a 0 == $1 $ hledger -f - stats ->2 /balance assertion.*line 10, column 15/ +>2 /balance assertion.*10:15/ >=1 # 18. Mix different commodities and total assignments diff --git a/hledger/test/journal/parse-errors.test b/hledger/test/journal/parse-errors.test index 52c2a680a..4d0b93df5 100644 --- a/hledger/test/journal/parse-errors.test +++ b/hledger/test/journal/parse-errors.test @@ -68,7 +68,7 @@ $ hledger -f journal:- print [c] $ hledger -f journal:- print ->2 /lines 1-4/ +>2 /:1-4/ >=1 # 7. Balancing error messages show the recorded precision, not the display precision. @@ -123,7 +123,7 @@ $ hledger -f- print b 1B $ hledger -f- print >2 -hledger: "-" (lines 1-3) +hledger: -:1-3 could not balance this transaction: real postings all have the same sign 2020-01-01