Preserve blank line between doc comment and attribute
This commit is contained in:
parent
c4c55285da
commit
eb828ed9ff
1 changed files with 49 additions and 18 deletions
|
@ -18,7 +18,8 @@ use syntax::parse::ParseSess;
|
||||||
|
|
||||||
use {Indent, Shape, Spanned};
|
use {Indent, Shape, Spanned};
|
||||||
use codemap::{LineRangeUtils, SpanUtils};
|
use codemap::{LineRangeUtils, SpanUtils};
|
||||||
use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
|
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
|
||||||
|
FindUncommented};
|
||||||
use comment::rewrite_comment;
|
use comment::rewrite_comment;
|
||||||
use config::{BraceStyle, Config};
|
use config::{BraceStyle, Config};
|
||||||
use expr::{format_expr, ExprType};
|
use expr::{format_expr, ExprType};
|
||||||
|
@ -928,6 +929,8 @@ impl<'a> Rewrite for [ast::Attribute] {
|
||||||
let mut derive_args = Vec::new();
|
let mut derive_args = Vec::new();
|
||||||
|
|
||||||
let mut iter = self.iter().enumerate().peekable();
|
let mut iter = self.iter().enumerate().peekable();
|
||||||
|
let mut insert_new_line = true;
|
||||||
|
let mut is_prev_sugared_doc = false;
|
||||||
while let Some((i, a)) = iter.next() {
|
while let Some((i, a)) = iter.next() {
|
||||||
let a_str = try_opt!(a.rewrite(context, shape));
|
let a_str = try_opt!(a.rewrite(context, shape));
|
||||||
|
|
||||||
|
@ -937,31 +940,61 @@ impl<'a> Rewrite for [ast::Attribute] {
|
||||||
// This particular horror show is to preserve line breaks in between doc
|
// This particular horror show is to preserve line breaks in between doc
|
||||||
// comments. An alternative would be to force such line breaks to start
|
// comments. An alternative would be to force such line breaks to start
|
||||||
// with the usual doc comment token.
|
// with the usual doc comment token.
|
||||||
let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
|
let (multi_line_before, multi_line_after) = if a.is_sugared_doc ||
|
||||||
let comment = comment.trim();
|
is_prev_sugared_doc
|
||||||
if !comment.is_empty() {
|
{
|
||||||
let comment = try_opt!(rewrite_comment(
|
// Look at before and after comment and see if there are any empty lines.
|
||||||
comment,
|
let comment_begin = comment.chars().position(|c| c == '/');
|
||||||
false,
|
let len = comment_begin.unwrap_or(comment.len());
|
||||||
Shape::legacy(
|
let mlb = comment.chars().take(len).filter(|c| *c == '\n').count() > 1;
|
||||||
context.config.comment_width() - shape.indent.width(),
|
let mla = if comment_begin.is_none() {
|
||||||
shape.indent,
|
mlb
|
||||||
),
|
} else {
|
||||||
context.config,
|
let comment_end = comment.chars().rev().position(|c| !c.is_whitespace());
|
||||||
|
let len = comment_end.unwrap();
|
||||||
|
comment
|
||||||
|
.chars()
|
||||||
|
.rev()
|
||||||
|
.take(len)
|
||||||
|
.filter(|c| *c == '\n')
|
||||||
|
.count() > 1
|
||||||
|
};
|
||||||
|
(mlb, mla)
|
||||||
|
} else {
|
||||||
|
(false, false)
|
||||||
|
};
|
||||||
|
|
||||||
|
let comment = try_opt!(recover_missing_comment_in_span(
|
||||||
|
mk_sp(self[i - 1].span.hi, a.span.lo),
|
||||||
|
shape.with_max_width(context.config),
|
||||||
|
context,
|
||||||
|
0,
|
||||||
));
|
));
|
||||||
result.push_str(&indent);
|
|
||||||
result.push_str(&comment);
|
if !comment.is_empty() {
|
||||||
result.push('\n');
|
if multi_line_before {
|
||||||
} else if multi_line {
|
|
||||||
result.push('\n');
|
result.push('\n');
|
||||||
}
|
}
|
||||||
if derive_args.is_empty() {
|
result.push_str(&comment);
|
||||||
result.push_str(&indent);
|
result.push('\n');
|
||||||
|
if multi_line_after {
|
||||||
|
result.push('\n')
|
||||||
|
}
|
||||||
|
} else if insert_new_line {
|
||||||
|
result.push('\n');
|
||||||
|
if multi_line_after {
|
||||||
|
result.push('\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if derive_args.is_empty() {
|
||||||
|
result.push_str(&indent);
|
||||||
|
}
|
||||||
|
|
||||||
|
insert_new_line = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Write the attribute itself.
|
// Write the attribute itself.
|
||||||
let mut insert_new_line = true;
|
|
||||||
if context.config.merge_derives() {
|
if context.config.merge_derives() {
|
||||||
// If the attribute is `#[derive(...)]`, take the arguments.
|
// If the attribute is `#[derive(...)]`, take the arguments.
|
||||||
if let Some(mut args) = get_derive_args(context, a) {
|
if let Some(mut args) = get_derive_args(context, a) {
|
||||||
|
@ -982,9 +1015,7 @@ impl<'a> Rewrite for [ast::Attribute] {
|
||||||
result.push_str(&a_str);
|
result.push_str(&a_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if insert_new_line && i < self.len() - 1 {
|
is_prev_sugared_doc = a.is_sugared_doc;
|
||||||
result.push('\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Some(result)
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue