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

Normalize all = to in in for loops #70

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 6 additions & 27 deletions src/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function add_node!(t::PTree, n::PTree, s::State; join_lines = false, max_padding
join_lines = true
loc = s.offset > length(s.doc.text) && t.typ === CSTParser.TopLevel ?
loc = cursor_loc(s, s.offset - 1) : cursor_loc(s)
for l = t.endline:loc[1]
for l in t.endline:loc[1]
if has_semicolon(s.doc, l)
# @info "found semicolon" l
n.startline = l
Expand Down Expand Up @@ -209,7 +209,7 @@ function length_to_newline(x::PTree, start = 1)
x.typ === PLACEHOLDER && (return 0, true)
is_leaf(x) && (return length(x), false)
len = 0
for i = start:length(x.nodes)
for i in start:length(x.nodes)
n = x.nodes[i]
ln, nl = length_to_newline(n)
len += ln
Expand Down Expand Up @@ -861,33 +861,12 @@ function p_let(x, s)
t
end



# Transforms
#
# for i = iter body end
#
# =>
#
# for i in iter body end
#
# AND
#
# for i in 1:10 body end
#
# =>
#
# for i = 1:10 body end
#
# https://github.com/domluna/JuliaFormatter.jl/issues/34
# Transform all `for i = iter body end` to `for i in iter body end` with no
# exceptions.
function eq_to_in_normalization!(x)
if x.typ === CSTParser.BinaryOpCall
op = x.args[2]
arg2 = x.args[3]
if op.kind === Tokens.EQ && !is_colon_op(arg2)
if x.args[2].kind === Tokens.EQ
x.args[2].kind = Tokens.IN
elseif op.kind === Tokens.IN && is_colon_op(arg2)
x.args[2].kind = Tokens.EQ
end
elseif x.typ === CSTParser.Block || x.typ === CSTParser.InvisBrackets
for a in x.args
Expand Down Expand Up @@ -1591,7 +1570,7 @@ function p_comprehension(x, s)
add_node!(t, pretty(a, s), s, join_lines = true)
add_node!(t, Whitespace(1), s)
if a.kind === Tokens.FOR
for j = i+1:length(x)
for j in i+1:length(x)
eq_to_in_normalization!(x.args[j])
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end

@inline function print_notcode(io, x, s)
prev_nl = true
for l = x.startline:x.endline
for l in x.startline:x.endline
v = getline(s.doc, l)
v == "" && continue
if l == x.endline && v[end] == '\n'
Expand Down
72 changes: 30 additions & 42 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ end
for i = 1:n
println(i)
end"""
@test fmt(str) == str

str = """
for i in itr
@test fmt(str) == """
for i in 1:n
println(i)
end"""
@test fmt(str) == str

str = """
for i = 1:n
for i = iter
println(i)
end"""
str_ = """
@test fmt(str) == """
for i in iter
println(i)
end"""

str = """
for i in 1:n
println(i)
end"""
Expand All @@ -110,47 +112,34 @@ end
for i in itr
println(i)
end"""
str_ = """
for i = itr
println(i)
end"""
@test fmt(str_) == str
@test fmt(str) == str

str_ = """
str = """
for i = I1, j in I2
println(i, j)
end"""
str = """
@test fmt(str) == """
for i in I1, j in I2
println(i, j)
end"""
@test fmt(str_) == str

str = """
for i = 1:30, j = 100:-2:1
println(i, j)
end"""
str_ = """
for i = 1:30, j in 100:-2:1
@test fmt(str) == """
for i in 1:30, j in 100:-2:1
println(i, j)
end"""
@test fmt(str) == str

str_ = "[(i,j) for i=I1,j=I2]"
str = "[(i, j) for i in I1, j in I2]"
@test fmt(str_) == str

str_ = "((i,j) for i=I1,j=I2)"
str = "((i, j) for i in I1, j in I2)"
@test fmt(str_) == str
@test fmt("[(i,j) for i=I1,j=I2]") == "[(i, j) for i in I1, j in I2]"
@test fmt("((i,j) for i=I1,j=I2)") == "((i, j) for i in I1, j in I2)"

str_ = "[(i,j) for i in 1:2:10,j in 100:-1:10]"
str = "[(i, j) for i = 1:2:10, j = 100:-1:10]"
@test fmt(str_) == str
str = "[(i, j) for i in 1:2:10, j in 100:-1:10]"
@test fmt(str) == str

str_ = "((i,j) for i in 1:2:10,j in 100:-1:10)"
str = "((i, j) for i = 1:2:10, j = 100:-1:10)"
@test fmt(str_) == str
str = "((i, j) for i in 1:2:10, j in 100:-1:10)"
@test fmt(str) == str
end

@testset "tuples" begin
Expand Down Expand Up @@ -270,7 +259,7 @@ end
str_ = """foo() = for i=1:10 body end"""
str = """
foo() =
for i = 1:10
for i in 1:10
body
end"""
@test fmt(str_) == str
Expand Down Expand Up @@ -334,7 +323,7 @@ end
str_ = """foo = for i=1:10 body end"""
str = """
foo =
for i = 1:10
for i in 1:10
body
end"""
@test fmt(str_, 2, 1) == str
Expand Down Expand Up @@ -634,7 +623,7 @@ end
body
end"""
t = run_pretty(str, 80)
@test length(t) == 12
@test length(t) == 13

str = """
for i in 1:10
Expand Down Expand Up @@ -1432,7 +1421,7 @@ end
# NOTE: `str_` has extra whitespace after
# keywords on purpose
str_ = """
begin
begin
# comment
end"""
str = """
Expand All @@ -1442,11 +1431,11 @@ end
@test fmt(str_) == str

str_ = """
try
try
# comment
catch
catch
# comment
finally
finally
# comment
end"""
str = """
Expand Down Expand Up @@ -1489,7 +1478,7 @@ end
AbstractFoo
end""") == str

str = """for i = 1:10
str = """for i in 1:10
1
2
3
Expand Down Expand Up @@ -1639,8 +1628,7 @@ end
str = """T[e for e in x]"""
@test fmt("T[e for e= x ]") == str

str = """T[e for e = 1:2:50]"""
@test fmt("T[e for e= 1:2:50 ]") == str
@test fmt("T[e for e= 1:2:50 ]") == "T[e for e in 1:2:50]"

str = """struct Foo end"""
@test fmt("struct Foo\n end") == str
Expand Down Expand Up @@ -1898,7 +1886,7 @@ end
@test fmt("(a;b;c)", 4, 100) == str
@test fmt("(a;b;c)", 4, 1) == str

str = "(x for x = 1:10)"
str = "(x for x in 1:10)"
@test fmt("(x for x in 1 : 10)", 4, 100) == str
@test fmt("(x for x in 1 : 10)", 4, 1) == str

Expand Down