Skip to content

Commit

Permalink
Allow doctests to span multiple lines with a trailing \ like the REPL…
Browse files Browse the repository at this point in the history
… already supports (#1757)

* Allow doctests to span multiple lines with a trailing \ like the REPL already supports
* Empty doctest lines to be skipped without error
  • Loading branch information
glguy authored Sep 27, 2024
1 parent 5089ad3 commit 8cd51e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/Cryptol/REPL/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,10 +2062,12 @@ data SubcommandResult = SubcommandResult
checkBlock ::
[T.Text] {- ^ lines of the code block -} ->
REPL [SubcommandResult]
checkBlock = isolated . go
checkBlock = isolated . go . continuedLines
where
go [] = pure []
go (line:block) =
go (line:block)
| T.all isSpace line = go block
| otherwise =
case parseCommand (findNbCommand True) (T.unpack line) of
Nothing -> do
pure [SubcommandResult
Expand Down Expand Up @@ -2104,6 +2106,19 @@ checkBlock = isolated . go
subresults <- checkBlock block
pure (subresult : subresults)

-- | Combine lines ending in a backslash with the next line.
continuedLines :: [T.Text] -> [T.Text]
continuedLines xs =
case span ("\\" `T.isSuffixOf`) xs of
([], []) -> []
(a, []) -> [concat' (map T.init a)] -- permissive
(a, b:bs) -> concat' (map T.init a ++ [b]) : continuedLines bs
where
-- concat that eats leading whitespace between elements
concat' [] = T.empty
concat' (y:ys) = T.concat (y : map (T.dropWhile isSpace) ys)


captureLog :: REPL a -> REPL (String, a)
captureLog m = do
previousLogger <- getLogger
Expand Down
4 changes: 3 additions & 1 deletion tests/docstrings/T02.cry
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ submodule F7 = submodule F where
/**
** ```repl
** let y = 20
** :check f 10 == 20
**
** :check f 10 \
** == 20
** ``` */
f : Integer -> Integer
f x = x + x

0 comments on commit 8cd51e8

Please sign in to comment.