Skip to content

Fix wrong indentation on type alias #2297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 11 additions & 27 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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)));
}

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)
result.push_str(&format!("\n{}=", indent.to_string(context.config)));
}

// 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) {
Expand Down
17 changes: 6 additions & 11 deletions tests/target/type_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ pub type LongGenericListTest<

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>;
Expand Down Expand Up @@ -71,11 +70,7 @@ where
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>>>;