diff --git a/Project.toml b/Project.toml index 658c940ea..655e8fdcb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,11 +1,10 @@ name = "JuliaFormatter" uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899" authors = ["Dominique Luna "] -version = "1.0.50" +version = "1.0.51" [deps] CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f" -Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" CommonMark = "a80b9123-70ca-4bc0-993e-6e3bcb318db6" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" Glob = "c27321d9-0574-5035-807b-f59d2c89b15c" @@ -15,7 +14,6 @@ Tokenize = "0796e94c-ce3b-5d07-9a54-7f471281c624" [compat] CSTParser = "^3.4.0" -Combinatorics = "1" CommonMark = "0.5, 0.6, 0.7, 0.8" DataStructures = "0.17, 0.18" Glob = "1.3" diff --git a/src/JuliaFormatter.jl b/src/JuliaFormatter.jl index a166ae508..97dd6c49d 100644 --- a/src/JuliaFormatter.jl +++ b/src/JuliaFormatter.jl @@ -11,7 +11,6 @@ using Pkg.TOML: parsefile using Glob import CommonMark: block_modifier import Base: get, pairs -using Combinatorics: permutations using CommonMark: AdmonitionRule, CodeBlock, diff --git a/src/nest_utils.jl b/src/nest_utils.jl index dc458b879..fd07feb82 100644 --- a/src/nest_utils.jl +++ b/src/nest_utils.jl @@ -175,13 +175,15 @@ function nest_if_over_margin!( return false end -function find_all_segment_splits(n::Int, k::Int) +# TOOD: further improve the runtime of this function +function find_all_segment_splits(dp::Matrix{Int}, k::Int, max_margin::Int) res = Vector{Int}[] + n = size(dp, 1) if n == k - return [fill(1, k)] + return Int[fill(1, k)] elseif k == 1 - return [[n]] + return Int[[n]] end function _backtrack(t::Vector{Int}, current_sum::Int) @@ -190,25 +192,34 @@ function find_all_segment_splits(n::Int, k::Int) push!(res, t) end return + elseif current_sum >= n + return end - start_val = isempty(t) ? 1 : last(t) - max_val = n - current_sum - (k - length(t) - 1) - - for i in start_val:min(n, max_val) + for i in 1:(n-k+1) + if current_sum + i > n + break + end + if dp[current_sum+1, current_sum+i] > max_margin + break + end _backtrack([t; i], current_sum + i) end end - _backtrack(Int[], 0) - all_splits = Vector{Int}[] - for r in res - for c in unique(permutations(r)) - push!(all_splits, c) + for i in 1:(n-k+1) + cm = dp[1, i] + if cm > max_margin + break end + _backtrack([i], i) end - return all_splits + if length(res) == 0 + return [[n]] + end + + return res end """ @@ -220,7 +231,7 @@ function find_optimal_nest_placeholders( max_margin::Int, )::Vector{Int} placeholder_inds = findall(n -> n.typ === PLACEHOLDER, fst.nodes) - if length(placeholder_inds) <= 1 + if length(placeholder_inds) <= 1 || length(placeholder_inds) >= 30 return placeholder_inds end newline_inds = findall(n -> n.typ === NEWLINE, fst.nodes) @@ -239,8 +250,6 @@ function find_optimal_nest_placeholders( end push!(placeholder_groups, current_group) - # @info "groups" placeholder_groups - optimal_placeholders = Int[] for (i, g) in enumerate(placeholder_groups) optinds = find_optimal_nest_placeholders( @@ -307,7 +316,7 @@ function find_optimal_nest_placeholders( ranges end - all_splits = find_all_segment_splits(N, s) + all_splits = find_all_segment_splits(dp, s, max_margin) best_split = UnitRange{Int}[] min_diff = 1_000_000 # big number! diff --git a/test/issues.jl b/test/issues.jl index e82832247..a5eb6872f 100644 --- a/test/issues.jl +++ b/test/issues.jl @@ -1793,4 +1793,25 @@ """ @test format_text(s, SciMLStyle()) == s end + + @testset "817" begin + s = raw""" + a = ["Unknown" => SubRegion.Unknown, "Northern Europe" => SubRegion.Northern_Europe, "Southern Asia" => SubRegion.Southern_Asia, "Western Europe" => SubRegion.Western_Europe, "Sub-Saharan Africa" => SubRegion.Sub_Saharan_Africa, "Western Asia" => SubRegion.Western_Asia, "Eastern Asia" => SubRegion.Eastern_Asia, "Northern America" => SubRegion.Northern_America, "South-eastern Asia" => SubRegion.South_eastern_Asia, "Australia and New Zealand" => SubRegion.Australia_and_New_Zealand, "Eastern Europe" => SubRegion.Eastern_Europe, "Latin America and the Caribbean" => SubRegion.Latin_America_and_the_Caribbean, "Southern Europe" => SubRegion.Southern_Europe, "Central Asia" => SubRegion.Central_Asia] + """ + s2 = raw""" + a = ["Unknown" => SubRegion.Unknown, "Northern Europe" => SubRegion.Northern_Europe, + "Southern Asia" => SubRegion.Southern_Asia, + "Western Europe" => SubRegion.Western_Europe, + "Sub-Saharan Africa" => SubRegion.Sub_Saharan_Africa, + "Western Asia" => SubRegion.Western_Asia, "Eastern Asia" => SubRegion.Eastern_Asia, + "Northern America" => SubRegion.Northern_America, + "South-eastern Asia" => SubRegion.South_eastern_Asia, + "Australia and New Zealand" => SubRegion.Australia_and_New_Zealand, + "Eastern Europe" => SubRegion.Eastern_Europe, + "Latin America and the Caribbean" => SubRegion.Latin_America_and_the_Caribbean, + "Southern Europe" => SubRegion.Southern_Europe, + "Central Asia" => SubRegion.Central_Asia] + """ + @test format_text(s, SciMLStyle()) == s2 + end end