Skip to content

Commit

Permalink
rewrite_result for GenericBounds and GenericParam
Browse files Browse the repository at this point in the history
  • Loading branch information
ding-young committed Jun 30, 2024
1 parent 68ee602 commit 959c65c
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ fn rewrite_bounded_lifetime(
"{}{}{}",
result,
colon,
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
join_bounds(context, shape.sub_width(overhead)?, bounds, true).ok()?
);
Some(result)
}
Expand Down Expand Up @@ -590,8 +590,12 @@ impl Rewrite for ast::GenericBound {

impl Rewrite for ast::GenericBounds {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
self.rewrite_result(context, shape).ok()
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
if self.is_empty() {
return Some(String::new());
return Ok(String::new());
}

join_bounds(context, shape, self, true)
Expand All @@ -600,8 +604,15 @@ impl Rewrite for ast::GenericBounds {

impl Rewrite for ast::GenericParam {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
self.rewrite_result(context, shape).ok()
}

fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
// FIXME: If there are more than one attributes, this will force multiline.
let mut result = self.attrs.rewrite(context, shape).unwrap_or(String::new());
let mut result = self
.attrs
.rewrite_result(context, shape)
.unwrap_or(String::new());
let has_attrs = !result.is_empty();

let mut param = String::with_capacity(128);
Expand All @@ -615,15 +626,19 @@ impl Rewrite for ast::GenericParam {
param.push_str("const ");
param.push_str(rewrite_ident(context, self.ident));
param.push_str(": ");
param.push_str(&ty.rewrite(context, shape)?);
param.push_str(&ty.rewrite_result(context, shape)?);
if let Some(default) = default {
let eq_str = match context.config.type_punctuation_density() {
TypeDensity::Compressed => "=",
TypeDensity::Wide => " = ",
};
param.push_str(eq_str);
let budget = shape.width.checked_sub(param.len())?;
let rewrite = default.rewrite(context, Shape::legacy(budget, shape.indent))?;
let budget = shape
.width
.checked_sub(param.len())
.max_width_error(shape.width, self.span())?;
let rewrite =
default.rewrite_result(context, Shape::legacy(budget, shape.indent))?;
param.push_str(&rewrite);
}
kw_span.lo()
Expand All @@ -634,7 +649,7 @@ impl Rewrite for ast::GenericParam {

if !self.bounds.is_empty() {
param.push_str(type_bound_colon(context));
param.push_str(&self.bounds.rewrite(context, shape)?)
param.push_str(&self.bounds.rewrite_result(context, shape)?)
}
if let ast::GenericParamKind::Type {
default: Some(ref def),
Expand All @@ -645,9 +660,12 @@ impl Rewrite for ast::GenericParam {
TypeDensity::Wide => " = ",
};
param.push_str(eq_str);
let budget = shape.width.checked_sub(param.len())?;
let budget = shape
.width
.checked_sub(param.len())
.max_width_error(shape.width, self.span())?;
let rewrite =
def.rewrite(context, Shape::legacy(budget, shape.indent + param.len()))?;
def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
param.push_str(&rewrite);
}

Expand All @@ -661,7 +679,8 @@ impl Rewrite for ast::GenericParam {
mk_sp(last_attr.span.hi(), param_start),
shape,
!last_attr.is_doc_comment(),
)?;
)
.unknown_error()?;
} else {
// When rewriting generic params, an extra newline should be put
// if the attributes end with a doc comment
Expand All @@ -673,7 +692,7 @@ impl Rewrite for ast::GenericParam {
result.push_str(&param);
}

Some(result)
Ok(result)
}
}

Expand Down Expand Up @@ -913,7 +932,7 @@ impl Rewrite for ast::Ty {
let rw = if context.config.version() == Version::One {
it.rewrite_result(context, shape)
} else {
join_bounds(context, shape, it, false).unknown_error()
join_bounds(context, shape, it, false)
};
rw.map(|it_str| {
let space = if it_str.is_empty() { "" } else { " " };
Expand Down Expand Up @@ -1013,7 +1032,7 @@ fn join_bounds(
shape: Shape,
items: &[ast::GenericBound],
need_indent: bool,
) -> Option<String> {
) -> RewriteResult {
join_bounds_inner(context, shape, items, need_indent, false)
}

Expand All @@ -1023,7 +1042,7 @@ fn join_bounds_inner(
items: &[ast::GenericBound],
need_indent: bool,
force_newline: bool,
) -> Option<String> {
) -> RewriteResult {
debug_assert!(!items.is_empty());

let generic_bounds_in_order = is_generic_bounds_in_order(items);
Expand Down Expand Up @@ -1116,16 +1135,17 @@ fn join_bounds_inner(
};

let (extendable, trailing_str) = if i == 0 {
let bound_str = item.rewrite(context, shape)?;
let bound_str = item.rewrite_result(context, shape)?;
(is_bound_extendable(&bound_str, item), bound_str)
} else {
let bound_str = &item.rewrite(context, shape)?;
let bound_str = &item.rewrite_result(context, shape)?;
match leading_span {
Some(ls) if has_leading_comment => (
is_bound_extendable(bound_str, item),
combine_strs_with_missing_comments(
context, joiner, bound_str, ls, shape, true,
)?,
)
.unknown_error()?,
),
_ => (
is_bound_extendable(bound_str, item),
Expand All @@ -1142,8 +1162,9 @@ fn join_bounds_inner(
shape,
true,
)
.unknown_error()
.map(|v| (v, trailing_span, extendable)),
_ => Some((strs + &trailing_str, trailing_span, extendable)),
_ => Ok((strs + &trailing_str, trailing_span, extendable)),
}
},
)?;
Expand All @@ -1168,7 +1189,7 @@ fn join_bounds_inner(
if retry_with_force_newline {
join_bounds_inner(context, shape, items, need_indent, true)
} else {
Some(result.0)
Ok(result.0)
}
}

Expand Down

0 comments on commit 959c65c

Please sign in to comment.