Merge pull request #345 from Manishearth/fix-match
Handle comments in match better (fixes #344)
This commit is contained in:
commit
5e18b03bb9
33
src/expr.rs
33
src/expr.rs
@ -117,7 +117,7 @@ impl Rewrite for ast::Expr {
|
||||
offset)
|
||||
}
|
||||
ast::Expr_::ExprMatch(ref cond, ref arms, _) => {
|
||||
rewrite_match(context, cond, arms, width, offset)
|
||||
rewrite_match(context, cond, arms, width, offset, self.span)
|
||||
}
|
||||
ast::Expr_::ExprPath(ref qself, ref path) => {
|
||||
rewrite_path(context, qself.as_ref(), path, width, offset)
|
||||
@ -624,7 +624,8 @@ fn rewrite_match(context: &RewriteContext,
|
||||
cond: &ast::Expr,
|
||||
arms: &[ast::Arm],
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
offset: Indent,
|
||||
span: Span)
|
||||
-> Option<String> {
|
||||
if arms.is_empty() {
|
||||
return None;
|
||||
@ -666,7 +667,11 @@ fn rewrite_match(context: &RewriteContext,
|
||||
if !missed_str.is_empty() {
|
||||
result.push('\n');
|
||||
result.push_str(&arm_indent_str);
|
||||
result.push_str(missed_str);
|
||||
result.push_str(&rewrite_comment(&missed_str,
|
||||
false,
|
||||
width,
|
||||
arm_indent,
|
||||
context.config));
|
||||
}
|
||||
result.push('\n');
|
||||
result.push_str(&arm_indent_str);
|
||||
@ -682,11 +687,27 @@ fn rewrite_match(context: &RewriteContext,
|
||||
result.push_str(&snippet);
|
||||
}
|
||||
}
|
||||
|
||||
// We'll miss any comments etc. between the last arm and the end of the
|
||||
// match expression, but meh.
|
||||
let last_comment = context.snippet(mk_sp(arm_end_pos(&arms[arms.len() - 1]), span.hi));
|
||||
let last_comment = match last_comment.find_uncommented(",") {
|
||||
Some(n) => &last_comment[n+1..],
|
||||
None => &last_comment[..],
|
||||
};
|
||||
let last_comment = match last_comment.find_uncommented("}") {
|
||||
Some(n) => &last_comment[..n-1],
|
||||
None => &last_comment[..],
|
||||
};
|
||||
let last_comment = last_comment.trim();
|
||||
|
||||
result.push('\n');
|
||||
if last_comment.len() > 0 {
|
||||
result.push_str(&arm_indent_str);
|
||||
result.push_str(&rewrite_comment(&last_comment,
|
||||
false,
|
||||
width,
|
||||
arm_indent,
|
||||
context.config));
|
||||
result.push('\n');
|
||||
}
|
||||
result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config));
|
||||
result.push('}');
|
||||
Some(result)
|
||||
|
@ -130,6 +130,16 @@ fn issue184(source: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn matches() {
|
||||
match 1 {
|
||||
1 => 1, // foo
|
||||
2 => 2,
|
||||
// bar
|
||||
3 => 3,
|
||||
_ => 0 // baz
|
||||
}
|
||||
}
|
||||
|
||||
fn arrays() {
|
||||
let x = [0,
|
||||
1,
|
||||
|
@ -167,6 +167,18 @@ fn issue184(source: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn matches() {
|
||||
match 1 {
|
||||
1 => 1,
|
||||
// foo
|
||||
2 => 2,
|
||||
// bar
|
||||
3 => 3,
|
||||
_ => 0,
|
||||
// baz
|
||||
}
|
||||
}
|
||||
|
||||
fn arrays() {
|
||||
let x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 8, 9, 0, 1, 2, 3, 4,
|
||||
5, 6, 7, 8, 9, 0];
|
||||
|
Loading…
x
Reference in New Issue
Block a user