Skip to content
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

Add spaces between consecutive .. ..= #2624

Merged
merged 1 commit into from
Apr 17, 2018
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
33 changes: 28 additions & 5 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,37 @@ pub fn format_expr(
}
}

fn needs_space_after_range(rhs: &ast::Expr) -> bool {
match rhs.node {
// Don't format `.. ..` into `....`, which is invalid.
//
// This check is unnecessary for `lhs`, because a range
// starting from another range needs parentheses as `(x ..) ..`
// (`x .. ..` is a range from `x` to `..`).
ast::ExprKind::Range(None, _, _) => true,
_ => false,
}
}

let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
let space_if = |b: bool| if b { " " } else { "" };

format!(
"{}{}{}",
lhs.map(|lhs| space_if(needs_space_before_range(context, lhs)))
.unwrap_or(""),
delim,
rhs.map(|rhs| space_if(needs_space_after_range(rhs)))
.unwrap_or(""),
)
};

match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
(Some(lhs), Some(rhs)) => {
let sp_delim = if context.config.spaces_around_ranges() {
format!(" {} ", delim)
} else if needs_space_before_range(context, lhs) {
format!(" {}", delim)
} else {
delim.to_owned()
default_sp_delim(Some(lhs), Some(rhs))
};
rewrite_pair(
&*lhs,
Expand All @@ -270,15 +293,15 @@ pub fn format_expr(
let sp_delim = if context.config.spaces_around_ranges() {
format!("{} ", delim)
} else {
delim.to_owned()
default_sp_delim(None, Some(rhs))
};
rewrite_unary_prefix(context, &sp_delim, &*rhs, shape)
}
(Some(lhs), None) => {
let sp_delim = if context.config.spaces_around_ranges() {
format!(" {}", delim)
} else {
delim.to_owned()
default_sp_delim(Some(lhs), None)
};
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,9 @@ fn bar(&self) {
}
}
}

fn dots() {
.. .. ..; // (.. (.. (..)))
..= ..= ..;
(..) .. ..; // ((..) .. (..))
}
6 changes: 6 additions & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,9 @@ impl Foo {
}
}
}

fn dots() {
.. .. ..; // (.. (.. (..)))
..= ..= ..;
(..).. ..; // ((..) .. (..))
}