Skip to content

Commit 8de2540

Browse files
committed
Add MatchesMacroItem and imp Rewrite, Spanned, and IntoOverflowableItem
These traits will make it easier to implement rewriting matches! in terms of `overflow::rewrite_with_*` methods.
1 parent fbcc07a commit 8de2540

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ use crate::comment::{
2727
use crate::config::lists::*;
2828
use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
2929
use crate::lists::{itemize_list, write_list, ListFormatting};
30+
use crate::matches::rewrite_guard;
3031
use crate::overflow;
3132
use crate::parse::macros::lazy_static::parse_lazy_static;
33+
use crate::parse::macros::matches::MatchesMacroItem;
3234
use crate::parse::macros::{parse_expr, parse_macro_args, ParsedMacroArgs};
3335
use crate::rewrite::{Rewrite, RewriteContext};
3436
use crate::shape::{Indent, Shape};
@@ -1313,6 +1315,19 @@ impl MacroBranch {
13131315
}
13141316
}
13151317

1318+
impl Rewrite for MatchesMacroItem {
1319+
fn rewrite(&self, context: &RewriteContext<'_>, shape: crate::shape::Shape) -> Option<String> {
1320+
match self {
1321+
Self::Expr(expr) => expr.rewrite(context, shape),
1322+
Self::Arm(pat, guard) => {
1323+
let pats_str = pat.rewrite(context, shape)?;
1324+
let guard_str = rewrite_guard(context, guard, shape, &pats_str)?;
1325+
Some(pats_str + &guard_str)
1326+
}
1327+
}
1328+
}
1329+
}
1330+
13161331
/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
13171332
///
13181333
/// # Expected syntax

src/overflow.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::lists::{
1818
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1919
};
2020
use crate::macros::MacroArg;
21+
use crate::parse::macros::matches::MatchesMacroItem;
2122
use crate::patterns::{can_be_overflowed_pat, TuplePatField};
2223
use crate::rewrite::{Rewrite, RewriteContext};
2324
use crate::shape::Shape;
@@ -76,6 +77,7 @@ pub(crate) enum OverflowableItem<'a> {
7677
TuplePatField(&'a TuplePatField<'a>),
7778
Ty(&'a ast::Ty),
7879
Pat(&'a ast::Pat),
80+
MatchesMacroItem(&'a MatchesMacroItem),
7981
}
8082

8183
impl<'a> Rewrite for OverflowableItem<'a> {
@@ -116,6 +118,7 @@ impl<'a> OverflowableItem<'a> {
116118
OverflowableItem::TuplePatField(pat) => f(*pat),
117119
OverflowableItem::Ty(ty) => f(*ty),
118120
OverflowableItem::Pat(pat) => f(*pat),
121+
OverflowableItem::MatchesMacroItem(item) => f(*item),
119122
}
120123
}
121124

@@ -137,7 +140,9 @@ impl<'a> OverflowableItem<'a> {
137140
pub(crate) fn is_expr(&self) -> bool {
138141
matches!(
139142
self,
140-
OverflowableItem::Expr(..) | OverflowableItem::MacroArg(MacroArg::Expr(..))
143+
OverflowableItem::Expr(..)
144+
| OverflowableItem::MacroArg(MacroArg::Expr(..))
145+
| OverflowableItem::MatchesMacroItem(MatchesMacroItem::Expr(..))
141146
)
142147
}
143148

@@ -153,6 +158,7 @@ impl<'a> OverflowableItem<'a> {
153158
match self {
154159
OverflowableItem::Expr(expr) => Some(expr),
155160
OverflowableItem::MacroArg(MacroArg::Expr(ref expr)) => Some(expr),
161+
OverflowableItem::MatchesMacroItem(MatchesMacroItem::Expr(expr)) => Some(expr),
156162
_ => None,
157163
}
158164
}
@@ -233,7 +239,10 @@ macro_rules! impl_into_overflowable_item_for_rustfmt_types {
233239
}
234240

235241
impl_into_overflowable_item_for_ast_node!(Expr, GenericParam, NestedMetaItem, FieldDef, Ty, Pat);
236-
impl_into_overflowable_item_for_rustfmt_types!([MacroArg], [SegmentParam, TuplePatField]);
242+
impl_into_overflowable_item_for_rustfmt_types!(
243+
[MacroArg, MatchesMacroItem],
244+
[SegmentParam, TuplePatField]
245+
);
237246

238247
pub(crate) fn into_overflowable_list<'a, T>(
239248
iter: impl Iterator<Item = &'a T>,

src/parse/macros/matches.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ pub(crate) fn parse_matches(context: &RewriteContext<'_>, ts: TokenStream) -> Op
5252
};
5353
Some(Matches { expr, pat, guard })
5454
}
55+
56+
impl Matches {
57+
pub(crate) fn items(self) -> [MatchesMacroItem; 2] {
58+
[
59+
MatchesMacroItem::Expr(self.expr),
60+
MatchesMacroItem::Arm(self.pat, self.guard),
61+
]
62+
}
63+
}
64+
65+
#[derive(Debug)]
66+
pub(crate) enum MatchesMacroItem {
67+
Expr(P<ast::Expr>),
68+
Arm(P<ast::Pat>, Option<P<ast::Expr>>),
69+
}

src/spanned.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast::{ast, ptr};
44
use rustc_span::{source_map, Span};
55

66
use crate::macros::MacroArg;
7+
use crate::parse::macros::matches::MatchesMacroItem;
78
use crate::utils::{mk_sp, outer_attributes};
89

910
/// Spanned returns a span including attributes, if available.
@@ -197,3 +198,13 @@ impl Spanned for ast::NestedMetaItem {
197198
self.span()
198199
}
199200
}
201+
202+
impl Spanned for MatchesMacroItem {
203+
fn span(&self) -> rustc_span::Span {
204+
match self {
205+
Self::Expr(expr) => expr.span,
206+
Self::Arm(pat, None) => pat.span,
207+
Self::Arm(pat, Some(guard)) => mk_sp(pat.span.lo(), guard.span.hi()),
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)