Skip to content

Commit

Permalink
feat: format commented parenthesized expr/pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadnucYard committed Nov 30, 2024
1 parent 6aa542c commit 84657d4
Show file tree
Hide file tree
Showing 21 changed files with 538 additions and 150 deletions.
18 changes: 18 additions & 0 deletions src/pretty/code_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ use super::{
};

impl<'a> PrettyPrinter<'a> {
pub(super) fn convert_parenthesized_impl(
&'a self,
parenthesized: Parenthesized<'a>,
) -> ArenaDoc<'a> {
ListStylist::new(self)
.process_list(parenthesized.to_untyped(), |node| {
self.convert_pattern(node)
})
.print_doc(ListStyle {
separator: "",
delim: ("(", ")"),
add_space_if_empty: false,
add_trailing_sep_single: false,
omit_delim_single: false,
omit_delim_flat: false,
})
}

pub(super) fn convert_array(&'a self, array: Array<'a>) -> ArenaDoc<'a> {
ListStylist::new(self)
.process_list(array.to_untyped(), |node| self.convert_array_item(node))
Expand Down
7 changes: 5 additions & 2 deletions src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a> PrettyPrinter<'a> {
Expr::Str(s) => self.convert_str(s),
Expr::Code(c) => self.convert_code_block(c),
Expr::Content(c) => self.convert_content_block(c),
Expr::Parenthesized(p) => self.convert_parenthesized(p, false),
Expr::Parenthesized(p) => self.convert_parenthesized(p),
Expr::Array(a) => self.convert_array(a),
Expr::Dict(d) => self.convert_dict(d),
Expr::Unary(u) => self.convert_unary(u),
Expand Down Expand Up @@ -548,11 +548,14 @@ impl<'a> PrettyPrinter<'a> {
}

fn convert_pattern(&'a self, pattern: Pattern<'a>) -> ArenaDoc<'a> {
if let Some(res) = self.check_disabled(pattern.to_untyped()) {
return res;
}
match pattern {
Pattern::Normal(n) => self.convert_expr(n),
Pattern::Placeholder(p) => self.convert_underscore(p),
Pattern::Destructuring(d) => self.convert_destructuring(d),
Pattern::Parenthesized(p) => self.convert_parenthesized(p, true),
Pattern::Parenthesized(p) => self.convert_parenthesized(p),
}
}

Expand Down
31 changes: 11 additions & 20 deletions src/pretty/parened_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@ use typst_syntax::ast::*;

use crate::PrettyPrinter;

use super::ArenaDoc;
use super::{util::is_comment_node, ArenaDoc};

impl<'a> PrettyPrinter<'a> {
/// We do not care whether it is `Pattern` or `Expr`.
/// It is safe to treat it as `Pattern`, since `Pattern` can be `Expr`.
pub(super) fn convert_parenthesized(
&'a self,
parenthesized: Parenthesized<'a>,
is_pattern: bool,
) -> ArenaDoc<'a> {
if let Some(res) = self.check_unformattable(parenthesized.to_untyped()) {
return res;
}
if is_pattern {
if let Pattern::Parenthesized(paren) = parenthesized.pattern() {
return self.convert_parenthesized(paren, true);
let pattern = parenthesized.pattern();
if let Pattern::Parenthesized(paren) = pattern {
if !parenthesized.to_untyped().children().any(is_comment_node) {
// Remove a layer of paren if no comment inside.
return self.convert_parenthesized(paren);
}
} else if let Expr::Parenthesized(paren) = parenthesized.expr() {
return self.convert_parenthesized(paren, false);
}
let inner = if is_pattern {
self.convert_pattern(parenthesized.pattern())
} else {
self.convert_expr(parenthesized.expr())
};
inner
.enclose(self.arena.line_(), self.arena.line_())
.nest(2)
.group()
.parens()

// Treat is as a list with a single item.
self.convert_parenthesized_impl(parenthesized)
}

/// Convert an expression with optional parentheses.
Expand Down
25 changes: 25 additions & 0 deletions tests/assets/unit/comment/comment-in-code.typ
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ let /* 1 */ ( /* 2 */ a /* 3 */ , /* 4 */ b /* 5 */ ) /* 6 */ = /* 7 */
#{
/* 0 */ not /* 1 */ true /* 2 */ and/* 3 */( /* 4 */ false /* 5 */or /* 6 */true/* 7 */)
}

#let /* 0 */ ((( /* 1 */ (( /* 2 */ ((a)),/* 3 */ ))))) =/* 4 */( /* 5 */ (( /* 6 */ (((1)),) /* 7 */ )))
#let /* 0 */ ((
( /* 1 */ (
(
/* 2 */ ((a)),/* 3 */ ))
)

)
) =/* 4 */( /* 5 */ (
( /* 6 */ (
((1)),) /* 7 */ /* 8 */ ))
)
#let /* 0 */ (( // a
( /* 1 */ (
( // b
/* 2 */ ((a)),/* 3 */ )) // c
)
// d
)
) =/* 4 */( /* 5 */ (
( /* 6 */ (
((1)),) /* 7 */ /* 8 */ )) // e
// f
)
Loading

0 comments on commit 84657d4

Please sign in to comment.