From 44a202bda87d60a6ebbc16fc35ad58634b88091e Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Sep 2023 20:27:40 -0400 Subject: [PATCH 1/8] sciml: double indent for function and macro definitions --- src/styles/sciml/pretty.jl | 51 +++++++++++++++++++++++++++++++++++--- test/sciml_style.jl | 4 +-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index cdd37a5b9..1bf6d2b62 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -18,7 +18,7 @@ struct SciMLStyle <: AbstractStyle end SciMLStyle() = SciMLStyle(NoopStyle()) -function options(style::SciMLStyle) +function options(::SciMLStyle) return (; always_for_in = true, always_use_return = false, @@ -98,7 +98,6 @@ function p_begin(ss::SciMLStyle, cst::CSTParser.EXPR, s::State) add_node!(t, Whitespace(1), s) add_node!(t, pretty(style, cst[end], s), s, join_lines = true) else - stmts_idxs = 2:length(cst)-1 s.indent += s.opts.indent nodes = CSTParser.EXPR[] for i in 2:length(cst)-1 @@ -116,7 +115,6 @@ function p_macrocall(ys::SciMLStyle, cst::CSTParser.EXPR, s::State) t = FST(MacroCall, cst, nspaces(s)) args = get_args(cst) - nest = length(args) > 0 && !(length(args) == 1 && unnestable_node(args[1])) has_closer = is_closer(cst[end]) !has_closer && (t.typ = MacroBlock) @@ -207,3 +205,50 @@ function p_unaryopcall(ds::SciMLStyle, cst::CSTParser.EXPR, s::State; kwargs...) end t end + +function p_functiondef(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) + style = getstyle(ds) + t = FST(FunctionN, cst, nspaces(s)) + add_node!(t, pretty(style, cst[1], s), s) + add_node!(t, Whitespace(1), s) + add_node!(t, pretty(style, cst[2], s), s, join_lines = true) + if length(cst) > 3 + if cst[3].fullspan == 0 + n = pretty(style, cst[4], s) + if s.opts.join_lines_based_on_source + join_lines = t.endline == n.startline + join_lines && (add_node!(t, Whitespace(1), s)) + add_node!(t, n, s, join_lines = join_lines) + else + add_node!(t, Whitespace(1), s) + add_node!(t, n, s, join_lines = true) + end + else + indent = s.opts.indent * 2 + s.indent += indent + n = pretty(style, cst[3], s, ignore_single_line = true) + s.opts.always_use_return && prepend_return!(n, s) + add_node!(t, n, s, max_padding = indent) + s.indent -= indent + add_node!(t, pretty(style, cst[4], s), s) + end + else + # function stub `function foo end` + n = pretty(style, cst[3], s) + if s.opts.join_lines_based_on_source + join_lines = t.endline == n.startline + join_lines && (add_node!(t, Whitespace(1), s)) + add_node!(t, n, s, join_lines = join_lines) + else + add_node!(t, Whitespace(1), s) + add_node!(t, n, s, join_lines = true) + end + end + t +end + +function p_macro(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) + t = p_functiondef(ds, cst, s) + t.typ = Macro + t +end diff --git a/test/sciml_style.jl b/test/sciml_style.jl index eb921b543..c4afe6932 100644 --- a/test/sciml_style.jl +++ b/test/sciml_style.jl @@ -37,7 +37,7 @@ function BipartiteGraph(fadj::AbstractVector, badj::Union{AbstractVector, Integer} = maximum(maximum, fadj); metadata = nothing) - BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) + BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) end """ @test format_text(str, SciMLStyle()) == formatted_str @@ -78,7 +78,7 @@ function my_large_function(argument1, argument2, argument3, argument4, argument5, x, y, z) - foo(x) + goo(y) + foo(x) + goo(y) end """ From dbb0b2de68d27c07e314ad12e102a44bfd584705 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Sep 2023 20:28:09 -0400 Subject: [PATCH 2/8] v --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 8d5ea8dff..33f107626 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "JuliaFormatter" uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899" authors = ["Dominique Luna "] -version = "1.0.37" +version = "1.0.38" [deps] CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f" From 1b33121fc81af26a4ff59cb881819993e153d534 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Sep 2023 20:34:31 -0400 Subject: [PATCH 3/8] switch yas to default style for indents --- src/styles/sciml/pretty.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index 1bf6d2b62..4ed5069cd 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -85,7 +85,11 @@ for f in [ for T in Ts @eval function $f(ss::SciMLStyle, cst::$T, s::State; kwargs...) style = getstyle(ss) - $f(YASStyle(style), cst, s; kwargs...) + if s.opts.yas_style_nesting + $f(YASStyle(style), cst, s) + else + $f(DefaultStyle(style), cst, s) + end end end end From ca3032ae871cd7437f86773d0a09956ce9e62057 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Sep 2023 20:49:56 -0400 Subject: [PATCH 4/8] rm unused --- src/styles/sciml/nest.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/styles/sciml/nest.jl b/src/styles/sciml/nest.jl index 9a1ad9fca..d55355813 100644 --- a/src/styles/sciml/nest.jl +++ b/src/styles/sciml/nest.jl @@ -48,7 +48,6 @@ function n_binaryopcall!(ss::SciMLStyle, fst::FST, s::State; indent::Int = -1) return end - start_line_offset = s.line_offset walk(increment_line_offset!, (fst.nodes::Vector)[1:end-1], s, fst.indent) nest!(style, fst[end], s) end From 9edea60c6d40551ebd22cf3f6a1fa0e724c764cf Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 27 Sep 2023 21:14:40 -0400 Subject: [PATCH 5/8] fix tests --- test/sciml_style.jl | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/test/sciml_style.jl b/test/sciml_style.jl index c4afe6932..73e13d239 100644 --- a/test/sciml_style.jl +++ b/test/sciml_style.jl @@ -4,8 +4,9 @@ m.inv_match === nothing && throw(ArgumentError("Backwards matching not defined. `complete` the matching first.")) """ formatted_str = raw""" - @noinline require_complete(m::Matching) = m.inv_match === nothing && - throw(ArgumentError("Backwards matching not defined. `complete` the matching first.")) + @noinline require_complete(m::Matching) = m.inv_match === nothing && throw( + ArgumentError("Backwards matching not defined. `complete` the matching first."), + ) """ @test format_text(str, SciMLStyle()) == formatted_str @@ -34,9 +35,11 @@ BipartiteGraph(fadj::AbstractVector, badj::Union{AbstractVector,Integer} = maximum(maximum, fadj); metadata = nothing) = BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) """ formatted_str = raw""" - function BipartiteGraph(fadj::AbstractVector, + function BipartiteGraph( + fadj::AbstractVector, badj::Union{AbstractVector, Integer} = maximum(maximum, fadj); - metadata = nothing) + metadata = nothing, + ) BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) end """ @@ -110,7 +113,8 @@ """ formatted_str1 = raw""" - Dict{Int, Int}(1 => 2, + Dict{Int, Int}( + 1 => 2, 3 => 4) """ @@ -131,7 +135,8 @@ """ formatted_str1 = raw""" - SVector(1.0, + SVector( + 1.0, 2.0) """ @@ -154,8 +159,10 @@ """ formatted_str = raw""" - Dict{Int, Int}(1 => 2, - 3 => 4) + Dict{Int, Int}( + 1 => 2, + 3 => 4, + ) """ formatted_str_yas_nesting = raw""" @@ -177,7 +184,8 @@ formatted_str1 = raw""" SomeLongerTypeThanJustString = String - y = Dict{Int, SomeLongerTypeThanJustString}(1 => "some arbitrary string bla bla bla bla bla bla", + y = Dict{Int, SomeLongerTypeThanJustString}( + 1 => "some arbitrary string bla bla bla bla bla bla", 2 => "another longer arbitrary string bla bla bla bla bla bla bla bla") """ @@ -236,7 +244,8 @@ """ formatted_str1 = raw""" - Dict{Int, Int}(1 => 2, + Dict{Int, Int}(# Comment + 1 => 2, 3 => 4) """ From e20cda3555c213d10e6ba47f51ee31f147745aa6 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sat, 30 Sep 2023 21:14:17 -0400 Subject: [PATCH 6/8] indent fargs --- src/styles/sciml/nest.jl | 15 +++++- src/styles/sciml/pretty.jl | 95 ++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/styles/sciml/nest.jl b/src/styles/sciml/nest.jl index d55355813..95b3a437f 100644 --- a/src/styles/sciml/nest.jl +++ b/src/styles/sciml/nest.jl @@ -1,10 +1,10 @@ for f in [ - :n_call!, + # :n_call!, :n_curly!, :n_ref!, :n_macrocall!, :n_typedcomprehension!, - :n_tuple!, + # :n_tuple!, :n_braces!, :n_parameters!, :n_invisbrackets!, @@ -51,3 +51,14 @@ function n_binaryopcall!(ss::SciMLStyle, fst::FST, s::State; indent::Int = -1) walk(increment_line_offset!, (fst.nodes::Vector)[1:end-1], s, fst.indent) nest!(style, fst[end], s) end + +n_call!(ds::SciMLStyle, fst::FST, s::State) = n_tuple!(ds, fst, s) +function n_tuple!(ds::SciMLStyle, fst::FST, s::State) + style = getstyle(ds) + n_tuple!(DefaultStyle(style), fst, s) + if fst.ref !== nothing && + parent_is(fst.ref[], n -> is_function_or_macro_def(n) || n.head == :macrocall) + add_indent!(fst, s, s.opts.indent) + end + return +end diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index 4ed5069cd..6fa58262a 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -210,49 +210,52 @@ function p_unaryopcall(ds::SciMLStyle, cst::CSTParser.EXPR, s::State; kwargs...) t end -function p_functiondef(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) - style = getstyle(ds) - t = FST(FunctionN, cst, nspaces(s)) - add_node!(t, pretty(style, cst[1], s), s) - add_node!(t, Whitespace(1), s) - add_node!(t, pretty(style, cst[2], s), s, join_lines = true) - if length(cst) > 3 - if cst[3].fullspan == 0 - n = pretty(style, cst[4], s) - if s.opts.join_lines_based_on_source - join_lines = t.endline == n.startline - join_lines && (add_node!(t, Whitespace(1), s)) - add_node!(t, n, s, join_lines = join_lines) - else - add_node!(t, Whitespace(1), s) - add_node!(t, n, s, join_lines = true) - end - else - indent = s.opts.indent * 2 - s.indent += indent - n = pretty(style, cst[3], s, ignore_single_line = true) - s.opts.always_use_return && prepend_return!(n, s) - add_node!(t, n, s, max_padding = indent) - s.indent -= indent - add_node!(t, pretty(style, cst[4], s), s) - end - else - # function stub `function foo end` - n = pretty(style, cst[3], s) - if s.opts.join_lines_based_on_source - join_lines = t.endline == n.startline - join_lines && (add_node!(t, Whitespace(1), s)) - add_node!(t, n, s, join_lines = join_lines) - else - add_node!(t, Whitespace(1), s) - add_node!(t, n, s, join_lines = true) - end - end - t -end - -function p_macro(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) - t = p_functiondef(ds, cst, s) - t.typ = Macro - t -end +# function p_functiondef(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) +# style = getstyle(ds) +# t = FST(FunctionN, cst, nspaces(s)) +# add_node!(t, pretty(style, cst[1], s), s) +# add_node!(t, Whitespace(1), s) +# fargs = pretty(style, cst[2], s) +# fargs.indent += s.opts.indent +# add_node!(t, fargs, s, join_lines = true) +# @info "" fargs.indent fargs.typ +# @info "" map(x -> x.typ, fargs) +# if length(cst) > 3 +# if cst[3].fullspan == 0 +# n = pretty(style, cst[4], s) +# if s.opts.join_lines_based_on_source +# join_lines = t.endline == n.startline +# join_lines && (add_node!(t, Whitespace(1), s)) +# add_node!(t, n, s, join_lines = join_lines) +# else +# add_node!(t, Whitespace(1), s) +# add_node!(t, n, s, join_lines = true) +# end +# else +# s.indent += s.opts.indent +# n = pretty(style, cst[3], s, ignore_single_line = true) +# s.opts.always_use_return && prepend_return!(n, s) +# add_node!(t, n, s, max_padding = s.opts.indent) +# s.indent -= s.opts.indent +# add_node!(t, pretty(style, cst[4], s), s) +# end +# else +# # function stub `function foo end` +# n = pretty(style, cst[3], s) +# if s.opts.join_lines_based_on_source +# join_lines = t.endline == n.startline +# join_lines && (add_node!(t, Whitespace(1), s)) +# add_node!(t, n, s, join_lines = join_lines) +# else +# add_node!(t, Whitespace(1), s) +# add_node!(t, n, s, join_lines = true) +# end +# end +# t +# end +# +# function p_macro(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) +# t = p_functiondef(ds, cst, s) +# t.typ = Macro +# t +# end From b00a0bad3194075141133c21474759b53445ce8b Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sat, 30 Sep 2023 21:14:25 -0400 Subject: [PATCH 7/8] double indent sciml --- src/styles/sciml/nest.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/styles/sciml/nest.jl b/src/styles/sciml/nest.jl index 95b3a437f..e5f26350a 100644 --- a/src/styles/sciml/nest.jl +++ b/src/styles/sciml/nest.jl @@ -59,6 +59,9 @@ function n_tuple!(ds::SciMLStyle, fst::FST, s::State) if fst.ref !== nothing && parent_is(fst.ref[], n -> is_function_or_macro_def(n) || n.head == :macrocall) add_indent!(fst, s, s.opts.indent) + if is_closer(fst[end]) + fst[end].indent -= s.opts.indent + end end return end From 3c849ed8ab73717f804bd3afd65b05d78eca66be Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Sat, 30 Sep 2023 21:30:46 -0400 Subject: [PATCH 8/8] ok --- src/styles/sciml/nest.jl | 30 +++++++++++++++++++-------- test/sciml_style.jl | 44 +++++++++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/styles/sciml/nest.jl b/src/styles/sciml/nest.jl index e5f26350a..87a5bb777 100644 --- a/src/styles/sciml/nest.jl +++ b/src/styles/sciml/nest.jl @@ -1,10 +1,8 @@ for f in [ - # :n_call!, :n_curly!, :n_ref!, :n_macrocall!, :n_typedcomprehension!, - # :n_tuple!, :n_braces!, :n_parameters!, :n_invisbrackets!, @@ -21,6 +19,8 @@ for f in [ :n_chainopcall!, :n_comparison!, :n_for!, + # :n_tuple!, + # :n_call!, #:n_vect! ] @eval function $f(ss::SciMLStyle, fst::FST, s::State) @@ -52,16 +52,28 @@ function n_binaryopcall!(ss::SciMLStyle, fst::FST, s::State; indent::Int = -1) nest!(style, fst[end], s) end -n_call!(ds::SciMLStyle, fst::FST, s::State) = n_tuple!(ds, fst, s) +function n_call!(ds::SciMLStyle, fst::FST, s::State) + style = getstyle(ds) + if s.opts.yas_style_nesting + n_call!(YASStyle(style), fst, s) + return + end + n_tuple!(ds, fst, s) +end + function n_tuple!(ds::SciMLStyle, fst::FST, s::State) style = getstyle(ds) + if s.opts.yas_style_nesting + n_tuple!(YASStyle(style), fst, s) + return + end + n_tuple!(DefaultStyle(style), fst, s) - if fst.ref !== nothing && - parent_is(fst.ref[], n -> is_function_or_macro_def(n) || n.head == :macrocall) - add_indent!(fst, s, s.opts.indent) - if is_closer(fst[end]) + if fst.ref !== nothing && + parent_is(fst.ref[], n -> is_function_or_macro_def(n) || n.head == :macrocall) + add_indent!(fst, s, s.opts.indent) + if is_closer(fst[end]) fst[end].indent -= s.opts.indent - end end - return + end end diff --git a/test/sciml_style.jl b/test/sciml_style.jl index 73e13d239..76e36cfe5 100644 --- a/test/sciml_style.jl +++ b/test/sciml_style.jl @@ -36,11 +36,11 @@ """ formatted_str = raw""" function BipartiteGraph( - fadj::AbstractVector, - badj::Union{AbstractVector, Integer} = maximum(maximum, fadj); - metadata = nothing, + fadj::AbstractVector, + badj::Union{AbstractVector, Integer} = maximum(maximum, fadj); + metadata = nothing, ) - BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) + BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata) end """ @test format_text(str, SciMLStyle()) == formatted_str @@ -71,8 +71,8 @@ formatted_str = raw""" function my_large_function(argument1, argument2, - argument3, argument4, - argument5, x, y, z) + argument3, argument4, + argument5, x, y, z) foo(x) + goo(y) end """ @@ -81,7 +81,7 @@ function my_large_function(argument1, argument2, argument3, argument4, argument5, x, y, z) - foo(x) + goo(y) + foo(x) + goo(y) end """ @@ -302,4 +302,34 @@ @test format_text(str, SciMLStyle(), variable_call_indent = ["test"]) == formatted_str @test format_text(str, SciMLStyle(), variable_call_indent = ["SVector", "test"]) == formatted_str + + @testset "741" begin + s = """ + function get_num_majumps(smaj::SpatialMassActionJump{Nothing,B,S,U,V}) where {B,S,U,V} + return size(smaj.spatial_rates, 1) + end + """ + fs = """ + function get_num_majumps( + smaj::SpatialMassActionJump{Nothing, B, S, U, V}, + ) where {B, S, U, V} + return size(smaj.spatial_rates, 1) + end + """ + @test format_text(s, SciMLStyle()) == fs + + s = """ + macro get_num_majumps(smaj::SpatialMassActionJump{Nothing,B,S,U,V}) where {B,S,U,V} + return size(smaj.spatial_rates, 1) + end + """ + fs = """ + macro get_num_majumps( + smaj::SpatialMassActionJump{Nothing, B, S, U, V}, + ) where {B, S, U, V} + return size(smaj.spatial_rates, 1) + end + """ + @test format_text(s, SciMLStyle(), margin=89) == fs + end end