From 641a409f3c18b1c410b124d542780278ca3271f5 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sat, 19 Oct 2024 16:06:19 -0400 Subject: [PATCH 1/7] fixes kwargs issues and returns being added to do expressions - we're just not doing that completely anymore --- src/fst.jl | 2 +- src/passes.jl | 6 +----- src/styles/default/pretty.jl | 7 ++----- test/options.jl | 15 ++++++++++++++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/fst.jl b/src/fst.jl index 4ae86485..6db1a176 100644 --- a/src/fst.jl +++ b/src/fst.jl @@ -630,7 +630,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 diff --git a/src/passes.jl b/src/passes.jl index a20b5bf0..232c4046 100644 --- a/src/passes.jl +++ b/src/passes.jl @@ -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 && diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index fd171bd0..8119fbba 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -894,7 +894,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 @@ -1518,9 +1518,6 @@ function p_do( elseif kind(c) === K"block" s.indent += s.opts.indent n = pretty(style, c, s, newctx(ctx; ignore_single_line = true), lineage) - if s.opts.always_use_return - prepend_return!(n, s) - end add_node!(t, n, s; max_padding = s.opts.indent) s.indent -= s.opts.indent else @@ -1734,8 +1731,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 diff --git a/test/options.jl b/test/options.jl index 78b9e69a..51e0d97a 100644 --- a/test/options.jl +++ b/test/options.jl @@ -422,7 +422,7 @@ str = """ map(arg1, arg2) do x, y expr1 - return expr2 + expr2 end""" @test fmt(str_, 4, length(str_) - 1; always_use_return = true) == str @@ -2240,6 +2240,19 @@ @test yasfmt1(str; separate_kwargs_with_semicolon = true) == str @test yasfmt(str; separate_kwargs_with_semicolon = true) == str + str = """ + function g(x::T, y = 1) where {T} + return x + y + end + function g(x::T, y = 1)::Int where {T} + return x + y + end + """ + @test fmt1(str; separate_kwargs_with_semicolon = true) == str + @test fmt(str; separate_kwargs_with_semicolon = true) == str + @test yasfmt1(str; separate_kwargs_with_semicolon = true) == str + @test yasfmt(str; separate_kwargs_with_semicolon = true) == str + str_ = """ x = foo(var = "some really really really really really really really really really really long string") """ From 043014df33036210dae58f865eaea2bdc0b99298 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Thu, 24 Oct 2024 00:03:36 -0400 Subject: [PATCH 2/7] fix more issues --- src/fst.jl | 8 ++++---- src/styles/default/pretty.jl | 8 ++++++-- test/default_style.jl | 6 +++--- test/issues.jl | 13 +++++++++++++ test/options.jl | 4 ++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/fst.jl b/src/fst.jl index 6db1a176..1794f68a 100644 --- a/src/fst.jl +++ b/src/fst.jl @@ -567,7 +567,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 @@ -583,7 +583,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) @@ -647,7 +647,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]) @@ -709,7 +709,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 diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index 8119fbba..5d8ac2ea 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -1774,6 +1774,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"<: >:" @@ -1854,8 +1856,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 diff --git a/test/default_style.jl b/test/default_style.jl index faba7f50..3b787ec8 100644 --- a/test/default_style.jl +++ b/test/default_style.jl @@ -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" diff --git a/test/issues.jl b/test/issues.jl index 9e95fef4..8f1f208e 100644 --- a/test/issues.jl +++ b/test/issues.jl @@ -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 diff --git a/test/options.jl b/test/options.jl index 51e0d97a..e965c611 100644 --- a/test/options.jl +++ b/test/options.jl @@ -997,8 +997,8 @@ end""" str_ = """ struct Foo - a :: T - longfieldname :: B + a::T + longfieldname::B end""" @test fmt(str; align_struct_field = true) == str @test fmt(str; align_struct_field = false) == str_ From 086e308627185838f2565aa44d36b817bc3cdf03 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sun, 27 Oct 2024 15:07:50 -0400 Subject: [PATCH 3/7] fix bug related to the whitespace_ops_in_indices option --- src/styles/default/pretty.jl | 2 +- test/options.jl | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index 5d8ac2ea..e6c11707 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -1786,7 +1786,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 diff --git a/test/options.jl b/test/options.jl index e965c611..c9da0e2f 100644 --- a/test/options.jl +++ b/test/options.jl @@ -123,9 +123,12 @@ str = "arr[a in b]" @test fmt(str; m = 1, whitespace_ops_in_indices = true) == str + # In v1 str_ = "a:b+c:d-e" - str = "a:(b+c):(d-e)" + str = "a:(b + c):(d - e)" @test fmt(str_; m = 1, whitespace_ops_in_indices = true) == str + str = "a:(b+c):(d-e)" + @test fmt(str_; m = 1, whitespace_ops_in_indices = false) == str str_ = "s[m+i+1]" # issue 180 From 4e604ea9916209613a51a8d98e665976e008875f Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Nov 2024 21:48:26 -0500 Subject: [PATCH 4/7] revert multiline comments handling for now --- src/styles/default/pretty.jl | 8 ++++---- test/issues.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index e6c11707..807dcc88 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -245,10 +245,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 diff --git a/test/issues.jl b/test/issues.jl index 8f1f208e..36364854 100644 --- a/test/issues.jl +++ b/test/issues.jl @@ -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=#) @@ -1397,7 +1397,7 @@ b,#=c=# ) """ - @test fmt(s1, 4, 1) == s2 + @test_broken fmt(s1, 4, 1) == s2 end @testset "604" begin From 7579b51a282d45a49ef82822b10b5ef9105eb6a7 Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Thu, 28 Nov 2024 14:43:58 +0100 Subject: [PATCH 5/7] add some support for `public` keyword (#886) * add some support for public keyword * fix the kset memberships for `public` * add a simple test --- src/fst.jl | 5 +++-- src/styles/default/nest.jl | 13 ++++++++++++- src/styles/default/pretty.jl | 18 ++++++++++++++++-- src/styles/sciml/nest.jl | 1 + src/styles/sciml/pretty.jl | 1 + src/styles/yas/nest.jl | 8 ++++++++ src/styles/yas/pretty.jl | 16 ++++++++++++++-- test/default_style.jl | 17 +++++++++++++++++ 8 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/fst.jl b/src/fst.jl index 1794f68a..9e129cba 100644 --- a/src/fst.jl +++ b/src/fst.jl @@ -83,6 +83,7 @@ Const, Import, Export, + Public, Using, File, Quotenode, @@ -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 @@ -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 """ diff --git a/src/styles/default/nest.jl b/src/styles/default/nest.jl index aade7ede..a1fd78f3 100644 --- a/src/styles/default/nest.jl +++ b/src/styles/default/nest.jl @@ -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 @@ -242,7 +244,7 @@ function n_do!( return nested end -# Import,Using,Export +# Import,Using,Export,Public function n_using!( ds::AbstractStyle, fst::FST, @@ -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, diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index 807dcc88..ac628545 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -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" @@ -2598,14 +2600,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) @@ -2633,6 +2635,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, diff --git a/src/styles/sciml/nest.jl b/src/styles/sciml/nest.jl index f8c79535..7c406cf7 100644 --- a/src/styles/sciml/nest.jl +++ b/src/styles/sciml/nest.jl @@ -2,6 +2,7 @@ for f in [ :n_import!, :n_using!, :n_export!, + :n_public!, :n_vcat!, :n_ncat!, :n_typedvcat!, diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index e465de6e..3282c2b7 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -66,6 +66,7 @@ for f in [ :p_import, :p_using, :p_export, + :p_public, :p_vcat, :p_ncat, :p_typedvcat, diff --git a/src/styles/yas/nest.jl b/src/styles/yas/nest.jl index 1e65faad..0febaa75 100644 --- a/src/styles/yas/nest.jl +++ b/src/styles/yas/nest.jl @@ -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, diff --git a/src/styles/yas/pretty.jl b/src/styles/yas/pretty.jl index 734ce0af..55cd090c 100644 --- a/src/styles/yas/pretty.jl +++ b/src/styles/yas/pretty.jl @@ -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) @@ -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, diff --git a/test/default_style.jl b/test/default_style.jl index 3b787ec8..eaddc50a 100644 --- a/test/default_style.jl +++ b/test/default_style.jl @@ -4741,6 +4741,23 @@ some_function( @test fmt(str, 4, 27; join_lines_based_on_source = true) == str end + @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 + @testset "block automatically assume nested when join_lines_based_on_source" begin str_ = """ let y = a, z = b From 692e15c63976600b2514f5b37be1559cb365e8f0 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sun, 1 Dec 2024 11:05:37 -0500 Subject: [PATCH 6/7] run public keyword test on 1.11+ --- test/default_style.jl | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/test/default_style.jl b/test/default_style.jl index eaddc50a..82533846 100644 --- a/test/default_style.jl +++ b/test/default_style.jl @@ -4741,23 +4741,6 @@ some_function( @test fmt(str, 4, 27; join_lines_based_on_source = true) == str end - @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 - @testset "block automatically assume nested when join_lines_based_on_source" begin str_ = """ let y = a, z = b @@ -4772,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 From 168507d6a7e35ebc567bdff8f91a4fe506920433 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sun, 1 Dec 2024 14:48:05 -0500 Subject: [PATCH 7/7] add always return for do blocks with yas style --- src/styles/blue/pretty.jl | 2 +- src/styles/default/pretty.jl | 3 +++ test/options.jl | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/styles/blue/pretty.jl b/src/styles/blue/pretty.jl index fbec1b10..caf5cf86 100644 --- a/src/styles/blue/pretty.jl +++ b/src/styles/blue/pretty.jl @@ -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, diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl index ac628545..9d89715c 100644 --- a/src/styles/default/pretty.jl +++ b/src/styles/default/pretty.jl @@ -1520,6 +1520,9 @@ function p_do( elseif kind(c) === K"block" s.indent += s.opts.indent n = pretty(style, c, s, newctx(ctx; ignore_single_line = true), lineage) + if s.opts.always_use_return + prepend_return!(n, s) + end add_node!(t, n, s; max_padding = s.opts.indent) s.indent -= s.opts.indent else diff --git a/test/options.jl b/test/options.jl index c9da0e2f..eec4aa39 100644 --- a/test/options.jl +++ b/test/options.jl @@ -425,7 +425,7 @@ str = """ map(arg1, arg2) do x, y expr1 - expr2 + return expr2 end""" @test fmt(str_, 4, length(str_) - 1; always_use_return = true) == str