})
.map(left_trim_comment_line)
.map(|line| {
- if line_breaks == 0 {
+ if orig.starts_with("/*") && line_breaks == 0 {
line.trim_left()
} else {
line
/// Return true if the two strings of code have the same payload of comments.
/// The payload of comments is everything in the string except:
-/// - actual code (not comments)
-/// - comment start/end marks
-/// - whitespace
-/// - '*' at the beginning of lines in block comments
+/// - actual code (not comments)
+/// - comment start/end marks
+/// - whitespace
+/// - '*' at the beginning of lines in block comments
fn changed_comment_content(orig: &str, new: &str) -> bool {
// Cannot write this as a fn since we cannot return types containing closures
let code_comment_content = |code| {
}
fn visit_item(&mut self, item: &ast::Item) {
- // Don't look at attributes for modules (except for rustfmt_skip).
- // We want to avoid looking at attributes in another file, which the AST
- // doesn't distinguish.
- // FIXME This is overly conservative and means we miss attributes on
- // inline modules.
+ // Only look at attributes for modules (except for rustfmt_skip) if the
+ // module is inline. We want to avoid looking at attributes in another
+ // file, which the AST doesn't distinguish.
match item.node {
- ast::ItemKind::Mod(_) => {
- if utils::contains_skip(&item.attrs) {
+ ast::ItemKind::Mod(ref m) => {
+ let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
+ let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
+ if outer_file.name == inner_file.name {
+ if self.visit_attrs(&item.attrs) {
+ self.push_rewrite(item.span, None);
+ return;
+ }
+ } else if utils::contains_skip(&item.attrs) {
return;
}
}
let indent = offset.to_string(context.config);
for (i, a) in self.iter().enumerate() {
- let a_str = context.snippet(a.span);
+ let mut a_str = context.snippet(a.span);
// Write comments and blank lines between attributes.
if i > 0 {
if !comment.is_empty() {
let comment = try_opt!(rewrite_comment(comment,
false,
- context.config.max_width -
+ context.config.ideal_width -
offset.width(),
offset,
context.config));
result.push_str(&indent);
}
+ if a_str.starts_with("//") {
+ a_str = try_opt!(rewrite_comment(&a_str,
+ false,
+ context.config.ideal_width - offset.width(),
+ offset,
+ context.config));
+ }
+
// Write the attribute itself.
result.push_str(&a_str);