Add basic rustfmt implementation & test

This commit is contained in:
Ross Smyth 2024-02-17 14:01:02 -05:00
parent f3892a06cc
commit 1709dd59bd
4 changed files with 64 additions and 13 deletions

View File

@ -3,7 +3,7 @@
use itertools::Itertools; use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind}; use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, MatchKind, ptr, token, ForLoopKind}; use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use crate::chains::rewrite_chain; use crate::chains::rewrite_chain;

View File

@ -2,7 +2,7 @@
use std::iter::repeat; use std::iter::repeat;
use rustc_ast::{ast, MatchKind, ptr}; use rustc_ast::{ast, ptr, MatchKind};
use rustc_span::{BytePos, Span}; use rustc_span::{BytePos, Span};
use crate::comment::{combine_strs_with_missing_comments, rewrite_comment}; use crate::comment::{combine_strs_with_missing_comments, rewrite_comment};
@ -72,8 +72,7 @@ pub(crate) fn rewrite_match(
shape: Shape, shape: Shape,
span: Span, span: Span,
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
// TODO: Use this match_kind: MatchKind,
_: MatchKind,
) -> Option<String> { ) -> Option<String> {
// Do not take the rhs overhead from the upper expressions into account // Do not take the rhs overhead from the upper expressions into account
// when rewriting match condition. // when rewriting match condition.
@ -133,7 +132,9 @@ pub(crate) fn rewrite_match(
} }
} else { } else {
let span_after_cond = mk_sp(cond.span.hi(), span.hi()); let span_after_cond = mk_sp(cond.span.hi(), span.hi());
Some(format!(
match match_kind {
MatchKind::Prefix => Some(format!(
"match {}{}{{\n{}{}{}\n{}}}", "match {}{}{{\n{}{}{}\n{}}}",
cond_str, cond_str,
block_sep, block_sep,
@ -141,7 +142,17 @@ pub(crate) fn rewrite_match(
nested_indent_str, nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?, rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config), shape.indent.to_string(context.config),
)) )),
MatchKind::Postfix => Some(format!(
"{}.match{}{{\n{}{}{}\n{}}}",
cond_str,
block_sep,
inner_attrs_str,
nested_indent_str,
rewrite_match_arms(context, arms, shape, span_after_cond, open_brace_pos)?,
shape.indent.to_string(context.config),
)),
}
} }
} }

View File

@ -0,0 +1,20 @@
#![feature(postfix_match)]
fn main() {
let val = Some(42);
val.match {
Some(_) => 2,
_ => 1
};
Some(2).match {
Some(_) => true,
None => false
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}

View File

@ -0,0 +1,20 @@
#![feature(postfix_match)]
fn main() {
let val = Some(42);
val.match {
Some(_) => 2,
_ => 1,
};
Some(2).match {
Some(_) => true,
None => false,
}.match {
false => "ferris is cute",
true => "I turn cats in to petted cats",
}.match {
_ => (),
}
}