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

🛠 Feature Request: Ignore Trailing Spaces in Comments #920

Open
evgzor opened this issue Feb 2, 2025 · 5 comments
Open

🛠 Feature Request: Ignore Trailing Spaces in Comments #920

evgzor opened this issue Feb 2, 2025 · 5 comments

Comments

@evgzor
Copy link

evgzor commented Feb 2, 2025

Is your feature request related to a problem? Please describe.

Currently, swift-format removes trailing spaces from all lines, including comments. This behavior can be problematic when formatting documentation-style comments or preserving intentional spacing for readability.

Describe the solution you'd like

It would be useful to have an option (e.g., "trailingWhitespace": "ignoreInComments") that allows trailing spaces to be ignored only in comments, while still removing them from regular code lines.

Describe alternatives you've considered

  • Completely disabling trailing whitespace trimming ("trailingWhitespace": "ignore"), but this also affects code formatting, which is not ideal.
  • Using swift-format followed by a post-processing script to restore trailing spaces in comments.
  • Switching to SwiftFormat, which has an except_comments option, but this is not an ideal solution for teams using swift-format.

Additional context

This would help maintain readable comments, especially for documentation-style comments where spacing matters.

@ahoppen
Copy link
Member

ahoppen commented Feb 3, 2025

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

@allevato
Copy link
Member

allevato commented Feb 3, 2025

This would help maintain readable comments, especially for documentation-style comments where spacing matters.

Do you have a concrete example where intentional trailing whitespace in comments helps readability? I'm extremely skeptical.

Regarding documentation comments, the Swift compiler expects them to be valid Markdown, and the only use I'm aware of for trailing whitespace in Markdown is that exactly 2 spaces at the end of a line can be used to insert a hard line break (e.g., <br>), but this can also be achieved with a trailing backslash.

@evgzor
Copy link
Author

evgzor commented Feb 4, 2025

It's not always possible to strictly follow the Markdown-style comment formatting, and if this issue is not ignored, a method is needed to correctly fix it automatically, either through the formatter itself or by another tool. Currently, SwiftFormat only diagnoses the problem but doesn't fix it. Is there a way for it to automatically correct issues in comments?

@allevato
Copy link
Member

allevato commented Feb 4, 2025

I'm still interested in a concrete use case where trailing spaces in a comment are necessary. Can you demonstrate one?

@evgzor
Copy link
Author

evgzor commented Feb 8, 2025

I agree that in common cases, it is not necessary. However, in some company standards, they could be required (and often it just copy/past). Removing it manually in a large codebase after introducing a new formatter requires using another tool that will do it automatically.

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