Skip to content

Commit 9d6a512

Browse files
committed
Special case matches! macro
Now that we're able to parse the TokenStream of `matches!` calls into a `Matches` struct and then convert that `Matches` struct into an iterable which implments `IntoOverflowableItem` we're able to implement `matches!` rewriting in terms of `overflow::rewrite_with_*` calls.
1 parent 8de2540 commit 9d6a512

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/macros.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ use crate::comment::{
2525
contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
2626
};
2727
use crate::config::lists::*;
28-
use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
28+
use crate::expr::{choose_separator_tactic, rewrite_array, rewrite_assign_rhs, RhsAssignKind};
2929
use crate::lists::{itemize_list, write_list, ListFormatting};
3030
use crate::matches::rewrite_guard;
3131
use crate::overflow;
3232
use crate::parse::macros::lazy_static::parse_lazy_static;
33-
use crate::parse::macros::matches::MatchesMacroItem;
33+
use crate::parse::macros::matches::{parse_matches, MatchesMacroItem};
3434
use crate::parse::macros::{parse_expr, parse_macro_args, ParsedMacroArgs};
3535
use crate::rewrite::{Rewrite, RewriteContext};
3636
use crate::shape::{Indent, Shape};
@@ -232,6 +232,10 @@ fn rewrite_macro_inner(
232232
if let success @ Some(..) = format_lazy_static(context, shape, ts.clone()) {
233233
return success;
234234
}
235+
} else if macro_name == "matches!" {
236+
if let success @ Some(..) = format_matches(context, shape, &macro_name, mac) {
237+
return success;
238+
}
235239
}
236240

237241
let ParsedMacroArgs {
@@ -1328,6 +1332,46 @@ impl Rewrite for MatchesMacroItem {
13281332
}
13291333
}
13301334

1335+
/// Format `matches!` from <https://doc.rust-lang.org/std/macro.matches.html>
1336+
///
1337+
/// # Expected syntax
1338+
///
1339+
/// ```text
1340+
/// matches!(expr, pat)
1341+
/// matches!(expr, pat if expr)
1342+
/// ```
1343+
fn format_matches(
1344+
context: &RewriteContext<'_>,
1345+
shape: Shape,
1346+
name: &str,
1347+
mac: &ast::MacCall,
1348+
) -> Option<String> {
1349+
let span = mac.span();
1350+
let matches = parse_matches(context, mac.args.tokens.clone())?.items();
1351+
let force_separator_tactic = choose_separator_tactic(context, span);
1352+
match mac.args.delim {
1353+
ast::MacDelimiter::Parenthesis => overflow::rewrite_with_parens(
1354+
context,
1355+
name,
1356+
matches.iter(),
1357+
shape,
1358+
span,
1359+
shape.width,
1360+
force_separator_tactic,
1361+
),
1362+
ast::MacDelimiter::Bracket => overflow::rewrite_with_square_brackets(
1363+
context,
1364+
name,
1365+
matches.iter(),
1366+
shape,
1367+
span,
1368+
force_separator_tactic,
1369+
None,
1370+
),
1371+
ast::MacDelimiter::Brace => None,
1372+
}
1373+
}
1374+
13311375
/// Format `lazy_static!` from <https://crates.io/crates/lazy_static>.
13321376
///
13331377
/// # Expected syntax

0 commit comments

Comments
 (0)