fix: adjust comment formatting in pipelines closes #985

This commit is contained in:
rvcas 2024-12-30 22:33:13 -05:00
parent e1d46fa8f0
commit 5f8b252783
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
5 changed files with 51 additions and 19 deletions

View File

@ -13,7 +13,8 @@
### Fixed ### 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 ## v1.1.9 - 2024-12-13

View File

@ -14,9 +14,7 @@ use crate::{
extra::{Comment, ModuleExtra}, extra::{Comment, ModuleExtra},
token::Base, token::Base,
}, },
pretty::{ pretty::{break_, concat, flex_break, join, line, lines, nil, Document, Documentable},
break_, concat, flex_break, join, line, lines, nil, prebreak, Document, Documentable,
},
tipo::{self, Type}, tipo::{self, Type},
}; };
use itertools::Itertools; use itertools::Itertools;
@ -1472,13 +1470,18 @@ impl<'comments> Formatter<'comments> {
one_liner: bool, one_liner: bool,
) -> Document<'a> { ) -> Document<'a> {
let mut docs = Vec::with_capacity(expressions.len() * 3); let mut docs = Vec::with_capacity(expressions.len() * 3);
let first = expressions.first(); let first = expressions.first();
let first_precedence = first.binop_precedence(); let first_precedence = first.binop_precedence();
let first = self.wrap_expr(first); let first = self.wrap_expr(first);
docs.push(self.operator_side(first, 5, first_precedence)); docs.push(self.operator_side(first, 5, first_precedence));
for expr in expressions.iter().skip(1) { for expr in expressions.iter().skip(1) {
let comments = self.pop_comments(expr.location().start); let comments = self.pop_comments(expr.location().start);
let doc = match expr { let doc = match expr {
UntypedExpr::Fn { UntypedExpr::Fn {
fn_style: FnStyle::Capture, fn_style: FnStyle::Capture,
@ -1489,23 +1492,19 @@ impl<'comments> Formatter<'comments> {
_ => self.wrap_expr(expr), _ => 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 let expr = self
.operator_side(doc, 4, expr.binop_precedence()) .operator_side(doc, 4, expr.binop_precedence())
.nest(2 * INDENT); .nest(2 * INDENT);
match printed_comments(comments, true) { docs.push(expr);
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())),
);
}
}
} }
if one_liner { if one_liner {

View File

@ -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
}
"#
);
}

View File

@ -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
}

View File

@ -1,14 +1,17 @@
--- ---
source: crates/aiken-lang/src/tests/format.rs 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" 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() { fn foo() {
a |> b |> c |> d a |> b |> c |> d
} }
fn foo() { fn foo() {
a // Foo a
|> b // Some comments // Foo
|> b
// Some comments
|> c |> c
|> d |> d
} }