Skip to content

Commit

Permalink
fix: adjust comment formatting in pipelines closes #985
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Dec 31, 2024
1 parent e1d46fa commit 5f8b252
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

### Fixed

- **aiken**: Fixed the panic error when using `aiken uplc decode` on cbor encoded flat bytes. @rvcas
- **aiken**: panic error when using `aiken uplc decode` on cbor encoded flat bytes. @rvcas
- **aiken-lang**: comment formatting in pipelines leading to confusion. @rvcas

## v1.1.9 - 2024-12-13

Expand Down
31 changes: 15 additions & 16 deletions crates/aiken-lang/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use crate::{
extra::{Comment, ModuleExtra},
token::Base,
},
pretty::{
break_, concat, flex_break, join, line, lines, nil, prebreak, Document, Documentable,
},
pretty::{break_, concat, flex_break, join, line, lines, nil, Document, Documentable},
tipo::{self, Type},
};
use itertools::Itertools;
Expand Down Expand Up @@ -1472,13 +1470,18 @@ impl<'comments> Formatter<'comments> {
one_liner: bool,
) -> Document<'a> {
let mut docs = Vec::with_capacity(expressions.len() * 3);

let first = expressions.first();

let first_precedence = first.binop_precedence();

let first = self.wrap_expr(first);

docs.push(self.operator_side(first, 5, first_precedence));

for expr in expressions.iter().skip(1) {
let comments = self.pop_comments(expr.location().start);

let doc = match expr {
UntypedExpr::Fn {
fn_style: FnStyle::Capture,
Expand All @@ -1489,23 +1492,19 @@ impl<'comments> Formatter<'comments> {
_ => self.wrap_expr(expr),
};

let space = if one_liner { break_("", " ") } else { line() };

let pipe = space
.append(commented("|> ".to_doc(), comments))
.nest(INDENT);

docs.push(pipe);

let expr = self
.operator_side(doc, 4, expr.binop_precedence())
.nest(2 * INDENT);

match printed_comments(comments, true) {
None => {
let pipe = prebreak("|> ", " |> ").nest(INDENT);
docs.push(pipe.append(expr));
}
Some(comments) => {
let pipe = prebreak("|> ", "|> ");
docs.push(
" ".to_doc()
.append(comments.nest(INDENT).append(pipe.append(expr).group())),
);
}
}
docs.push(expr);
}

if one_liner {
Expand Down
16 changes: 16 additions & 0 deletions crates/aiken-lang/src/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,3 +1420,19 @@ fn multiline_if_is_2() {
"#
);
}

#[test]
fn comment_in_pipeline() {
assert_format!(
r#"
fn foo() {
a
// stuff
// warning: wow
|> b
// Comment
|> c
}
"#
);
}
13 changes: 13 additions & 0 deletions crates/aiken-lang/src/tests/snapshots/comment_in_pipeline.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n a\n // stuff\n // warning: wow\n |> b\n // Comment\n |> c\n}\n"
snapshot_kind: text
---
fn foo() {
a
// stuff
// warning: wow
|> b
// Comment
|> c
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
---
source: crates/aiken-lang/src/tests/format.rs
description: "Code:\n\nfn foo() {\n a |> b |> c |> d\n}\n\nfn foo() {\n a\n // Foo\n |> b// Some comments\n |> c\n |> d\n}\n\nfn baz() {\n // Commented\n however |> it_automatically_breaks |> into_multiple_lines |> anytime_when |> it_is_too_long // What?\n}\n"
snapshot_kind: text
---
fn foo() {
a |> b |> c |> d
}

fn foo() {
a // Foo
|> b // Some comments
a
// Foo
|> b
// Some comments
|> c
|> d
}
Expand Down

0 comments on commit 5f8b252

Please sign in to comment.