-
Notifications
You must be signed in to change notification settings - Fork 892
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
avoid inserting newline between opening curly brace and comment #6265
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,41 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { | |
self.push_str("{"); | ||
self.trim_spaces_after_opening_brace(b, inner_attrs); | ||
|
||
// Try to detect comments that refer to the block, not the first statement in the block. | ||
if has_braces && self.config.style_edition() >= StyleEdition::Edition2024 { | ||
let block_line_range = self.psess.lookup_line_range(b.span); | ||
if block_line_range.lo != block_line_range.hi { | ||
// Skipping if a single line block | ||
// Make sure there is no code on the first line we have to worry about. | ||
// That would also be ambiguous: what should `if { /* comment */ statement;` | ||
// even do -- should the comment be attached to the `if` or the `statement`? | ||
// The current logic answers this with "statement". | ||
let first_line_contains_stmt = if let Some(first_stmt) = b.stmts.first() { | ||
self.psess.lookup_line_range(first_stmt.span).lo == block_line_range.lo | ||
} else { | ||
false | ||
}; | ||
if !first_line_contains_stmt { | ||
let first_line_bounds = self.psess.line_bounds(self.last_pos).unwrap(); | ||
let first_line_snip = self | ||
.snippet(mk_sp(self.last_pos, first_line_bounds.end)) | ||
.trim(); | ||
|
||
if contains_comment(first_line_snip) { | ||
if let Ok(comment) = | ||
rewrite_comment(first_line_snip, false, self.shape(), self.config) | ||
{ | ||
self.push_str(" "); | ||
self.push_str(&comment); | ||
// This line has no statements, so we can jump to the end of it | ||
// (but *before* the newline). | ||
self.last_pos = first_line_bounds.end - BytePos::from_usize(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original PR did I'm not sure what the proper fix for that is. Skipping to the end of the line seemed to make most sense, since we should have processed the entire line. |
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Format inner attributes if available. | ||
if let Some(attrs) = inner_attrs { | ||
self.visit_attrs(attrs, ast::AttrStyle::Inner); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// rustfmt-style_edition: 2024 | ||
|
||
fn foo(){ | ||
if true { // Sample comment | ||
// second-line comment | ||
1 | ||
} | ||
} | ||
|
||
|
||
fn foo(){ | ||
if true { | ||
// Sample comment | ||
1 | ||
} | ||
} | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn foo(){ | ||
if true { /* Sample comment */ | ||
1 | ||
} | ||
} | ||
|
||
fn foo(){ | ||
if true { /* Sample | ||
comment */ | ||
1 | ||
} | ||
} | ||
Comment on lines
+24
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably good to add a test case with a multi-line block comment that doesn't have bare lines. Something like this for example: fn foo(){
if true { /* Sample
* another line
* another line
* end
*/
1
}
} I noticed that the indentation of the first line was the only one that got updated. Maybe the other's should have as well since they're part of the same block comment? I imagine that might be tougher to get right though. This is the output that I'm currently getting when formatting with this branch: fn foo() {
if true { /* Sample
* another line
* another line
* end
*/
1
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the entire approach is based on processing just the code from the The alternative originally implemented by #4745 was to not touch |
||
|
||
// This isn't ideal. | ||
fn foo(){ | ||
if true { /* Sample | ||
* another line | ||
* another line | ||
* end | ||
*/ | ||
1 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// rustfmt-style_edition: 2024 | ||
|
||
fn foo() { | ||
if true { // Sample comment | ||
// second-line comment | ||
1 | ||
} | ||
} | ||
|
||
fn foo() { | ||
if true { | ||
// Sample comment | ||
1 | ||
} | ||
} | ||
|
||
fn foo() { | ||
if true { /* Sample comment */ | ||
1 | ||
} | ||
} | ||
|
||
fn foo() { | ||
if true { /* Sample | ||
comment */ | ||
1 | ||
} | ||
} | ||
|
||
// This isn't ideal. | ||
fn foo() { | ||
if true { /* Sample | ||
* another line | ||
* another line | ||
* end | ||
*/ | ||
1 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SO there's no way to adjust the
shape
that we pass torewrite_comment
to tell it how much space it actually has available?How does it usually know how much space it has (e.g. to account for indentation)? I would have thought it looks at the cursor position in the current line in the output, which should always be reliable, but that doesn't seem to be the case.