Skip to content

Commit da8a07f

Browse files
authored
Merge pull request #1907 from topecongiro/issue-1885
Add indent to macro we could not format
2 parents 7e17183 + a18a40c commit da8a07f

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/macros.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// List-like invocations with parentheses will be formatted as function calls,
2020
// and those with brackets will be formatted as array literals.
2121

22+
use std::iter::repeat;
23+
2224
use syntax::ast;
2325
use syntax::codemap::BytePos;
2426
use syntax::parse::new_parser_from_tts;
@@ -27,7 +29,7 @@ use syntax::symbol;
2729
use syntax::tokenstream::TokenStream;
2830
use syntax::util::ThinVec;
2931

30-
use Shape;
32+
use {Indent, Shape};
3133
use codemap::SpanUtils;
3234
use comment::{contains_comment, FindUncommented};
3335
use expr::{rewrite_array, rewrite_call_inner};
@@ -116,14 +118,14 @@ pub fn rewrite_macro(
116118
Ok(expr) => {
117119
// Recovered errors.
118120
if context.parse_session.span_diagnostic.has_errors() {
119-
return Some(context.snippet(mac.span));
121+
return indent_macro_snippet(&context.snippet(mac.span), shape.indent);
120122
}
121123

122124
expr
123125
}
124126
Err(mut e) => {
125127
e.cancel();
126-
return Some(context.snippet(mac.span));
128+
return indent_macro_snippet(&context.snippet(mac.span), shape.indent);
127129
}
128130
};
129131

@@ -242,7 +244,7 @@ pub fn rewrite_macro(
242244
}
243245
MacroStyle::Braces => {
244246
// Skip macro invocations with braces, for now.
245-
None
247+
indent_macro_snippet(&context.snippet(mac.span), shape.indent)
246248
}
247249
}
248250
}
@@ -280,3 +282,58 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
280282
MacroStyle::Braces
281283
}
282284
}
285+
286+
/// Indent each line according to the specified `indent`.
287+
/// e.g.
288+
/// ```rust
289+
/// foo!{
290+
/// x,
291+
/// y,
292+
/// foo(
293+
/// a,
294+
/// b,
295+
/// c,
296+
/// ),
297+
/// }
298+
/// ```
299+
/// will become
300+
/// ```rust
301+
/// foo!{
302+
/// x,
303+
/// y,
304+
/// foo(
305+
/// a,
306+
/// b,
307+
/// c,
308+
// ),
309+
/// }
310+
/// ```
311+
fn indent_macro_snippet(macro_str: &str, indent: Indent) -> Option<String> {
312+
let min_prefix_space_width =
313+
try_opt!(macro_str.lines().skip(1).map(get_prefix_space_width).min());
314+
315+
let mut lines = macro_str.lines();
316+
let first_line = try_opt!(lines.next());
317+
318+
Some(
319+
String::from(first_line) + "\n" +
320+
&lines
321+
.map(|line| {
322+
let new_indent_width = indent.width() +
323+
get_prefix_space_width(line)
324+
.checked_sub(min_prefix_space_width)
325+
.unwrap_or(0);
326+
repeat_white_space(new_indent_width) + line.trim()
327+
})
328+
.collect::<Vec<_>>()
329+
.join("\n"),
330+
)
331+
}
332+
333+
fn get_prefix_space_width(s: &str) -> usize {
334+
s.chars().position(|c| c != ' ').unwrap_or(0)
335+
}
336+
337+
fn repeat_white_space(ws_count: usize) -> String {
338+
repeat(" ").take(ws_count).collect::<String>()
339+
}

tests/source/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ fn issue1178() {
112112

113113
foo!(#[doc = "bar"] baz);
114114
}
115+
115116
fn issue1739() {
116117
sql_function!(add_rss_item,
117118
add_rss_item_t,
@@ -125,6 +126,14 @@ fn issue1739() {
125126
.par_map_inplace(|el| *el = 0.);
126127
}
127128

129+
fn issue_1885() {
130+
let threads = people.into_iter().map(|name| {
131+
chan_select! {
132+
rx.recv() => {}
133+
}
134+
}).collect::<Vec<_>>();
135+
}
136+
128137
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
129138
// at the end of the file for now.
130139

tests/target/macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ fn issue1178() {
146146
baz
147147
);
148148
}
149+
149150
fn issue1739() {
150151
sql_function!(
151152
add_rss_item,
@@ -166,6 +167,17 @@ fn issue1739() {
166167
]).par_map_inplace(|el| *el = 0.);
167168
}
168169

170+
fn issue_1885() {
171+
let threads = people
172+
.into_iter()
173+
.map(|name| {
174+
chan_select! {
175+
rx.recv() => {}
176+
}
177+
})
178+
.collect::<Vec<_>>();
179+
}
180+
169181
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
170182
// at the end of the file for now.
171183

0 commit comments

Comments
 (0)