Merge pull request #1522 from topecongiro/poor-formatting/if-else

Inherit alignment to 'else'
This commit is contained in:
Nick Cameron 2017-05-10 08:20:24 +12:00 committed by GitHub
commit 4194d0f194
3 changed files with 64 additions and 18 deletions

View File

@ -988,8 +988,11 @@ impl<'a> Rewrite for ControlFlow<'a> {
if let Some(else_block) = self.else_block {
// Since this is an else block, we should not indent for the assignment preceding
// the original if, so set shape.offset to 0.
let shape = Shape { offset: 0, ..shape };
// the original if, so set shape.offset to shape.indent.alignment.
let shape = Shape {
offset: shape.indent.alignment,
..shape
};
let mut last_in_chain = false;
let rewrite = match else_block.node {
// If the else expression is another if-else expression, prevent it
@ -1494,6 +1497,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
shape: Shape)
-> Option<String> {
debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
let mut pat_string = String::new();
let mut result = match pat {
Some(pat) => {
let matcher = if matcher.is_empty() {
@ -1503,7 +1507,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
};
let pat_shape = try_opt!(try_opt!(shape.shrink_left(matcher.len()))
.sub_width(connector.len()));
let pat_string = try_opt!(pat.rewrite(context, pat_shape));
pat_string = try_opt!(pat.rewrite(context, pat_shape));
format!("{}{}{}", matcher, pat_string, connector)
}
None => String::new(),
@ -1516,19 +1520,11 @@ fn rewrite_pat_expr(context: &RewriteContext,
if shape.width > extra_offset + 1 {
let spacer = if pat.is_some() { " " } else { "" };
let expr_shape = try_opt!(shape.sub_width(extra_offset + spacer.len()))
.add_offset(extra_offset + spacer.len());
let expr_shape = try_opt!(shape.offset_left(extra_offset + spacer.len()));
let expr_rewrite = expr.rewrite(context, expr_shape);
if let Some(expr_string) = expr_rewrite {
let pat_simple = pat.and_then(|p| {
p.rewrite(context,
Shape::legacy(context.config.max_width,
Indent::empty()))
})
.map(|s| pat_is_simple(&s));
if pat.is_none() || pat_simple.unwrap_or(false) || !expr_string.contains('\n') {
if pat.is_none() || pat_is_simple(&pat_string) || !expr_string.contains('\n') {
result.push_str(spacer);
result.push_str(&expr_string);
return Some(result);
@ -1542,11 +1538,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
result.push('\n');
result.push_str(&nested_indent.to_string(context.config));
let expr_rewrite = expr.rewrite(&context,
Shape::legacy(try_opt!(context.config
.max_width
.checked_sub(nested_indent.width())),
nested_indent));
let expr_rewrite = expr.rewrite(&context, Shape::indented(nested_indent, context.config));
result.push_str(&try_opt!(expr_rewrite));
Some(result)

View File

@ -0,0 +1,27 @@
fn issue1468() {
euc_jp_decoder_functions!({
let trail_minus_offset = byte.wrapping_sub(0xA1);
// Fast-track Hiragana (60% according to Lunde)
// and Katakana (10% acconding to Lunde).
if jis0208_lead_minus_offset == 0x03 &&
trail_minus_offset < 0x53 {
// Hiragana
handle.write_upper_bmp(0x3041 + trail_minus_offset as u16)
} else if jis0208_lead_minus_offset == 0x04 &&
trail_minus_offset < 0x56 {
// Katakana
handle.write_upper_bmp(0x30A1 + trail_minus_offset as u16)
} else if trail_minus_offset > (0xFE - 0xA1) {
if byte < 0x80 {
return (DecoderResult::Malformed(1, 0),
unread_handle_trail.unread(),
handle.written());
}
return (DecoderResult::Malformed(2, 0),
unread_handle_trail.consumed(),
handle.written());
} else {
unreachable!();
}
});
}

View File

@ -0,0 +1,27 @@
fn issue1468() {
euc_jp_decoder_functions!({
let trail_minus_offset = byte.wrapping_sub(0xA1);
// Fast-track Hiragana (60% according to Lunde)
// and Katakana (10% acconding to Lunde).
if jis0208_lead_minus_offset == 0x03 &&
trail_minus_offset < 0x53 {
// Hiragana
handle.write_upper_bmp(0x3041 + trail_minus_offset as u16)
} else if jis0208_lead_minus_offset == 0x04 &&
trail_minus_offset < 0x56 {
// Katakana
handle.write_upper_bmp(0x30A1 + trail_minus_offset as u16)
} else if trail_minus_offset > (0xFE - 0xA1) {
if byte < 0x80 {
return (DecoderResult::Malformed(1, 0),
unread_handle_trail.unread(),
handle.written());
}
return (DecoderResult::Malformed(2, 0),
unread_handle_trail.consumed(),
handle.written());
} else {
unreachable!();
}
});
}