Handle multiline string at the start of a pattern

This commit is contained in:
Joshua Warner 2024-12-15 10:21:49 -08:00
parent 10e7e24184
commit 0d182fbd28
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
6 changed files with 104 additions and 1 deletions

View file

@ -465,6 +465,7 @@ pub fn pattern_fmt_apply(
.count();
before = &before[..before.len() - chop_off];
}
handle_multiline_str_spaces(&arg.item, &mut before);
if !is_multiline {
was_multiline |= before.iter().any(|s| s.is_comment());
@ -571,6 +572,9 @@ pub fn pattern_lift_spaces<'a, 'b: 'a>(
Pattern::SpaceBefore(expr, spaces) => {
let mut inner = pattern_lift_spaces(arena, expr);
inner.before = merge_spaces(arena, spaces, inner.before);
handle_multiline_str_spaces(expr, &mut inner.before);
inner
}
Pattern::SpaceAfter(expr, spaces) => {
@ -586,6 +590,23 @@ pub fn pattern_lift_spaces<'a, 'b: 'a>(
}
}
fn handle_multiline_str_spaces<'a>(pat: &Pattern<'_>, before: &mut &'a [CommentOrNewline<'a>]) {
if starts_with_block_str(pat) {
// Ick!
// The block string will keep "generating" newlines when formatted (it wants to start on its own line),
// so we strip one out here.
//
// Note that this doesn't affect Expr's because those have explicit parens, and we can control
// whether spaces cross that boundary.
let chop_off = before
.iter()
.rev()
.take_while(|&&s| matches!(s, CommentOrNewline::Newline))
.count();
*before = &before[..before.len() - chop_off];
}
}
fn starts_with_block_str(item: &Pattern<'_>) -> bool {
match item {
Pattern::As(inner, _) | Pattern::Apply(inner, _) => starts_with_block_str(&inner.value),