From 716f9d86e6a77f26dfe6d9b4c0d889da418da408 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Wed, 22 Apr 2020 19:14:49 +0200 Subject: [PATCH 1/7] mention methodtable Ctrl+Q trick in methodshow --- base/methodshow.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/base/methodshow.jl b/base/methodshow.jl index e878660a0305e..cd6dc82015da0 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -266,6 +266,14 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru end end end + + if isinteractive() + print( + io, + "\n\nTo edit a specific method, type the corresponding number into the " * + "REPL and press Ctrl+Q", + ) + end end show(io::IO, ms::MethodList) = show_method_table(io, ms) From 3971667c91b78ec2886273cc88532d3b46c2c1da Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 11 May 2020 13:05:02 +0200 Subject: [PATCH 2/7] set custom interactive option when in REPL --- base/methodshow.jl | 2 +- stdlib/REPL/src/REPL.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/base/methodshow.jl b/base/methodshow.jl index cd6dc82015da0..fc3b67b524b1b 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -267,7 +267,7 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru end end - if isinteractive() + if get(io, :interactive, false) print( io, "\n\nTo edit a specific method, type the corresponding number into the " * diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 30af00a6cdc06..be3e14538d25e 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -206,6 +206,7 @@ end function display(d::REPLDisplay, mime::MIME"text/plain", x) io = outstream(d.repl) get(io, :color, false) && write(io, answer_color(d.repl)) + io = IOContext(io, :interactive => true) if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext) # this can override the :limit property set initially io = foldl(IOContext, d.repl.options.iocontext, From 2a32e1313b7bc5e021a1bc689720d4379ef04acc Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 25 May 2020 20:35:56 +0200 Subject: [PATCH 3/7] implement @tkf's suggestion --- base/errorshow.jl | 13 ++++++------- base/methodshow.jl | 10 ++-------- stdlib/REPL/src/REPL.jl | 20 ++++++++++++++++++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/base/errorshow.jl b/base/errorshow.jl index a4583bc819856..4616181f6c4d8 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -541,13 +541,10 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=() end end -# Contains file name and file number. Gets set when a backtrace -# or methodlist is shown. Used by the REPL to make it possible to open -# the location of a stackframe/method in the editor. -global LAST_SHOWN_LINE_INFOS = Tuple{String, Int}[] - function show_trace_entry(io, frame, n; prefix = "") - push!(LAST_SHOWN_LINE_INFOS, (string(frame.file), frame.line)) + if haskey(io, :LAST_SHOWN_LINE_INFOS) + push!(io[:LAST_SHOWN_LINE_INFOS], (string(frame.file), frame.line)) + end print(io, "\n", prefix) show(io, frame, full_path=true) n > 1 && print(io, " (repeats ", n, " times)") @@ -634,7 +631,9 @@ function show_reduced_backtrace(io::IO, t::Vector, with_prefix::Bool) end function show_backtrace(io::IO, t::Vector) - resize!(LAST_SHOWN_LINE_INFOS, 0) + if haskey(io, :LAST_SHOWN_LINE_INFOS) + resize!(io[:LAST_SHOWN_LINE_INFOS], 0) + end filtered = process_backtrace(t) isempty(filtered) && return diff --git a/base/methodshow.jl b/base/methodshow.jl index fc3b67b524b1b..2762acfb52a5c 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -240,6 +240,7 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru end n = rest = 0 local last + LAST_SHOWN_LINE_INFOS = get(io, :LAST_SHOWN_LINE_INFOS, Tuple{String,Int}[]) resize!(LAST_SHOWN_LINE_INFOS, 0) for meth in ms @@ -266,14 +267,6 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru end end end - - if get(io, :interactive, false) - print( - io, - "\n\nTo edit a specific method, type the corresponding number into the " * - "REPL and press Ctrl+Q", - ) - end end show(io::IO, ms::MethodList) = show_method_table(io, ms) @@ -381,6 +374,7 @@ show(io::IO, mime::MIME"text/html", mt::Core.MethodTable) = show(io, mime, Metho # pretty-printing of AbstractVector{Method} function show(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method}) + LAST_SHOWN_LINE_INFOS = get(io, :LAST_SHOWN_LINE_INFOS, Tuple{String,Int}[]) resize!(LAST_SHOWN_LINE_INFOS, 0) first = true for (i, m) in enumerate(mt) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index be3e14538d25e..cb7e15c807f8a 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -212,8 +212,23 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x) io = foldl(IOContext, d.repl.options.iocontext, init=IOContext(io, :limit => true, :module => Main)) end + + if isdefined(d.repl, :last_shown_line_infos) + infos = d.repl.last_shown_line_infos + io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos) + end + show(io, mime, x) println(io) + + if !isempty(get(io, :last_shown_line_infos, Tuple{String,Int}[])) + println( + io, + "\nTo edit a specific method, type the corresponding number into the " * + "REPL and press Ctrl+Q", + ) + end + nothing end display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x) @@ -433,9 +448,10 @@ mutable struct LineEditREPL <: AbstractREPL mistate::Union{MIState,Nothing} interface::ModalInterface backendref::REPLBackendRef + last_shown_line_infos::Vector{Tuple{String,Int}} LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) = new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell, - in_help,envcolors,false,nothing, Options(), nothing) + in_help,envcolors,false,nothing, Options(), nothing, Tuple{String,Int}[]) end outstream(r::LineEditREPL) = r.t specialdisplay(r::LineEditREPL) = r.specialdisplay @@ -1096,7 +1112,7 @@ function setup_interface( # This is accessing a global variable that gets set in # the show_backtrace and show_method_table functions. "^Q" => (s, o...) -> begin - linfos = Base.LAST_SHOWN_LINE_INFOS + linfos = repl.last_shown_line_infos str = String(take!(LineEdit.buffer(s))) n = tryparse(Int, str) n === nothing && @goto writeback From d4269ffd162ab199571d5697c672ec7bbb63e15f Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Tue, 26 May 2020 10:18:19 +0200 Subject: [PATCH 4/7] fix build failure --- stdlib/REPL/src/REPL.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index cb7e15c807f8a..a950515d6fdfd 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -213,6 +213,7 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x) init=IOContext(io, :limit => true, :module => Main)) end + infos = Tuple{String,Int}[] if isdefined(d.repl, :last_shown_line_infos) infos = d.repl.last_shown_line_infos io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos) @@ -221,7 +222,7 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x) show(io, mime, x) println(io) - if !isempty(get(io, :last_shown_line_infos, Tuple{String,Int}[])) + if !isempty(infos) println( io, "\nTo edit a specific method, type the corresponding number into the " * @@ -446,9 +447,9 @@ mutable struct LineEditREPL <: AbstractREPL specialdisplay::Union{Nothing,AbstractDisplay} options::Options mistate::Union{MIState,Nothing} + last_shown_line_infos::Vector{Tuple{String,Int}} interface::ModalInterface backendref::REPLBackendRef - last_shown_line_infos::Vector{Tuple{String,Int}} LineEditREPL(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell,in_help,envcolors) = new(t,hascolor,prompt_color,input_color,answer_color,shell_color,help_color,history_file,in_shell, in_help,envcolors,false,nothing, Options(), nothing, Tuple{String,Int}[]) From 19d57b38a42514e6bb42641d744914c4bf815c6a Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Tue, 26 May 2020 23:10:09 +0200 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Takafumi Arakaki --- stdlib/REPL/src/REPL.jl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index a950515d6fdfd..3748913aef4cd 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -214,15 +214,13 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x) end infos = Tuple{String,Int}[] - if isdefined(d.repl, :last_shown_line_infos) - infos = d.repl.last_shown_line_infos - io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos) - end + io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos) show(io, mime, x) println(io) if !isempty(infos) + d.repl.last_shown_line_infos = infos println( io, "\nTo edit a specific method, type the corresponding number into the " * From 0ca2803b342f19664d6d9391544bb9e825c5a4fd Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Tue, 26 May 2020 23:30:43 +0200 Subject: [PATCH 6/7] Update stdlib/REPL/src/REPL.jl Co-authored-by: Takafumi Arakaki --- stdlib/REPL/src/REPL.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 3748913aef4cd..754b99acce0af 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -206,7 +206,6 @@ end function display(d::REPLDisplay, mime::MIME"text/plain", x) io = outstream(d.repl) get(io, :color, false) && write(io, answer_color(d.repl)) - io = IOContext(io, :interactive => true) if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext) # this can override the :limit property set initially io = foldl(IOContext, d.repl.options.iocontext, From c3dd3d437cc2f3a3aff177d0b1537356a533013a Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Sat, 13 Jun 2020 20:53:08 +0200 Subject: [PATCH 7/7] Update stdlib/REPL/src/REPL.jl Co-authored-by: Jameson Nash --- stdlib/REPL/src/REPL.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl index 754b99acce0af..a85ae7354f886 100644 --- a/stdlib/REPL/src/REPL.jl +++ b/stdlib/REPL/src/REPL.jl @@ -1107,7 +1107,7 @@ function setup_interface( end, # Open the editor at the location of a stackframe or method - # This is accessing a global variable that gets set in + # This is accessing a contextual variable that gets set in # the show_backtrace and show_method_table functions. "^Q" => (s, o...) -> begin linfos = repl.last_shown_line_infos