Skip to content

Commit 9ca41ff

Browse files
committed
refactor: centralize range rewriting in src/range.rs
Move the range rewrite logic from `src/exrp.rs` to `src/range.rs`. There are a few different places where we rewrite ranges so centralizing the logic in a single location is ideal.
1 parent b47f06f commit 9ca41ff

3 files changed

Lines changed: 87 additions & 72 deletions

File tree

src/expr.rs

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::macros::{MacroPosition, rewrite_macro};
2323
use crate::matches::rewrite_match;
2424
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
2525
use crate::pairs::{PairParts, rewrite_all_pairs, rewrite_pair};
26+
use crate::range::rewrite_range;
2627
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
2728
use crate::shape::{Indent, Shape};
2829
use crate::source_map::{LineRangeUtils, SpanUtils};
@@ -315,78 +316,13 @@ pub(crate) fn format_expr(
315316
shape,
316317
SeparatorPlace::Back,
317318
),
318-
ast::ExprKind::Range(ref lhs, ref rhs, limits) => {
319-
let delim = match limits {
320-
ast::RangeLimits::HalfOpen => "..",
321-
ast::RangeLimits::Closed => "..=",
322-
};
323-
324-
fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
325-
match lhs.kind {
326-
ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit, context),
327-
ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
328-
ast::ExprKind::Binary(_, _, ref rhs_expr) => {
329-
needs_space_before_range(context, rhs_expr)
330-
}
331-
_ => false,
332-
}
333-
}
334-
335-
fn needs_space_after_range(rhs: &ast::Expr) -> bool {
336-
// Don't format `.. ..` into `....`, which is invalid.
337-
//
338-
// This check is unnecessary for `lhs`, because a range
339-
// starting from another range needs parentheses as `(x ..) ..`
340-
// (`x .. ..` is a range from `x` to `..`).
341-
matches!(rhs.kind, ast::ExprKind::Range(None, _, _))
342-
}
343-
344-
let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
345-
let space_if = |b: bool| if b { " " } else { "" };
346-
347-
format!(
348-
"{}{}{}",
349-
lhs.map_or("", |lhs| space_if(needs_space_before_range(context, lhs))),
350-
delim,
351-
rhs.map_or("", |rhs| space_if(needs_space_after_range(rhs))),
352-
)
353-
};
354-
355-
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
356-
(Some(lhs), Some(rhs)) => {
357-
let sp_delim = if context.config.spaces_around_ranges() {
358-
format!(" {delim} ")
359-
} else {
360-
default_sp_delim(Some(lhs), Some(rhs))
361-
};
362-
rewrite_pair(
363-
&*lhs,
364-
&*rhs,
365-
PairParts::infix(&sp_delim),
366-
context,
367-
shape,
368-
context.config.binop_separator(),
369-
)
370-
}
371-
(None, Some(rhs)) => {
372-
let sp_delim = if context.config.spaces_around_ranges() {
373-
format!("{delim} ")
374-
} else {
375-
default_sp_delim(None, Some(rhs))
376-
};
377-
rewrite_unary_prefix(context, &sp_delim, &*rhs, shape)
378-
}
379-
(Some(lhs), None) => {
380-
let sp_delim = if context.config.spaces_around_ranges() {
381-
format!(" {delim}")
382-
} else {
383-
default_sp_delim(Some(lhs), None)
384-
};
385-
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
386-
}
387-
(None, None) => Ok(delim.to_owned()),
388-
}
389-
}
319+
ast::ExprKind::Range(ref lhs, ref rhs, limits) => rewrite_range(
320+
context,
321+
shape,
322+
lhs.as_deref(),
323+
rhs.as_deref(),
324+
limits.as_str(),
325+
),
390326
// We do not format these expressions yet, but they should still
391327
// satisfy our width restrictions.
392328
// Style Guide RFC for InlineAsm variant pending

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ mod overflow;
8484
mod pairs;
8585
mod parse;
8686
mod patterns;
87+
mod range;
8788
mod release_channel;
8889
mod reorder;
8990
mod rewrite;

src/range.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::expr::{lit_ends_in_dot, rewrite_unary_prefix, rewrite_unary_suffix};
2+
use crate::pairs::{PairParts, rewrite_pair};
3+
use crate::rewrite::{RewriteContext, RewriteResult};
4+
use crate::shape::Shape;
5+
6+
use rustc_ast::ast;
7+
8+
fn needs_space_before_range(context: &RewriteContext<'_>, lhs: &ast::Expr) -> bool {
9+
match lhs.kind {
10+
ast::ExprKind::Lit(token_lit) => lit_ends_in_dot(&token_lit, context),
11+
ast::ExprKind::Unary(_, ref expr) => needs_space_before_range(context, expr),
12+
ast::ExprKind::Binary(_, _, ref rhs_expr) => needs_space_before_range(context, rhs_expr),
13+
_ => false,
14+
}
15+
}
16+
17+
fn needs_space_after_range(rhs: &ast::Expr) -> bool {
18+
// Don't format `.. ..` into `....`, which is invalid.
19+
//
20+
// This check is unnecessary for `lhs`, because a range
21+
// starting from another range needs parentheses as `(x ..) ..`
22+
// (`x .. ..` is a range from `x` to `..`).
23+
matches!(rhs.kind, ast::ExprKind::Range(None, _, _))
24+
}
25+
26+
pub(crate) fn rewrite_range(
27+
context: &RewriteContext<'_>,
28+
shape: Shape,
29+
lhs: Option<&ast::Expr>,
30+
rhs: Option<&ast::Expr>,
31+
delim: &str,
32+
) -> RewriteResult {
33+
let default_sp_delim = |lhs: Option<&ast::Expr>, rhs: Option<&ast::Expr>| {
34+
let space_if = |b: bool| if b { " " } else { "" };
35+
36+
format!(
37+
"{}{}{}",
38+
lhs.map_or("", |lhs| space_if(needs_space_before_range(context, lhs))),
39+
delim,
40+
rhs.map_or("", |rhs| space_if(needs_space_after_range(rhs))),
41+
)
42+
};
43+
44+
match (lhs.as_ref().map(|x| &**x), rhs.as_ref().map(|x| &**x)) {
45+
(Some(lhs), Some(rhs)) => {
46+
let sp_delim = if context.config.spaces_around_ranges() {
47+
format!(" {delim} ")
48+
} else {
49+
default_sp_delim(Some(lhs), Some(rhs))
50+
};
51+
rewrite_pair(
52+
&*lhs,
53+
&*rhs,
54+
PairParts::infix(&sp_delim),
55+
context,
56+
shape,
57+
context.config.binop_separator(),
58+
)
59+
}
60+
(None, Some(rhs)) => {
61+
let sp_delim = if context.config.spaces_around_ranges() {
62+
format!("{delim} ")
63+
} else {
64+
default_sp_delim(None, Some(rhs))
65+
};
66+
rewrite_unary_prefix(context, &sp_delim, &*rhs, shape)
67+
}
68+
(Some(lhs), None) => {
69+
let sp_delim = if context.config.spaces_around_ranges() {
70+
format!(" {delim}")
71+
} else {
72+
default_sp_delim(Some(lhs), None)
73+
};
74+
rewrite_unary_suffix(context, &sp_delim, &*lhs, shape)
75+
}
76+
(None, None) => Ok(delim.to_owned()),
77+
}
78+
}

0 commit comments

Comments
 (0)