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 28, 2024
1 parent 3b647e2 commit 25f0a2a
Show file tree
Hide file tree
Showing 17 changed files with 475 additions and 114 deletions.
15 changes: 15 additions & 0 deletions src/pretty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ impl<'a> ListStylist<'a> {
}
}

pub fn convert_parenthesized(mut self, parenthesized: Parenthesized<'a>) -> ArenaDoc<'a> {
self.process_list(parenthesized.to_untyped(), |node| {
self.printer.convert_pattern(node)
});

self.pretty_commented_items(ListStyle {
separator: "",
delim: ("(", ")"),
add_space_if_empty: false,
add_trailing_sep_single: false,
omit_delim_single: false,
omit_delim_flat: false,
})
}

pub fn convert_array(mut self, array: Array<'a>) -> ArenaDoc<'a> {
self.fold_style = self.printer.get_fold_style(array);
self.process_list(array.to_untyped(), |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 @@ -560,11 +560,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::{list::ListStylist, 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_comment_unable(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 the 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.
ListStylist::new(self).convert_parenthesized(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 25f0a2a

Please sign in to comment.