Skip to content

Commit c63e1db

Browse files
authored
Merge pull request JuliaCollections#129 from nhz2/master
Undo breaking `print_tree` change
2 parents 7065e2f + 27355dd commit c63e1db

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/printing.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
print_tree(tree; kw...)
33
print_tree(io::IO, tree; kw...)
4+
print_tree(f::Function, io::IO, tree; kw...)
45
print_tree(f::Function, g::Function, io::IO, tree; kw...)
56
67
Print a text representation of `tree` to the given `io` object.
@@ -20,6 +21,7 @@ signature `g(io::IO, key;)`.
2021
* `printkeys::Union{Bool, Nothing}` - Whether to print keys of child nodes (using
2122
`pairs(children(node))`). A value of `nothing` uses [`printkeys_default`](@ref) do decide the
2223
behavior on a node-by-node basis.
24+
* `printnode_kw = (;)` - keyword arguments to forward to `f`.
2325
2426
# Examples
2527
@@ -77,12 +79,14 @@ function print_tree end
7779

7880

7981
"""
80-
printnode(io::IO, node)
82+
printnode(io::IO, node; kw...)
8183
8284
Print a compact representation of a single node. By default, this prints `nodevalue(node)`.
8385
8486
**OPTIONAL**: This can be extended for custom types and controls how nodes are shown
8587
in [`print_tree`](@ref).
88+
89+
The keyword argument `printnode_kw` of [`print_tree`](@ref) will be passed to this function.
8690
"""
8791
printnode(io::IO, node; kw...) = show(IOContext(io, :compact => true, :limit => true), nodevalue(node))
8892

@@ -194,11 +198,11 @@ function print_tree(printnode::Function, print_child_key::Function, io::IO, node
194198
printkeys::Union{Bool,Nothing}=nothing,
195199
depth::Integer=0,
196200
prefix::AbstractString="",
197-
kw...
201+
printnode_kw=(;),
198202
)
199203
# Get node representation as string
200204
buf = IOBuffer()
201-
printnode(IOContext(buf, io), node; kw...)
205+
printnode(IOContext(buf, io), node; printnode_kw...)
202206
str = String(take!(buf))
203207

204208
# Copy buffer to output, prepending prefix to each line
@@ -264,24 +268,27 @@ function print_tree(printnode::Function, print_child_key::Function, io::IO, node
264268

265269
print_tree(printnode, print_child_key, io, child;
266270
maxdepth=maxdepth, indicate_truncation=indicate_truncation, charset=charset,
267-
printkeys=printkeys, depth=depth+1, prefix=child_prefix
271+
printkeys=printkeys, depth=depth+1, prefix=child_prefix, printnode_kw=printnode_kw,
268272
)
269273
end
270274
end
271275

276+
print_tree(printnode::Function, io::IO, node; kw...) = print_tree(printnode, print_child_key, io, node; kw...)
272277
print_tree(io::IO, node; kw...) = print_tree(printnode, print_child_key, io, node; kw...)
273278
print_tree(node; kw...) = print_tree(stdout, node; kw...)
274279

275280

276281
"""
277282
repr_tree(tree; context=nothing, kw...)
278283
repr_tree(f, tree; context=nothing, kw...)
284+
repr_tree(f, g, tree; context=nothing, kw...)
279285
280286
Get the string result of calling [`print_tree`](@ref) with the supplied arguments.
281287
282288
The `context` argument works as it does in `Base.repr`.
283289
"""
284290
repr_tree(tree; context=nothing, kw...) = repr_tree(printnode, print_child_key, tree; context=nothing, kw...)
291+
repr_tree(f::Function, tree; context=nothing, kw...) = repr_tree(f, print_child_key, tree; context=nothing, kw...)
285292
function repr_tree(f::Function, g::Function, tree; context=nothing, kw...)
286293
buf = IOBuffer()
287294
io = context === nothing ? buf : IOContext(buf, context)

test/printing.jl

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,15 @@ end
189189
└─ "baz"
190190
""")
191191

192-
# Test printnode override
193-
_f(io, s) = s isa BoxNode ? print(io, s.s) : AbstractTrees.printnode(io, s)
194-
_g(io, k) = AbstractTrees.print_child_key(io, k)
195-
196-
str = repr_tree(_f, _g, tree)
197-
@test endswith(str, """
192+
# Test printnode override do block syntax
193+
str_do = repr_tree(tree) do io, s
194+
if s isa BoxNode
195+
print(io, s.s)
196+
else
197+
AbstractTrees.printnode(io, s)
198+
end
199+
end
200+
@test endswith(str_do, """
198201
├─ "foo"
199202
├─ bar
200203
│ ├─ 1
@@ -205,4 +208,20 @@ end
205208
│ └─ 5
206209
└─ "baz"
207210
""")
211+
212+
# Test printnode and print_child_key override
213+
_f(io, s) = s isa BoxNode ? print(io, s.s) : AbstractTrees.printnode(io, s)
214+
_g(io, k) = AbstractTrees.print_child_key(io, k-1)
215+
str = repr_tree(_f, _g, tree; printkeys=true)
216+
@test endswith(str, """
217+
├─ 0 ⇒ "foo"
218+
├─ 1 ⇒ bar
219+
│ ├─ 0 ⇒ 1
220+
│ ├─ 1 ⇒ 2:4
221+
│ │ ├─ 0 ⇒ 2
222+
│ │ ├─ 1 ⇒ 3
223+
│ │ └─ 2 ⇒ 4
224+
│ └─ 2 ⇒ 5
225+
└─ 2 ⇒ "baz"
226+
""")
208227
end

0 commit comments

Comments
 (0)