Skip to content

Commit

Permalink
Fix vector nesting with SciMLStyle and yas_style_nesting = true (#…
Browse files Browse the repository at this point in the history
…807)

* Add test for vector formatting with `SciMLStyle` and `yas_style_nesting = true`

* Fix vector nesting

* Use best fit with tuples

* Revert "Use best fit with tuples"

This reverts commit 5ea87bb.

* Add more tests

* Fix best fit args with SciML style
  • Loading branch information
efaulhaber authored Feb 15, 2024
1 parent ec9a3b5 commit 50f8e60
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 46 deletions.
12 changes: 11 additions & 1 deletion src/styles/sciml/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ for f in [
:n_curly!,
:n_macrocall!,
:n_ref!,
:n_vect!,
# :n_vect!,
:n_braces!,
:n_parameters!,
:n_invisbrackets!,
Expand All @@ -200,6 +200,16 @@ for f in [
end
end

function n_vect!(ss::SciMLStyle, fst::FST, s::State)
style = getstyle(ss)
if s.opts.yas_style_nesting
# Allow a line break after the opening brackets without aligning
n_vect!(DefaultStyle(style), fst, s)
else
_n_tuple!(style, fst, s)
end
end

for f in [:n_chainopcall!, :n_comparison!, :n_for!]
@eval function $f(ss::SciMLStyle, fst::FST, s::State)
style = getstyle(ss)
Expand Down
2 changes: 1 addition & 1 deletion src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ for f in [
:p_curly,
:p_ref,
:p_braces,
:p_vect,
# :p_vect, don't use YAS style vector formatting with `yas_style_nesting = true`
:p_parameters,
:p_invisbrackets,
:p_bracescat,
Expand Down
180 changes: 136 additions & 44 deletions test/sciml_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,61 +430,153 @@
@test format_text(str_, SciMLStyle(), margin = 1) == str

@testset "optimal nesting" begin
str = """
function foo(arg1, arg2, arg3, arg4, arg5)
body
@testset "function definition" begin
str = """
function foo(arg1, arg2, arg3, arg4, arg5)
body
end
"""

fstr = """
function foo(
arg1, arg2, arg3, arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 41) == fstr
@test format_text(str, SciMLStyle(), margin = 37) == fstr

fstr = """
function foo(arg1, arg2, arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 36) == fstr
# should be 30? might be a unnesting off by 1 error
@test format_text(str, SciMLStyle(), margin = 31) == fstr

fstr = """
function foo(
arg1, arg2, arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 29) == fstr
@test format_text(str, SciMLStyle(), margin = 25) == fstr

fstr = """
function foo(
arg1, arg2,
arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 24) == fstr

fstr = """
function foo(
arg1,
arg2,
arg3,
arg4,
arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 18) == fstr
end
"""

fstr = """
function foo(
arg1, arg2, arg3, arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 41) == fstr
@test format_text(str, SciMLStyle(), margin = 37) == fstr

fstr = """
function foo(arg1, arg2, arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 36) == fstr
# should be 30? might be a unnesting off by 1 error
@test format_text(str, SciMLStyle(), margin = 31) == fstr

fstr = """
function foo(
@testset "vector definition" begin
str = """
test = [arg1, arg2, arg3, arg4, arg5]
"""

# Fits within the margin
@test format_text(str, SciMLStyle(), margin = 41) == str
@test format_text(str, SciMLStyle(), margin = 37) == str

fstr = """
test = [
arg1, arg2, arg3, arg4, arg5]
"""
@test format_text(str, SciMLStyle(), margin = 36) == fstr
@test format_text(str, SciMLStyle(), margin = 33) == fstr

fstr = """
test = [arg1, arg2, arg3,
arg4, arg5]
"""
@test format_text(str, SciMLStyle(), margin = 32) == fstr
# should be 25? might be a unnesting off by 1 error
@test format_text(str, SciMLStyle(), margin = 26) == fstr

fstr = """
test = [
arg1, arg2, arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 29) == fstr
@test format_text(str, SciMLStyle(), margin = 25) == fstr
arg4, arg5]
"""
@test format_text(str, SciMLStyle(), margin = 25) == fstr
@test format_text(str, SciMLStyle(), margin = 21) == fstr

fstr = """
function foo(
fstr = """
test = [arg1, arg2,
arg3,
arg4, arg5]
"""
@test format_text(str, SciMLStyle(), margin = 20) == fstr
# should be 19? might be a unnesting off by 1 error
# @test format_text(str, SciMLStyle(), margin = 19) == fstr

fstr = """
test = [
arg1, arg2,
arg3,
arg4, arg5)
body
end
"""
@test format_text(str, SciMLStyle(), margin = 24) == fstr
arg4, arg5]
"""
@test format_text(str, SciMLStyle(), margin = 19) == fstr
# should be 15? might be a unnesting off by 1 error
@test format_text(str, SciMLStyle(), margin = 16) == fstr

fstr = """
test = [
arg1, arg2,
arg3,
arg4,
arg5]
"""
@test format_text(str, SciMLStyle(), margin = 15) == fstr

fstr = """
function foo(
fstr = """
test = [arg1,
arg2,
arg3,
arg4,
arg5]
"""
@test format_text(str, SciMLStyle(), margin = 14) == fstr

fstr = """
test = [
arg1,
arg2,
arg3,
arg4,
arg5)
body
arg5]
"""
@test format_text(str, SciMLStyle(), margin = 13) == fstr
end
"""
@test format_text(str, SciMLStyle(), margin = 18) == fstr
end

str = raw"""
x = [
1, 2, 3
]
"""

# This should be valid with and without `yas_style_nesting`
@test format_text(str, SciMLStyle()) == str
@test format_text(str, SciMLStyle(), yas_style_nesting = true) == str
end

0 comments on commit 50f8e60

Please sign in to comment.