Correctly nest multiline pipeline expressions.

This commit is contained in:
KtorZ 2023-03-30 12:48:15 +02:00
parent cb5dc75326
commit 17431daaa4
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
4 changed files with 39 additions and 8 deletions

View File

@ -1030,18 +1030,20 @@ impl<'comments> Formatter<'comments> {
_ => self.wrap_expr(expr), _ => self.wrap_expr(expr),
}; };
let expr = self.operator_side(doc, 4, expr.binop_precedence()); let expr = self
.operator_side(doc, 4, expr.binop_precedence())
.nest(2 * INDENT + 1);
match printed_comments(comments, true) { match printed_comments(comments, true) {
None => { None => {
let pipe = prebreak("|> ", " |> ").nest(2); let pipe = prebreak("|> ", " |> ").nest(INDENT);
docs.push(pipe.append(expr)); docs.push(pipe.append(expr));
} }
Some(comments) => { Some(comments) => {
let pipe = prebreak("|> ", "|> "); let pipe = prebreak("|> ", "|> ");
docs.push( docs.push(
" ".to_doc() " ".to_doc()
.append(comments.nest(2).append(pipe.append(expr).group())), .append(comments.nest(INDENT).append(pipe.append(expr).group())),
); );
} }
} }

View File

@ -723,3 +723,28 @@ fn format_trace_callback() {
assert_fmt(src, expected); assert_fmt(src, expected);
} }
#[test]
fn format_pipe_fn() {
let src = indoc! { r#"
fn foo() {
outputs
|> list.any(
fn(output) { value.quantity_of(output.value, policy_id, asset_name) == 1 },
)
}
"#};
let expected = indoc! { r#"
fn foo() {
outputs
|> list.any(
fn(output) {
value.quantity_of(output.value, policy_id, asset_name) == 1
},
)
}
"#};
assert_fmt(src, expected);
}

View File

@ -20,7 +20,7 @@ validator {
} }
} }
validator (output_reference: OutputReference) { validator(output_reference: OutputReference) {
fn mint(_redeemer: Void, ctx: ScriptContext) -> Bool { fn mint(_redeemer: Void, ctx: ScriptContext) -> Bool {
when when
list.find( list.find(

View File

@ -10,8 +10,10 @@ type DayOfTheWeek {
fn is_work(day: DayOfTheWeek) { fn is_work(day: DayOfTheWeek) {
when day is { when day is {
Tuesday | Wednesday | Thursday | Friday | Saturday -> True Tuesday | Wednesday | Thursday | Friday | Saturday ->
_ -> False True
_ ->
False
} }
} }
@ -25,10 +27,12 @@ test is_work_2() {
fn is_happy_hour(day: DayOfTheWeek, current_time: Int) { fn is_happy_hour(day: DayOfTheWeek, current_time: Int) {
when day is { when day is {
Monday | Sunday -> True Monday | Sunday ->
True
Tuesday | Wednesday | Thursday | Friday | Saturday if current_time > 18 -> Tuesday | Wednesday | Thursday | Friday | Saturday if current_time > 18 ->
True True
_ -> False _ ->
False
} }
} }