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

Add applicable check to @show_special #1595

Closed
Closed
Changes from 1 commit
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
Prev Previous commit
Add applicable check to @show_special
  • Loading branch information
lgoettgens committed Feb 5, 2024
commit e4c8f1fffba2515fe6a83de1fe3f0b9699e1d564
8 changes: 4 additions & 4 deletions src/PrettyPrinting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1389,12 +1389,12 @@

const CurrentModule = Ref(Main)

function set_current_module(m)
CurrentModule[] = m

Check warning on line 1393 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1392-L1393

Added lines #L1392 - L1393 were not covered by tests
end

function get_current_module()
return CurrentModule[]

Check warning on line 1397 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1396-L1397

Added lines #L1396 - L1397 were not covered by tests
end

"""
Expand All @@ -1406,9 +1406,9 @@

This function errors if `obj` does not support attribute storage.
"""
function set_name!(obj, name::String; override::Bool=true)
override || isnothing(get_attribute(obj, :name)) || return
set_attribute!(obj, :name => name)

Check warning on line 1411 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1409-L1411

Added lines #L1409 - L1411 were not covered by tests
end

"""
Expand All @@ -1421,11 +1421,11 @@

This function errors if `obj` does not support attribute storage.
"""
function set_name!(obj; override::Bool=true)
override || isnothing(get_attribute(obj, :name)) || return
sy = find_name(obj)
isnothing(sy) && return
set_name!(obj, string(sy); override=true)

Check warning on line 1428 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1424-L1428

Added lines #L1424 - L1428 were not covered by tests
end

"""
Expand All @@ -1434,7 +1434,7 @@
May be overloaded to provide a fallback name for the object `obj` in [`AbstractAlgebra.get_name`](@ref).
The default implementation returns `nothing`.
"""
extra_name(obj) = nothing

Check warning on line 1437 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1437

Added line #L1437 was not covered by tests

"""
find_name(obj, M = Main; all::Bool = false) -> Union{String,Nothing}
Expand All @@ -1454,45 +1454,45 @@
!!! warning
This function should not be used directly, but rather through [`AbstractAlgebra.get_name`](@ref).
"""
function find_name(obj, M=Main; all::Bool=false)
AbstractAlgebra._is_attribute_storing_type(typeof(obj)) || return find_new_name(obj, M; all)

Check warning on line 1458 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1457-L1458

Added lines #L1457 - L1458 were not covered by tests

cached_name = get_attribute(obj, :cached_name)
if !isnothing(cached_name)
cached_name_sy = Symbol(cached_name)
if M === Main && get_current_module() != Main
if isdefined(get_current_module(), cached_name_sy) && getproperty(get_current_module(), cached_name_sy) === obj
return cached_name

Check warning on line 1465 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1460-L1465

Added lines #L1460 - L1465 were not covered by tests
end
end
if isdefined(M, cached_name_sy) && getproperty(M, cached_name_sy) === obj
return cached_name

Check warning on line 1469 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1468-L1469

Added lines #L1468 - L1469 were not covered by tests
end
end
name = find_new_name(obj, M; all)
set_attribute!(obj, :cached_name => name)
return name

Check warning on line 1474 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1472-L1474

Added lines #L1472 - L1474 were not covered by tests
end

function find_new_name(obj, M=Main; all::Bool=false)

Check warning on line 1477 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1477

Added line #L1477 was not covered by tests
# in Documenter, the examples are not run in the REPL.
# in the REPL: A = ... adds A to the global name space (Main....)
# in Documenter (doctests) all examples are run in their own module
# which is stored in CurrentModule, hence we need to search there as well
#furthermore, they are not exported, hence the "all" option
if M === Main && get_current_module() != Main
a = find_name(obj, get_current_module(), all=true)
if !isnothing(a)

Check warning on line 1485 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1483-L1485

Added lines #L1483 - L1485 were not covered by tests
return a
end
end
for a = names(M; all=all)

Check warning on line 1489 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1489

Added line #L1489 was not covered by tests
a === :ans && continue
if isdefined(M, a) && getfield(M, a) === obj
return string(a)

Check warning on line 1492 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1491-L1492

Added lines #L1491 - L1492 were not covered by tests
end
end
return nothing

Check warning on line 1495 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1495

Added line #L1495 was not covered by tests
end

"""
Expand All @@ -1504,19 +1504,19 @@
2. The name of a variable in global (`Main` module) namespace with value bound to the object `obj` (see [`AbstractAlgebra.find_name`](@ref)).
3. The name returned by [`AbstractAlgebra.extra_name`](@ref).
"""
function get_name(obj)
if AbstractAlgebra._is_attribute_storing_type(typeof(obj))
name = get_attribute(obj, :name)
isnothing(name) || return name

Check warning on line 1510 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1507-L1510

Added lines #L1507 - L1510 were not covered by tests
end

sy = find_name(obj)
isnothing(sy) || return string(sy)

Check warning on line 1514 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1513-L1514

Added lines #L1513 - L1514 were not covered by tests

name_maybe = extra_name(obj)::Union{String,Nothing}
isnothing(name_maybe) || return name_maybe

Check warning on line 1517 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1516-L1517

Added lines #L1516 - L1517 were not covered by tests

return nothing

Check warning on line 1519 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1519

Added line #L1519 was not covered by tests
end

"""
Expand All @@ -1536,13 +1536,13 @@
local i = $(esc(io))
local o = $(esc(obj))
if get(i, :supercompact, false) || get(i, :compact, false)
name = get_name(o)
if !isnothing(name)
if AbstractAlgebra.PrettyPrinting._supports_io_custom(i)
print(i, LowercaseOff())

Check warning on line 1542 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1539-L1542

Added lines #L1539 - L1542 were not covered by tests
end
print(i, name)
return

Check warning on line 1545 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1544-L1545

Added lines #L1544 - L1545 were not covered by tests
end
end
end
Expand All @@ -1563,9 +1563,9 @@
local i = $(esc(io))
local o = $(esc(obj))
s = get_attribute(o, :show)
if s !== nothing
if s !== nothing && applicable(s, i, o)
s(i, o)
return

Check warning on line 1568 in src/PrettyPrinting.jl

View check run for this annotation

Codecov / codecov/patch

src/PrettyPrinting.jl#L1567-L1568

Added lines #L1567 - L1568 were not covered by tests
end
end
)
Expand All @@ -1586,7 +1586,7 @@
local m = $(esc(mime))
local o = $(esc(obj))
s = get_attribute(o, :show)
if s !== nothing
if s !== nothing && applicable(s, i, m, o)
s(i, m, o)
return
end
Expand All @@ -1609,7 +1609,7 @@
local o = $(esc(obj))
local p = parent(o)
s = get_attribute(p, :show_elem)
if s !== nothing
if s !== nothing && applicable(s, i, o)
s(i, o)
return
end
Expand All @@ -1633,7 +1633,7 @@
local o = $(esc(obj))
local p = parent(o)
s = get_attribute(p, :show_elem)
if s !== nothing
if s !== nothing && applicable(s, i, m, o)
s(i, m, o)
return
end
Expand Down
Loading