Implement mut ref/mut ref mut

This commit is contained in:
Jules Bertholet 2024-03-23 21:04:45 -04:00
parent 5e141e034c
commit 645b94c155

View File

@ -107,18 +107,19 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
}
PatKind::Box(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, shape),
PatKind::Ident(BindingAnnotation(by_ref, mutability), ident, ref sub_pat) => {
let prefix = match by_ref {
ByRef::Yes => "ref",
ByRef::No => "",
let mut_prefix = format_mutability(mutability).trim();
let (ref_kw, mut_infix) = match by_ref {
ByRef::Yes(rmutbl) => ("ref", format_mutability(rmutbl).trim()),
ByRef::No => ("", ""),
};
let mut_infix = format_mutability(mutability).trim();
let id_str = rewrite_ident(context, ident);
let sub_pat = match *sub_pat {
Some(ref p) => {
// 2 - `@ `.
let width = shape
.width
.checked_sub(prefix.len() + mut_infix.len() + id_str.len() + 2)?;
let width = shape.width.checked_sub(
mut_prefix.len() + ref_kw.len() + mut_infix.len() + id_str.len() + 2,
)?;
let lo = context.snippet_provider.span_after(self.span, "@");
combine_strs_with_missing_comments(
context,
@ -132,33 +133,55 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
None => "".to_owned(),
};
// combine prefix and mut
let (first_lo, first) = if !prefix.is_empty() && !mut_infix.is_empty() {
let hi = context.snippet_provider.span_before(self.span, "mut");
let lo = context.snippet_provider.span_after(self.span, "ref");
(
// combine prefix and ref
let (first_lo, first) = match (mut_prefix.is_empty(), ref_kw.is_empty()) {
(false, false) => {
let lo = context.snippet_provider.span_after(self.span, "mut");
let hi = context.snippet_provider.span_before(self.span, "ref");
(
context.snippet_provider.span_after(self.span, "ref"),
combine_strs_with_missing_comments(
context,
mut_prefix,
ref_kw,
mk_sp(lo, hi),
shape,
true,
)?,
)
}
(false, true) => (
context.snippet_provider.span_after(self.span, "mut"),
combine_strs_with_missing_comments(
context,
prefix,
mut_infix,
mk_sp(lo, hi),
shape,
true,
)?,
)
} else if !prefix.is_empty() {
(
mut_prefix.to_owned(),
),
(true, false) => (
context.snippet_provider.span_after(self.span, "ref"),
prefix.to_owned(),
)
} else if !mut_infix.is_empty() {
(
context.snippet_provider.span_after(self.span, "mut"),
mut_infix.to_owned(),
)
} else {
(self.span.lo(), "".to_owned())
ref_kw.to_owned(),
),
(true, true) => (self.span.lo(), "".to_owned()),
};
// combine result of above and mut
let (second_lo, second) = match (first.is_empty(), mut_infix.is_empty()) {
(false, false) => {
let lo = context.snippet_provider.span_after(self.span, "ref");
let end_span = mk_sp(first_lo, self.span.hi());
let hi = context.snippet_provider.span_before(end_span, "mut");
(
context.snippet_provider.span_after(end_span, "mut"),
combine_strs_with_missing_comments(
context,
&first,
mut_infix,
mk_sp(lo, hi),
shape,
true,
)?,
)
}
(false, true) => (first_lo, first),
(true, false) => unreachable!("mut_infix necessarily follows a ref"),
(true, true) => (self.span.lo(), "".to_owned()),
};
let next = if !sub_pat.is_empty() {
@ -177,9 +200,9 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
combine_strs_with_missing_comments(
context,
&first,
&second,
&next,
mk_sp(first_lo, ident.span.lo()),
mk_sp(second_lo, ident.span.lo()),
shape,
true,
)