Skip to content

Commit

Permalink
Changes per review comments (v3)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBar-On committed Mar 30, 2021
1 parent cc0daad commit e4ab01e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
43 changes: 12 additions & 31 deletions src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,16 @@ pub(crate) fn rewrite_rhs_expr<R: Rewrite>(
)
}

fn rewrite_assign_rhs_expr<R: Rewrite>(
context: &RewriteContext<'_>,
lhs: &str,
ex: &R,
shape: Shape,
rhs_tactics: RhsTactics,
) -> Option<String> {
rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, "=")
}

pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
context: &RewriteContext<'_>,
lhs: S,
Expand All @@ -1981,7 +1991,7 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
rhs_tactics: RhsTactics,
) -> Option<String> {
let lhs = lhs.into();
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, "=")?;
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
Some(lhs + &rhs)
}

Expand All @@ -2001,7 +2011,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
} else {
shape
};
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, "=")?;
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;

if contains_comment {
let rhs = rhs.trim_start();
Expand All @@ -2011,35 +2021,6 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
}
}

pub(crate) fn rewrite_trait_rhs_with_comments<S: Into<String>, R: Rewrite>(
context: &RewriteContext<'_>,
lhs: S,
ex: &R,
shape: Shape,
rhs_tactics: RhsTactics,
between_span: Span,
allow_extend: bool,
) -> Option<String> {
let lhs = lhs.into();
let contains_comment = contains_comment(context.snippet(between_span));

let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, ":")?;

if contains_comment {
let rhs = rhs.trim_start();
combine_strs_with_missing_comments(
context,
&lhs,
&rhs,
between_span,
shape.block_left(context.config.tab_spaces())?,
allow_extend,
)
} else {
Some(lhs + &rhs)
}
}

fn choose_rhs<R: Rewrite>(
context: &RewriteContext<'_>,
expr: &R,
Expand Down
32 changes: 31 additions & 1 deletion src/formatting/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::formatting::{
},
expr::{
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
rewrite_assign_rhs_with_comments, rewrite_trait_rhs_with_comments, RhsTactics,
rewrite_assign_rhs_with_comments, rewrite_rhs_expr, RhsTactics,
},
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator},
macros::{rewrite_macro, MacroPosition},
Expand Down Expand Up @@ -1114,6 +1114,36 @@ fn format_struct(
}
}

pub(crate) fn rewrite_trait_rhs_with_comments<S: Into<String>, R: Rewrite>(
context: &RewriteContext<'_>,
lhs: S,
ex: &R,
shape: Shape,
rhs_tactics: RhsTactics,
between_span: Span,
allow_extend: bool,
) -> Option<String> {
let lhs = lhs.into();
let contains_comment = contains_comment(context.snippet(between_span));

// Not as with `expr` - `shape` is changed only for comments combine, not for rewrite
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, ":")?;

if contains_comment {
let rhs = rhs.trim_start();
combine_strs_with_missing_comments(
context,
&lhs,
&rhs,
between_span,
shape.block_left(context.config.tab_spaces())?,
allow_extend,
)
} else {
Some(lhs + &rhs)
}
}

pub(crate) fn format_trait(
context: &RewriteContext<'_>,
item: &ast::Item,
Expand Down

0 comments on commit e4ab01e

Please sign in to comment.