Skip to content

Commit

Permalink
Fix ugly line breaks in curly braces outside of page margin when usin…
Browse files Browse the repository at this point in the history
…g `SciMLStyle` with `yas_style_nesting=true` (#785)

* Fix ugly linebreaks after curly braces outside margin

* Use default `p_curly` with `yas_style_nesting=false`

* Add test
  • Loading branch information
efaulhaber authored Nov 29, 2023
1 parent bc1c4e4 commit a4ec84f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/styles/yas/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ 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)
if CSTParser.is_comma(a) && i == length(cst) - 1
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,
Expand Down
20 changes: 19 additions & 1 deletion test/sciml_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

0 comments on commit a4ec84f

Please sign in to comment.