mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-08 04:50:32 +00:00
Handle multiline string at the start of a pattern
This commit is contained in:
parent
10e7e24184
commit
0d182fbd28
6 changed files with 104 additions and 1 deletions
|
@ -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),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue