From 4db2bddf58bc586193a3671fc4a9f1d49dd7cbe9 Mon Sep 17 00:00:00 2001 From: MichaelHatherly Date: Tue, 15 Oct 2024 10:47:29 +0100 Subject: [PATCH] Fixup pkg mode --- src/QuartoNotebookWorker/src/render.jl | 47 +++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/QuartoNotebookWorker/src/render.jl b/src/QuartoNotebookWorker/src/render.jl index 5f1a1c7..6912544 100644 --- a/src/QuartoNotebookWorker/src/render.jl +++ b/src/QuartoNotebookWorker/src/render.jl @@ -122,9 +122,20 @@ end # Setting the `helpmode` module isn't an option on Julia 1.6 so we need to # manually replace `Main` with the module we want. function _helpmode(code::AbstractString, mod::Module) - ex = REPL.helpmode(code) - return postwalk(ex) do x - return x == Main ? mod : x + @static if VERSION < v"1.11.0" + ex = REPL.helpmode(code) + ex = postwalk(ex) do x + return x == Main ? mod : x + end + # helpmode embeds object references to `stdout` into the expression, but + # since we are capturing the output it refers to a different stream. We + # need to replace the first `stdout` reference with `:stdout` and remove + # the argument from the other call so that it uses the redirected one. + ex.args[2] = :stdout + deleteat!(ex.args[end].args, 3) + return ex + else + return :(Core.eval($(mod), $(REPL).helpmode(stdout, $(code), $(mod)))) end end @@ -138,15 +149,6 @@ function _process_code( if startswith(code, help_regex) code = String(chomp(replace(code, help_regex => ""; count = 1))) ex = _helpmode(code, mod) - - # helpmode embeds object references to `stdout` into the - # expression, but since we are capturing the output it refers to - # a different stream. We need to replace the first `stdout` - # reference with `:stdout` and remove the argument from the - # other call so that it uses the redirected one. - ex.args[2] = :stdout - deleteat!(ex.args[end].args, 3) - return Expr(:toplevel, ex) end @@ -166,19 +168,16 @@ function _process_code( pkg_regex = r"^\s*\]" if startswith(code, pkg_regex) code = String(chomp(replace(code, pkg_regex => ""; count = 1))) - return Expr( - :toplevel, - :( - let printed = $(Pkg).REPLMode.PRINTED_REPL_WARNING[] - $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = true - try - $(Pkg).REPLMode.do_cmd($(Pkg).REPLMode.MiniREPL(), $code) - finally - $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = printed - end + return Expr(:toplevel, :( + let printed = $(Pkg).REPLMode.PRINTED_REPL_WARNING[] + $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = true + try + $(Pkg).REPLMode.@pkg_str $code + finally + $(Pkg).REPLMode.PRINTED_REPL_WARNING[] = printed end - ), - ) + end + )) end return _parseall(code; filename, lineno)