fredrikekre / Runic.jl

A code formatter for Julia with rules set in stone.
MIT License
92 stars 2 forks source link

Ignore comments when formatting #21

Closed DanielVandH closed 1 month ago

DanielVandH commented 1 month ago

Currently, something like

A = [ # comment 
x 
]

gets transformed into

A = [
    # comment
    x,
]

Should # comment be ignored?

Something you can copy-paste more easily:

julia> str = raw"""
       A = [ # comment
       x
       ]
       """
"A = [ # comment\nx\n]\n"

julia> Runic.format_string(str)
"A = [\n    # comment\n    x,\n]\n"

julia> print(ans)
A = [
    # comment
    x,
]

This came up here https://github.com/JuliaGeometry/DelaunayTriangulation.jl/pull/141#discussion_r1685675048

fredrikekre commented 1 month ago

This one is deliberate but perhaps it is (too) problematic? My thought was that if you have a comment in a list like this then the comment most likely apply to the following item, e.g.

A = [
    # First element is a
    a,
    # Second element is b
    b,
]

If you want a comment for the whole expression I would have put it before, e.g.

# This is array A
A = [
    a,
    b,
]

What do you think?

DanielVandH commented 1 month ago

That's a good point. I can easily work around it, I only happened to notice since it broke after it was converted into markdown with Literate.jl. I think you are right that, in general, this transformation is a good idea. You can probably close this and I'll just make the edits to fix it in my PR

fredrikekre commented 1 month ago

You probably know, but if you need Julia source comments in a Literate file you can use ##.