Skip to content

Commit

Permalink
Fix code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenpi committed Jul 6, 2024
1 parent 7d779e3 commit 33639ba
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
24 changes: 10 additions & 14 deletions src/replblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,27 @@ See also: [`REPLResult`](@ref), [`join_to_string`](@ref).
blocks.
"""
function replblock!(
sandbox::Sandbox, code::AbstractString;
sandbox::Sandbox,
code::AbstractString;
color::Bool=true,
post_process_inputs = identity,
post_process_inputs=identity,
)
exprs = parseblock(
code;
keywords = false,
keywords=false,
# line unused, set to 0
linenumbernode = LineNumberNode(0, "REPL"),
linenumbernode=LineNumberNode(0, "REPL"),
)
codeblocks = CodeBlock[]
source_exprs = map(exprs) do pex
input = post_process_inputs(pex.code)
result = evaluate!(sandbox, pex.expr; color, softscope=true, setans = true)
result = evaluate!(sandbox, pex.expr; color, softscope=true, setans=true)
# Add the input and output to the codeblocks, if appropriate.
if !isempty(input)
push!(codeblocks, CodeBlock(true, _prepend_prompt(input)))
end
# Determine the output string and add to codeblocks
object_repl_repr = let buffer = IOContext(IOBuffer(), :color=>color)
object_repl_repr = let buffer = IOContext(IOBuffer(), :color => color)
if !result.error
hide = REPL.ends_with_semicolon(input)
_result_to_string(buffer, hide ? nothing : result.value)
Expand All @@ -101,12 +102,7 @@ function replblock!(
end
outstr = _remove_sandbox_from_output(sandbox, String(take!(out)))
push!(codeblocks, CodeBlock(false, outstr))
return (;
expr = pex,
result,
input,
outstr,
)
return (; expr=pex, result, input, outstr,)
end
return REPLResult(sandbox, codeblocks, code, source_exprs)
end
Expand All @@ -119,7 +115,7 @@ end
function _prepend_prompt(input::AbstractString)
prompt = "julia> "
padding = " "^length(prompt)
out = IOBuffer()
out = IOBuffer()
for (n, line) in enumerate(split(input, '\n'))
line = rstrip(line)
println(out, n == 1 ? prompt : padding, line)
Expand All @@ -144,7 +140,7 @@ function _error_to_string(buffer::IO, e::Any, bt)
bt = _remove_common_backtrace(bt, backtrace())
# Remove everything below the last eval call (which should be the one in IOCapture.capture)
index = findlast(ptr -> Base.ip_matches_func(ptr, :eval), bt)
bt = (index === nothing) ? bt : bt[1:(index - 1)]
bt = (index === nothing) ? bt : bt[1:(index-1)]
# Print a REPL-like error message.
print(buffer, "ERROR: ")
Base.invokelatest(showerror, buffer, e, bt)
Expand Down
14 changes: 5 additions & 9 deletions src/sandbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,17 @@ function evaluate!(
end
AnsValue(c.value)
end
return Result(
sandbox,
value,
c.output,
expr,
)
return Result(sandbox, value, c.output, expr,)
end

function _remove_common_backtrace(bt, reference_bt = backtrace())
function _remove_common_backtrace(bt, reference_bt=backtrace())
cutoff = nothing
# We'll start from the top of the backtrace (end of the array) and go down, checking
# if the backtraces agree
for ridx in 1:length(bt)
for ridx = 1:length(bt)
# Cancel search if we run out the reference BT or find a non-matching one frames:
if ridx > length(reference_bt) || bt[length(bt) - ridx + 1] != reference_bt[length(reference_bt) - ridx + 1]
if ridx > length(reference_bt) ||
bt[length(bt)-ridx+1] != reference_bt[length(reference_bt)-ridx+1]
cutoff = length(bt) - ridx + 1
break
end
Expand Down
27 changes: 18 additions & 9 deletions test/codeblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,30 @@ end
end

write(joinpath(path, "test.txt"), "123")
let r = CodeEvaluation.codeblock!(sb, """
isfile("test.txt"), read("test.txt", String)
""")
let r = CodeEvaluation.codeblock!(
sb,
"""
isfile("test.txt"), read("test.txt", String)
"""
)
@test !r.error
@test r.value === (true, "123")
end
let r = CodeEvaluation.codeblock!(sb, """
isfile("does-not-exist.txt")
""")
let r = CodeEvaluation.codeblock!(
sb,
"""
isfile("does-not-exist.txt")
"""
)
@test !r.error
@test r.value === false
end
let r = CodeEvaluation.codeblock!(sb, """
read("does-not-exist.txt", String)
""")
let r = CodeEvaluation.codeblock!(
sb,
"""
read("does-not-exist.txt", String)
"""
)
@test r.error
@test r.value isa SystemError
end
Expand Down
12 changes: 5 additions & 7 deletions test/parseblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
end

@testset "complex" begin
exprs = CodeEvaluation.parseblock(
"""
x += 3
γγγ_γγγ
γγγ
"""
)
exprs = CodeEvaluation.parseblock("""
x += 3
γγγ_γγγ
γγγ
""")
@test isa(exprs, Vector{CodeEvaluation.ParsedExpression})
@test length(exprs) == 3

Expand Down
22 changes: 14 additions & 8 deletions test/replblock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ end

@testset "replblock! - multiple expressions" begin
sb = CodeEvaluation.Sandbox()
r = CodeEvaluation.replblock!(sb, """
x = 2
x += 2
x ^ 2
""")
r = CodeEvaluation.replblock!(
sb,
"""
x = 2
x += 2
x ^ 2
"""
)
@test length(r.blocks) == 6
let b = r.blocks[1]
@test b.input
Expand Down Expand Up @@ -101,9 +104,12 @@ end

@testset "replblock! - output & results" begin
sb = CodeEvaluation.Sandbox()
r = CodeEvaluation.replblock!(sb, """
print(stdout, "out"); print(stderr, "err"); 42
""")
r = CodeEvaluation.replblock!(
sb,
"""
print(stdout, "out"); print(stderr, "err"); 42
"""
)
@test length(r.blocks) == 2
let b = r.blocks[1]
@test b.input
Expand Down

0 comments on commit 33639ba

Please sign in to comment.