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

Show a "Did you mean:" suggestion for thrown InvalidAttributeErrors #4394

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@
- Fix NaN handling in WGLMakie [#4282](https://github.com/MakieOrg/Makie.jl/pull/4282).
- Show DataInspector tooltip on NaN values if `nan_color` has been set to other than `:transparent` [#4310](https://github.com/MakieOrg/Makie.jl/pull/4310)
- Fix `linestyle` not being used in `triplot` [#4332](https://github.com/MakieOrg/Makie.jl/pull/4332)
- The error shown for invalid attributes will now also show suggestions for nearby attributes (if there are any) [#4394](https://github.com/MakieOrg/Makie.jl/pull/4394)
- Fix voxel clipping not being based on voxel centers [#4397](https://github.com/MakieOrg/Makie.jl/pull/4397)
- Parsing `Q` and `q` commands in svg paths with `BezierPath` is now supported [#4413](https://github.com/MakieOrg/Makie.jl/pull/4413)


## [0.21.11] - 2024-09-13

- Hot fixes for 0.21.10 [#4356](https://github.com/MakieOrg/Makie.jl/pull/4356).
Expand Down
150 changes: 150 additions & 0 deletions MakieCore/src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,134 @@ function print_columns(io::IO, v::Vector{String}; gapsize = 2, rows_first = true
return
end

function _levenshtein_matrix(s1, s2)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L648
a, b = collect(s1), collect(s2)
m, n = length(a), length(b)
d = Matrix{Int}(undef, m + 1, n + 1)
d[1:m+1, 1] = 0:m
d[1, 1:n+1] = 0:n
for i in 1:m
for j in 1:n
d[i+1, j+1] = min(d[i, j+1] + 1, d[i+1, j] + 1, d[i, j] + (a[i] != b[j]))
end
end
return d
end
function _levenshtein(s1, s2)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L648
d = _levenshtein_matrix(s1, s2)
return d[end]
end
function _fuzzyscore(needle, haystack)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L631
score = 0.0
is, acro = _bestmatch(needle, haystack)
score += (acro ? 2 : 1) * length(is) # Matched characters
score -= 2(length(needle) - length(is)) # Missing characters
!acro && (score -= _avgdistance(is)/10) # Contiguous
!isempty(is) && (score -= sum(is)/length(is)/100) # Closer to beginning
end
function _matchinds(needle, haystack; acronym::Bool = false)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L602
chars = collect(needle)
is = Int[]
lastc = '\0'
for (i, char) in enumerate(haystack)
while !isempty(chars) && isspace(first(chars))
popfirst!(chars)
end
isempty(chars) && break
if lowercase(char) == lowercase(chars[1]) && (!acronym || !isletter(lastc))
push!(is, i)
popfirst!(chars)
end
lastc = char
end
return is
end
function _longer(x, y)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L621
return length(x) ≥ length(y) ? (x, true) : (y, false)
end
function _bestmatch(needle, haystack)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L623
return _longer(
_matchinds(needle, haystack, acronym = true),
_matchinds(needle, haystack)
)
end
function _avgdistance(xs)
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L627
return isempty(xs) ? 0 : (xs[end] - xs[1] - length(xs) + 1) / length(xs)
end
function _levsort(search::String, candidates::Vector{String})
# https://github.com/JuliaLang/julia/blob/6f3fdf7b36250fb95f512a2b927ad2518c07d2b5/stdlib/REPL/src/docview.jl#L666
scores = map(candidates) do cand
lev = Float64(_levenshtein(search, cand))
fuz = -_fuzzyscore(search, cand)
return (lev, -fuz)
end
candidates = candidates[sortperm(scores)]
valid = _levenshtein(search, candidates[1]) < 3 # is the first close enough?
return candidates[1], valid # Only return one suggestion per search
end
function find_nearby_attributes(attributes, candidates)
d = Vector{Tuple{String, Bool}}(undef, length(attributes))
any_close = false
for (i, attr) in enumerate(attributes)
candidate, valid = _levsort(String(attr), candidates)
any_close = any_close || valid
d[i] = (candidate, valid)
end
return d, any_close
end

function textdiff(X::String, Y::String)
d = _levenshtein_matrix(X, Y)
a, b = collect(X), collect(Y)
m, n = length(a), length(b)

# Backtrack to print the differences with style
i, j = m, n
results = Vector{Tuple{Char, Symbol}}()

while i > 0 || j > 0
if i > 0 && j > 0 && a[i] == b[j]
# Characters match, print normally
push!(results, (b[j], :normal))
i -= 1
j -= 1
elseif i > 0 && j > 0 && d[i+1, j+1] == d[i, j] + 1
# Substitution (different characters between `X` and `Y`)
push!(results, (b[j], :orange)) # Highlighting the new character. Not showing the old one
i -= 1
j -= 1
elseif j > 0 && d[i+1, j+1] == d[i+1, j] + 1
# Insertion in `Y` (character in `Y` but not in `X`)
push!(results, (b[j], :red)) # Highlighting the added character
j -= 1
elseif i > 0 && d[i+1, j+1] == d[i, j+1] + 1
# Deletion in `X` (character in `X` but not in `Y`)
i -= 1 # Just move the index for X. Not showing the deletion here.
end
end

reverse!(results)
io = IOBuffer()
cio = IOContext(io, :color => true)

for (char, clr) in results
if clr == :normal
print(io, char)
else
printstyled(cio, char; color = :blue, bold = true) # Ignoring different color choices here
end
end

return String(take!(io))
end

function Base.showerror(io::IO, i::InvalidAttributeError)
n = length(i.attributes)
print(io, "Invalid attribute$(n > 1 ? "s" : "") ")
Expand All @@ -729,6 +857,28 @@ function Base.showerror(io::IO, i::InvalidAttributeError)
printstyled(io, i.plottype; color = :blue, bold = true)
println(io, ".")
nameset = sort(string.(collect(attribute_names(i.plottype))))
attrs = string.(collect(i.attributes))
possible_cands, any_close = find_nearby_attributes(attrs, nameset)
any_close && println(io)
if any_close && length(possible_cands) == 1
print(io, "Did you mean ", textdiff(attrs[1], possible_cands[1][1]), "?")
println(io)
elseif any_close
print(io, "Did you mean:")
for (id, (passed, (suggestion, close))) in enumerate(zip(attrs, possible_cands))
close || continue
any_next = any(x -> x[2], view(possible_cands, id+1:length(possible_cands)))
if (id == length(i.attributes)) || (id < length(i.attributes) && !any_next)
print(io, " and")
end
print(io, " ", textdiff(passed, suggestion))
if id < length(i.attributes) && any_next
print(io, ",")
end
end
println(io, "?")
println(io)
end
println(io)
println(io, "The available plot attributes for $(i.plottype) are:")
println(io)
Expand Down
16 changes: 16 additions & 0 deletions test/pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ import Makie.MakieCore: InvalidAttributeError
@test_throws InvalidAttributeError mesh(rand(Point3f, 3); does_not_exist = 123)
end

import Makie.MakieCore: find_nearby_attributes, attribute_names, textdiff

@testset "attribute suggestions" begin
@test find_nearby_attributes(Set([:clr]), sort(string.(collect(attribute_names(Lines))))) == ([("color", true)], true)
triplot_attrs = sort(string.(collect(attribute_names(Triplot))))
attrs = [:recompute_centres, :clr, :strokecolour, :blahblahblahblahblah]
suggestions = find_nearby_attributes(attrs, triplot_attrs)
@test suggestions == ([("recompute_centers", 1), ("marker", 0), ("strokecolor", 1), ("convex_hull_color", 0)], true)

@test textdiff("clr", "color") == "c\e[34m\e[1mo\e[22m\e[39ml\e[34m\e[1mo\e[22m\e[39mr"
@test textdiff("clor", "color") == "c\e[34m\e[1mo\e[22m\e[39mlor"
@test textdiff("", "color") == "\e[34m\e[1mc\e[22m\e[39m\e[34m\e[1mo\e[22m\e[39m\e[34m\e[1ml\e[22m\e[39m\e[34m\e[1mo\e[22m\e[39m\e[34m\e[1mr\e[22m\e[39m"
@test textdiff("colorcolor", "color") == "color"
@test textdiff("cloourm", "color") == "co\e[34m\e[1ml\e[22m\e[39m\e[34m\e[1mo\e[22m\e[39mr"
@test textdiff("ssoa", "ssao") == "ss\e[34m\e[1ma\e[22m\e[39m\e[34m\e[1mo\e[22m\e[39m"
end

@recipe(TestRecipe, x, y) do scene
Attributes()
Expand Down
Loading