Skip to content

Commit 25c2382

Browse files
committed
avoid inserting newline between curly brace and comment
1 parent a1361bd commit 25c2382

File tree

9 files changed

+101
-10
lines changed

9 files changed

+101
-10
lines changed

src/parse/session.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::ops::Range;
12
use std::path::Path;
23
use std::sync::atomic::{AtomicBool, Ordering};
34

@@ -237,6 +238,15 @@ impl ParseSess {
237238
}
238239
}
239240

241+
pub(crate) fn line_bounds(&self, pos: BytePos) -> Option<Range<BytePos>> {
242+
let line = self.raw_psess.source_map().lookup_line(pos).ok();
243+
244+
match line {
245+
Some(line_info) => Some(line_info.sf.line_bounds(line_info.line)),
246+
None => None,
247+
}
248+
}
249+
240250
pub(crate) fn line_of_byte_pos(&self, pos: BytePos) -> usize {
241251
self.raw_psess.source_map().lookup_char_pos(pos).line
242252
}

src/visitor.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,41 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
223223
self.push_str("{");
224224
self.trim_spaces_after_opening_brace(b, inner_attrs);
225225

226+
// Try to detect comments that refer to the block, not the first statement in the block.
227+
if has_braces {
228+
let block_line_range = self.psess.lookup_line_range(b.span);
229+
if block_line_range.lo != block_line_range.hi {
230+
// Skipping if a single line block
231+
// Make sure there is no code on the first line we have to worry about.
232+
// That would also be ambiguous: what should `if { /* comment */ statement;`
233+
// even do -- should the comment be attached to the `if` or the `statement`?
234+
// The current logic answers this with "statement".
235+
let first_line_contains_stmt = if let Some(first_stmt) = b.stmts.first() {
236+
self.psess.lookup_line_range(first_stmt.span).lo == block_line_range.lo
237+
} else {
238+
false
239+
};
240+
if !first_line_contains_stmt {
241+
let first_line_bounds = self.psess.line_bounds(self.last_pos).unwrap();
242+
let first_line_snip = self
243+
.snippet(mk_sp(self.last_pos, first_line_bounds.end))
244+
.trim();
245+
246+
if contains_comment(first_line_snip) {
247+
if let Ok(comment) =
248+
rewrite_comment(first_line_snip, false, self.shape(), self.config)
249+
{
250+
self.push_str(" ");
251+
self.push_str(&comment);
252+
// This line has no statements, so we can jump to the end of it
253+
// (but *before* the newline).
254+
self.last_pos = first_line_bounds.end - BytePos::from_usize(1);
255+
}
256+
}
257+
}
258+
}
259+
}
260+
226261
// Format inner attributes if available.
227262
if let Some(attrs) = inner_attrs {
228263
self.visit_attrs(attrs, ast::AttrStyle::Inner);

tests/source/issue-3255.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fn foo(){
2+
if true { // Sample comment
3+
1
4+
}
5+
}
6+
7+
8+
fn foo(){
9+
if true {
10+
// Sample comment
11+
1
12+
}
13+
}
14+
15+
fn foo(){
16+
if true { /* Sample comment */
17+
1
18+
}
19+
}
20+
21+
fn foo(){
22+
if true { /* Sample
23+
comment */
24+
1
25+
}
26+
}

tests/target/async_fn.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ async unsafe fn foo() {
1313
}
1414

1515
async unsafe fn rust() {
16-
async move {
17-
// comment
16+
async move { // comment
1817
Ok(())
1918
}
2019
}

tests/target/control-brace-style-always-next-line.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ fn main() {
2020
}
2121

2222
'while_label: while cond
23-
{
24-
// while comment
23+
{ // while comment
2524
();
2625
}
2726

tests/target/control-brace-style-always-same-line.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ fn main() {
1515
();
1616
}
1717

18-
'while_label: while cond {
19-
// while comment
18+
'while_label: while cond { // while comment
2019
();
2120
}
2221

tests/target/issue-3255.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fn foo() {
2+
if true { // Sample comment
3+
1
4+
}
5+
}
6+
7+
fn foo() {
8+
if true {
9+
// Sample comment
10+
1
11+
}
12+
}
13+
14+
fn foo() {
15+
if true { /* Sample comment */
16+
1
17+
}
18+
}
19+
20+
fn foo() {
21+
if true { /* Sample
22+
comment */
23+
1
24+
}
25+
}

tests/target/label_break.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn main() {
1919
// comment
2020
break 'block 1;
2121
}
22-
if bar() {
23-
/* comment */
22+
if bar() { /* comment */
2423
break 'block 2;
2524
}
2625
3

tests/target/match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn foo() {
77
// Some comment.
88
a => foo(),
99
b if 0 < 42 => foo(),
10-
c => {
11-
// Another comment.
10+
c => { // Another comment.
1211
// Comment.
1312
an_expression;
1413
foo()

0 commit comments

Comments
 (0)