Skip to content

Commit

Permalink
Cache found names
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Feb 5, 2024
1 parent 92022d1 commit 84dbaa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ import .PrettyPrinting: Lowercase
import .PrettyPrinting: Indent
import .PrettyPrinting: Dedent

import .PrettyPrinting: find_name # remove once all call-sites use get_name instead
import .PrettyPrinting: find_new_name as find_name # remove once all call-sites use get_name instead

export @enable_all_show_via_expressify

Expand Down
24 changes: 23 additions & 1 deletion src/PrettyPrinting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,8 @@ or `nothing` if no such variable exists.
If `all` is `true`, private and non-exported variables are also searched.
!!! note
If the object is stored in several variable, the first one will be used.
If the object is stored in several variables, the first one will be used,
but a name returned once is kept until the variable no longer contains this object.
For this to work in doctests, one should call
`AbstractAlgebra.set_current_module(@__MODULE__)` in the `value` argument of
Expand All @@ -1454,6 +1455,26 @@ For this to work in doctests, one should call
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
Expand All @@ -1471,6 +1492,7 @@ function find_name(obj, M=Main; all::Bool=false)
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 Down

0 comments on commit 84dbaa3

Please sign in to comment.