domluna / JuliaFormatter.jl

An opinionated code formatter for Julia. Plot twist - the opinion is your own.
https://domluna.github.io/JuliaFormatter.jl/dev/
MIT License
578 stars 69 forks source link

Docstring breaks short_to_long_function_def #867

Open jw3126 opened 2 months ago

jw3126 commented 2 months ago

It seems short function defs that have a docstring are never expanded:

using JuliaFormatter
using JuliaFormatter: options, DefaultStyle

fmt1(s; i = 4, m = 80, kwargs...) =
    JuliaFormatter.format_text(s; kwargs..., indent = i, margin = m)
fmt1(s, i, m; kwargs...) = fmt1(s; kwargs..., i = i, m = m)

# Verifies formatting the formatted text
# results in the same output
function fmt(s; i = 4, m = 80, kwargs...)
    kws = merge(options(DefaultStyle()), kwargs)
    s1 = fmt1(s; kws..., i = i, m = m)
    return fmt1(s1; kws..., i = i, m = m)
end
fmt(s, i, m; kwargs...) = fmt(s; kwargs..., i = i, m = m)

str_ = """
    f() = (1,2,3,4,5,6,7,8,9,10)
"""
print(fmt(str_, 4, 10, short_to_long_function_def = true))
# ok

str_ = """
"docstring"
f() = (1,2,3,4,5,6,7,8,9,10)
"""
print(fmt(str_, 4, 10, short_to_long_function_def = true))
# bad
function f()
    (
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
    )
end
"docstring"
f() = (
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10,
)
domluna commented 1 month ago
julia> format_text(str_, short_to_long_function_def=true, margin=10) |> print
"docstring"
function f()
    (
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
    )
end

seems it's fixed in the rewrite to JuliaSyntax. I'll merge that PR sometime this week.