From 2de2039c43cecdcc7be76917a27c5a3f5ef17403 Mon Sep 17 00:00:00 2001 From: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:37:03 +0100 Subject: [PATCH 1/3] Fix ugly linebreaks after curly braces outside margin --- src/styles/sciml/pretty.jl | 3 ++- src/styles/yas/pretty.jl | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index ddd7c7524..6a5a96b8c 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -64,7 +64,8 @@ for f in [ :p_import, :p_using, :p_export, - :p_invisbrackets, #:p_curly, :p_braces, + :p_invisbrackets, + :p_curly, #:p_braces, :p_call, :p_tuple, :p_vcat, diff --git a/src/styles/yas/pretty.jl b/src/styles/yas/pretty.jl index fd0c391fa..71f6bcabe 100644 --- a/src/styles/yas/pretty.jl +++ b/src/styles/yas/pretty.jl @@ -85,6 +85,7 @@ end function p_curly(ys::YASStyle, cst::CSTParser.EXPR, s::State) style = getstyle(ys) + nws = s.opts.whitespace_typedefs ? 1 : 0 t = FST(Curly, cst, nspaces(s)) for (i, a) in enumerate(cst) n = pretty(style, a, s) @@ -92,7 +93,7 @@ function p_curly(ys::YASStyle, cst::CSTParser.EXPR, s::State) continue elseif CSTParser.is_comma(a) && i < length(cst) && !is_punc(cst[i+1]) add_node!(t, n, s, join_lines = true) - add_node!(t, Placeholder(0), s) + add_node!(t, Placeholder(nws), s) elseif is_closer(n) add_node!( t, From c5a4c7f7d4490265871e506af57a259e13bfbb84 Mon Sep 17 00:00:00 2001 From: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:43:33 +0100 Subject: [PATCH 2/3] Use default `p_curly` with `yas_style_nesting=false` --- src/styles/sciml/pretty.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl index 6a5a96b8c..0e8045713 100644 --- a/src/styles/sciml/pretty.jl +++ b/src/styles/sciml/pretty.jl @@ -64,8 +64,7 @@ for f in [ :p_import, :p_using, :p_export, - :p_invisbrackets, - :p_curly, #:p_braces, + :p_invisbrackets, #:p_curly, #:p_braces, :p_call, :p_tuple, :p_vcat, @@ -254,3 +253,12 @@ end function p_macro(ds::SciMLStyle, cst::CSTParser.EXPR, s::State) p_functiondef(ds, cst, s) end + +function p_curly(ss::SciMLStyle, cst::CSTParser.EXPR, s::State) + style = getstyle(ss) + if s.opts.yas_style_nesting + p_curly(YASStyle(style), cst, s) + else + p_curly(DefaultStyle(style), cst, s) + end +end From 892ebaac4571508494c8a9ec4ad3120694591239 Mon Sep 17 00:00:00 2001 From: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:47:26 +0100 Subject: [PATCH 3/3] Add test --- test/sciml_style.jl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/sciml_style.jl b/test/sciml_style.jl index 0d2f2d698..3fe09c604 100644 --- a/test/sciml_style.jl +++ b/test/sciml_style.jl @@ -297,7 +297,7 @@ str = """ function alg_cache(alg::FineRKN4, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, - dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, + dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits,tTypeNoUnits} reduced_rate_prototype = rate_prototype.x[2] @@ -355,4 +355,22 @@ useiszero, nocopy) end""" @test format_text(str, SciMLStyle()) == formatted_str + + str = """ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::SomePrettyLongTypeName{Foo} + """ + + # The line is too long, so a line break will be inserted in the curly braces + formatted_str = """ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::SomePrettyLongTypeName{ + Foo, + } + """ + + @test format_text(str, SciMLStyle()) == formatted_str + # With `yas_style_nesting=true`, we don't want the line break, as it will look like this: + # xxxxx::SomePrettyLongTypeName{ + # Foo + # } + @test format_text(str, SciMLStyle(), yas_style_nesting=true) == str end