Merge pull request #2017 from topecongiro/issue-1987

Format strings in attributes when `format_strings = true`
This commit is contained in:
Nick Cameron 2017-10-05 17:27:17 +08:00 committed by GitHub
commit 2abe119d88
5 changed files with 65 additions and 58 deletions

View file

@ -72,12 +72,7 @@ pub fn format_expr(
shape,
false,
),
ast::ExprKind::Lit(ref l) => match l.node {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
rewrite_string_lit(context, l.span, shape)
}
_ => Some(context.snippet(expr.span)),
},
ast::ExprKind::Lit(ref l) => rewrite_literal(context, l, shape),
ast::ExprKind::Call(ref callee, ref args) => {
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
let callee_str = try_opt!(callee.rewrite(context, shape));
@ -1938,6 +1933,13 @@ fn rewrite_pat_expr(
.map(|expr_rw| format!("\n{}{}", nested_indent_str, expr_rw))
}
pub fn rewrite_literal(context: &RewriteContext, l: &ast::Lit, shape: Shape) -> Option<String> {
match l.node {
ast::LitKind::Str(_, ast::StrStyle::Cooked) => rewrite_string_lit(context, l.span, shape),
_ => Some(context.snippet(l.span)),
}
}
fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Option<String> {
let string_lit = context.snippet(span);
@ -1974,20 +1976,10 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
return Some(string_lit);
}
let fmt = StringFormat {
opener: "\"",
closer: "\"",
line_start: " ",
line_end: "\\",
shape: shape,
trim_end: false,
config: context.config,
};
// Remove the quote characters.
let str_lit = &string_lit[1..string_lit.len() - 1];
rewrite_string(str_lit, &fmt)
rewrite_string(str_lit, &StringFormat::new(shape, context.config))
}
fn string_requires_rewrite(

View file

@ -29,6 +29,20 @@ pub struct StringFormat<'a> {
pub config: &'a Config,
}
impl<'a> StringFormat<'a> {
pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
StringFormat {
opener: "\"",
closer: "\"",
line_start: " ",
line_end: "\\",
shape: shape,
trim_end: false,
config: config,
}
}
}
// FIXME: simplify this!
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
// Strip line breaks.
@ -133,16 +147,7 @@ mod test {
#[test]
fn issue343() {
let config = Default::default();
let fmt = StringFormat {
opener: "\"",
closer: "\"",
line_start: " ",
line_end: "\\",
shape: Shape::legacy(2, Indent::empty()),
trim_end: false,
config: &config,
};
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
rewrite_string("eq_", &fmt);
}
}

View file

@ -16,6 +16,7 @@ use syntax::attr::HasAttrs;
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
use syntax::parse::ParseSess;
use expr::rewrite_literal;
use spanned::Spanned;
use codemap::{LineRangeUtils, SpanUtils};
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
@ -798,7 +799,7 @@ impl Rewrite for ast::NestedMetaItem {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match self.node {
ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)),
ast::NestedMetaItemKind::Literal(ref l) => rewrite_literal(context, l, shape),
}
}
}
@ -809,10 +810,11 @@ impl Rewrite for ast::MetaItem {
ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
ast::MetaItemKind::List(ref list) => {
let name = self.name.as_str();
// 3 = `#[` and `(`, 2 = `]` and `)`
// 1 = `(`, 2 = `]` and `)`
let item_shape = try_opt!(
shape
.shrink_left(name.len() + 3)
.visual_indent(0)
.shrink_left(name.len() + 1)
.and_then(|s| s.sub_width(2))
);
let items = itemize_list(
@ -841,18 +843,10 @@ impl Rewrite for ast::MetaItem {
}
ast::MetaItemKind::NameValue(ref literal) => {
let name = self.name.as_str();
let value = context.snippet(literal.span);
if &*name == "doc" && contains_comment(&value) {
let doc_shape = Shape {
width: cmp::min(shape.width, context.config.comment_width())
.checked_sub(shape.indent.width())
.unwrap_or(0),
..shape
};
try_opt!(rewrite_comment(&value, false, doc_shape, context.config))
} else {
format!("{} = {}", name, value)
}
// 3 = ` = `
let lit_shape = try_opt!(shape.shrink_left(name.len() + 3));
let value = try_opt!(rewrite_literal(context, literal, lit_shape));
format!("{} = {}", name, value)
}
})
}
@ -860,22 +854,29 @@ impl Rewrite for ast::MetaItem {
impl Rewrite for ast::Attribute {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
try_opt!(self.meta())
.rewrite(context, shape)
.map(|rw| if self.is_sugared_doc {
rw
} else {
let original = context.snippet(self.span);
let prefix = match self.style {
ast::AttrStyle::Inner => "#!",
ast::AttrStyle::Outer => "#",
};
if contains_comment(&original) {
original
} else {
format!("{}[{}]", prefix, rw)
}
})
let prefix = match self.style {
ast::AttrStyle::Inner => "#!",
ast::AttrStyle::Outer => "#",
};
let snippet = context.snippet(self.span);
if self.is_sugared_doc {
let doc_shape = Shape {
width: cmp::min(shape.width, context.config.comment_width())
.checked_sub(shape.indent.width())
.unwrap_or(0),
..shape
};
rewrite_comment(&snippet, false, doc_shape, context.config)
} else {
if contains_comment(&snippet) {
return Some(snippet);
}
// 1 = `[`
let shape = try_opt!(shape.offset_left(prefix.len() + 1));
try_opt!(self.meta())
.rewrite(context, shape)
.map(|rw| format!("{}[{}]", prefix, rw))
}
}
}

View file

@ -56,3 +56,7 @@ fn issue_1282() {
}
}
}
// #1987
#[link_args = "-s NO_FILESYSTEM=1 -s NO_EXIT_RUNTIME=1 -s EXPORTED_RUNTIME_METHODS=[\"_malloc\"] -s NO_DYNAMIC_EXECUTION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s EVAL_CTORS=1"]
extern "C" {}

View file

@ -57,3 +57,8 @@ fn issue_1282() {
}
}
}
// #1987
#[link_args = "-s NO_FILESYSTEM=1 -s NO_EXIT_RUNTIME=1 -s EXPORTED_RUNTIME_METHODS=[\"_malloc\"] \
-s NO_DYNAMIC_EXECUTION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s EVAL_CTORS=1"]
extern "C" {}