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

V2 Fixes #883

Merged
merged 7 commits into from
Dec 26, 2024
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
15 changes: 8 additions & 7 deletions src/fst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
Const,
Import,
Export,
Public,
Using,
File,
Quotenode,
Expand Down Expand Up @@ -425,7 +426,7 @@ is_opener(t::JuliaSyntax.GreenNode) = kind(t) in KSet"{ ( ["
function is_iterable(t::JuliaSyntax.GreenNode)
if !(
kind(t) in
KSet"parens tuple vect vcat braces curly comprehension typed_comprehension macrocall ref typed_vcat import using export"
KSet"parens tuple vect vcat braces curly comprehension typed_comprehension macrocall ref typed_vcat import using export public"
)
is_func_call(t)
else
Expand All @@ -447,7 +448,7 @@ function is_named_iterable(x::FST)
end

function is_import_expr(x::FST)
return x.typ in (Import, Using, Export)
return x.typ in (Import, Using, Export, Public)
end

"""
Expand Down Expand Up @@ -567,7 +568,7 @@ function is_unary(x::JuliaSyntax.GreenNode)
if JuliaSyntax.is_unary_op(x)
return true
end
if kind(x) === K"call" || (JuliaSyntax.is_operator(x) && haschildren(x))
if kind(x) in KSet"call dotcall" || (JuliaSyntax.is_operator(x) && haschildren(x))
nops, nargs = _callinfo(x)
return nops == 1 && nargs == 1
end
Expand All @@ -583,7 +584,7 @@ function is_binary(x)
end

function is_chain(x::JuliaSyntax.GreenNode)
if !(kind(x) === K"call")
if !(kind(x) in KSet"call dotcall")
return false
end
nops, nargs = _callinfo(x)
Expand Down Expand Up @@ -630,7 +631,7 @@ function is_function_or_macro_def(cst::JuliaSyntax.GreenNode)
return false
end
k = kind(cst)
if (k == K"function" || k == K"macro")
if k in KSet"function macro"
return true
end

Expand All @@ -647,7 +648,7 @@ end

function is_function_like_lhs(node::JuliaSyntax.GreenNode)
k = kind(node)
if k == K"call"
if k in KSet"call dotcall"
return true
elseif k == K"where" || k == K"::"
return haschildren(node) && is_function_like_lhs(node[1])
Expand Down Expand Up @@ -709,7 +710,7 @@ function get_op(cst::JuliaSyntax.GreenNode)::Union{JuliaSyntax.GreenNode,Nothing
for c in children(cst)
if kind(cst) === K"dotcall" && kind(c) === K"."
continue
elseif JuliaSyntax.is_operator(c)
elseif JuliaSyntax.is_operator(c) && !haschildren(c)
return c
end
end
Expand Down
6 changes: 1 addition & 5 deletions src/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,8 @@ function prepend_return!(fst::FST, s::State)
if fst.typ !== Block || length(fst.nodes::Vector{FST}) == 0
return
end

ln = fst[end]
if is_block(ln)
return
end
if ln.typ in (Return, MacroCall, MacroBlock, MacroStr)
if is_block(ln) || ln.typ in (Return, MacroCall, MacroBlock, MacroStr)
return
end
if length(fst.nodes::Vector{FST}) > 2 &&
Expand Down
2 changes: 1 addition & 1 deletion src/styles/blue/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ getstyle(s::BlueStyle) = s.innerstyle isa NoopStyle ? s : s.innerstyle

function options(::BlueStyle)
return (;
always_use_return = true,
always_use_return = false,
short_to_long_function_def = true,
long_to_short_function_def = false,
whitespace_ops_in_indices = true,
Expand Down
13 changes: 12 additions & 1 deletion src/styles/default/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function nest!(
n_import!(style, fst, s, lineage)
elseif fst.typ === Export
n_export!(style, fst, s, lineage)
elseif fst.typ === Public
n_public!(style, fst, s, lineage)
elseif fst.typ === Using
n_using!(style, fst, s, lineage)
elseif fst.typ === Where
Expand Down Expand Up @@ -242,7 +244,7 @@ function n_do!(
return nested
end

# Import,Using,Export
# Import,Using,Export,Public
function n_using!(
ds::AbstractStyle,
fst::FST,
Expand Down Expand Up @@ -303,6 +305,15 @@ function n_export!(
n_using!(ds, fst, s, lineage)
end

function n_public!(
ds::AbstractStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
n_using!(ds, fst, s, lineage)
end

function n_import!(
ds::AbstractStyle,
fst::FST,
Expand Down
40 changes: 29 additions & 11 deletions src/styles/default/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ function pretty(
p_import(style, node, s, ctx, lineage)
elseif k === K"export"
p_export(style, node, s, ctx, lineage)
elseif k === K"public"
p_public(style, node, s, ctx, lineage)
elseif k === K"using"
p_using(style, node, s, ctx, lineage)
elseif k === K"importpath"
Expand Down Expand Up @@ -245,10 +247,10 @@ function p_comment(
loc = cursor_loc(s)
same_line = on_same_line(s, s.offset, s.offset + span(cst) - 1)
val = getsrcval(s.doc, s.offset:s.offset+span(cst)-1)
if same_line && startswith(val, "#=") && endswith(val, "=#")
s.offset += span(cst)
return FST(HASHEQCOMMENT, loc[2], loc[1], loc[1], val)
end
# if same_line && startswith(val, "#=") && endswith(val, "=#")
# s.offset += span(cst)
# return FST(HASHEQCOMMENT, loc[2], loc[1], loc[1], val)
# end
s.offset += span(cst)
FST(NONE, loc[2], loc[1], loc[1], "")
end
Expand Down Expand Up @@ -894,7 +896,7 @@ function p_functiondef(
end
add_node!(t, n, s; max_padding = s.opts.indent)
s.indent -= s.opts.indent
elseif kind(c) === K"call"
elseif is_func_call(c)
n = pretty(style, c, s, newctx(ctx; can_separate_kwargs = false), lineage)
add_node!(t, n, s; join_lines = true)
else
Expand Down Expand Up @@ -1734,8 +1736,8 @@ function p_binaryopcall(

is_short_form_function = defines_function(cst) && !ctx.from_let
op_dotted = kind(cst) === K"dotcall"
can_separate_kwargs = !is_function_or_macro_def(cst)
standalone_binary_circuit = ctx.standalone_binary_circuit
can_separate_kwargs = ctx.can_separate_kwargs && !is_function_or_macro_def(cst)

lazy_op = is_lazy_op(opkind)
# check if expression is a lazy circuit
Expand Down Expand Up @@ -1777,6 +1779,8 @@ function p_binaryopcall(
if opkind === K":"
nospace = true
from_colon = true
elseif opkind in KSet"::"
nospace = true
elseif opkind in KSet"in ∈ isa ."
nospace = false
elseif ctx.from_typedef && opkind in KSet"<: >:"
Expand All @@ -1787,7 +1791,7 @@ function p_binaryopcall(
nospace = true
has_ws = false
end
elseif ctx.from_ref
elseif ctx.from_ref || from_colon
if s.opts.whitespace_ops_in_indices
nospace = false
has_ws = true
Expand Down Expand Up @@ -1857,8 +1861,10 @@ function p_binaryopcall(
ns = is_dot ? 1 : nws

# Add whitespace before the operator, unless it's a dot in a dotted operator
if ns > 0
if i > 1 && !(kind(childs[i-1]) === K".") # Don't add space if previous was a dot
if ns > 0 && i > 1
if kind(childs[i-1]) !== K"." # Don't add space if previous was a dot
add_node!(t, Whitespace(ns), s)
elseif kind(childs[i-1]) === K"." && haschildren(childs[i-1]) # Don't add space if previous was a dot
add_node!(t, Whitespace(ns), s)
end
end
Expand Down Expand Up @@ -2597,14 +2603,14 @@ function p_import(
end

for a in children(cst)
if kind(a) in KSet"import export using"
if kind(a) in KSet"import export public using"
add_node!(t, pretty(style, a, s, ctx, lineage), s; join_lines = true)
add_node!(t, Whitespace(1), s)
elseif kind(a) === K":" && haschildren(a)
nodes = children(a)
for n in nodes
add_node!(t, pretty(style, n, s, ctx, lineage), s; join_lines = true)
if kind(n) in KSet"import export using"
if kind(n) in KSet"import export public using"
add_node!(t, Whitespace(1), s)
elseif kind(n) in KSet", :"
add_node!(t, Placeholder(1), s)
Expand Down Expand Up @@ -2632,6 +2638,18 @@ function p_export(
t
end

function p_public(
ds::AbstractStyle,
cst::JuliaSyntax.GreenNode,
s::State,
ctx::PrettyContext,
lineage::Vector{Tuple{JuliaSyntax.Kind,Bool,Bool}},
)
t = p_import(ds, cst, s, ctx, lineage)
t.typ = Public
t
end

function p_using(
ds::AbstractStyle,
cst::JuliaSyntax.GreenNode,
Expand Down
1 change: 1 addition & 0 deletions src/styles/sciml/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ for f in [
:n_import!,
:n_using!,
:n_export!,
:n_public!,
:n_vcat!,
:n_ncat!,
:n_typedvcat!,
Expand Down
1 change: 1 addition & 0 deletions src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ for f in [
:p_import,
:p_using,
:p_export,
:p_public,
:p_vcat,
:p_ncat,
:p_typedvcat,
Expand Down
8 changes: 8 additions & 0 deletions src/styles/yas/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ function n_export!(
)
n_using!(ys, fst, s, lineage)
end
function n_public!(
ys::YASStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
n_using!(ys, fst, s, lineage)
end
function n_import!(
ys::YASStyle,
fst::FST,
Expand Down
16 changes: 14 additions & 2 deletions src/styles/yas/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ function p_import(
end

for a in children(cst)
if kind(a) in KSet"import export using"
if kind(a) in KSet"import export using public"
add_node!(t, pretty(style, a, s, ctx, lineage), s; join_lines = true)
add_node!(t, Whitespace(1), s)
elseif kind(a) === K":" && haschildren(a)
nodes = children(a)
for n in nodes
add_node!(t, pretty(style, n, s, ctx, lineage), s; join_lines = true)
if kind(n) in KSet"import export using :"
if kind(n) in KSet"import export using public :"
add_node!(t, Whitespace(1), s)
elseif kind(n) in KSet","
add_node!(t, Placeholder(1), s)
Expand Down Expand Up @@ -116,6 +116,18 @@ function p_export(
t
end

function p_public(
ys::YASStyle,
cst::JuliaSyntax.GreenNode,
s::State,
ctx::PrettyContext,
lineage::Vector{Tuple{JuliaSyntax.Kind,Bool,Bool}},
)
t = p_import(ys, cst, s, ctx, lineage)
t.typ = Public
t
end

function p_curly(
ys::YASStyle,
cst::JuliaSyntax.GreenNode,
Expand Down
25 changes: 22 additions & 3 deletions test/default_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@
@test fmt("a :b") == "a:b"
@test fmt("a +1 :b -1") == "(a+1):(b-1)"

@test fmt("a::b:: c") == "a::b :: c"
@test fmt("a :: b::c") == "a :: b::c"
@test fmt("a :: b :: c") == "a :: b :: c"
@test fmt("a::b:: c") == "a::b::c"
@test fmt("a :: b::c") == "a::b::c"
@test fmt("a :: b :: c") == "a::b::c"
# issue 74
@test fmt("0:1/3:2") == "0:(1/3):2"
@test fmt("2a") == "2a"
Expand Down Expand Up @@ -4755,4 +4755,23 @@ some_function(
@test fmt(str_, 4, 16; join_lines_based_on_source = true) == str_
@test fmt(str_, 4, 15; join_lines_based_on_source = true) == str
end

if VERSION >= v"1.11.0"
@testset "public keyword support" begin
str_ = """
public a,b,
c
"""
str = """
public a, b, c
"""
@test fmt(str_, 4, 14) == str
str = """
public a,
b,
c
"""
@test fmt(str_, 4, 1) == str
end
end
end
17 changes: 15 additions & 2 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@
arraycopy_common(false#=fwd=#, LLVM.Builder(B), orig, origops[1], gutils)
return nothing
"""
@test fmt(s) == s_
@test_broken fmt(s) == s_

s1 = """
foo(a, b, #=c=#)
Expand All @@ -1397,7 +1397,7 @@
b,#=c=#
)
"""
@test fmt(s1, 4, 1) == s2
@test_broken fmt(s1, 4, 1) == s2
end

@testset "604" begin
Expand Down Expand Up @@ -1897,4 +1897,17 @@
@test f1 == str
end
end

@testset "880" begin
s1 = "constant_list[node_index.val:: UInt16]"
s2 = "constant_list[node_index.val::UInt16]"
@test s1 = fmt(s1, 4, 100, whitespace_ops_in_indices=true) == s2

s1 = "constant_list[node_index.val+ UInt16]"
s2 = "constant_list[node_index.val + UInt16]"
@test s1 = fmt(s1, 4, 100, whitespace_ops_in_indices=true) == s2

s = ".!purge"
@test s = fmt(s, 4, 100) == s
end
end
Loading
Loading