Skip to content

Commit

Permalink
pretty-printing for ncat and nrow expressions
Browse files Browse the repository at this point in the history
fixes #41134
  • Loading branch information
simeonschaub committed Jul 17, 2021
1 parent 2893de7 commit 9df2054
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
21 changes: 14 additions & 7 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,7 @@ const expr_calls = Dict(:call => ('(',')'), :calldecl => ('(',')'),
:ref => ('[',']'), :curly => ('{','}'), :(.) => ('(',')'))
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'),
:ncat =>('[',']'), :nrow =>('[',']'),
:braces=>('{','}'), :bracescat=>('{','}'))

## AST decoding helpers ##
Expand Down Expand Up @@ -1811,14 +1812,16 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In

# list-like forms, e.g. "[1, 2, 3]"
elseif haskey(expr_parens, head) || # :vcat etc.
head === :typed_vcat || head === :typed_hcat
head === :typed_vcat || head === :typed_hcat || head === :typed_ncat
# print the type and defer to the untyped case
if head === :typed_vcat || head === :typed_hcat
if head === :typed_vcat || head === :typed_hcat || head === :typed_ncat
show_unquoted(io, args[1], indent, prec, quote_level)
if head === :typed_vcat
head = :vcat
else
elseif head === :typed_hcat
head = :hcat
else
head = :ncat
end
args = args[2:end]
nargs = nargs - 1
Expand All @@ -1828,15 +1831,19 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
sep = "; "
elseif head === :hcat || head === :row
sep = " "
elseif head === :ncat || head === :nrow
sep = ";"^args[1] * " "
args = args[2:end]
nargs = nargs - 1
else
sep = ", "
end
head !== :row && print(io, op)
head !== :row && head !== :nrow && print(io, op)
show_list(io, args, sep, indent, 0, quote_level)
if nargs == 1 && head === :vcat
print(io, ';')
if nargs <= 1 && (head === :vcat || head === :ncat)
print(io, sep[1:end-1])
end
head !== :row && print(io, cl)
head !== :row && head !== :nrow && print(io, cl)

# transpose
elseif (head === Symbol("'") && nargs == 1) || (
Expand Down
16 changes: 16 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2304,3 +2304,19 @@ end
@test replstr([[1;;]]) == "1-element Vector{Matrix{$Int}}:\n [1;;]"
@test replstr([[1;;;]]) == "1-element Vector{Array{$Int, 3}}:\n [1;;;]"
end

@testset "ncat and nrow" begin
@test_repr "[1;;]"
@test_repr "[1;;;]"
@test_repr "[1;; 2]"
@test_repr "[1;;; 2]"
@test_repr "[1;;; 2 3;;; 4]"
@test_repr "[1;;; 2;;;; 3;;; 4]"

@test_repr "T[1;;]"
@test_repr "T[1;;;]"
@test_repr "T[1;; 2]"
@test_repr "T[1;;; 2]"
@test_repr "T[1;;; 2 3;;; 4]"
@test_repr "T[1;;; 2;;;; 3;;; 4]"
end

0 comments on commit 9df2054

Please sign in to comment.