Skip to content

Commit

Permalink
fix: should never fold with line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadnucYard committed Nov 28, 2024
1 parent 3b647e2 commit 2de8a57
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/pretty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ListStylist<'a> {
free_comments: Vec<ArenaDoc<'a>>,
items: Vec<Item<'a>>,
item_count: usize,
has_line_comment: bool,
fold_style: FoldStyle,
}

Expand Down Expand Up @@ -45,6 +46,7 @@ impl<'a> ListStylist<'a> {
free_comments: Default::default(),
items: Default::default(),
item_count: 0,
has_line_comment: false,
fold_style: FoldStyle::Fit,
}
}
Expand Down Expand Up @@ -190,6 +192,7 @@ impl<'a> ListStylist<'a> {
} else if is_comment_node(node) {
// Line comment cannot appear in single line block
if node.kind() == SyntaxKind::LineComment {
self.has_line_comment = true;
self.fold_style = FoldStyle::Never;
}
self.free_comments.push(self.printer.convert_comment(node));
Expand Down Expand Up @@ -244,7 +247,7 @@ impl<'a> ListStylist<'a> {
};
}
let (open, close) = delim;
let multi = {
let multi = || {
let mut inner = self.arena.nil();
for item in self.items.iter() {
match item {
Expand Down Expand Up @@ -286,9 +289,14 @@ impl<'a> ListStylist<'a> {
inner.enclose(open, close)
}
};
match self.fold_style {
FoldStyle::Never => multi,
FoldStyle::Fit => multi.clone().flat_alt(flat()).group(),
let fold_style = if self.has_line_comment {
FoldStyle::Never
} else {
self.fold_style
};
match fold_style {
FoldStyle::Never => multi(),
FoldStyle::Fit => multi().flat_alt(flat()).group(),
FoldStyle::Always => flat(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/pretty/style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// A style for formatting items
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FoldStyle {
/// Fold items if them can fit in a single line
Fit,
Expand Down
3 changes: 3 additions & 0 deletions tests/assets/unit/comment/comment-in-destruct.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ a, /* 1 */b, /* 2 */c, /* 3 */
#let (a, b:/* 8 */ (../* 9 */, /* 10 */d), ..c) = (a:1, b:(c: 4, d: 5))
#let (a/* 11 */,) = (a:1, b:(c: 4, d: 5))
#let (../* 12 */) = (a:1, b:(c: 4, d: 5))

#let ( // b
/* 2 */ (a ) , /* 3 */) = ( ( ), )
31 changes: 31 additions & 0 deletions tests/assets/unit/comment/comment-in-params.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#let f( // aaa
a // bbb
) = {}

#let f(// aaa
a // bbb
) = {}

#let g = ( // ccc
a // ddd
) => { }

#let g = (// ccc
a // ddd
) => { }

#let g = (a// ccc
// ddd
) => { }

#let f( (// aaa
a // bbb
)) = {}

#let g = ((// ccc
a // ddd
)) => { }

#let g = ((a// ccc
// ddd
)) => { }
10 changes: 10 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ snapshot_kind: text
d: 5,
),
)

#let (
// b
/* 2 */
(
a
), /* 3 */
) = (
(),
)
5 changes: 5 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ snapshot_kind: text
#let (a, b: /* 8 */ (.. /* 9 */, /* 10 */ d), ..c) = (a: 1, b: (c: 4, d: 5))
#let (a /* 11 */,) = (a: 1, b: (c: 4, d: 5))
#let (.. /* 12 */) = (a: 1, b: (c: 4, d: 5))

#let (
// b
/* 2 */ (a), /* 3 */
) = ((),)
5 changes: 5 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ snapshot_kind: text
a: 1,
b: (c: 4, d: 5),
)

#let (
// b
/* 2 */ (a), /* 3 */
) = ((),)
5 changes: 5 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ snapshot_kind: text
#let (a, b: /* 8 */ (.. /* 9 */, /* 10 */ d), ..c) = (a: 1, b: (c: 4, d: 5))
#let (a /* 11 */,) = (a: 1, b: (c: 4, d: 5))
#let (.. /* 12 */) = (a: 1, b: (c: 4, d: 5))

#let (
// b
/* 2 */ (a), /* 3 */
) = ((),)
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-in-params.typ
snapshot_kind: text
---
#let f(
// aaa
a, // bbb
) = { }

#let f(
// aaa
a, // bbb
) = { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
a, // ccc
// ddd
) => { }

#let f(
(// aaa
a // bbb
),
) = { }

#let g = (
(// ccc
a // ddd
),
) => { }

#let g = (
(a// ccc
// ddd
),
) => { }
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-in-params.typ
snapshot_kind: text
---
#let f(
// aaa
a, // bbb
) = { }

#let f(
// aaa
a, // bbb
) = { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
a, // ccc
// ddd
) => { }

#let f(
(// aaa
a // bbb
),
) = { }

#let g = (
(// ccc
a // ddd
),
) => { }

#let g = (
(a// ccc
// ddd
),
) => { }
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-in-params.typ
snapshot_kind: text
---
#let f(
// aaa
a, // bbb
) = { }

#let f(
// aaa
a, // bbb
) = { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
a, // ccc
// ddd
) => { }

#let f(
(// aaa
a // bbb
),
) = { }

#let g = (
(// ccc
a // ddd
),
) => { }

#let g = (
(a// ccc
// ddd
),
) => { }
48 changes: 48 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-in-params.typ
snapshot_kind: text
---
#let f(
// aaa
a, // bbb
) = { }

#let f(
// aaa
a, // bbb
) = { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
// ccc
a, // ddd
) => { }

#let g = (
a, // ccc
// ddd
) => { }

#let f(
(// aaa
a // bbb
),
) = { }

#let g = (
(// ccc
a // ddd
),
) => { }

#let g = (
(a// ccc
// ddd
),
) => { }

0 comments on commit 2de8a57

Please sign in to comment.