Repository: domluna/JuliaFormatter.jl
Pull request #785 (2023-11-28 12:00:09)

Fix ugly line breaks in curly braces outside of page margin when using `SciMLStyle` with `yas_style_nesting=true`
On master, this
```julia
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::SomePrettyLongTypeName{Foo}
```
will be reformatted to this:
```julia
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx::SomePrettyLongTypeName{
                                                                                            Foo
                                                                                            }
```

I modified `p_curly` of `YASStyle` to not always insert a `Placeholder(0)` but add a `Placeholder(1)` when `whitespace_typedefs=true`. This is identical to what is done in `DefaultStyle`. With this change, I can use `p_curly` of `YASStyle` for `SciMLStyle` as well when `yas_style_nesting=true`.

Here is a demonstration of what happens in a large project when reformatted with this PR:
https://github.com/trixi-framework/Trixi.jl/pull/1759

IMHO, many things get prettier, and everything else gets, well, at least not uglier `¯\_(ツ)_/¯`
The only thing that gets slightly worse IMO is the file `src/solvers/dgmulti/sbp.jl`, but that wasn't pretty to begin with.

domluna: this looks good to me. seems ready to merge anything else you want to add?
efaulhaber: Ready to merge from my side.

Patch:
diff --git a/src/styles/sciml/pretty.jl b/src/styles/sciml/pretty.jl
index ddd7c752..0e804571 100644
--- a/src/styles/sciml/pretty.jl
+++ b/src/styles/sciml/pretty.jl
@@ -64,7 +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,
@@ -253,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
diff --git a/src/styles/yas/pretty.jl b/src/styles/yas/pretty.jl
index fd0c391f..71f6bcab 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,


Test patch:
diff --git a/test/sciml_style.jl b/test/sciml_style.jl
index 0d2f2d69..3fe09c60 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