-
Notifications
You must be signed in to change notification settings - Fork 902
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
Indentation of commented code #1264
Comments
Related to #1224 |
This is still an issue for me, so lets try to explain this better. Issue: reformatting (indentation) of commented code can result in double-indentation, from: fn trivial(x: i32) -> i32 {
// x += 1;
x
} to: fn trivial(x: i32) -> i32 {
// x += 1;
x
} instead of: fn trivial(x: i32) -> i32 {
// x += 1;
x
} Solution: when adding indentation before a comment, also remove indentation within the comment, if appropriate. Or: correct indentation within comments. Sub-problem: detecting correct indentation within comments. Extra indents are valid within doc-examples and Markdown lists/indented sections. Sub-solution (doc-comments): indents within documentation is not valid for the first line of documentation. If the first line has extra indentation, it can be removed (along with equivalent indents on following lines). [This may not be technically correct, e.g. for Markdown starting with an indented example, but I have never seen such a case and there exists a good alternative.] Sub-solution for non-doc comments: apply the same heuristic as for doc-comments. Since we have no applicable formatting rules to reason about the validity of this approach it is technically wrong, but I suspect it will work fine in practice. |
We could add a configuration option (something like |
You could, and it would break Markdown formatted lists etc. Luckily those are usually only in doc-comments. Can you not follow my suggestion — if a comment (without another comment on the previous line) has more than one leading space, consider it an invalid indent, then do the same for each comment directly following it (until reaching a non-comment or a comment without extra leading spaces). |
Yeah, you are right. That wasn't a good idea.
Sorry but the ruling sounds just too complex and the benefit does not seem to worth the effort. I don't know which editor you are using, but is it possible to handle the issue by configuring/modifying the editor or the plugin you are using? (e.g., add |
Using Kate. There's no setting for that, but it is OS so in theory that may be possible.
You're trying to avoid complex heuristics? IMO there are a few not-so-simple things needed to get formatting right (read our discussion here). |
There may also be regular comments with ASCII-art-like content to illustrate something, and spaces after |
Kate now has better commenting with regards to indentation which mitigates this issue for me. fn trivial(x: i32) -> i32 {
x += 1;
x
} and using the editor to comment the second line yields: fn trivial(x: i32) -> i32 {
// x += 1;
x
} Not sure whether to close this issue, but I'll unsubscribe myself. |
I still get the erratic huge indent jumps in VS Code when selecting a block and pressing Ctrl+/, Ctrl+S. |
There is a broader feature request: 'do_not_indent_left_margin_code_or_comments'. As most modern languages, including Rust, feature at least a single level of indentation for the majority of statements the left margin is useful as secondary notation for ephemeral or debug specific constructs. In the snippet below there is a developer specific log macro
|
My editor has a feature to comment-out code by adding
//
at the beginning of the line (I expect most have something similar). When rustfmt sees this, it just adds indentation and warns if the line gets too long.Usually when this happens it's because some code is temporarily disabled for testing. Would it be too much to ask if rustfmt would not indent comments starting at the beginning of the line but just warn about it?
"Comment at the beginning of the line" is really just a proxy identifier for commented-out-code; unfortunately I can't think of a better one since identifying code is non-trivial and in any case comments can contain code.
Example:
The text was updated successfully, but these errors were encountered: