-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
impl rewrite_result for ArmWrapper #6239
Conversation
let cond_shape = Shape::indented(shape.indent.block_indent(context.config), context.config) | ||
.offset_left(3) | ||
.and_then(|s| s.sub_width(5)); | ||
if let Some(cond_shape) = cond_shape { | ||
if let Some(cond_str) = guard.rewrite(context, cond_shape) { | ||
return Some(format!( | ||
"{}if {}", | ||
cond_shape.indent.to_string_with_newline(context.config), | ||
cond_str | ||
)); | ||
} | ||
} | ||
|
||
None | ||
.and_then(|s| s.sub_width(5)) | ||
.max_width_error(shape.width, guard.span)?; | ||
let cond_str = guard.rewrite_result(context, cond_shape)?; | ||
Ok(format!( | ||
"{}if {}", | ||
cond_shape.indent.to_string_with_newline(context.config), | ||
cond_str | ||
)) | ||
} else { | ||
Some(String::new()) | ||
Ok(String::new()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some small refactoring here: we can return RewriteError
early when cond_shape
exceeds the max width and when it fails to rewrite the arm guard. The original source code returns Some(..)
only when both conditions are not met.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I think this change is sound
79071c3
to
ad586a7
Compare
src/matches.rs
Outdated
shape.sub_width(7 + label_len)?.offset_left(pipe_offset)? | ||
shape | ||
.sub_width(7 + label_len) | ||
.max_width_error(shape.width, arm.span)? | ||
.offset_left(pipe_offset) | ||
.max_width_error(shape.width, arm.span)? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunate that we have to call .max_width_error
twice here, but it makes sense. I think we should eventually change Shap::sub_width
and other methods to return Result<Shape, RewriteError>
, but that can wait for a future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can replace that with .sub_width().and_then(|| offset_left()).max_width_error()
to avoid calling .max_width_error
twice, but there might be a better approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I think that would work well here!
ad586a7
to
7678f3f
Compare
7678f3f
to
6d1e4a9
Compare
Running the Diff-Check job now. Edit: The job passed ✅ |
Tracked by #6206
Description
This pr implements
rewrite_result
forArmWrapper
(wrapper for astArm
), while modifying functions used for formatting match expression likerewrite_match
,rewrite_match_arm
and so on.