diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 7d1ae4c1..cc69bbaf 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -2,3 +2,4 @@ remove_extra_newlines = true always_for_in = true conditional_to_if = true separate_kwargs_with_semicolon = true +short_to_long_function_def = true diff --git a/src/JuliaFormatter.jl b/src/JuliaFormatter.jl index 74f05b18..0ce57fde 100644 --- a/src/JuliaFormatter.jl +++ b/src/JuliaFormatter.jl @@ -347,8 +347,9 @@ See [Configuration File](@ref) for more details. Returns a boolean indicating whether the file was already formatted (`true`) or not (`false`). """ -format(paths; options...) = +function format(paths; options...) format(paths, Configuration(Dict{String,Any}(String(k) => v for (k, v) in options))) +end function format(paths, options::Configuration)::Bool already_formatted = true # Don't parallelize this, since there could be a race condition on a @@ -359,8 +360,9 @@ function format(paths, options::Configuration)::Bool return already_formatted end -format(path::AbstractString; options...) = +function format(path::AbstractString; options...) format(path, Configuration(Dict{String,Any}(String(k) => v for (k, v) in options))) +end function format(path::AbstractString, options::Configuration) path = realpath(path) if !get(options, "config_applied", false) diff --git a/src/fst.jl b/src/fst.jl index d3dace14..c27133cc 100644 --- a/src/fst.jl +++ b/src/fst.jl @@ -168,8 +168,9 @@ function show(io::IO, ::MIME"text/plain", fst::FST, indent::String = "") end end -FST(typ::FNode, indent::Int) = +function FST(typ::FNode, indent::Int) FST(typ, 0, 0, indent, 0, "", FST[], AllowNest, 0, -1, nothing) +end function FST( typ::FNode, @@ -223,22 +224,26 @@ cant_nest(fst::FST) = fst.nest_behavior === NeverNest can_nest(fst::FST) = fst.nest_behavior in (AllowNest, AllowNestButDontRemove) can_remove(fst::FST) = fst.nest_behavior !== AllowNestButDontRemove -Newline(; length = 0, nest_behavior = AllowNest) = +function Newline(; length = 0, nest_behavior = AllowNest) FST(NEWLINE, 0, 0, 0, length, "\n", (), nest_behavior, 0, -1, nothing) +end Semicolon() = FST(SEMICOLON, 0, 0, 0, 1, ";", (), AllowNest, 0, -1, nothing) TrailingComma() = FST(TRAILINGCOMMA, 0, 0, 0, 0, "", (), AllowNest, 0, -1, nothing) Whitespace(n) = FST(WHITESPACE, 0, 0, 0, n, " "^n, (), AllowNest, 0, -1, nothing) Placeholder(n) = FST(PLACEHOLDER, 0, 0, 0, n, " "^n, (), AllowNest, 0, -1, nothing) -Notcode(startline, endline) = +function Notcode(startline, endline) FST(NOTCODE, startline, endline, 0, 0, "", (), AllowNest, 0, -1, nothing) -InlineComment(line) = +end +function InlineComment(line) FST(INLINECOMMENT, line, line, 0, 0, "", (), AllowNest, 0, -1, nothing) +end is_leaf(cst::JuliaSyntax.GreenNode) = !haschildren(cst) is_leaf(fst::FST) = typeof(fst.nodes) === Tuple{} -is_punc(cst::JuliaSyntax.GreenNode) = +function is_punc(cst::JuliaSyntax.GreenNode) kind(cst) in KSet", ( ) [ ] { } @" || kind(cst) === K"." && !haschildren(cst) +end is_punc(fst::FST) = fst.typ === PUNCTUATION is_end(x::JuliaSyntax.GreenNode) = kind(x) === K"end" @@ -435,12 +440,14 @@ function length_to(fst::FST, ntyps; start::Int = 1) return len, false end -is_closer(fst::FST) = +function is_closer(fst::FST) fst.typ === PUNCTUATION && (fst.val == "}" || fst.val == ")" || fst.val == "]") +end is_closer(t::JuliaSyntax.GreenNode) = kind(t) in KSet"} ) ]" -is_opener(fst::FST) = +function is_opener(fst::FST) fst.typ === PUNCTUATION && (fst.val == "{" || fst.val == "(" || fst.val == "[") +end is_opener(t::JuliaSyntax.GreenNode) = kind(t) in KSet"{ ( [" function is_iterable(t::JuliaSyntax.GreenNode) @@ -898,11 +905,13 @@ function needs_placeholder( return false end -next_node_is(k::JuliaSyntax.Kind, nn::JuliaSyntax.GreenNode) = +function next_node_is(k::JuliaSyntax.Kind, nn::JuliaSyntax.GreenNode) kind(nn) === k || (haschildren(nn) && next_node_is(k, nn[1])) +end -next_node_is(f::Function, nn::JuliaSyntax.GreenNode) = +function next_node_is(f::Function, nn::JuliaSyntax.GreenNode) f(nn) || (haschildren(nn) && next_node_is(f, nn[1])) +end """ add_node!( diff --git a/src/styles/blue/nest.jl b/src/styles/blue/nest.jl index 0293bb17..678f7386 100644 --- a/src/styles/blue/nest.jl +++ b/src/styles/blue/nest.jl @@ -127,66 +127,86 @@ function n_tuple!( end return nested end -n_call!( +function n_call!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_curly!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_curly!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_macrocall!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_macrocall!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_ref!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_ref!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_braces!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_braces!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_vect!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_vect!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_parameters!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_parameters!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_invisbrackets!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_invisbrackets!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_bracescat!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_bracescat!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) -n_cartesian_iterator!( +) + n_tuple!(bs, fst, s, lineage) +end +function n_cartesian_iterator!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(bs, fst, s, lineage) +) + n_tuple!(bs, fst, s, lineage) +end function n_conditionalopcall!( bs::BlueStyle, @@ -237,9 +257,11 @@ function n_chainopcall!( end end -n_comparison!( +function n_comparison!( bs::BlueStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_chainopcall!(bs, fst, s, lineage) +) + n_chainopcall!(bs, fst, s, lineage) +end diff --git a/src/styles/default/nest.jl b/src/styles/default/nest.jl index c1663ead..1dc366b0 100644 --- a/src/styles/default/nest.jl +++ b/src/styles/default/nest.jl @@ -173,12 +173,14 @@ function n_functiondef!( nest!(ds, fst.nodes::Vector, s, fst.indent, lineage; extra_margin = fst.extra_margin) end -n_macro!( +function n_macro!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_functiondef!(ds, fst, s, lineage) +) + n_functiondef!(ds, fst, s, lineage) +end function n_string!( ds::AbstractStyle, @@ -286,19 +288,23 @@ function n_using!( return nested end -n_export!( +function n_export!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_using!(ds, fst, s, lineage) +) + n_using!(ds, fst, s, lineage) +end -n_import!( +function n_import!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_using!(ds, fst, s, lineage) +) + n_using!(ds, fst, s, lineage) +end function n_tuple!( ds::AbstractStyle, @@ -381,96 +387,122 @@ function n_tuple!( return nested end -n_cartesian_iterator!( +function n_cartesian_iterator!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_vect!( +function n_vect!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_vcat!( +function n_vcat!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_braces!( +function n_braces!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_bracescat!( +function n_bracescat!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_parameters!( +function n_parameters!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_invisbrackets!( +function n_invisbrackets!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_call!( +function n_call!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_curly!( +function n_curly!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_macrocall!( +function n_macrocall!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_ref!( +function n_ref!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_row!( +function n_row!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end -n_typedvcat!( +function n_typedvcat!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ds, fst, s, lineage) +) + n_tuple!(ds, fst, s, lineage) +end function n_comprehension!( ds::AbstractStyle, @@ -539,12 +571,14 @@ function _n_comprehension!( return nested end -n_typedcomprehension!( +function n_typedcomprehension!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_comprehension!(ds, fst, s, lineage) +) + n_comprehension!(ds, fst, s, lineage) +end function n_generator!( ds::AbstractStyle, @@ -632,12 +666,14 @@ function n_generator!( return nested end -n_filter!( +function n_filter!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_generator!(ds, fst, s, lineage) +) + n_generator!(ds, fst, s, lineage) +end function n_whereopcall!( ds::AbstractStyle, @@ -967,12 +1003,14 @@ function n_for!( return nested end -n_let!( +function n_let!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_for!(ds, fst, s, lineage) +) + n_for!(ds, fst, s, lineage) +end function n_block!( ds::AbstractStyle, @@ -1075,16 +1113,20 @@ function n_block!( return nested end -n_comparison!( +function n_comparison!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_block!(ds, fst, s, lineage; indent = s.line_offset) +) + n_block!(ds, fst, s, lineage; indent = s.line_offset) +end -n_chainopcall!( +function n_chainopcall!( ds::AbstractStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_block!(ds, fst, s, lineage; indent = s.line_offset) +) + n_block!(ds, fst, s, lineage; indent = s.line_offset) +end diff --git a/src/styles/yas/nest.jl b/src/styles/yas/nest.jl index be6e0094..e3fab383 100644 --- a/src/styles/yas/nest.jl +++ b/src/styles/yas/nest.jl @@ -70,36 +70,46 @@ function n_call!( return nested end -n_curly!( +function n_curly!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_call!(ys, fst, s, lineage) -n_ref!( +) + n_call!(ys, fst, s, lineage) +end +function n_ref!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_call!(ys, fst, s, lineage) -n_macrocall!( +) + n_call!(ys, fst, s, lineage) +end +function n_macrocall!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_call!(ys, fst, s, lineage) -n_typedcomprehension!( +) + n_call!(ys, fst, s, lineage) +end +function n_typedcomprehension!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_call!(ys, fst, s, lineage) -n_typedvcat!( +) + n_call!(ys, fst, s, lineage) +end +function n_typedvcat!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_call!(ys, fst, s, lineage) +) + n_call!(ys, fst, s, lineage) +end function n_tuple!( ys::YASStyle, @@ -139,54 +149,70 @@ function n_tuple!( end return nested end -n_braces!( +function n_braces!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_vect!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_vect!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_parameters!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_parameters!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_invisbrackets!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_invisbrackets!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_comprehension!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_comprehension!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_vcat!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_vcat!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_bracescat!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_bracescat!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) -n_cartesian_iterator!( +) + n_tuple!(ys, fst, s, lineage) +end +function n_cartesian_iterator!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_tuple!(ys, fst, s, lineage) +) + n_tuple!(ys, fst, s, lineage) +end function n_generator!( ys::YASStyle, @@ -221,12 +247,14 @@ function n_generator!( end return nested end -n_filter!( +function n_filter!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_generator!(ys, fst, s, lineage) +) + n_generator!(ys, fst, s, lineage) +end function n_whereopcall!( ys::YASStyle, @@ -290,18 +318,22 @@ function n_using!( end return nested end -n_export!( +function n_export!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_using!(ys, fst, s, lineage) -n_import!( +) + n_using!(ys, fst, s, lineage) +end +function n_import!( ys::YASStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}, -) = n_using!(ys, fst, s, lineage) +) + n_using!(ys, fst, s, lineage) +end function n_chainopcall!( ys::YASStyle, diff --git a/test/runtests.jl b/test/runtests.jl index 5e43c44f..a3bd6292 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,8 +3,9 @@ using JuliaFormatter: DefaultStyle, YASStyle, Options, options, CONFIG_FILE_NAME using Test using JuliaSyntax -fmt1(s; i = 4, m = 80, kwargs...) = +function fmt1(s; i = 4, m = 80, kwargs...) JuliaFormatter.format_text(s; kwargs..., indent = i, margin = m) +end fmt1(s, i, m; kwargs...) = fmt1(s; kwargs..., i = i, m = m) # Verifies formatting the formatted text @@ -16,20 +17,24 @@ function fmt(s; i = 4, m = 80, kwargs...) end fmt(s, i, m; kwargs...) = fmt(s; kwargs..., i = i, m = m) -yasfmt1(str; kwargs...) = +function yasfmt1(str; kwargs...) fmt1(str; style = YASStyle(), options(DefaultStyle())..., kwargs...) -yasfmt(str; i = 4, m = 80, kwargs...) = +end +function yasfmt(str; i = 4, m = 80, kwargs...) fmt(str; i = i, m = m, style = YASStyle(), kwargs...) +end yasfmt(str, i::Int, m::Int; kwargs...) = yasfmt(str; i = i, m = m, kwargs...) bluefmt1(str) = fmt1(str; style = BlueStyle(), options(DefaultStyle())...) -bluefmt(str; i = 4, m = 80, kwargs...) = +function bluefmt(str; i = 4, m = 80, kwargs...) fmt(str; i = i, m = m, style = BlueStyle(), kwargs...) +end bluefmt(str, i::Int, m::Int; kwargs...) = bluefmt(str; i = i, m = m, kwargs...) minimalfmt1(str) = fmt1(str; style = MinimalStyle(), options(DefaultStyle())...) -minimalfmt(str; i = 4, m = 92, kwargs...) = +function minimalfmt(str; i = 4, m = 92, kwargs...) fmt(str; i = i, m = m, style = MinimalStyle(), kwargs...) +end minimalfmt(str, i::Int, m::Int; kwargs...) = minimalfmt(str; i = i, m = m, kwargs...) function run_pretty(text::String; style = DefaultStyle(), opts = Options())