nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

Add option to preserve all doc comments, preserve regular comments before grouped declarations #1638

Closed calda closed 3 months ago

calda commented 3 months ago

This PR makes two improvements to the docComments rule.

First, this PR adds a --doccomments preserve options. When this option is enabled, it disables the part of the rule that converts doc comments into regular comments, so the rule will only ever convert regular comments into doc comments.

Second, this PR updates the rule to preserve any regular comments preceding a block of grouped declarations. For example:

/// The planets of the solar system
enum Planets {
    // Inner planets
    case mercury
    case venus
    case earth
    case mars

    // Inner planets
    case jupiter
    case saturn
    case uranus
    case neptune

    // Dwarf planets
    case pluto
    case ceres
}

Since the comments refer to the code block as a whole, and not a specific property, it makes sense to use regular comments rather than doc comments (doc comments are fine too and also preserved in this case).

This is (finally!) a follow-up to https://github.com/nicklockwood/SwiftFormat/pull/1283#issuecomment-1271989083.

codecov[bot] commented 3 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 95.11%. Comparing base (5d0c8ce) to head (ee80f90).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #1638 +/- ## ======================================== Coverage 95.11% 95.11% ======================================== Files 20 20 Lines 22339 22374 +35 ======================================== + Hits 21247 21282 +35 Misses 1092 1092 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nicklockwood commented 3 months ago

@calda this is great, thanks so much!