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>,

View file

@ -0,0 +1,58 @@
@0-16 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(2147483648),
],
regions: [
@0-14,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
spaces: [],
type_defs: [],
value_defs: [
Annotation(
@0-1 Apply(
@0-1 Identifier {
ident: "u",
},
[
@2-11 Apply(
@2-8 StrLiteral(
Block(
[],
),
),
[
@9-10 NumLiteral(
"0",
),
],
),
],
),
@13-14 BoundVariable(
"f",
),
),
],
},
@15-16 SpaceBefore(
Var {
module_name: "",
ident: "s",
},
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -471,6 +471,7 @@ mod test_snapshots {
pass/multiline_apply_equals_multiline_apply.expr,
pass/multiline_backpassing.expr,
pass/multiline_binop_when_with_comments.expr,
pass/multiline_str_apply_in_parens_pat.expr,
pass/multiline_str_crazyness.expr,
pass/multiline_str_in_pat.expr,
pass/multiline_str_opt_field.expr,