Skip to content

Commit c2954cf

Browse files
authored
Merge pull request #17521 from Sacha0/dotcalldisp
Naive fix for pretty-printing of compact broadcast expressions
2 parents 2966b3a + 1545ac3 commit c2954cf

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

base/show.jl

+11-6
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ const expr_infix_wide = Set{Symbol}([
407407
const expr_infix = Set{Symbol}([:(:), :(->), Symbol("::")])
408408
const expr_infix_any = union(expr_infix, expr_infix_wide)
409409
const all_ops = union(quoted_syms, uni_ops, expr_infix_any)
410-
const expr_calls = Dict(:call =>('(',')'), :calldecl =>('(',')'), :ref =>('[',']'), :curly =>('{','}'))
410+
const expr_calls = Dict(:call => ('(',')'), :calldecl => ('(',')'),
411+
:ref => ('[',']'), :curly => ('{','}'), :(.) => ('(',')'))
411412
const expr_parens = Dict(:tuple=>('(',')'), :vcat=>('[',']'),
412413
:hcat =>('[',']'), :row =>('[',']'), :vect=>('[',']'))
413414

@@ -538,6 +539,9 @@ function show_call(io::IO, head, func, func_args, indent)
538539
show_unquoted(io, func, indent)
539540
print(io, ')')
540541
end
542+
if head == :(.)
543+
print(io, '.')
544+
end
541545
if !isempty(func_args) && isa(func_args[1], Expr) && func_args[1].head === :parameters
542546
print(io, op)
543547
show_list(io, func_args[2:end], ',', indent)
@@ -648,8 +652,8 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
648652
if !emphstate && ex.typ === Any
649653
show_type = false
650654
end
651-
# dot (i.e. "x.y")
652-
if is(head, :(.))
655+
# dot (i.e. "x.y"), but not compact broadcast exps
656+
if is(head, :(.)) && !is_expr(args[2], :tuple)
653657
show_unquoted(io, args[1], indent + indent_width)
654658
print(io, '.')
655659
if is_quoted(args[2])
@@ -750,9 +754,10 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
750754
show_call(io, head, func, func_args, indent)
751755
end
752756

753-
# other call-like expressions ("A[1,2]", "T{X,Y}")
754-
elseif haskey(expr_calls, head) && nargs >= 1 # :ref/:curly/:calldecl
755-
show_call(io, head, ex.args[1], ex.args[2:end], indent)
757+
# other call-like expressions ("A[1,2]", "T{X,Y}", "f.(X,Y)")
758+
elseif haskey(expr_calls, head) && nargs >= 1 # :ref/:curly/:calldecl/:(.)
759+
funcargslike = head == :(.) ? ex.args[2].args : ex.args[2:end]
760+
show_call(io, head, ex.args[1], funcargslike, indent)
756761

757762
# comprehensions
758763
elseif (head === :typed_comprehension || head === :typed_dict_comprehension) && length(args) == 2

test/show.jl

+5
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,8 @@ end
565565
for op in (:(.=), :(.+=), :(.&=))
566566
@test repr(parse("x $op y")) == ":(x $op y)"
567567
end
568+
569+
# pretty-printing of compact broadcast expressions (#17289)
570+
@test repr(:(f.(X,Y))) == ":(f.(X,Y))"
571+
@test repr(:(f.(X))) == ":(f.(X))"
572+
@test repr(:(f.())) == ":(f.())"

0 commit comments

Comments
 (0)