Skip to content

Commit 2fdb1c1

Browse files
KristofferCKristofferCaviatesk
authored
improve REPL completion for locals (#357)
* improve REPL completion for locals * Update src/repl.jl Co-authored-by: Shuhei Kadowaki <[email protected]> --------- Co-authored-by: KristofferC <[email protected]> Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 1340768 commit 2fdb1c1

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/repl.jl

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ function julia_prompt(state::DebuggerState)
188188
LineEdit.transition(s, :abort)
189189
return false
190190
end
191-
xbuf = copy(buf)
192191
command = String(take!(buf))
193192
@static if VERSION >= v"1.2.0-DEV.253"
194193
response = _eval_code(active_frame(state), command)
@@ -241,19 +240,29 @@ function completions(c::DebugCompletionProvider, full, partial)
241240
frame = c.state.frame
242241

243242
# repl backend completions
244-
comps, range, should_complete = REPLCompletions.completions(full, lastindex(partial), moduleof(frame))
245-
ret = map(REPLCompletions.completion_text, comps) |> unique!
243+
comps1, range1, should_complete1 = REPLCompletions.completions(full, lastindex(partial), moduleof(frame))
244+
ret1 = map(REPLCompletions.completion_text, comps1)
246245

247-
# local completions
248-
vars = filter!(locals(frame)) do v
249-
# ref: https://github.com/JuliaDebug/JuliaInterpreter.jl/blob/master/src/utils.jl#L365-L370
250-
if v.name == Symbol("#self") && (v.value isa Type || sizeof(v.value) == 0)
251-
return false
252-
else
253-
return startswith(string(v.name), partial)
254-
end
255-
end |> vars -> map(v -> string(v.name), vars)
256-
pushfirst!(ret, vars...)
246+
ignore_local(v) = v.name == Symbol("#self") && (v.value isa Type || sizeof(v.value) == 0)
247+
m = Module()
248+
for v in locals(frame)
249+
ignore_local(v) && continue
250+
Base.eval(m, :($(v.name) = $(QuoteNode(v.value))))
251+
end
252+
253+
comps2, range2, should_complete2 = REPLCompletions.completions(full, lastindex(partial), m)
254+
ret2 = map(REPLCompletions.completion_text, comps2)
255+
256+
ret = sort!(unique!(vcat(ret1, ret2)))
257+
should_complete = should_complete1 | should_complete2
258+
range = min(range1, range2) # Not sure about this one
259+
260+
# Attempt to allow values to be garbage collected
261+
# because I don't think Julia ever GCs modules.
262+
for v in locals(frame)
263+
ignore_local(v) && continue
264+
Base.eval(m, :($(v.name) = nothing))
265+
end
257266

258267
ret, range, should_complete
259268
end

0 commit comments

Comments
 (0)