Skip to content

Commit

Permalink
Fixed 'Incorrect comment indent inside if/else' issue. (#4459)
Browse files Browse the repository at this point in the history
* Added test cases

* Fixed if condition comment issue

* Fixed extern C issue

* Removed previous test case

* Removed tmp file

* honor the authors intent

* Changed the file name to its original name

* Removed extra whitespace
  • Loading branch information
whizsid authored Oct 15, 2020
1 parent 55fcbab commit 4834547
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,7 @@ impl<'a> FmtVisitor<'a> {
self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
self.block_indent = self.block_indent.block_indent(self.config);

if item.body.is_empty() {
self.format_missing_no_indent(item.span.hi() - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str);
} else {
if !item.body.is_empty() {
let first_non_ws = item.body.first().map(|s| s.span().lo());
if let Some(opening_nls) = self.advance_to_first_block_item(first_non_ws) {
self.push_str(&opening_nls);
Expand All @@ -363,10 +358,11 @@ impl<'a> FmtVisitor<'a> {
for item in &item.body {
self.format_body_element(item);
}

self.block_indent = self.block_indent.block_unindent(self.config);
self.format_missing_with_indent(item.span.hi() - BytePos(1));
}
self.format_missing_no_indent(item.span.hi() - BytePos(1));
self.block_indent = self.block_indent.block_unindent(self.config);
let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str);

self.push_str("}");
} else {
Expand Down
20 changes: 17 additions & 3 deletions src/formatting/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP};
use crate::config::{BraceStyle, Config};
use crate::formatting::{
attr::*,
comment::{rewrite_comment, CodeCharKind, CommentCodeSlices},
comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices},
items::{
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
rewrite_associated_impl_type, rewrite_extern_crate, rewrite_opaque_impl_type,
Expand Down Expand Up @@ -316,8 +316,17 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
self.block_indent = self.block_indent.block_unindent(config);
}

let comment_snippet = self.snippet(span);

let align_to_right = if unindent_comment && contains_comment(&comment_snippet) {
let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or("");
last_line_width(first_lines) > last_line_width(&comment_snippet)
} else {
false
};

let mut iter = CommentCodeSlices::with_offset(
self.snippet(span),
comment_snippet,
last_line_offset,
self.config.tab_spaces(),
)
Expand Down Expand Up @@ -380,8 +389,13 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
_ => self.push_str("\n"),
}
newline_inserted = true;

if unindent_comment && align_to_right {
self.block_indent = self.block_indent.block_indent(self.config);
}
self.push_str(&self.block_indent.to_string_with_newline(config));
if unindent_comment && align_to_right {
self.block_indent = self.block_indent.block_unindent(self.config);
}
}
}
prev_kind = kind;
Expand Down
85 changes: 85 additions & 0 deletions tests/source/issue-4120.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
fn main() {
let x = if true {
1
// In if
} else {
0
// In else
};

let x = if true {
1
/* In if */
} else {
0
/* In else */
};

let z = if true {
if true {
1

// In if level 2
} else {
2
}
} else {
3
};

let a = if true {
1
// In if
} else {
0
// In else
};

let a = if true {
1

// In if
} else {
0
// In else
};

let b = if true {
1

// In if
} else {
0
// In else
};

let c = if true {
1

// In if
} else {
0
// In else
};
for i in 0..2 {
println!("Something");
// In for
}

for i in 0..2 {
println!("Something");
/* In for */
}

extern "C" {
fn first();

// In foreign mod
}

extern "C" {
fn first();

/* In foreign mod */
}
}
85 changes: 85 additions & 0 deletions tests/target/issue-4120.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
fn main() {
let x = if true {
1
// In if
} else {
0
// In else
};

let x = if true {
1
/* In if */
} else {
0
/* In else */
};

let z = if true {
if true {
1

// In if level 2
} else {
2
}
} else {
3
};

let a = if true {
1
// In if
} else {
0
// In else
};

let a = if true {
1

// In if
} else {
0
// In else
};

let b = if true {
1

// In if
} else {
0
// In else
};

let c = if true {
1

// In if
} else {
0
// In else
};
for i in 0..2 {
println!("Something");
// In for
}

for i in 0..2 {
println!("Something");
/* In for */
}

extern "C" {
fn first();

// In foreign mod
}

extern "C" {
fn first();

/* In foreign mod */
}
}

0 comments on commit 4834547

Please sign in to comment.