From 2371f677e59aebb703fae77b95b6369d363933bb Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 27 Apr 2025 08:27:48 -1000 Subject: [PATCH] imp:journal: include directive now allows a same-line comment --- hledger-lib/Hledger/Read/Common.hs | 3 +-- hledger-lib/Hledger/Read/JournalReader.hs | 8 ++++++-- hledger/test/journal/include.test | 17 ++++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/hledger-lib/Hledger/Read/Common.hs b/hledger-lib/Hledger/Read/Common.hs index 3035be48c..0c498cab9 100644 --- a/hledger-lib/Hledger/Read/Common.hs +++ b/hledger-lib/Hledger/Read/Common.hs @@ -1340,8 +1340,7 @@ isSameLineCommentStart _ = False -- Right "\n\n" -- followingcommentp :: TextParser m Text -followingcommentp = - fst <$> followingcommentpWith (void $ takeWhileP Nothing (/= '\n')) -- XXX support \r\n ? +followingcommentp = fst <$> followingcommentpWith (void $ takeWhileP Nothing (/= '\n')) {-# INLINABLE followingcommentp #-} diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index c848136eb..64315283c 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -289,18 +289,22 @@ directivep = (do -- Examples: foo.j, ../foo/bar.j, timedot:/foo/2020*, *.journal includedirectivep :: MonadIO m => ErroringJournalParser m () includedirectivep = do + -- parse string "include" lift skipNonNewlineSpaces1 - prefixedglob <- rstrip . T.unpack <$> takeWhileP Nothing (/= '\n') -- don't consume newline yet + prefixedglob <- rstrip . T.unpack <$> takeWhileP Nothing (`notElem` [';','\n']) + lift followingcommentp + -- save the position (does sequencing wrt newline matter ? seems not) parentoff <- getOffset parentpos <- getSourcePos + -- find file(s) let (mprefix,glb) = splitReaderPrefix prefixedglob paths <- getFilePaths parentoff parentpos glb let prefixedpaths = case mprefix of Nothing -> paths Just fmt -> map ((show fmt++":")++) paths + -- parse them inline forM_ prefixedpaths $ parseChild parentpos - void newline where getFilePaths diff --git a/hledger/test/journal/include.test b/hledger/test/journal/include.test index 65bd490d1..134673164 100644 --- a/hledger/test/journal/include.test +++ b/hledger/test/journal/include.test @@ -61,12 +61,19 @@ $ hledger -f - print >2 // >= 1 -# ** 6. trailing whitespace after the filename does not make a file nonexistent +# ** 6. trailing whitespace after the filename is ignored < include a.timeclock $ hledger -f - check -# ** 7. include relative to home +# ** 7. a same-line or multi-line following comment is ignored +< +include a.timeclock ; comment + ; comment + ; comment +$ hledger -f - check + +# ** 8. include relative to home < include ~/included.journal $ printf '2018/01/01\n (A) 1\n' >included.journal; HOME="$PWD" hledger -f - print; rm -rf included.journal @@ -78,17 +85,17 @@ $ printf '2018/01/01\n (A) 1\n' >included.journal; HOME="$PWD" hledger -f - pr # The next tests require hard coded file names, so are not concurrent-safe. # They use different file names so a single concurrent shelltest invocation will be fine. -# ** 8. test that order of include files is maintained +# ** 9. test that order of include files is maintained $ printf 'include _b\n' >_a; touch _b; hledger -f _a stats -v | grep _ | sed -e 's%.*/%%'; rm -rf _a _b _a _b -# ** 9. and with --auto code path +# ** 10. and with --auto code path $ printf 'include _d\n=\n' >_c; touch _d; hledger -f _c stats -v --auto | grep _ | sed -e 's%.*/%%'; rm -rf _c _d _c _d -# ** 10. include using old !include directive +# ** 11. include using old !include directive < !include f.journal $ printf '2018/01/01\n (A) 1\n' >f.journal; hledger -f - print; rm -f f.journal