Skip to content

Commit

Permalink
Add initial color pass to MethodError showing, like in stacktraces
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed Apr 23, 2022
1 parent 3cff21e commit 29ddf22
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
end
end

modulecolordict = copy(STACKTRACE_FIXEDCOLORS)
modulecolorcycler = Iterators.Stateful(Iterators.cycle(STACKTRACE_MODULECOLORS))

for (func, arg_types_param) in funcs
for method in methods(func)
buf = IOBuffer()
Expand Down Expand Up @@ -513,7 +516,9 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
file = string(method.file)
end
stacktrace_contract_userdir() && (file = contractuser(file))
print(iob, " at ", file, ":", line)
printstyled(iob, " @ "; color=:light_black)
printstyled(iob, method.module, ' '; color=draw_module_color(method.module, modulecolordict, modulecolorcycler))
printstyled(iob, file, ":", line; color=:light_black, underline=true)
if !isempty(kwargs)::Bool
unexpected = Symbol[]
if isempty(kwords) || !(any(endswith(string(kword), "...") for kword in kwords))
Expand Down Expand Up @@ -543,7 +548,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()

if !isempty(lines) # Display up to three closest candidates
Base.with_output_color(:normal, io) do io
print(io, "\nClosest candidates are:")
print(io, "\n\nClosest candidates are:")
sort!(lines, by = x -> -x[2])
i = 0
for line in lines
Expand All @@ -555,6 +560,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
i += 1
print(io, String(take!(line[1])))
end
println(io) # extra newline for spacing to stacktrace
end
end
end
Expand Down Expand Up @@ -682,21 +688,23 @@ end
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolordict, modulecolorcycler)
m = Base.parentmodule(frame)
if m !== nothing
while parentmodule(m) !== m
pm = parentmodule(m)
pm == Main && break
m = pm
end
if !haskey(modulecolordict, m)
modulecolordict[m] = popfirst!(modulecolorcycler)
end
modulecolor = modulecolordict[m]
modulecolor = draw_module_color(m, modulecolordict, modulecolorcycler)
else
modulecolor = :default
end
print_stackframe(io, i, frame, n, digit_align_width, modulecolor)
end

# Draw a module color to be used for display purposes
function draw_module_color(m, modulecolordict, modulecolorcycler)
while parentmodule(m) !== m
pm = parentmodule(m)
pm == Main && break
m = pm
end
get!(() -> popfirst!(modulecolorcycler), modulecolordict, m)
end


# Print a stack frame where the module color is set manually with `modulecolor`.
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
Expand Down

0 comments on commit 29ddf22

Please sign in to comment.