mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-19 02:57:33 +00:00
Merge pull request #2017 from topecongiro/issue-1987
Format strings in attributes when `format_strings = true`
This commit is contained in:
commit
2abe119d88
26
src/expr.rs
26
src/expr.rs
@ -72,12 +72,7 @@ pub fn format_expr(
|
|||||||
shape,
|
shape,
|
||||||
false,
|
false,
|
||||||
),
|
),
|
||||||
ast::ExprKind::Lit(ref l) => match l.node {
|
ast::ExprKind::Lit(ref l) => rewrite_literal(context, l, shape),
|
||||||
ast::LitKind::Str(_, ast::StrStyle::Cooked) => {
|
|
||||||
rewrite_string_lit(context, l.span, shape)
|
|
||||||
}
|
|
||||||
_ => Some(context.snippet(expr.span)),
|
|
||||||
},
|
|
||||||
ast::ExprKind::Call(ref callee, ref args) => {
|
ast::ExprKind::Call(ref callee, ref args) => {
|
||||||
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
|
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
|
||||||
let callee_str = try_opt!(callee.rewrite(context, shape));
|
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))
|
.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> {
|
fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Option<String> {
|
||||||
let string_lit = context.snippet(span);
|
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);
|
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.
|
// Remove the quote characters.
|
||||||
let str_lit = &string_lit[1..string_lit.len() - 1];
|
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(
|
fn string_requires_rewrite(
|
||||||
|
@ -29,6 +29,20 @@ pub struct StringFormat<'a> {
|
|||||||
pub config: &'a Config,
|
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!
|
// FIXME: simplify this!
|
||||||
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
|
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
|
||||||
// Strip line breaks.
|
// Strip line breaks.
|
||||||
@ -133,16 +147,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn issue343() {
|
fn issue343() {
|
||||||
let config = Default::default();
|
let config = Default::default();
|
||||||
let fmt = StringFormat {
|
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
|
||||||
opener: "\"",
|
|
||||||
closer: "\"",
|
|
||||||
line_start: " ",
|
|
||||||
line_end: "\\",
|
|
||||||
shape: Shape::legacy(2, Indent::empty()),
|
|
||||||
trim_end: false,
|
|
||||||
config: &config,
|
|
||||||
};
|
|
||||||
|
|
||||||
rewrite_string("eq_", &fmt);
|
rewrite_string("eq_", &fmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ use syntax::attr::HasAttrs;
|
|||||||
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
|
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
|
||||||
use syntax::parse::ParseSess;
|
use syntax::parse::ParseSess;
|
||||||
|
|
||||||
|
use expr::rewrite_literal;
|
||||||
use spanned::Spanned;
|
use spanned::Spanned;
|
||||||
use codemap::{LineRangeUtils, SpanUtils};
|
use codemap::{LineRangeUtils, SpanUtils};
|
||||||
use comment::{contains_comment, recover_missing_comment_in_span, CodeCharKind, CommentCodeSlices,
|
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> {
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||||
match self.node {
|
match self.node {
|
||||||
ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
|
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::Word => String::from(&*self.name.as_str()),
|
||||||
ast::MetaItemKind::List(ref list) => {
|
ast::MetaItemKind::List(ref list) => {
|
||||||
let name = self.name.as_str();
|
let name = self.name.as_str();
|
||||||
// 3 = `#[` and `(`, 2 = `]` and `)`
|
// 1 = `(`, 2 = `]` and `)`
|
||||||
let item_shape = try_opt!(
|
let item_shape = try_opt!(
|
||||||
shape
|
shape
|
||||||
.shrink_left(name.len() + 3)
|
.visual_indent(0)
|
||||||
|
.shrink_left(name.len() + 1)
|
||||||
.and_then(|s| s.sub_width(2))
|
.and_then(|s| s.sub_width(2))
|
||||||
);
|
);
|
||||||
let items = itemize_list(
|
let items = itemize_list(
|
||||||
@ -841,41 +843,40 @@ impl Rewrite for ast::MetaItem {
|
|||||||
}
|
}
|
||||||
ast::MetaItemKind::NameValue(ref literal) => {
|
ast::MetaItemKind::NameValue(ref literal) => {
|
||||||
let name = self.name.as_str();
|
let name = self.name.as_str();
|
||||||
let value = context.snippet(literal.span);
|
// 3 = ` = `
|
||||||
if &*name == "doc" && contains_comment(&value) {
|
let lit_shape = try_opt!(shape.shrink_left(name.len() + 3));
|
||||||
let doc_shape = Shape {
|
let value = try_opt!(rewrite_literal(context, literal, lit_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)
|
format!("{} = {}", name, value)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rewrite for ast::Attribute {
|
impl Rewrite for ast::Attribute {
|
||||||
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
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 {
|
let prefix = match self.style {
|
||||||
ast::AttrStyle::Inner => "#!",
|
ast::AttrStyle::Inner => "#!",
|
||||||
ast::AttrStyle::Outer => "#",
|
ast::AttrStyle::Outer => "#",
|
||||||
};
|
};
|
||||||
if contains_comment(&original) {
|
let snippet = context.snippet(self.span);
|
||||||
original
|
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 {
|
} else {
|
||||||
format!("{}[{}]", prefix, rw)
|
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))
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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" {}
|
||||||
|
@ -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" {}
|
||||||
|
Loading…
Reference in New Issue
Block a user