From 2de8a5727f391db9bbc0f2e621b230f61263d2d2 Mon Sep 17 00:00:00 2001 From: QuadnucYard <2380433991@qq.com> Date: Thu, 28 Nov 2024 18:25:43 +0800 Subject: [PATCH] fix: should never fold with line comments --- src/pretty/list.rs | 16 +++++-- src/pretty/style.rs | 1 + .../unit/comment/comment-in-destruct.typ | 3 ++ .../assets/unit/comment/comment-in-params.typ | 31 ++++++++++++ ...nit-comment-comment-in-destruct.typ-0.snap | 10 ++++ ...t-comment-comment-in-destruct.typ-120.snap | 5 ++ ...it-comment-comment-in-destruct.typ-40.snap | 5 ++ ...it-comment-comment-in-destruct.typ-80.snap | 5 ++ ...@unit-comment-comment-in-params.typ-0.snap | 48 +++++++++++++++++++ ...nit-comment-comment-in-params.typ-120.snap | 48 +++++++++++++++++++ ...unit-comment-comment-in-params.typ-40.snap | 48 +++++++++++++++++++ ...unit-comment-comment-in-params.typ-80.snap | 48 +++++++++++++++++++ 12 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 tests/assets/unit/comment/comment-in-params.typ create mode 100644 tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-0.snap create mode 100644 tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-120.snap create mode 100644 tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-40.snap create mode 100644 tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-80.snap diff --git a/src/pretty/list.rs b/src/pretty/list.rs index 5b9ab7d..910c0f5 100644 --- a/src/pretty/list.rs +++ b/src/pretty/list.rs @@ -18,6 +18,7 @@ pub struct ListStylist<'a> { free_comments: Vec>, items: Vec>, item_count: usize, + has_line_comment: bool, fold_style: FoldStyle, } @@ -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, } } @@ -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)); @@ -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 { @@ -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(), } } diff --git a/src/pretty/style.rs b/src/pretty/style.rs index 88dfbc9..8e6765c 100644 --- a/src/pretty/style.rs +++ b/src/pretty/style.rs @@ -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, diff --git a/tests/assets/unit/comment/comment-in-destruct.typ b/tests/assets/unit/comment/comment-in-destruct.typ index 43dc42d..be23acb 100644 --- a/tests/assets/unit/comment/comment-in-destruct.typ +++ b/tests/assets/unit/comment/comment-in-destruct.typ @@ -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 */) = ( ( ), ) diff --git a/tests/assets/unit/comment/comment-in-params.typ b/tests/assets/unit/comment/comment-in-params.typ new file mode 100644 index 0000000..6fb18c2 --- /dev/null +++ b/tests/assets/unit/comment/comment-in-params.typ @@ -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 +)) => { } diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-0.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-0.snap index 34dece5..4f09ac4 100644 --- a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-0.snap +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-0.snap @@ -85,3 +85,13 @@ snapshot_kind: text d: 5, ), ) + +#let ( + // b + /* 2 */ + ( + a + ), /* 3 */ +) = ( + (), +) diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-120.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-120.snap index 2f7416e..3a6fb54 100644 --- a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-120.snap +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-120.snap @@ -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 */ +) = ((),) diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-40.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-40.snap index 13e9ba3..6a9aa99 100644 --- a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-40.snap +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-40.snap @@ -41,3 +41,8 @@ snapshot_kind: text a: 1, b: (c: 4, d: 5), ) + +#let ( + // b + /* 2 */ (a), /* 3 */ +) = ((),) diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-80.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-80.snap index 2f7416e..3a6fb54 100644 --- a/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-80.snap +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-destruct.typ-80.snap @@ -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 */ +) = ((),) diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-0.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-0.snap new file mode 100644 index 0000000..11a54c4 --- /dev/null +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-0.snap @@ -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 +), +) => { } diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-120.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-120.snap new file mode 100644 index 0000000..11a54c4 --- /dev/null +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-120.snap @@ -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 +), +) => { } diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-40.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-40.snap new file mode 100644 index 0000000..11a54c4 --- /dev/null +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-40.snap @@ -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 +), +) => { } diff --git a/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-80.snap b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-80.snap new file mode 100644 index 0000000..11a54c4 --- /dev/null +++ b/tests/snapshots/assets__check_file@unit-comment-comment-in-params.typ-80.snap @@ -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 +), +) => { }