Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
domluna committed Feb 15, 2024
1 parent 0ccf6b9 commit de690b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
13 changes: 5 additions & 8 deletions src/nest_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Finds the optimal placeholders to turn into a newlines such that the length of t
"""
function find_optimal_nest_placeholders(
fst::FST,
start_line_offset::Int,
indent_offset::Int,
max_margin::Int,
)::Vector{Int}
placeholder_inds = findall(n -> n.typ === PLACEHOLDER, fst.nodes)
Expand Down Expand Up @@ -246,7 +246,7 @@ function find_optimal_nest_placeholders(
optinds = find_optimal_nest_placeholders(
fst,
g,
start_line_offset,
indent_offset,
max_margin,
last_group = i == length(placeholder_groups),
)
Expand All @@ -260,7 +260,7 @@ end
function find_optimal_nest_placeholders(
fst::FST,
placeholder_inds::Vector{Int},
initial_offset::Int,
indent_offset::Int,
max_margin::Int;
last_group::Bool = false,
)::Vector{Int}
Expand All @@ -285,8 +285,6 @@ function find_optimal_nest_placeholders(
end
end

# @info "" dp placeholder_inds

N = size(dp, 1)

function find_best_segments(s::Int)
Expand Down Expand Up @@ -325,17 +323,16 @@ function find_optimal_nest_placeholders(
return best_split
end

fst_line_offset = fst.indent
# Calculate best splits for each number of segments s
segments = Tuple{Int,Int}[]
for s in 1:N
segments = find_best_segments(s)
fits = true
for (i, s) in enumerate(segments)
if i == 1
fits &= fst_line_offset + dp[first(s), last(s)] <= max_margin
fits &= indent_offset + dp[first(s), last(s)] <= max_margin
else
fits &= fst_line_offset + dp[first(s), last(s)] <= max_margin
fits &= indent_offset + dp[first(s), last(s)] <= max_margin
end
end
fits && break
Expand Down
3 changes: 1 addition & 2 deletions src/styles/sciml/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function _n_tuple!(ss::SciMLStyle, fst::FST, s::State)
false
end

optimal_placeholders =
find_optimal_nest_placeholders(fst, start_line_offset, s.opts.margin)
optimal_placeholders = find_optimal_nest_placeholders(fst, fst.indent, s.opts.margin)

for i in optimal_placeholders
fst[i] = Newline(length = fst[i].len)
Expand Down
7 changes: 7 additions & 0 deletions src/styles/yas/nest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function n_call!(ys::YASStyle, fst::FST, s::State)

f = n -> n.typ === PLACEHOLDER || n.typ === NEWLINE

indent_offset = s.line_offset + sum(length.(fst[1:2]))
optimal_placeholders = find_optimal_nest_placeholders(fst, indent_offset, s.opts.margin)

for i in optimal_placeholders
fst[i] = Newline(length = fst[i].len)
end

nodes = fst.nodes::Vector
for (i, n) in enumerate(nodes)
if i == 3
Expand Down

0 comments on commit de690b9

Please sign in to comment.