Skip to content

Commit

Permalink
fix: the last line of block comment doesn't converge
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Oct 7, 2024
1 parent faf0caa commit dbe4418
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/pretty/func_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use itertools::Itertools;
use pretty::BoxDoc;
use typst_syntax::{ast::*, SyntaxKind, SyntaxNode};

use crate::{pretty::trivia, util::FoldStyle, PrettyPrinter};
use crate::{util::FoldStyle, PrettyPrinter};

use super::{
table,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> ParenthesizedFuncCallArg<'a> {
inner
}
ParenthesizedFuncCallArg::LineComment(comment)
| ParenthesizedFuncCallArg::BlockComment(comment) => trivia(comment),
| ParenthesizedFuncCallArg::BlockComment(comment) => super::comment(comment),
}
}
}
Expand Down
55 changes: 37 additions & 18 deletions src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl PrettyPrinter {
let expr_doc = self.convert_expr(expr);
doc = doc.append(expr_doc);
} else {
doc = doc.append(trivia(node));
doc = doc.append(comment(node));
}
}
}
Expand Down Expand Up @@ -464,12 +464,9 @@ impl PrettyPrinter {
{
if !codes.is_empty() && is_attached_comment(i) {
let last = codes.pop().unwrap();
codes.push(
last.append(BoxDoc::space())
.append(to_doc(std::borrow::Cow::Borrowed(node.text()), true)),
);
codes.push(last.append(BoxDoc::space()).append(comment(node)));
} else {
codes.push(to_doc(std::borrow::Cow::Borrowed(node.text()), true));
codes.push(comment(node));
}
} else if node.kind() == SyntaxKind::Space {
let newline_cnt = node.text().chars().filter(|c| *c == '\n').count();
Expand Down Expand Up @@ -815,10 +812,10 @@ impl PrettyPrinter {
doc = doc.append(BoxDoc::text("else"));
doc = doc.append(BoxDoc::space());
} else if child.kind() == SyntaxKind::BlockComment {
doc = doc.append(trivia(child));
doc = doc.append(comment(child));
doc = doc.append(BoxDoc::space());
} else if child.kind() == SyntaxKind::LineComment {
doc = doc.append(trivia(child));
doc = doc.append(comment(child));
doc = doc.append(BoxDoc::hardline());
} else {
match expr_type {
Expand Down Expand Up @@ -862,10 +859,10 @@ impl PrettyPrinter {
doc = doc.append(BoxDoc::text("while"));
doc = doc.append(BoxDoc::space());
} else if child.kind() == SyntaxKind::BlockComment {
doc = doc.append(trivia(child));
doc = doc.append(comment(child));
doc = doc.append(BoxDoc::space());
} else if child.kind() == SyntaxKind::LineComment {
doc = doc.append(trivia(child));
doc = doc.append(comment(child));
doc = doc.append(BoxDoc::hardline());
} else if let Some(expr) = child.cast() {
doc = doc.append(self.convert_expr(expr));
Expand Down Expand Up @@ -1069,22 +1066,44 @@ impl PrettyPrinter {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StripMode {
None,
Prefix,
PrefixOnBoundaryMarkers,
}

fn trivia(node: &SyntaxNode) -> BoxDoc<'_, ()> {
to_doc(std::borrow::Cow::Borrowed(node.text()), false)
to_doc(std::borrow::Cow::Borrowed(node.text()), StripMode::None)
}

fn comment(node: &SyntaxNode) -> BoxDoc<'_, ()> {
to_doc(std::borrow::Cow::Borrowed(node.text()), StripMode::Prefix)
}

pub fn to_doc(s: Cow<'_, str>, strip_prefix: bool) -> BoxDoc<'_, ()> {
let get_line = |s: &str| {
if strip_prefix {
s.trim_start().to_string()
pub fn to_doc(s: Cow<'_, str>, strip_prefix: StripMode) -> BoxDoc<'_, ()> {
let get_line = |i: itertools::Position, line: &str| {
let should_trim = matches!(strip_prefix, StripMode::Prefix)
|| (matches!(strip_prefix, StripMode::PrefixOnBoundaryMarkers)
&& matches!(
i,
itertools::Position::First
| itertools::Position::Last
| itertools::Position::Only
));

if should_trim {
line.trim_start().to_string()
} else {
s.to_string()
line.to_string()
}
};
// String::lines() doesn't include the trailing newline
let has_trailing_newline = s.ends_with('\n');
let res = BoxDoc::intersperse(
s.lines().map(|s| BoxDoc::text(get_line(s))),
s.lines()
.with_position()
.map(|(i, s)| BoxDoc::text(get_line(i, s))),
BoxDoc::hardline(),
);
if has_trailing_newline {
Expand All @@ -1109,7 +1128,7 @@ mod tests {
"123\n4568\n789\n",
];
for test in tests.into_iter() {
insta::assert_debug_snapshot!(to_doc(test.into(), false));
insta::assert_debug_snapshot!(to_doc(test.into(), StripMode::None));
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/assets/unit/comment/comment-convergence.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[
/* bar
*/
]

- /* bar
*/ 123

- /* bar
*/

#[
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
]

#[
// Somebody write this up:
// - 1000 participants.
// - 2x2 data design.
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ input_file: tests/assets/unit/comment/block-comment.typ
---
Our study design is as follows:
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
- 1000 participants.
- 2x2 data design. */

#if draw-edge == auto {
draw-edge = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ input_file: tests/assets/unit/comment/block-comment.typ
---
Our study design is as follows:
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
- 1000 participants.
- 2x2 data design. */

#if draw-edge == auto {
draw-edge = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ input_file: tests/assets/unit/comment/block-comment.typ
---
Our study design is as follows:
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
- 1000 participants.
- 2x2 data design. */

#if draw-edge == auto {
draw-edge = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ input_file: tests/assets/unit/comment/block-comment.typ
---
Our study design is as follows:
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
- 1000 participants.
- 2x2 data design. */

#if draw-edge == auto {
draw-edge = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-convergence.typ
---
#[
/* bar
*/
]

- /* bar
*/ 123

- /* bar
*/

#[
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
]

#[
// Somebody write this up:
// - 1000 participants.
// - 2x2 data design.
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-convergence.typ
---
#[
/* bar
*/
]

- /* bar
*/ 123

- /* bar
*/

#[
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
]

#[
// Somebody write this up:
// - 1000 participants.
// - 2x2 data design.
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-convergence.typ
---
#[
/* bar
*/
]

- /* bar
*/ 123

- /* bar
*/

#[
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
]

#[
// Somebody write this up:
// - 1000 participants.
// - 2x2 data design.
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/unit/comment/comment-convergence.typ
---
#[
/* bar
*/
]

- /* bar
*/ 123

- /* bar
*/

#[
/* Somebody write this up:
- 1000 participants.
- 2x2 data design. */
]

#[
// Somebody write this up:
// - 1000 participants.
// - 2x2 data design.
]

0 comments on commit dbe4418

Please sign in to comment.