Skip to content

Commit

Permalink
rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhuichen committed Sep 1, 2024
1 parent 46cb7d3 commit 6da81be
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,25 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
rhs_kind: &RhsAssignKind<'_>,
rhs_tactics: RhsTactics,
) -> Option<String> {
let get_lhs_last_line_shape = || {
let last_line = lhs.rsplitn(2, "\n").next().unwrap_or_default();
let tab_spaces = context.config.tab_spaces();
let new_shape = shape
.block_indent(tab_spaces)
.saturating_sub_width(tab_spaces);
let extra_indent_string = new_shape.to_string(&context.config).to_string();
if last_line.starts_with(&extra_indent_string) {
new_shape
} else {
shape
}
};
let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
get_lhs_last_line_shape()
} else {
shape
};

let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ impl Shape {
offset_indent.to_string_inner(config, 0)
}

pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> {
let mut offset_indent = self.indent;
offset_indent.alignment = self.offset;
offset_indent.to_string_inner(config, 1)
}

/// Creates a `Shape` with a virtually infinite width.
pub(crate) fn infinite_width(&self) -> Shape {
Shape {
Expand Down
24 changes: 24 additions & 0 deletions tests/source/rust-lang/style-team#189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
}
}
24 changes: 24 additions & 0 deletions tests/target/rust-lang/style-team#189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
}
}

0 comments on commit 6da81be

Please sign in to comment.