Best fit arguments with YAS style#809
Conversation
src/styles/yas/nest.jl
Outdated
| has_closer = is_closer(fst[end]) | ||
| start_line_offset = s.line_offset | ||
|
|
||
| optimal_placeholders = | ||
| find_optimal_nest_placeholders(fst, start_line_offset, s.opts.margin) | ||
|
|
||
| for i in optimal_placeholders | ||
| fst[i] = Newline(length = fst[i].len) | ||
| end | ||
|
|
||
| # placeholder_inds = findall(n -> n.typ === PLACEHOLDER, fst.nodes) | ||
| # for (i, ph) in enumerate(placeholder_inds) | ||
| # if i == 1 || | ||
| # i == length(placeholder_inds) || | ||
| # (ph < length(fst) && is_comment(fst[ph+1])) || | ||
| # (ph > 1 && is_comment(fst[ph-1])) | ||
| # continue | ||
| # end | ||
| # fst[ph] = Whitespace(fst[ph].len) | ||
| # end | ||
|
|
||
| # if has_closer && length(placeholder_inds) > 0 | ||
| # fst[placeholder_inds[end]] = Whitespace(10) | ||
| # end | ||
| @info "" fst.nodes | ||
| @info "" optimal_placeholders |
There was a problem hiding this comment.
For some reason, this doesn't find any optimal placeholders:
julia> print(format_text(str, YASStyle(), margin = 36))
┌ Info:
│ fst.nodes =
│ 16-element Vector{JuliaFormatter.FST}:
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 3, "foo", nothing, nothing, JuliaFormatter.AllowNest, 0, 10, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, "(", nothing, nothing, JuliaFormatter.AllowNest, 0, 13, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg1", nothing, nothing, JuliaFormatter.AllowNest, 0, 14, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 18, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg2", nothing, nothing, JuliaFormatter.AllowNest, 0, 20, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 24, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg3", nothing, nothing, JuliaFormatter.AllowNest, 0, 26, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 30, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg4", nothing, nothing, JuliaFormatter.AllowNest, 0, 32, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 36, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg5", nothing, nothing, JuliaFormatter.AllowNest, 0, 38, nothing)
└ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ")", nothing, nothing, JuliaFormatter.AllowNest, 0, 42, nothing)
┌ Info:
└ optimal_placeholders = Int64[]
function foo(arg1, arg2, arg3, arg4,
arg5)
return body
endWhen I run the same with SciML style, I get
┌ Info:
│ fst.nodes =
│ 18-element Vector{JuliaFormatter.FST}:
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 3, "foo", nothing, nothing, JuliaFormatter.AllowNest, 0, 10, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, "(", nothing, nothing, JuliaFormatter.AllowNest, 0, 13, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 0, "", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 4, "arg1", nothing, nothing, JuliaFormatter.AllowNest, 0, 14, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 18, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 4, "arg2", nothing, nothing, JuliaFormatter.AllowNest, 0, 20, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 24, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 4, "arg3", nothing, nothing, JuliaFormatter.AllowNest, 0, 26, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 30, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 4, "arg4", nothing, nothing, JuliaFormatter.AllowNest, 0, 32, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 36, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 4, 4, "arg5", nothing, nothing, JuliaFormatter.AllowNest, 0, 38, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 4, 0, "", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
└ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 4, 1, ")", nothing, nothing, JuliaFormatter.AllowNest, 0, 42, nothing)
┌ Info:
│ optimal_placeholders =
│ 1-element Vector{Int64}:
└ 12There are only two extra placeholders with SciML style. One after the opening parenthesis, one before the closing parenthesis. @domluna Why does it find an optimal placeholder there after arg3, but not with the FST above without the two extra placeholders?
I tried looking into find_optimal_nest_placeholders, but it's too complicated.
There was a problem hiding this comment.
There was a problem hiding this comment.
It now works for margin 36,
julia> print(format_text(str, YASStyle(), margin = 36))
┌ Info:
│ fst.nodes =
│ 16-element Vector{JuliaFormatter.FST}:
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 3, "foo", nothing, nothing, JuliaFormatter.AllowNest, 0, 10, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, "(", nothing, nothing, JuliaFormatter.AllowNest, 0, 13, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg1", nothing, nothing, JuliaFormatter.AllowNest, 0, 14, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 18, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg2", nothing, nothing, JuliaFormatter.AllowNest, 0, 20, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 24, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg3", nothing, nothing, JuliaFormatter.AllowNest, 0, 26, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 30, nothing)
│ JuliaFormatter.FST(JuliaFormatter.NEWLINE, -1, -1, 0, 1, "\n", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg4", nothing, nothing, JuliaFormatter.AllowNest, 0, 32, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 36, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg5", nothing, nothing, JuliaFormatter.AllowNest, 0, 38, nothing)
└ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ")", nothing, nothing, JuliaFormatter.AllowNest, 0, 42, nothing)
┌ Info:
│ optimal_placeholders =
│ 1-element Vector{Int64}:
└ 11
function foo(arg1, arg2, arg3,
arg4, arg5)
return body
endbut not for 38:
julia> print(format_text(str, YASStyle(), margin = 38))
┌ Info:
│ fst.nodes =
│ 16-element Vector{JuliaFormatter.FST}:
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 3, "foo", nothing, nothing, JuliaFormatter.AllowNest, 0, 10, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, "(", nothing, nothing, JuliaFormatter.AllowNest, 0, 13, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg1", nothing, nothing, JuliaFormatter.AllowNest, 0, 14, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 18, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg2", nothing, nothing, JuliaFormatter.AllowNest, 0, 20, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 24, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg3", nothing, nothing, JuliaFormatter.AllowNest, 0, 26, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 30, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg4", nothing, nothing, JuliaFormatter.AllowNest, 0, 32, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ",", nothing, nothing, JuliaFormatter.AllowNest, 0, 36, nothing)
│ JuliaFormatter.FST(JuliaFormatter.PLACEHOLDER, 1, 1, 0, 1, " ", nothing, nothing, JuliaFormatter.AllowNest, 0, -1, nothing)
│ JuliaFormatter.FST(JuliaFormatter.IDENTIFIER, 1, 1, 0, 4, "arg5", nothing, nothing, JuliaFormatter.AllowNest, 0, 38, nothing)
└ JuliaFormatter.FST(JuliaFormatter.PUNCTUATION, 1, 1, 0, 1, ")", nothing, nothing, JuliaFormatter.AllowNest, 0, 42, nothing)
┌ Info:
└ optimal_placeholders = Int64[]
function foo(arg1, arg2, arg3, arg4,
arg5)
return body
endThere was a problem hiding this comment.
ok another patch: 6175baa
The initial segment could have a different offset than the other segments. Particularly, this is true for YAS, where it's illegal to put the initial argument on the next line.
There was a problem hiding this comment.
Thanks! Best fit now works for calls with YASStyle. But I broke #807 again...
There was a problem hiding this comment.
Hold on, now it works. Probably a Revise issue.
I'll hopefully find time this week to check the failing tests and make this work for n_vect! as well.
No description provided.