-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrepl.jl
240 lines (217 loc) · 8.62 KB
/
repl.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
promptname(level, name) = "$level|$name> "
function RunDebugger(frame, repl = nothing, terminal = nothing; initial_continue=false)
if repl === nothing
if !isdefined(Base, :active_repl)
error("Debugger.jl needs to be run in a Julia REPL")
end
repl = Base.active_repl
end
if !isa(repl, REPL.LineEditREPL)
error("Debugger.jl requires a LineEditREPL type of REPL")
end
if terminal === nothing
terminal = Base.active_repl.t
end
state = DebuggerState(; frame=frame, repl=repl, terminal=terminal)
# Setup debug panel
normal_prefix = Sys.iswindows() ? "\e[33m" : "\e[38;5;166m"
compiled_prefix = "\e[96m"
panel = LineEdit.Prompt(promptname(state.level, "debug");
prompt_prefix = () -> state.mode == Compiled() ? compiled_prefix : normal_prefix,
prompt_suffix = Base.text_colors[:normal],
on_enter = s->true)
panel.hist = REPL.REPLHistoryProvider(Dict{Symbol,Any}(:debug => panel))
REPL.history_reset_state(panel.hist)
search_prompt, skeymap = LineEdit.setup_search_keymap(panel.hist)
search_prompt.complete = REPL.LatexCompletions()
state.main_mode = panel
panel.on_done = (s,buf,ok)->begin
line = String(take!(buf))
old_level = state.level
if !ok || strip(line) == "q"
LineEdit.transition(s, :abort)
LineEdit.reset_state(s)
return false
end
if length(panel.hist.history) == 0
printstyled(stderr, "no previous command executed\n"; color=Base.error_color())
return false
end
if isempty(strip(line))
command = panel.hist.history[end]
else
command = strip(line)
end
do_print_status = true
cmd1 = split(command,' ')[1]
do_print_status = try
execute_command(state, Val{Symbol(cmd1)}(), command)
catch err
# This will only show the stacktrae up to the current frame because
# currently, the unwinding in JuliaInterpreter unlinks the frames to
# where the error is thrown
# Buffer error printing
io = IOContext(IOBuffer(), Base.pipe_writer(terminal))
Base.display_error(io, err, JuliaInterpreter.leaf(state.frame))
print(Base.pipe_writer(terminal), String(take!(io.io)))
# Comment below out if you are debugging the Debugger
# Base.display_error(Base.pipe_writer(terminal), err, catch_backtrace())
LineEdit.transition(s, :abort)
LineEdit.reset_state(s)
return false
end
if old_level != state.level
panel.prompt = promptname(state.level, "debug")
end
LineEdit.reset_state(s)
if state.frame === nothing
LineEdit.transition(s, :abort)
LineEdit.reset_state(s)
return false
end
if do_print_status
print_status(Base.pipe_writer(terminal), active_frame(state); force_lowered = state.lowered_status)
end
return true
end
key = '`'
repl_switch = Dict{Any,Any}(
key => function (s,args...)
if isempty(s) || position(LineEdit.buffer(s)) == 0
prompt = julia_prompt(state)
buf = copy(LineEdit.buffer(s))
LineEdit.transition(s, prompt) do
LineEdit.state(s, prompt).input_buffer = buf
end
else
LineEdit.edit_insert(s,key)
end
end,
"C" => function (s, args...)
if isempty(s) || position(LineEdit.buffer(s)) == 0
toggle_mode(state)
write(state.terminal, '\r')
LineEdit.write_prompt(state.terminal, panel)
else
LineEdit.edit_insert(s,key)
end
end,
"L" => function (s, args...)
if isempty(s) || position(LineEdit.buffer(s)) == 0
toggle_lowered(state)
println(Base.pipe_writer(terminal))
print_status(Base.pipe_writer(terminal), active_frame(state); force_lowered=state.lowered_status)
LineEdit.write_prompt(state.terminal, panel)
else
LineEdit.edit_insert(s,key)
end
end
)
state.standard_keymap = Dict{Any,Any}[skeymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
panel.keymap_dict = LineEdit.keymap([repl_switch;state.standard_keymap])
if initial_continue
try
execute_command(state, Val(:c), "c")
catch err
# Buffer error printing
io = IOContext(IOBuffer(), Base.pipe_writer(terminal))
Base.display_error(io, err, JuliaInterpreter.leaf(state.frame))
print(Base.pipe_writer(terminal), String(take!(io.io)))
return
end
state.frame === nothing && return state.overall_result
end
print_status(Base.pipe_writer(terminal), active_frame(state); force_lowered=state.lowered_status)
REPL.run_interface(terminal, LineEdit.ModalInterface([panel,search_prompt]))
return state.overall_result
end
function julia_prompt(state::DebuggerState)
# Return early if this has already been called on the state
isassigned(state.julia_prompt) && return state.julia_prompt[]
julia_prompt = LineEdit.Prompt(() -> promptname(state.level, "julia");
# Copy colors from the prompt object
prompt_prefix = state.repl.prompt_color,
prompt_suffix = (state.repl.envcolors ? Base.input_color : state.repl.input_color),
complete = DebugCompletionProvider(state),
on_enter = REPL.return_callback)
julia_prompt.hist = state.main_mode.hist
julia_prompt.hist.mode_mapping[:julia] = julia_prompt
julia_prompt.on_done = (s,buf,ok)->begin
if !ok
LineEdit.transition(s, :abort)
return false
end
xbuf = copy(buf)
command = String(take!(buf))
@static if VERSION >= v"1.2.0-DEV.253"
response = _eval_code(active_frame(state), command)
REPL.print_response(state.repl, response, true, true)
else
ok, result = _eval_code(active_frame(state), command)
REPL.print_response(state.repl, ok ? result : result[1], ok ? nothing : result[2], true, true)
end
println(state.terminal)
LineEdit.reset_state(s)
end
julia_prompt.keymap_dict = LineEdit.keymap([REPL.mode_keymap(state.main_mode); state.standard_keymap])
state.julia_prompt[] = julia_prompt
return julia_prompt
end
@static if VERSION >= v"1.2.0-DEV.253"
function _eval_code(frame::Frame, code::AbstractString)
try
return eval_code(frame, code), false
catch
return Base.catch_stack(), true
end
end
else
function _eval_code(frame::Frame, code::AbstractString)
try
return true, eval_code(frame, code)
catch err
return false, (err, catch_backtrace())
end
end
end
function eval_code(frame::Frame, command::AbstractString)
expr = Base.parse_input_line(command)
isexpr(expr, :toplevel) && (expr = expr.args[end])
# see https://github.com/JuliaLang/julia/issues/31255 for the Symbol("") check
vars = filter(v -> v.name != Symbol(""), JuliaInterpreter.locals(frame))
res = gensym()
eval_expr = Expr(:let,
Expr(:block, map(x->Expr(:(=), x...), [(v.name, maybe_quote(v.value)) for v in vars])...,
Expr(:(=), :__FRAME__ , frame)),
Expr(:block,
Expr(:(=), res, expr),
Expr(:tuple, res, Expr(:tuple, [v.name for v in vars]...))
))
eval_res, res = Core.eval(moduleof(frame), eval_expr)
j = 1
for (i, v) in enumerate(vars)
if v.isparam
frame.framedata.sparams[j] = res[i]
j += 1
else
frame.framedata.locals[frame.framedata.last_reference[v.name]] = Some{Any}(res[i])
end
end
eval_res
end
# Completions
mutable struct DebugCompletionProvider <: REPL.CompletionProvider
state::DebuggerState
end
function LineEdit.complete_line(c::DebugCompletionProvider, s)
partial = REPL.beforecursor(s.input_buffer)
full = LineEdit.input_string(s)
ret, range, should_complete = completions(c, full, lastindex(partial))
return unique!(map(REPLCompletions.completion_text, ret)), partial[range], should_complete
end
function completions(c::DebugCompletionProvider, full, partial)
mod = moduleof(c.state.frame)
ret, range, should_complete = REPLCompletions.completions(full, partial, mod)
# TODO Add local variable completions
return ret, range, should_complete
end