Skip to content

Commit cd8bb50

Browse files
kestredtopecongiro
authored andcommitted
Trim the indentation on macros which heuristically appear to use block-style indentation (rust-lang#3178)
1 parent 16d5f50 commit cd8bb50

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

src/comment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ fn identify_comment(
332332
let (first_group, rest) = orig.split_at(first_group_ending);
333333
let rewritten_first_group =
334334
if !config.normalize_comments() && has_bare_lines && style.is_block_comment() {
335-
trim_left_preserve_layout(first_group, &shape.indent, config)?
335+
trim_left_preserve_layout(first_group, shape.indent, config)?
336336
} else if !config.normalize_comments()
337337
&& !config.wrap_comments()
338338
&& !config.format_doc_comments()

src/macros.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,32 @@ fn rewrite_macro_name(
144144
}
145145

146146
// Use this on failing to format the macro call.
147-
fn return_original_snippet_with_failure_marked(
147+
fn return_macro_parse_failure_fallback(
148148
context: &RewriteContext,
149+
indent: Indent,
149150
span: Span,
150151
) -> Option<String> {
152+
// Mark this as a failure however we format it
151153
context.macro_rewrite_failure.replace(true);
154+
155+
// Heuristically determine whether the last line of the macro uses "Block" style
156+
// rather than using "Visual" style, or another indentation style.
157+
let is_like_block_indent_style = context
158+
.snippet(span)
159+
.lines()
160+
.last()
161+
.map(|closing_line| {
162+
closing_line.trim().chars().all(|ch| match ch {
163+
'}' | ')' | ']' => true,
164+
_ => false,
165+
})
166+
})
167+
.unwrap_or(false);
168+
if is_like_block_indent_style {
169+
return trim_left_preserve_layout(context.snippet(span), indent, &context.config);
170+
}
171+
172+
// Return the snippet unmodified if the macro is not block-like
152173
Some(context.snippet(span).to_owned())
153174
}
154175

@@ -239,7 +260,9 @@ pub fn rewrite_macro_inner(
239260
loop {
240261
match parse_macro_arg(&mut parser) {
241262
Some(arg) => arg_vec.push(arg),
242-
None => return return_original_snippet_with_failure_marked(context, mac.span),
263+
None => {
264+
return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
265+
}
243266
}
244267

245268
match parser.token {
@@ -260,17 +283,19 @@ pub fn rewrite_macro_inner(
260283
}
261284
}
262285
None => {
263-
return return_original_snippet_with_failure_marked(
264-
context, mac.span,
265-
)
286+
return return_macro_parse_failure_fallback(
287+
context,
288+
shape.indent,
289+
mac.span,
290+
);
266291
}
267292
}
268293
}
269294
}
270-
return return_original_snippet_with_failure_marked(context, mac.span);
295+
return return_macro_parse_failure_fallback(context, shape.indent, mac.span);
271296
}
272297
_ if arg_vec.last().map_or(false, MacroArg::is_item) => continue,
273-
_ => return return_original_snippet_with_failure_marked(context, mac.span),
298+
_ => return return_macro_parse_failure_fallback(context, shape.indent, mac.span),
274299
}
275300

276301
parser.bump();
@@ -376,7 +401,7 @@ pub fn rewrite_macro_inner(
376401
}
377402
DelimToken::Brace => {
378403
// Skip macro invocations with braces, for now.
379-
trim_left_preserve_layout(context.snippet(mac.span), &shape.indent, &context.config)
404+
trim_left_preserve_layout(context.snippet(mac.span), shape.indent, &context.config)
380405
}
381406
_ => unreachable!(),
382407
}

src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ impl ConfigCodeBlock {
749749
.code_block
750750
.as_ref()
751751
.unwrap()
752-
.split('\n')
752+
.lines()
753753
.nth(0)
754754
.unwrap_or("")
755755
== "#![rustfmt::skip]";

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ pub fn remove_trailing_white_spaces(text: &str) -> String {
511511
/// ),
512512
/// }
513513
/// ```
514-
pub fn trim_left_preserve_layout(orig: &str, indent: &Indent, config: &Config) -> Option<String> {
514+
pub fn trim_left_preserve_layout(orig: &str, indent: Indent, config: &Config) -> Option<String> {
515515
let mut lines = LineClasses::new(orig);
516516
let first_line = lines.next().map(|(_, s)| s.trim_right().to_owned())?;
517517
let mut trimmed_lines = Vec::with_capacity(16);
@@ -598,7 +598,7 @@ mod test {
598598
let config = Config::default();
599599
let indent = Indent::new(4, 0);
600600
assert_eq!(
601-
trim_left_preserve_layout(&s, &indent, &config),
601+
trim_left_preserve_layout(&s, indent, &config),
602602
Some("aaa\n bbb\n ccc".to_string())
603603
);
604604
}

tests/source/macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ fn issue1577() {
184184
});
185185
}
186186

187+
// #3174
188+
fn issue_3174() {
189+
let data =
190+
if let Some(debug) = error.debug_info() {
191+
json!({
192+
"errorKind": format!("{:?}", error.err_kind()),
193+
"debugMessage": debug.message,
194+
})
195+
} else {
196+
json!({"errorKind": format!("{:?}", error.err_kind())})
197+
};
198+
}
199+
187200
gfx_pipeline!(pipe {
188201
vbuf: gfx::VertexBuffer<Vertex> = (),
189202
out: gfx::RenderTarget<ColorFormat> = "Target0",

tests/target/macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ fn issue1577() {
225225
});
226226
}
227227

228+
// #3174
229+
fn issue_3174() {
230+
let data = if let Some(debug) = error.debug_info() {
231+
json!({
232+
"errorKind": format!("{:?}", error.err_kind()),
233+
"debugMessage": debug.message,
234+
})
235+
} else {
236+
json!({ "errorKind": format!("{:?}", error.err_kind()) })
237+
};
238+
}
239+
228240
gfx_pipeline!(pipe {
229241
vbuf: gfx::VertexBuffer<Vertex> = (),
230242
out: gfx::RenderTarget<ColorFormat> = "Target0",

0 commit comments

Comments
 (0)