Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align line ending comments across multiple lines. #817

Open
thomasvl opened this issue Sep 24, 2024 · 3 comments
Open

Align line ending comments across multiple lines. #817

thomasvl opened this issue Sep 24, 2024 · 3 comments

Comments

@thomasvl
Copy link

thomasvl commented Sep 24, 2024

Given code like:

let base64Values: [Int] = [
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0x00-0x0f
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,  // 0x20-0x2f
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,  // 0x30-0x3f
    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  // 0x40-0x4f
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,  // 0x50-0x5f
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,  // 0x60-0x6f
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,  // 0x70-0x7f
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0x80-0x8f
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0x90-0x9f
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xa0-0xaf
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xb0-0xbf
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xc0-0xcf
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xd0-0xdf
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xe0-0xef
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  // 0xf0-0xff
]

It would be nice if swift-format made all the trailing line comments align. i.e. - the one short line should get extra spaces before the comment so all the // aligned. i.e. -

let base64Values: [Int] = [
    ...
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,  // 0x30-0x3f
    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,            // 0x40-0x4f
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,  // 0x50-0x5f
    ...
]

It would also apply to things like:

var foo = m.something()  // Start of long comment that continues
                         // to the next line.

and

var foo = m.something(
   mumble: 123,  // Line one.
   x: False)     // Line two.
@allevato
Copy link
Member

My gut feeling (without thinking too hard) is that this would require some nontrivial changes to our core algorithm since we make a single linear pass over the token stream. We don't really have a way to look ahead at all the possible line comments to figure out what the spacing would need to be for alignment of later lines, and we don't really have a way to "rewind" either.

Right now the pretty printer is a class, but an interesting idea would be to refactor it into a value type—I don't think there's a fundamental reason we couldn't do that. That would give us the ability to more easily do advanced things like lookahead formatting—have the printer clone itself at certain checkpoints, do some printing up to another checkpoint, then inspect the state of the clone and incorporate that state into the real formatting pass after throwing away the clone.

We'd need to be careful about performance if we did too much lookahead/rewinding, but I think there's some potential there.

@thomasvl
Copy link
Author

ps - clang-format does this sorta alignment, which is part of why it seems like it might make sense to support.

@ahoppen
Copy link
Member

ahoppen commented Sep 24, 2024

Synced to Apple’s issue tracker as rdar://136604806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants