Skip to content

Commit 13b07fc

Browse files
simeonschaubtkfvtjnash
authored
mention methodtable Ctrl+Q trick in methodshow (#35556)
Co-authored-by: Takafumi Arakaki <[email protected]> Co-authored-by: Jameson Nash <[email protected]>
1 parent 446618d commit 13b07fc

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

base/errorshow.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,10 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
538538
end
539539
end
540540

541-
# Contains file name and file number. Gets set when a backtrace
542-
# or methodlist is shown. Used by the REPL to make it possible to open
543-
# the location of a stackframe/method in the editor.
544-
global LAST_SHOWN_LINE_INFOS = Tuple{String, Int}[]
545-
546541
function show_trace_entry(io, frame, n; prefix = "")
547-
push!(LAST_SHOWN_LINE_INFOS, (string(frame.file), frame.line))
542+
if haskey(io, :LAST_SHOWN_LINE_INFOS)
543+
push!(io[:LAST_SHOWN_LINE_INFOS], (string(frame.file), frame.line))
544+
end
548545
print(io, "\n", prefix)
549546
show(io, frame, full_path=true)
550547
n > 1 && print(io, " (repeats ", n, " times)")
@@ -631,7 +628,9 @@ function show_reduced_backtrace(io::IO, t::Vector, with_prefix::Bool)
631628
end
632629

633630
function show_backtrace(io::IO, t::Vector)
634-
resize!(LAST_SHOWN_LINE_INFOS, 0)
631+
if haskey(io, :LAST_SHOWN_LINE_INFOS)
632+
resize!(io[:LAST_SHOWN_LINE_INFOS], 0)
633+
end
635634
filtered = process_backtrace(t)
636635
isempty(filtered) && return
637636

base/methodshow.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
240240
end
241241
n = rest = 0
242242
local last
243+
LAST_SHOWN_LINE_INFOS = get(io, :LAST_SHOWN_LINE_INFOS, Tuple{String,Int}[])
243244

244245
resize!(LAST_SHOWN_LINE_INFOS, 0)
245246
for meth in ms
@@ -373,6 +374,7 @@ show(io::IO, mime::MIME"text/html", mt::Core.MethodTable) = show(io, mime, Metho
373374

374375
# pretty-printing of AbstractVector{Method}
375376
function show(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method})
377+
LAST_SHOWN_LINE_INFOS = get(io, :LAST_SHOWN_LINE_INFOS, Tuple{String,Int}[])
376378
resize!(LAST_SHOWN_LINE_INFOS, 0)
377379
first = true
378380
for (i, m) in enumerate(mt)

stdlib/REPL/src/REPL.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,22 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
211211
io = foldl(IOContext, d.repl.options.iocontext,
212212
init=IOContext(io, :limit => true, :module => Main))
213213
end
214+
215+
infos = Tuple{String,Int}[]
216+
io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos)
217+
214218
show(io, mime, x)
215219
println(io)
220+
221+
if !isempty(infos)
222+
d.repl.last_shown_line_infos = infos
223+
println(
224+
io,
225+
"\nTo edit a specific method, type the corresponding number into the " *
226+
"REPL and press Ctrl+Q",
227+
)
228+
end
229+
216230
nothing
217231
end
218232
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
@@ -430,11 +444,12 @@ mutable struct LineEditREPL <: AbstractREPL
430444
specialdisplay::Union{Nothing,AbstractDisplay}
431445
options::Options
432446
mistate::Union{MIState,Nothing}
447+
last_shown_line_infos::Vector{Tuple{String,Int}}
433448
interface::ModalInterface
434449
backendref::REPLBackendRef
435450
LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) =
436451
new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,
437-
in_help,envcolors,false,nothing, Options(), nothing)
452+
in_help,envcolors,false,nothing, Options(), nothing, Tuple{String,Int}[])
438453
end
439454
outstream(r::LineEditREPL) = r.t
440455
specialdisplay(r::LineEditREPL) = r.specialdisplay
@@ -1092,10 +1107,10 @@ function setup_interface(
10921107
end,
10931108

10941109
# Open the editor at the location of a stackframe or method
1095-
# This is accessing a global variable that gets set in
1110+
# This is accessing a contextual variable that gets set in
10961111
# the show_backtrace and show_method_table functions.
10971112
"^Q" => (s, o...) -> begin
1098-
linfos = Base.LAST_SHOWN_LINE_INFOS
1113+
linfos = repl.last_shown_line_infos
10991114
str = String(take!(LineEdit.buffer(s)))
11001115
n = tryparse(Int, str)
11011116
n === nothing && @goto writeback

0 commit comments

Comments
 (0)