domluna / JuliaFormatter.jl

An opinionated code formatter for Julia. Plot twist - the opinion is your own.

Home Page:https://domluna.github.io/JuliaFormatter.jl/dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JuliaFormatter deletes comments after trailing comma

topolarity opened this issue · comments

The "# diff_to_primal" comment below is deleted by format_file when using SciMLStyle:

julia> str = String(read("test.jl")); print(str)
var_to_diff = DiffGraph(
    [2, 3, nothing, 5, 6, nothing], # primal_to_diff
    [nothing, 1, 2, nothing, 4, 5], # diff_to_primal
)

julia> print(format_text(str; style=SciMLStyle()))
var_to_diff = DiffGraph([2, 3, nothing, 5, 6, nothing], # primal_to_diff
                        [nothing, 1, 2, nothing, 4, 5])

SciMLStyle enforces the trailing ) will be joined to the last argument so if there's a comment there it will get deleted. CSTParser doesn't have any notion of comments so tracking them is difficult. Once we switch to JuliaSyntax these things should be easier to keep track of but for now my advice is to not place a comment there. I know it's not ideal!

Would it be possible to add a warning whenever a comment gets deleted? I imagine 99% of the time that happens it is unintentional and the user would like to know about it.

Making a note here because we ran into the same issue for opening inline comments.

Using YAS with margin = 120 on

some_exceedingly_long_variable_name = 1
some_other_fairly_long_variable_name = 2
some_additional_long_variable_name = 3
if (# opening inline
    some_exceedingly_long_variable_name > some_other_fairly_long_variable_name # another inline
    # stand-alone
    || some_additional_long_variable_name > some_other_fairly_long_variable_name # closing inline
)
    print("something")
end

results in

some_exceedingly_long_variable_name = 1
some_other_fairly_long_variable_name = 2
some_additional_long_variable_name = 3
if (some_exceedingly_long_variable_name > some_other_fairly_long_variable_name # another inline
    # stand-alone
    ||
    some_additional_long_variable_name > some_other_fairly_long_variable_name)
    print("something")
end