Fix multiline str pattern newline multiplication

This commit is contained in:
Joshua Warner 2024-12-12 21:30:42 -08:00
parent e9a8588c3e
commit f1d9667ea0
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 92 additions and 0 deletions

View file

@ -473,6 +473,7 @@ pub fn pattern_lift_spaces<'a, 'b: 'a>(
match pat {
Pattern::Apply(func, args) => {
let func_lifted = pattern_lift_spaces(arena, &func.value);
let args = arena.alloc_slice_copy(args);
let (before, func, after) = if let Some(last) = args.last_mut() {
let last_lifted = pattern_lift_spaces(arena, &last.value);
@ -528,6 +529,19 @@ 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);
if starts_with_block_str(&inner.item)
&& matches!(inner.before.last(), Some(CommentOrNewline::Newline))
{
// 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.
inner.before = &inner.before[..inner.before.len() - 1];
}
inner
}
Pattern::SpaceAfter(expr, spaces) => {
@ -543,6 +557,17 @@ pub fn pattern_lift_spaces<'a, 'b: 'a>(
}
}
fn starts_with_block_str(item: &Pattern<'_>) -> bool {
match item {
Pattern::As(inner, _) | Pattern::Apply(inner, _) => starts_with_block_str(&inner.value),
Pattern::SpaceBefore(inner, _) | Pattern::SpaceAfter(inner, _) => {
starts_with_block_str(inner)
}
Pattern::StrLiteral(str_literal) => is_str_multiline(str_literal),
_ => false,
}
}
pub fn pattern_lift_spaces_before<'a, 'b: 'a>(
arena: &'a Bump,
pat: &Pattern<'b>,