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