JuliaFormatter.jl icon indicating copy to clipboard operation
JuliaFormatter.jl copied to clipboard

plus sign on JuMP expression breaks formatter

Open guilhermebodin opened this issue 2 years ago • 2 comments

I am using JuliaFormatter on version 1.0 and this breaks because of the first plus sign

julia> format_text("@constraint(Lower(model), +x[1] + x[2] <= 2)")
ERROR: Error while PARSING formatted text:

1 @constraint(Lower(model), x[1] + x[2] +  <= 2)

...

Error occurred on line 1, offset 45 of formatted text.

The error might not be precisely on this line but it should be in the region of the code block. Try commenting the region out and see if that removes the error.
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] format_text(cst::CSTParser.EXPR, style::DefaultStyle, s::JuliaFormatter.State)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:699
 [3] format_text(text::String, style::DefaultStyle, opts::JuliaFormatter.Options)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:632
 [4] #format_text#238
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:604 [inlined]
 [5] format_text
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:602 [inlined]
 [6] #format_text#237
   @ C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:598 [inlined]
 [7] format_text(text::String)
   @ JuliaFormatter C:\Users\guilhermebodin\.julia\packages\JuliaFormatter\xKNsr\src\JuliaFormatter.jl:598
 [8] top-level scope
   @ REPL[13]:1

guilhermebodin avatar Oct 05 '22 20:10 guilhermebodin

looks like this might be a bug with CSTParser

domluna avatar Oct 07 '22 16:10 domluna

I have the same issue with having a + at the start of a block for consistency:

sum(
        + _shutdown_margin(u, ng, d, s, t0, t, case, part)
        * _unit_flow_capacity(u, ng, d, s, t0, t)
        * _switch(
            d; from_node=nonspin_units_started_up, to_node=nonspin_units_shut_down
        )[u, n, s, t_over]
        * overlap_duration(t_over, t)
        for (u, n, s, t_over) in _switch(
            d; from_node=nonspin_units_started_up_indices, to_node=nonspin_units_shut_down_indices
        )(m; unit=u, stochastic_scenario=s, t=t_overlaps_t(m; t=t));
        init=0
    )

The formatter throws a parsing error which is fixed by removing the "+" before shutdown_margin.

clizbe avatar Mar 15 '24 12:03 clizbe