Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ugly line breaks in curly braces outside of page margin when using SciMLStyle with yas_style_nesting=true #785

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading