Skip to content

Commit 24efc3a

Browse files
committed
Ignore empty lines inside arguments of macro with brace
1 parent 6554e7c commit 24efc3a

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

src/macros.rs

+33-13
Original file line numberDiff line numberDiff line change
@@ -309,31 +309,51 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
309309
/// }
310310
/// ```
311311
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-
315312
let mut lines = macro_str.lines();
316-
let first_line = try_opt!(lines.next());
313+
let first_line = try_opt!(lines.next().map(|s| s.trim_right()));
314+
let mut trimmed_lines = Vec::with_capacity(16);
315+
316+
let min_prefix_space_width = try_opt!(
317+
lines
318+
.filter_map(|line| {
319+
let prefix_space_width = if is_empty_line(line) {
320+
None
321+
} else {
322+
get_prefix_space_width(line)
323+
};
324+
trimmed_lines.push((line.trim(), prefix_space_width));
325+
prefix_space_width
326+
})
327+
.min()
328+
);
317329

318330
Some(
319331
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()
332+
&trimmed_lines
333+
.iter()
334+
.map(|&(line, prefix_space_width)| match prefix_space_width {
335+
Some(original_indent_width) => {
336+
let new_indent_width = indent.width() +
337+
original_indent_width
338+
.checked_sub(min_prefix_space_width)
339+
.unwrap_or(0);
340+
repeat_white_space(new_indent_width) + line.trim()
341+
}
342+
None => String::new(),
327343
})
328344
.collect::<Vec<_>>()
329345
.join("\n"),
330346
)
331347
}
332348

333-
fn get_prefix_space_width(s: &str) -> usize {
334-
s.chars().position(|c| c != ' ').unwrap_or(0)
349+
fn get_prefix_space_width(s: &str) -> Option<usize> {
350+
s.chars().position(|c| c != ' ')
335351
}
336352

337353
fn repeat_white_space(ws_count: usize) -> String {
338354
repeat(" ").take(ws_count).collect::<String>()
339355
}
356+
357+
fn is_empty_line(s: &str) -> bool {
358+
s.is_empty() || s.chars().all(char::is_whitespace)
359+
}

tests/source/macros.rs

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ fn issue_1885() {
134134
}).collect::<Vec<_>>();
135135
}
136136

137+
fn issue_1917() {
138+
mod x {
139+
quickcheck! {
140+
fn test(a: String, s: String, b: String) -> TestResult {
141+
if a.find(&s).is_none() {
142+
143+
TestResult::from_bool(true)
144+
} else {
145+
TestResult::discard()
146+
}
147+
}
148+
}
149+
}
150+
}
151+
137152
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
138153
// at the end of the file for now.
139154

tests/target/macros.rs

+15
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ fn issue_1885() {
178178
.collect::<Vec<_>>();
179179
}
180180

181+
fn issue_1917() {
182+
mod x {
183+
quickcheck! {
184+
fn test(a: String, s: String, b: String) -> TestResult {
185+
if a.find(&s).is_none() {
186+
187+
TestResult::from_bool(true)
188+
} else {
189+
TestResult::discard()
190+
}
191+
}
192+
}
193+
}
194+
}
195+
181196
// Put the following tests with macro invocations whose arguments cannot be parsed as expressioins
182197
// at the end of the file for now.
183198

0 commit comments

Comments
 (0)