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

Give // the same binaryop whitespace exception as ^ #288

Merged
merged 2 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 16 additions & 5 deletions src/styles/default/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1364,14 +1364,25 @@ function p_binaryopcall(
elseif op.kind === Tokens.EX_OR
add_node!(t, Whitespace(1), s)
add_node!(t, pretty(style, op, s), s, join_lines = true)
elseif (is_number(cst[1]) || op.kind === Tokens.CIRCUMFLEX_ACCENT) && op.dot
elseif (
is_number(cst[1]) ||
op.kind === Tokens.FWDFWD_SLASH ||
op.kind === Tokens.CIRCUMFLEX_ACCENT
) && op.dot
add_node!(t, Whitespace(1), s)
add_node!(t, pretty(style, op, s), s, join_lines = true)
nest ? add_node!(t, Placeholder(1), s) : add_node!(t, Whitespace(1), s)
elseif (
nospace ||
(CSTParser.precedence(op) in (8, 13, 14, 16) && op.kind !== Tokens.ANON_FUNC)
) && op.kind !== Tokens.IN
elseif op.kind !== Tokens.IN && (
nospace || (
op.kind !== Tokens.ANON_FUNC && CSTParser.precedence(op) in (
CSTParser.ColonOp,
CSTParser.RationalOp,
CSTParser.PowerOp,
CSTParser.DeclarationOp,
CSTParser.DotOp,
Copy link
Contributor Author

@nickrobinson251 nickrobinson251 Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed (8, 13, 14, 16) -> (CSTParser.ColonOp, CSTParser.PowerOp, CSTParser.DeclarationOp, CSTParser.DotOp) since it took me quite a while to work out what these mystery numbers were. I think the additional verbosity is worth it. Then i've added RationalOp to catch the // case.

)
)
)
add_node!(t, pretty(style, op, s), s, join_lines = true)
else
add_node!(t, Whitespace(1), s)
Expand Down
4 changes: 4 additions & 0 deletions test/default_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
@test fmt("10.0 .^ a") == "10.0 .^ a"
@test fmt("a.^b") == "a .^ b"
@test fmt("a.^10.") == "a .^ 10.0"
@test fmt("a.//10") == "a .// 10"
end

@testset "toplevel" begin
Expand Down Expand Up @@ -376,6 +377,9 @@
str = "!(typ <: ArithmeticTypes)"
@test fmt(str) == str

# `//` and `^` are binary ops without whitespace around them
@test fmt("1 // 2 + 3 ^ 4") == "1//2 + 3^4"

# Function def

str_ = """foo() = if cond a else b end"""
Expand Down