Merge pull request #1980 from topecongiro/less-recover_comment_removed

Run recover_comment_removed() only if the text changed after format
This commit is contained in:
Nick Cameron 2017-09-19 07:46:32 +12:00 committed by GitHub
commit 8974f89381
3 changed files with 7 additions and 13 deletions

View File

@ -18,7 +18,7 @@
use config::Config;
use rewrite::RewriteContext;
use string::{rewrite_string, StringFormat};
use utils::{first_line_width, last_line_width, wrap_str};
use utils::{first_line_width, last_line_width};
fn is_custom_comment(comment: &str) -> bool {
if !comment.starts_with("//") {
@ -823,13 +823,11 @@ pub fn recover_comment_removed(
new: String,
span: Span,
context: &RewriteContext,
shape: Shape,
) -> Option<String> {
let snippet = context.snippet(span);
if changed_comment_content(&snippet, &new) {
// We missed some comments
// Keep previous formatting if it satisfies the constrains
wrap_str(snippet, context.config.max_width(), shape)
if snippet != new && changed_comment_content(&snippet, &new) {
// We missed some comments. Keep the original text.
Some(snippet)
} else {
Some(new)
}

View File

@ -328,7 +328,7 @@ fn needs_space_before_range(context: &RewriteContext, lhs: &ast::Expr) -> bool {
expr_rw
.and_then(|expr_str| {
recover_comment_removed(expr_str, expr.span, context, shape)
recover_comment_removed(expr_str, expr.span, context)
})
.and_then(|expr_str| {
let attrs = outer_attributes(&expr.attrs);
@ -920,9 +920,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
}
ast::StmtKind::Mac(..) | ast::StmtKind::Item(..) => None,
};
result.and_then(|res| {
recover_comment_removed(res, self.span(), context, shape)
})
result.and_then(|res| recover_comment_removed(res, self.span(), context))
}
}

View File

@ -1488,9 +1488,7 @@ pub fn rewrite_static(
lhs,
expr,
Shape::legacy(remaining_width, offset.block_only()),
).and_then(|res| {
recover_comment_removed(res, span, context, Shape::indented(offset, context.config))
})
).and_then(|res| recover_comment_removed(res, span, context))
.map(|s| if s.ends_with(';') { s } else { s + ";" })
} else {
Some(format!("{}{};", prefix, ty_str))