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

sciml: double indent for function and macro definitions #761

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JuliaFormatter"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
authors = ["Dominique Luna <[email protected]>"]
version = "1.0.37"
version = "1.0.38"

[deps]
CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
Expand Down
31 changes: 28 additions & 3 deletions src/styles/sciml/nest.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
for f in [
:n_call!,
:n_curly!,
:n_ref!,
:n_macrocall!,
:n_typedcomprehension!,
:n_tuple!,
:n_braces!,
:n_parameters!,
:n_invisbrackets!,
Expand All @@ -21,6 +19,8 @@ for f in [
:n_chainopcall!,
:n_comparison!,
:n_for!,
# :n_tuple!,
# :n_call!,
#:n_vect!
]
@eval function $f(ss::SciMLStyle, fst::FST, s::State)
Expand Down Expand Up @@ -48,7 +48,32 @@ function n_binaryopcall!(ss::SciMLStyle, fst::FST, s::State; indent::Int = -1)
return
end

start_line_offset = s.line_offset
walk(increment_line_offset!, (fst.nodes::Vector)[1:end-1], s, fst.indent)
nest!(style, fst[end], s)
end

function n_call!(ds::SciMLStyle, fst::FST, s::State)
style = getstyle(ds)
if s.opts.yas_style_nesting
n_call!(YASStyle(style), fst, s)
return
end
n_tuple!(ds, fst, s)
end

function n_tuple!(ds::SciMLStyle, fst::FST, s::State)
style = getstyle(ds)
if s.opts.yas_style_nesting
n_tuple!(YASStyle(style), fst, s)
return
end

n_tuple!(DefaultStyle(style), fst, s)
if fst.ref !== nothing &&
parent_is(fst.ref[], n -> is_function_or_macro_def(n) || n.head == :macrocall)
add_indent!(fst, s, s.opts.indent)
if is_closer(fst[end])
fst[end].indent -= s.opts.indent
end
end
end
60 changes: 56 additions & 4 deletions src/styles/sciml/pretty.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct SciMLStyle <: AbstractStyle
end
SciMLStyle() = SciMLStyle(NoopStyle())

function options(style::SciMLStyle)
function options(::SciMLStyle)
return (;
always_for_in = true,
always_use_return = false,
Expand Down Expand Up @@ -85,7 +85,11 @@ for f in [
for T in Ts
@eval function $f(ss::SciMLStyle, cst::$T, s::State; kwargs...)
style = getstyle(ss)
$f(YASStyle(style), cst, s; kwargs...)
if s.opts.yas_style_nesting
$f(YASStyle(style), cst, s)
else
$f(DefaultStyle(style), cst, s)
end
end
end
end
Expand All @@ -98,7 +102,6 @@ function p_begin(ss::SciMLStyle, cst::CSTParser.EXPR, s::State)
add_node!(t, Whitespace(1), s)
add_node!(t, pretty(style, cst[end], s), s, join_lines = true)
else
stmts_idxs = 2:length(cst)-1
s.indent += s.opts.indent
nodes = CSTParser.EXPR[]
for i in 2:length(cst)-1
Expand All @@ -116,7 +119,6 @@ function p_macrocall(ys::SciMLStyle, cst::CSTParser.EXPR, s::State)
t = FST(MacroCall, cst, nspaces(s))

args = get_args(cst)
nest = length(args) > 0 && !(length(args) == 1 && unnestable_node(args[1]))
has_closer = is_closer(cst[end])

!has_closer && (t.typ = MacroBlock)
Expand Down Expand Up @@ -207,3 +209,53 @@ function p_unaryopcall(ds::SciMLStyle, cst::CSTParser.EXPR, s::State; kwargs...)
end
t
end

# function p_functiondef(ds::SciMLStyle, cst::CSTParser.EXPR, s::State)
# style = getstyle(ds)
# t = FST(FunctionN, cst, nspaces(s))
# add_node!(t, pretty(style, cst[1], s), s)
# add_node!(t, Whitespace(1), s)
# fargs = pretty(style, cst[2], s)
# fargs.indent += s.opts.indent
# add_node!(t, fargs, s, join_lines = true)
# @info "" fargs.indent fargs.typ
# @info "" map(x -> x.typ, fargs)
# if length(cst) > 3
# if cst[3].fullspan == 0
# n = pretty(style, cst[4], s)
# if s.opts.join_lines_based_on_source
# join_lines = t.endline == n.startline
# join_lines && (add_node!(t, Whitespace(1), s))
# add_node!(t, n, s, join_lines = join_lines)
# else
# add_node!(t, Whitespace(1), s)
# add_node!(t, n, s, join_lines = true)
# end
# else
# s.indent += s.opts.indent
# n = pretty(style, cst[3], s, ignore_single_line = true)
# s.opts.always_use_return && prepend_return!(n, s)
# add_node!(t, n, s, max_padding = s.opts.indent)
# s.indent -= s.opts.indent
# add_node!(t, pretty(style, cst[4], s), s)
# end
# else
# # function stub `function foo end`
# n = pretty(style, cst[3], s)
# if s.opts.join_lines_based_on_source
# join_lines = t.endline == n.startline
# join_lines && (add_node!(t, Whitespace(1), s))
# add_node!(t, n, s, join_lines = join_lines)
# else
# add_node!(t, Whitespace(1), s)
# add_node!(t, n, s, join_lines = true)
# end
# end
# t
# end
#
# function p_macro(ds::SciMLStyle, cst::CSTParser.EXPR, s::State)
# t = p_functiondef(ds, cst, s)
# t.typ = Macro
# t
# end
65 changes: 52 additions & 13 deletions test/sciml_style.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
m.inv_match === nothing && throw(ArgumentError("Backwards matching not defined. `complete` the matching first."))
"""
formatted_str = raw"""
@noinline require_complete(m::Matching) = m.inv_match === nothing &&
throw(ArgumentError("Backwards matching not defined. `complete` the matching first."))
@noinline require_complete(m::Matching) = m.inv_match === nothing && throw(
ArgumentError("Backwards matching not defined. `complete` the matching first."),
)
"""
@test format_text(str, SciMLStyle()) == formatted_str

Expand Down Expand Up @@ -34,9 +35,11 @@
BipartiteGraph(fadj::AbstractVector, badj::Union{AbstractVector,Integer} = maximum(maximum, fadj); metadata = nothing) = BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata)
"""
formatted_str = raw"""
function BipartiteGraph(fadj::AbstractVector,
badj::Union{AbstractVector, Integer} = maximum(maximum, fadj);
metadata = nothing)
function BipartiteGraph(
fadj::AbstractVector,
badj::Union{AbstractVector, Integer} = maximum(maximum, fadj);
metadata = nothing,
)
BipartiteGraph(mapreduce(length, +, fadj; init = 0), fadj, badj, metadata)
end
"""
Expand Down Expand Up @@ -68,8 +71,8 @@

formatted_str = raw"""
function my_large_function(argument1, argument2,
argument3, argument4,
argument5, x, y, z)
argument3, argument4,
argument5, x, y, z)
foo(x) + goo(y)
end
"""
Expand Down Expand Up @@ -110,7 +113,8 @@
"""

formatted_str1 = raw"""
Dict{Int, Int}(1 => 2,
Dict{Int, Int}(
1 => 2,
3 => 4)
"""

Expand All @@ -131,7 +135,8 @@
"""

formatted_str1 = raw"""
SVector(1.0,
SVector(
1.0,
2.0)
"""

Expand All @@ -154,8 +159,10 @@
"""

formatted_str = raw"""
Dict{Int, Int}(1 => 2,
3 => 4)
Dict{Int, Int}(
1 => 2,
3 => 4,
)
"""

formatted_str_yas_nesting = raw"""
Expand All @@ -177,7 +184,8 @@

formatted_str1 = raw"""
SomeLongerTypeThanJustString = String
y = Dict{Int, SomeLongerTypeThanJustString}(1 => "some arbitrary string bla bla bla bla bla bla",
y = Dict{Int, SomeLongerTypeThanJustString}(
1 => "some arbitrary string bla bla bla bla bla bla",
2 => "another longer arbitrary string bla bla bla bla bla bla bla bla")
"""

Expand Down Expand Up @@ -236,7 +244,8 @@
"""

formatted_str1 = raw"""
Dict{Int, Int}(1 => 2,
Dict{Int, Int}(# Comment
1 => 2,
3 => 4)
"""

Expand Down Expand Up @@ -293,4 +302,34 @@
@test format_text(str, SciMLStyle(), variable_call_indent = ["test"]) == formatted_str
@test format_text(str, SciMLStyle(), variable_call_indent = ["SVector", "test"]) ==
formatted_str

@testset "741" begin
s = """
function get_num_majumps(smaj::SpatialMassActionJump{Nothing,B,S,U,V}) where {B,S,U,V}
return size(smaj.spatial_rates, 1)
end
"""
fs = """
function get_num_majumps(
smaj::SpatialMassActionJump{Nothing, B, S, U, V},
) where {B, S, U, V}
return size(smaj.spatial_rates, 1)
end
"""
@test format_text(s, SciMLStyle()) == fs

s = """
macro get_num_majumps(smaj::SpatialMassActionJump{Nothing,B,S,U,V}) where {B,S,U,V}
return size(smaj.spatial_rates, 1)
end
"""
fs = """
macro get_num_majumps(
smaj::SpatialMassActionJump{Nothing, B, S, U, V},
) where {B, S, U, V}
return size(smaj.spatial_rates, 1)
end
"""
@test format_text(s, SciMLStyle(), margin=89) == fs
end
end
Loading