Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mention methodtable Ctrl+Q trick in methodshow #35556

Merged
merged 7 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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
Comment on lines +634 to +636
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if haskey(io, :LAST_SHOWN_LINE_INFOS)
resize!(io[:LAST_SHOWN_LINE_INFOS], 0)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK to skip resize! here? What happens when multiple stack traces are shown (via nested errors)? Would IOContext be reset appropriately?

filtered = process_backtrace(t)
isempty(filtered) && return

Expand Down
2 changes: 2 additions & 0 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resize!(LAST_SHOWN_LINE_INFOS, 0)

for meth in ms
Expand Down Expand Up @@ -373,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resize!(LAST_SHOWN_LINE_INFOS, 0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need resize! for something like this?

struct TwoMethodTables
    methods1
    methods2
end

function Base.show(io::IO, ::MIME"text/plain", two::TwoMethodTables)
    println(io, "TwoMethodTables:")
    show(io, MIME"text/plain"(), two.methods1)
    show(io, MIME"text/plain"(), two.methods2)
end

Currently, I think Ctrl+Q works for numbering in methods2. If we remove resize!, it'd refer to the numbering in methods1 (and also in methods2 shifted by length(methods)).

first = true
for (i, m) in enumerate(mt)
Expand Down
21 changes: 18 additions & 3 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,22 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
io = foldl(IOContext, d.repl.options.iocontext,
init=IOContext(io, :limit => true, :module => Main))
end

infos = Tuple{String,Int}[]
io = IOContext(io, :LAST_SHOWN_LINE_INFOS => infos)

show(io, mime, x)
println(io)

if !isempty(infos)
simeonschaub marked this conversation as resolved.
Show resolved Hide resolved
d.repl.last_shown_line_infos = infos
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)
Expand Down Expand Up @@ -430,11 +444,12 @@ 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
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}[])
simeonschaub marked this conversation as resolved.
Show resolved Hide resolved
end
outstream(r::LineEditREPL) = r.t
specialdisplay(r::LineEditREPL) = r.specialdisplay
Expand Down Expand Up @@ -1092,10 +1107,10 @@ 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 = 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
Expand Down