Fix formatting of pipelined call exprs

This commit is contained in:
Joshua Warner 2021-12-12 19:14:29 -08:00
parent c7b826e3d4
commit 61cdbd1531
2 changed files with 38 additions and 3 deletions

View File

@ -2574,6 +2574,17 @@ mod test_fmt {
);
}
#[test]
fn pipline_op_with_apply() {
expr_formats_same(indoc!(
r#"
output
|> List.set (offset + 0) b
|> List.set (offset + 1) a
"#
));
}
// MODULES
#[test]

View File

@ -1,5 +1,6 @@
use crate::ast::{
AssignedField, Collection, CommentOrNewline, Def, Expr, Pattern, Spaceable, TypeAnnotation,
AssignedField, Collection, CommentOrNewline, Def, Expr, ExtractSpaces, Pattern, Spaceable,
TypeAnnotation,
};
use crate::blankspace::{space0_after_e, space0_around_ee, space0_before_e, space0_e};
use crate::ident::{lowercase_ident, parse_ident, Ident};
@ -483,7 +484,7 @@ fn parse_expr_final<'a>(
fn to_call<'a>(
arena: &'a Bump,
arguments: Vec<'a, &'a Located<Expr<'a>>>,
mut arguments: Vec<'a, &'a Located<Expr<'a>>>,
loc_expr1: Located<Expr<'a>>,
) -> Located<Expr<'a>> {
if arguments.is_empty() {
@ -492,12 +493,35 @@ fn to_call<'a>(
let last = arguments.last().map(|x| x.region).unwrap_or_default();
let region = Region::span_across(&loc_expr1.region, &last);
let apply = Expr::Apply(
let spaces = if let Some(last) = arguments.last_mut() {
let spaces = last.value.extract_spaces();
if spaces.after.is_empty() {
&[]
} else {
let inner = if !spaces.before.is_empty() {
arena.alloc(spaces.item).before(spaces.before)
} else {
spaces.item
};
*last = arena.alloc(Located::at(last.region, inner));
spaces.after
}
} else {
&[]
};
let mut apply = Expr::Apply(
arena.alloc(loc_expr1),
arguments.into_bump_slice(),
CalledVia::Space,
);
if !spaces.is_empty() {
apply = arena.alloc(apply).after(spaces)
}
Located::at(region, apply)
}
}