-
Notifications
You must be signed in to change notification settings - Fork 503
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
Errors for unnecessary space(s) before comments #269
Comments
Under https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace,
So I would think that all of the above should be considered erroneous. |
On a related note, ktlint 0.33.0 also reports "Unnecessary space(s)" for this line: So I do get the report also for adjacent lines. Can anyone shed a light on whether this is intended or not? |
Yeah, unfortunately that commit message lacks a rationale, so it's really hard to tell. |
It would be great if there was a separate rule for horizontal alignment of comments. I'd like to disable that one but keep the "Unnecessary space(s)" for code spacing. |
Any ideas when it'll be fixed? It's really terrible that it has an effect on docs (formatted with tabs):
Result: |
I understand that the style guide recommends against horizontal alignment, but the val mu = 3 // Mean
val sigma = 2 // Variance I think that adding two spaces in front more clearly separates the comment from the code, and it is not against the Kotlin style guide as far as I know. Would it be possible to add an option to disable this rule only for spaces around comments? |
Ktlint will not support flags for 'small' details like this. I do understand that it might not be a 'small' details for you but we really should try to avoid to bloat ktlint with tens of configuration issues. |
@paul-dingemans Fair, I understand that. Adding many configuration options creates a combinatorial explosion in terms of maintenance and testing. So, instead, what do you think of bringing Ktlint in line with the Kotlin Style Guide and Android Kotlin Style Guide by allowing additional whitespace before comments, without a corresponding configuration option? That is, make the behaviour suggested in this issue the default without the option to change it, since it's more in line with those major style guides. |
As was pointed out before: Under https://kotlinlang.org/docs/reference/coding-conventions.html#horizontal-whitespace,
I have seen no convincing argument to allow additional spacing before comment that do not violate the general rule 'avoid horizontal alignment of any kind'. Neither is this requested widely by many users. |
I see what you mean, as some comments did ask for allowing horizontal alignment, even though that is strictly against the style guides. I agree with you here that multiple spaces before a comment with the goal of aligning multiple comments should be disallowed. The issue raised by OP, however, was slightly different from some of the comments:
Both style guides allow the latter, so I think it would make sense for Ktlint to allow it as well. I don't really see any use cases beyond Python-style comments, so what do you think of changing |
I do understand from your perspective that allowing 1 or either 2 spaces before an EOL does not violate the style guide when it is not done for alignment purposes. Although the code change will be fairly simple, I still against it for following reasons:
If it is really important for you to have this functionality, I would suggest that you create a custom ruleset that allows/enforces the double space before the EOL comment and disable the rule which is provided by KtLint. |
Thank you for the response. I understand and completely agree with you. I might consider writing a custom ruleset that enforces a double space before comments if I find the time to do that, and release it separately. |
Extra spaces before comments are flagged as errors.
I can't find anything in the Kotlin Style Guide that bans extra spaces before comments. The Android Kotlin style guide specifically allows extra spaces before a comment (see bullet 7).
ktlint allows extra spaces when adjacent lines have aligned comments, but not when those lines are not immediately adjacent (see screenshot). I think multiple spaces should be allowed before a comment even if the comment stands alone, because a double-space before a comment is not uncommon.
The text was updated successfully, but these errors were encountered: