-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathwatch.jl
48 lines (45 loc) · 1.77 KB
/
watch.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function add_watch_entry!(state::DebuggerState, cmd::AbstractString)
expr = Base.parse_input_line(cmd)
if isexpr(expr, :error) || isexpr(expr, :incomplete)
printstyled(stderr, "ERROR: syntax: ", expr.args[1], "\n", color=Base.error_color())
return false
end
isexpr(expr, :toplevel) && (expr = expr.args[end])
push!(state.watch_list, expr)
return true
end
function show_watch_list(io, state::DebuggerState)
frame = active_frame(state)
outbuf = IOContext(IOBuffer(), io)
for (i, expr) in enumerate(state.watch_list)
vars = filter(v -> v.name != Symbol(""), JuliaInterpreter.locals(frame))
eval_expr = Expr(:let,
Expr(:block, map(x->Expr(:(=), x...), [(v.name, maybe_quote(v.value)) for v in vars])...,
Expr(:(=), :__FRAME__ , frame)),
expr)
errored = false
res = try
Core.eval(moduleof(frame), eval_expr)
catch err
errored = true
err
end
expr_str = highlight_code(string(expr); context=io) # Should maybe use repr here in some cases (strings)
if errored
res_str = sprint((io, args...) -> printstyled(io, args...; color=Base.error_color()), res; context=io)
else
res_str = highlight_code(repr(res), context=io)
end
print(outbuf, "$i] $(expr_str): $(res_str)\n")
end
print(io, String(take!(outbuf.io)))
println(io)
end
clear_watch_list!(state::DebuggerState) = empty!(state.watch_list)
function clear_watch_list!(state::DebuggerState, i::Int)
if !checkbounds(Bool, state.watch_list, i)
printstyled(stderr, "ERROR: watch entry $i does not exist\n\n"; color=Base.error_color())
return
end
deleteat!(state.watch_list, i)
end