Fix wrong indentation on type alias

Use rewrite_assign_rhs() when rewriting type alias.
This commit is contained in:
topecongiro 2017-12-21 09:58:13 +09:00
parent 8998c1d5b5
commit 1d8619d49a
3 changed files with 22 additions and 41 deletions

View File

@ -2754,11 +2754,13 @@ pub fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
shape: Shape,
) -> Option<String> {
let lhs = lhs.into();
let last_line_width = last_line_width(&lhs) - if lhs.contains('\n') {
shape.indent.width()
} else {
0
};
let last_line_width = last_line_width(&lhs)
.checked_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
0
})
.unwrap_or(0);
// 1 = space between operator and rhs.
let orig_shape = shape.offset_left(last_line_width + 1).unwrap_or(Shape {
width: 0,

View File

@ -1365,9 +1365,11 @@ pub fn rewrite_type_alias(
result.push_str(&ident.to_string());
// 2 = `= `
let shape = Shape::indented(indent + result.len(), context.config).sub_width(2)?;
let g_shape = Shape::indented(indent, context.config)
.offset_left(result.len())?
.sub_width(2)?;
let g_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo());
let generics_str = rewrite_generics(context, generics, shape, g_span)?;
let generics_str = rewrite_generics(context, generics, g_shape, g_span)?;
result.push_str(&generics_str);
let where_budget = context.budget(last_line_width(&result));
@ -1386,32 +1388,14 @@ pub fn rewrite_type_alias(
)?;
result.push_str(&where_clause_str);
if where_clause_str.is_empty() {
result.push_str(" = ");
result.push_str(" =");
} else {
result.push_str(&format!("\n{}= ", indent.to_string(context.config)));
result.push_str(&format!("\n{}=", indent.to_string(context.config)));
}
let line_width = last_line_width(&result);
// This checked_sub may fail as the extra space after '=' is not taken into account
// In that case the budget is set to 0 which will make ty.rewrite retry on a new line
let budget = context.budget(indent.width() + line_width + ";".len());
let type_indent = indent + line_width;
// Try to fit the type on the same line
let ty_str = ty.rewrite(context, Shape::legacy(budget, type_indent))
.or_else(|| {
// The line was too short, try to put the type on the next line
// Remove the space after '='
result.pop();
let type_indent = indent.block_indent(context.config);
result.push('\n');
result.push_str(&type_indent.to_string(context.config));
let budget = context.budget(type_indent.width() + ";".len());
ty.rewrite(context, Shape::legacy(budget, type_indent))
})?;
result.push_str(&ty_str);
result.push_str(";");
Some(result)
// 1 = ";"
let ty_shape = Shape::indented(indent, context.config).sub_width(1)?;
rewrite_assign_rhs(context, result, ty, ty_shape).map(|s| s + ";")
}
fn type_annotation_spacing(config: &Config) -> (&str, &str) {

View File

@ -26,9 +26,8 @@
pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec<i32>;
pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec<
Test,
>;
pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> =
Vec<Test>;
pub type Exactly100CharsToEqualTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B, C> =
Vec<i32>;
@ -71,11 +70,7 @@
type RegisterPlugin = unsafe fn(pt: *const c_char, plugin: *mut c_void, data: *mut CallbackData);
// #1683
pub type Between<Lhs, Rhs> = super::operators::Between<
Lhs,
super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>,
>;
pub type NotBetween<Lhs, Rhs> = super::operators::NotBetween<
Lhs,
super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>,
>;
pub type Between<Lhs, Rhs> =
super::operators::Between<Lhs, super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;
pub type NotBetween<Lhs, Rhs> =
super::operators::NotBetween<Lhs, super::operators::And<AsExpr<Rhs, Lhs>, AsExpr<Rhs, Lhs>>>;