Merge pull request #1988 from chrislemaire/1984-source-name-and-position-for-forecast

journal: json: Add source positions to forecast transactions
This commit is contained in:
Simon Michael 2023-01-23 16:27:36 -10:00 committed by GitHub
commit ce87a89ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 3 deletions

View File

@ -61,6 +61,7 @@ instance Show PeriodicTransaction where
("ptperiodexpr=" ++ show ptperiodexpr) ("ptperiodexpr=" ++ show ptperiodexpr)
("ptinterval=" ++ show ptinterval) ("ptinterval=" ++ show ptinterval)
("ptspan=" ++ show (show ptspan)) ("ptspan=" ++ show (show ptspan))
("ptsourcepos=" ++ show ptsourcepos)
("ptstatus=" ++ show (show ptstatus)) ("ptstatus=" ++ show (show ptstatus))
("ptcode=" ++ show ptcode) ("ptcode=" ++ show ptcode)
("ptdescription=" ++ show ptdescription) ("ptdescription=" ++ show ptdescription)
@ -236,7 +237,8 @@ runPeriodicTransaction PeriodicTransaction{..} requestedspan =
[ t{tdate=d} | (DateSpan (Just d) _) <- alltxnspans, spanContainsDate requestedspan d ] [ t{tdate=d} | (DateSpan (Just d) _) <- alltxnspans, spanContainsDate requestedspan d ]
where where
t = nulltransaction{ t = nulltransaction{
tstatus = ptstatus tsourcepos = ptsourcepos
,tstatus = ptstatus
,tcode = ptcode ,tcode = ptcode
,tdescription = ptdescription ,tdescription = ptdescription
,tcomment = ptcomment ,tcomment = ptcomment

View File

@ -455,6 +455,7 @@ data PeriodicTransaction = PeriodicTransaction {
ptinterval :: Interval, -- ^ the interval at which this transaction recurs 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. 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 ptstatus :: Status, -- ^ some of Transaction's fields
ptcode :: Text, ptcode :: Text,
ptdescription :: Text, ptdescription :: Text,
@ -467,6 +468,7 @@ nullperiodictransaction = PeriodicTransaction{
ptperiodexpr = "" ptperiodexpr = ""
,ptinterval = def ,ptinterval = def
,ptspan = def ,ptspan = def
,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 1) (mkPos 1))
,ptstatus = Unmarked ,ptstatus = Unmarked
,ptcode = "" ,ptcode = ""
,ptdescription = "" ,ptdescription = ""

View File

@ -708,6 +708,7 @@ transactionmodifierp = do
-- relative to Y/1/1. If not, they are calculated related to today as usual. -- relative to Y/1/1. If not, they are calculated related to today as usual.
periodictransactionp :: MonadIO m => JournalParser m PeriodicTransaction periodictransactionp :: MonadIO m => JournalParser m PeriodicTransaction
periodictransactionp = do periodictransactionp = do
startpos <- getSourcePos
-- first line -- first line
char '~' <?> "periodic transaction" char '~' <?> "periodic transaction"
@ -752,10 +753,14 @@ periodictransactionp = do
-- next lines; use same year determined above -- next lines; use same year determined above
postings <- postingsp (Just $ first3 $ toGregorian refdate) postings <- postingsp (Just $ first3 $ toGregorian refdate)
endpos <- getSourcePos
let sourcepos = (startpos, endpos)
return $ nullperiodictransaction{ return $ nullperiodictransaction{
ptperiodexpr=periodtxt ptperiodexpr=periodtxt
,ptinterval=interval ,ptinterval=interval
,ptspan=spn ,ptspan=spn
,ptsourcepos=sourcepos
,ptstatus=status ,ptstatus=status
,ptcode=code ,ptcode=code
,ptdescription=description ,ptdescription=description
@ -892,6 +897,7 @@ tests_JournalReader = testGroup "JournalReader" [
ptperiodexpr = "monthly from 2018/6" ptperiodexpr = "monthly from 2018/6"
,ptinterval = Months 1 ,ptinterval = Months 1
,ptspan = DateSpan (Just $ fromGregorian 2018 6 1) Nothing ,ptspan = DateSpan (Just $ fromGregorian 2018 6 1) Nothing
,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1))
,ptdescription = "" ,ptdescription = ""
,ptcomment = "In 2019 we will change this\n" ,ptcomment = "In 2019 we will change this\n"
} }
@ -902,6 +908,7 @@ tests_JournalReader = testGroup "JournalReader" [
ptperiodexpr = "monthly from 2018/6" ptperiodexpr = "monthly from 2018/6"
,ptinterval = Months 1 ,ptinterval = Months 1
,ptspan = DateSpan (Just $ fromGregorian 2018 6 1) Nothing ,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" ,ptdescription = "In 2019 we will change this"
,ptcomment = "" ,ptcomment = ""
} }
@ -912,6 +919,7 @@ tests_JournalReader = testGroup "JournalReader" [
ptperiodexpr = "monthly" ptperiodexpr = "monthly"
,ptinterval = Months 1 ,ptinterval = Months 1
,ptspan = DateSpan Nothing Nothing ,ptspan = DateSpan Nothing Nothing
,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1))
,ptdescription = "Next year blah blah" ,ptdescription = "Next year blah blah"
,ptcomment = "" ,ptcomment = ""
} }
@ -922,6 +930,7 @@ tests_JournalReader = testGroup "JournalReader" [
ptperiodexpr = "2019-01-04" ptperiodexpr = "2019-01-04"
,ptinterval = NoInterval ,ptinterval = NoInterval
,ptspan = DateSpan (Just $ fromGregorian 2019 1 4) (Just $ fromGregorian 2019 1 5) ,ptspan = DateSpan (Just $ fromGregorian 2019 1 4) (Just $ fromGregorian 2019 1 5)
,ptsourcepos = (SourcePos "" (mkPos 1) (mkPos 1), SourcePos "" (mkPos 2) (mkPos 1))
,ptdescription = "" ,ptdescription = ""
,ptcomment = "" ,ptcomment = ""
} }

View File

@ -97,5 +97,5 @@ hledgerWebTest = do
yit "serves a journal page showing forecasted transactions" $ do yit "serves a journal page showing forecasted transactions" $ do
get JournalR get JournalR
statusIs 200 statusIs 200
bodyContains "id=\"transaction-0-1\"" -- 0 indicates a fileless (forecasted) txn bodyContains "id=\"transaction-2-1\""
bodyContains "id=\"transaction-0-2\"" -- etc. bodyContains "id=\"transaction-2-2\""

View File

@ -374,3 +374,21 @@ $ hledger -f- --today=2021-05-01 reg --period=2020-2022 --forecast=2020-2022
# 2021-07-01 (a) 1 2 # 2021-07-01 (a) 1 2
# 2021-10-01 (a) 1 3 # 2021-10-01 (a) 1 3
# >=0 # >=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": "-"
.*/