mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-01 21:40:58 +00:00
Generalize fmt rules around multiline strs in pats
This commit is contained in:
parent
d52595738f
commit
8b7ac512f8
5 changed files with 83 additions and 18 deletions
|
|
@ -201,9 +201,24 @@ fn fmt_pattern_only(
|
|||
|
||||
let mut was_multiline = arg.item.is_multiline();
|
||||
|
||||
let before = merge_spaces(buf.text.bump(), last_after, arg.before);
|
||||
let mut before = merge_spaces(buf.text.bump(), last_after, arg.before);
|
||||
|
||||
if !before.is_empty() {
|
||||
if starts_with_block_str(&arg.item) {
|
||||
// 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];
|
||||
}
|
||||
|
||||
if !is_multiline {
|
||||
was_multiline |= before.iter().any(|s| s.is_comment());
|
||||
fmt_comments_only(buf, before.iter(), NewlineAt::Bottom, indent_more)
|
||||
|
|
@ -528,23 +543,6 @@ 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) {
|
||||
// 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 = inner
|
||||
.before
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|&&s| matches!(s, CommentOrNewline::Newline))
|
||||
.count();
|
||||
inner.before = &inner.before[..inner.before.len() - chop_off];
|
||||
}
|
||||
|
||||
inner
|
||||
}
|
||||
Pattern::SpaceAfter(expr, spaces) => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
4
|
||||
"""
|
||||
""" : C
|
||||
U
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
@0-15 SpaceAfter(
|
||||
Defs(
|
||||
Defs {
|
||||
tags: [
|
||||
EitherIndex(2147483648),
|
||||
],
|
||||
regions: [
|
||||
@0-13,
|
||||
],
|
||||
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(
|
||||
@1-2 Apply(
|
||||
@1-2 SpaceAfter(
|
||||
NumLiteral(
|
||||
"4",
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
[
|
||||
@5-11 StrLiteral(
|
||||
Block(
|
||||
[],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@12-13 Apply(
|
||||
"",
|
||||
"C",
|
||||
[],
|
||||
),
|
||||
),
|
||||
],
|
||||
},
|
||||
@14-15 SpaceBefore(
|
||||
Tag(
|
||||
"U",
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
),
|
||||
),
|
||||
[
|
||||
Newline,
|
||||
],
|
||||
)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(4
|
||||
|
||||
)"""""":C
|
||||
U
|
||||
|
|
@ -480,6 +480,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_after_newlines_in_pat.expr,
|
||||
pass/multiline_str_apply_in_parens_pat.expr,
|
||||
pass/multiline_str_crazyness.expr,
|
||||
pass/multiline_str_in_backpassing_pats.expr,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue