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 #817 #818

Merged
merged 9 commits into from
Mar 2, 2024
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
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name = "JuliaFormatter"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
authors = ["Dominique Luna <dluna132@gmail.com>"]
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"
Expand All @@ -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"
Expand Down
1 change: 0 additions & 1 deletion src/JuliaFormatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 26 additions & 17 deletions src/nest_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

"""
Expand All @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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!
Expand Down
21 changes: 21 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading