imp:journal: include directive now allows a same-line comment

This commit is contained in:
Simon Michael 2025-04-27 08:27:48 -10:00
parent dbf54facc2
commit 2371f677e5
3 changed files with 19 additions and 9 deletions

View File

@ -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 #-}

View File

@ -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

View File

@ -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