Skip to content

Commit 5e7013d

Browse files
aviateskKristofferC
authored andcommitted
Local completions (#217)
* implement completions for local bindings * fix tests * update ref
1 parent 8762911 commit 5e7013d

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/Debugger.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ using REPL.LineEdit
1111
using REPL.REPLCompletions
1212

1313
using CodeTracking
14-
using JuliaInterpreter: JuliaInterpreter, Frame, @lookup, FrameCode, BreakpointRef, debug_command, leaf, root, BreakpointState,
14+
using JuliaInterpreter: JuliaInterpreter, Frame, @lookup, FrameCode, BreakpointRef, debug_command, leaf, root, BreakpointState,
1515
finish_and_return!, Compiled
1616

17-
using JuliaInterpreter: pc_expr, moduleof, linenumber, extract_args,
17+
using JuliaInterpreter: pc_expr, moduleof, linenumber, extract_args, locals,
1818
root, caller, whereis, get_return, nstatements, getargs
1919

2020
const SEARCH_PATH = []

src/repl.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,28 @@ end
220220
function LineEdit.complete_line(c::DebugCompletionProvider, s)
221221
partial = REPL.beforecursor(s.input_buffer)
222222
full = LineEdit.input_string(s)
223-
ret, range, should_complete = completions(c, full, lastindex(partial))
224-
return unique!(map(REPLCompletions.completion_text, ret)), partial[range], should_complete
223+
224+
ret, range, should_complete = completions(c, full, partial)
225+
return ret, partial[range], should_complete
225226
end
226227

227228
function completions(c::DebugCompletionProvider, full, partial)
228-
mod = moduleof(c.state.frame)
229-
ret, range, should_complete = REPLCompletions.completions(full, partial, mod)
229+
frame = c.state.frame
230+
231+
# repl backend completions
232+
comps, range, should_complete = REPLCompletions.completions(full, lastindex(partial), moduleof(frame))
233+
ret = map(REPLCompletions.completion_text, comps) |> unique!
234+
235+
# local completions
236+
vars = filter!(locals(frame)) do v
237+
# ref: https://github.com/JuliaDebug/JuliaInterpreter.jl/blob/master/src/utils.jl#L365-L370
238+
if v.name == Symbol("#self") && (v.value isa Type || sizeof(v.value) == 0)
239+
return false
240+
else
241+
return startswith(string(v.name), partial)
242+
end
243+
end |> vars -> map(v -> string(v.name), vars)
244+
pushfirst!(ret, vars...)
230245

231-
# TODO Add local variable completions
232-
return ret, range, should_complete
246+
ret, range, should_complete
233247
end

test/ui.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,20 @@ end
169169
end
170170

171171
# Completions
172-
function test_complete(c, s)
173-
c, r, s = Debugger.completions(c, s, lastindex(s))
174-
return unique!(map(REPL.REPLCompletions.completion_text, c)), r, s
175-
end
172+
test_complete(c, s) = Debugger.completions(c, s, s)
176173

177174
module F
178175
local_var = 1
179-
f(x) = x
176+
f(f_args) = f_args
180177
end
181178

182179
@testset "REPL completions" begin
183180
frame = JuliaInterpreter.enter_call_expr(:($(F.f)(1)))
184181
state = dummy_state(frame)
185182
prov = Debugger.DebugCompletionProvider(state)
186183

187-
c, r = test_complete(prov, "local")
184+
c, _ = test_complete(prov, "local")
188185
@test "local_var" in c
186+
c, _ = test_complete(prov, "f_")
187+
@test "f_args" in c
189188
end

0 commit comments

Comments
 (0)