Merge pull request #1907 from topecongiro/issue-1885

Add indent to macro we could not format
This commit is contained in:
Nick Cameron 2017-08-22 12:58:27 +12:00 committed by GitHub
commit da8a07f146
3 changed files with 82 additions and 4 deletions

View File

@ -19,6 +19,8 @@
// List-like invocations with parentheses will be formatted as function calls,
// and those with brackets will be formatted as array literals.
use std::iter::repeat;
use syntax::ast;
use syntax::codemap::BytePos;
use syntax::parse::new_parser_from_tts;
@ -27,7 +29,7 @@
use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;
use Shape;
use {Indent, Shape};
use codemap::SpanUtils;
use comment::{contains_comment, FindUncommented};
use expr::{rewrite_array, rewrite_call_inner};
@ -116,14 +118,14 @@ pub fn rewrite_macro(
Ok(expr) => {
// Recovered errors.
if context.parse_session.span_diagnostic.has_errors() {
return Some(context.snippet(mac.span));
return indent_macro_snippet(&context.snippet(mac.span), shape.indent);
}
expr
}
Err(mut e) => {
e.cancel();
return Some(context.snippet(mac.span));
return indent_macro_snippet(&context.snippet(mac.span), shape.indent);
}
};
@ -242,7 +244,7 @@ pub fn rewrite_macro(
}
MacroStyle::Braces => {
// Skip macro invocations with braces, for now.
None
indent_macro_snippet(&context.snippet(mac.span), shape.indent)
}
}
}
@ -280,3 +282,58 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
MacroStyle::Braces
}
}
/// Indent each line according to the specified `indent`.
/// e.g.
/// ```rust
/// foo!{
/// x,
/// y,
/// foo(
/// a,
/// b,
/// c,
/// ),
/// }
/// ```
/// will become
/// ```rust
/// foo!{
/// x,
/// y,
/// foo(
/// a,
/// b,
/// c,
// ),
/// }
/// ```
fn indent_macro_snippet(macro_str: &str, indent: Indent) -> Option<String> {
let min_prefix_space_width =
try_opt!(macro_str.lines().skip(1).map(get_prefix_space_width).min());
let mut lines = macro_str.lines();
let first_line = try_opt!(lines.next());
Some(
String::from(first_line) + "\n" +
&lines
.map(|line| {
let new_indent_width = indent.width() +
get_prefix_space_width(line)
.checked_sub(min_prefix_space_width)
.unwrap_or(0);
repeat_white_space(new_indent_width) + line.trim()
})
.collect::<Vec<_>>()
.join("\n"),
)
}
fn get_prefix_space_width(s: &str) -> usize {
s.chars().position(|c| c != ' ').unwrap_or(0)
}
fn repeat_white_space(ws_count: usize) -> String {
repeat(" ").take(ws_count).collect::<String>()
}

View File

@ -112,6 +112,7 @@ macro_rules! foo {
foo!(#[doc = "bar"] baz);
}
fn issue1739() {
sql_function!(add_rss_item,
add_rss_item_t,
@ -125,6 +126,14 @@ fn issue1739() {
.par_map_inplace(|el| *el = 0.);
}
fn issue_1885() {
let threads = people.into_iter().map(|name| {
chan_select! {
rx.recv() => {}
}
}).collect::<Vec<_>>();
}
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
// at the end of the file for now.

View File

@ -146,6 +146,7 @@ macro_rules! foo {
baz
);
}
fn issue1739() {
sql_function!(
add_rss_item,
@ -166,6 +167,17 @@ fn issue1739() {
]).par_map_inplace(|el| *el = 0.);
}
fn issue_1885() {
let threads = people
.into_iter()
.map(|name| {
chan_select! {
rx.recv() => {}
}
})
.collect::<Vec<_>>();
}
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
// at the end of the file for now.