From 9443fe0e0dc63512b8509435b579460ca6525891 Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Sat, 21 Jan 2023 22:18:38 +0100 Subject: [PATCH 1/3] journal: json: Add source positions to forecast transactions --- hledger-lib/Hledger/Data/PeriodicTransaction.hs | 4 +++- hledger-lib/Hledger/Data/Types.hs | 2 ++ hledger-lib/Hledger/Read/JournalReader.hs | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Data/PeriodicTransaction.hs b/hledger-lib/Hledger/Data/PeriodicTransaction.hs index a119bb1ee..93c6fbd72 100644 --- a/hledger-lib/Hledger/Data/PeriodicTransaction.hs +++ b/hledger-lib/Hledger/Data/PeriodicTransaction.hs @@ -61,6 +61,7 @@ instance Show PeriodicTransaction where ("ptperiodexpr=" ++ show ptperiodexpr) ("ptinterval=" ++ show ptinterval) ("ptspan=" ++ show (show ptspan)) + ("ptsourcepos=" ++ show ptsourcepos) ("ptstatus=" ++ show (show ptstatus)) ("ptcode=" ++ show ptcode) ("ptdescription=" ++ show ptdescription) @@ -236,7 +237,8 @@ runPeriodicTransaction PeriodicTransaction{..} requestedspan = [ t{tdate=d} | (DateSpan (Just d) _) <- alltxnspans, spanContainsDate requestedspan d ] where t = nulltransaction{ - tstatus = ptstatus + tsourcepos = ptsourcepos + ,tstatus = ptstatus ,tcode = ptcode ,tdescription = ptdescription ,tcomment = ptcomment diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index 2ec0dbef4..c7a512e17 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -455,6 +455,7 @@ data PeriodicTransaction = PeriodicTransaction { ptinterval :: Interval, -- ^ the interval at which this transaction recurs ptspan :: DateSpan, -- ^ the (possibly unbounded) period during which this transaction recurs. Contains a whole number of intervals. -- + ptsourcepos :: (SourcePos, SourcePos), -- ^ the file position where the period expression starts, and where the last posting ends ptstatus :: Status, -- ^ some of Transaction's fields ptcode :: Text, ptdescription :: Text, @@ -467,6 +468,7 @@ nullperiodictransaction = PeriodicTransaction{ ptperiodexpr = "" ,ptinterval = def ,ptspan = def + ,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 1) (mkPos 1)) ,ptstatus = Unmarked ,ptcode = "" ,ptdescription = "" diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index b55dc7178..fe3b03cd6 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -708,6 +708,7 @@ transactionmodifierp = do -- relative to Y/1/1. If not, they are calculated related to today as usual. periodictransactionp :: MonadIO m => JournalParser m PeriodicTransaction periodictransactionp = do + startpos <- getSourcePos -- first line char '~' "periodic transaction" @@ -752,10 +753,14 @@ periodictransactionp = do -- next lines; use same year determined above postings <- postingsp (Just $ first3 $ toGregorian refdate) + endpos <- getSourcePos + let sourcepos = (startpos, endpos) + return $ nullperiodictransaction{ ptperiodexpr=periodtxt ,ptinterval=interval ,ptspan=spn + ,ptsourcepos=sourcepos ,ptstatus=status ,ptcode=code ,ptdescription=description @@ -892,6 +897,7 @@ tests_JournalReader = testGroup "JournalReader" [ ptperiodexpr = "monthly from 2018/6" ,ptinterval = Months 1 ,ptspan = DateSpan (Just $ fromGregorian 2018 6 1) Nothing + ,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1)) ,ptdescription = "" ,ptcomment = "In 2019 we will change this\n" } @@ -902,6 +908,7 @@ tests_JournalReader = testGroup "JournalReader" [ ptperiodexpr = "monthly from 2018/6" ,ptinterval = Months 1 ,ptspan = DateSpan (Just $ fromGregorian 2018 6 1) Nothing + ,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1)) ,ptdescription = "In 2019 we will change this" ,ptcomment = "" } @@ -912,6 +919,7 @@ tests_JournalReader = testGroup "JournalReader" [ ptperiodexpr = "monthly" ,ptinterval = Months 1 ,ptspan = DateSpan Nothing Nothing + ,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1)) ,ptdescription = "Next year blah blah" ,ptcomment = "" } @@ -922,6 +930,7 @@ tests_JournalReader = testGroup "JournalReader" [ ptperiodexpr = "2019-01-04" ,ptinterval = NoInterval ,ptspan = DateSpan (Just $ fromGregorian 2019 1 4) (Just $ fromGregorian 2019 1 5) + ,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1)) ,ptdescription = "" ,ptcomment = "" } From 97f15eac03486385c42f9549e84dfbf08ce0fe58 Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Sat, 21 Jan 2023 23:24:43 +0100 Subject: [PATCH 2/3] Fix failing hledger-web test This test appeared to have failed after adding source positions to generated transactions. This is because the generated transaction id for web pages appears to be dependent on whether the transaction has an associated source file. Because it has one associated now, the id was updated to be 'transaction-2-1' for instance, instead of 'transaction-0-1' as it would have been before. The fix is to change the test. --- hledger-web/Hledger/Web/Test.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hledger-web/Hledger/Web/Test.hs b/hledger-web/Hledger/Web/Test.hs index 27e7cc67f..f99718d3f 100644 --- a/hledger-web/Hledger/Web/Test.hs +++ b/hledger-web/Hledger/Web/Test.hs @@ -97,5 +97,5 @@ hledgerWebTest = do yit "serves a journal page showing forecasted transactions" $ do get JournalR statusIs 200 - bodyContains "id=\"transaction-0-1\"" -- 0 indicates a fileless (forecasted) txn - bodyContains "id=\"transaction-0-2\"" -- etc. + bodyContains "id=\"transaction-2-1\"" + bodyContains "id=\"transaction-2-2\"" From b987c42e63a520b7850fa5d44485d67577adb6b9 Mon Sep 17 00:00:00 2001 From: Chris Lemaire Date: Sun, 22 Jan 2023 09:18:32 +0100 Subject: [PATCH 3/3] Add test checking forecast source links to periodic transaction --- hledger/test/forecast.test | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hledger/test/forecast.test b/hledger/test/forecast.test index 3307d070e..ccda92384 100644 --- a/hledger/test/forecast.test +++ b/hledger/test/forecast.test @@ -374,3 +374,21 @@ $ hledger -f- --today=2021-05-01 reg --period=2020-2022 --forecast=2020-2022 # 2021-07-01 (a) 1 2 # 2021-10-01 (a) 1 3 # >=0 + +# 22. Printed JSON with forecast transactions reference periodic transactions source position +< +~ next quarter Forecasted + A -1 + B 1 + +$ hledger -f- print --forecast -O json +> /.* + "sourceColumn": 1, + "sourceLine": 1, + "sourceName": "-" + }, + { + "sourceColumn": 1, + "sourceLine": 4, + "sourceName": "-" +.*/