Skip to content

Commit

Permalink
feat: remove unnecessary parens
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadnucYard committed Nov 30, 2024
1 parent 84657d4 commit 13f0ce2
Show file tree
Hide file tree
Showing 27 changed files with 182 additions and 111 deletions.
24 changes: 20 additions & 4 deletions src/pretty/code_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use typst_syntax::{ast::*, SyntaxKind};

use super::{
list::{ListStyle, ListStylist},
util::is_only_one_and,
util::{has_comment_children, is_only_one_and},
ArenaDoc, PrettyPrinter,
};

Expand All @@ -11,6 +11,20 @@ impl<'a> PrettyPrinter<'a> {
&'a self,
parenthesized: Parenthesized<'a>,
) -> ArenaDoc<'a> {
// NOTE: This is a safe cast. The parentheses for patterns are all optional.
let expr = parenthesized.expr();
let can_omit = (expr.is_literal()
|| matches!(
expr.to_untyped().kind(),
SyntaxKind::Ident
| SyntaxKind::Array
| SyntaxKind::Dict
| SyntaxKind::Destructuring
| SyntaxKind::CodeBlock
| SyntaxKind::ContentBlock
))
&& !has_comment_children(parenthesized.to_untyped());

ListStylist::new(self)
.process_list(parenthesized.to_untyped(), |node| {
self.convert_pattern(node)
Expand All @@ -21,7 +35,7 @@ impl<'a> PrettyPrinter<'a> {
add_space_if_empty: false,
add_trailing_sep_single: false,
omit_delim_single: false,
omit_delim_flat: false,
omit_delim_flat: can_omit,
})
}

Expand Down Expand Up @@ -65,7 +79,9 @@ impl<'a> PrettyPrinter<'a> {
.process_list(destructuring.to_untyped(), |node| {
self.convert_destructuring_item(node)
})
.always_fold_if(|| only_one_pattern)
.always_fold_if(|| {
only_one_pattern && !has_comment_children(destructuring.to_untyped())
})
.print_doc(ListStyle {
separator: ",",
delim: ("(", ")"),
Expand All @@ -87,7 +103,7 @@ impl<'a> PrettyPrinter<'a> {

ListStylist::new(self)
.process_list(params.to_untyped(), |node| self.convert_param(node))
.always_fold_if(|| is_single_simple)
.always_fold_if(|| is_single_simple && !has_comment_children(params.to_untyped()))
.print_doc(ListStyle {
separator: ",",
delim: ("(", ")"),
Expand Down
2 changes: 1 addition & 1 deletion src/pretty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a> ListStylist<'a> {
}
}
if is_single && sty.omit_delim_single {
inner
inner.group()
} else {
inner = (arena.line_() + inner).nest(2);
if sty.omit_delim_flat {
Expand Down
4 changes: 2 additions & 2 deletions src/pretty/parened_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use typst_syntax::ast::*;

use crate::PrettyPrinter;

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

impl<'a> PrettyPrinter<'a> {
/// We do not care whether it is `Pattern` or `Expr`.
Expand All @@ -14,7 +14,7 @@ impl<'a> PrettyPrinter<'a> {
) -> ArenaDoc<'a> {
let pattern = parenthesized.pattern();
if let Pattern::Parenthesized(paren) = pattern {
if !parenthesized.to_untyped().children().any(is_comment_node) {
if !has_comment_children(parenthesized.to_untyped()) {
// Remove a layer of paren if no comment inside.
return self.convert_parenthesized(paren);
}
Expand Down
4 changes: 4 additions & 0 deletions src/pretty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn is_comment_node(node: &SyntaxNode) -> bool {
)
}

pub fn has_comment_children(node: &SyntaxNode) -> bool {
node.children().any(is_comment_node)
}

pub(super) fn indent_func_name(node: FuncCall<'_>) -> Option<&str> {
node.callee()
.to_untyped()
Expand Down
4 changes: 4 additions & 0 deletions tests/assets/unit/code/extra-paren.typ
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@

#let (( (( ((a)),))) ) =((( ( (1)),)) )
#let (((( (( (a)),)) ))) =((((( (1)),)) ))

#let a = ((b:((c : ((3))))))
#let a = ({(true)})
#let a = (([()]))
22 changes: 11 additions & 11 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ plot.plot(
===== Keys
#doc-style.show-parameter-block(
"orientation",
("direction"),
"direction",
default: ttb,
[
The direction the legend items get laid out to.
Expand All @@ -833,79 +833,79 @@ plot.plot(
)
#doc-style.show-parameter-block(
"layer",
("number"),
"number",
default: 1,
[
The layer index the legend gets drawn at, see on-layer.
],
)
#doc-style.show-parameter-block(
"fill",
("paint"),
"paint",
default: rgb(255, 255, 255, 200),
[
The legends frame background color.
],
)
#doc-style.show-parameter-block(
"stroke",
("stroke"),
"stroke",
default: black,
[
The legends frame stroke style.
],
)
#doc-style.show-parameter-block(
"padding",
("float"),
"float",
default: .1,
[
The legends frame padding, that is the distance added between its items and its frame.
],
)
#doc-style.show-parameter-block(
"offset",
("tuple"),
"tuple",
default: (0, 0),
[
An offset tuple (x and y coordinates) to add to the legends position.
],
)
#doc-style.show-parameter-block(
"spacing",
("number"),
"number",
default: .1,
[
The spacing between the legend position and its frame.
],
)
#doc-style.show-parameter-block(
"item.spacing",
("number"),
"number",
default: .05,
[
The spacing between two legend items in canvas units.
],
)
#doc-style.show-parameter-block(
"item.preview.width",
("number"),
"number",
default: .75,
[
The width of a legend items preview picture, a small preview of the graph the legend item belongs to.
],
)
#doc-style.show-parameter-block(
"item.preview.height",
("number"),
"number",
default: .3,
[
The height of a legend items preview picture.
],
)
#doc-style.show-parameter-block(
"item.preview.margin",
("number"),
"number",
default: .1,
[
Margin between the preview picture and the item label.
Expand Down
23 changes: 12 additions & 11 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source: tests/assets.rs
expression: doc_string
input_file: tests/assets/cetz-manual.typ
snapshot_kind: text
---
#import "doc/util.typ": *
#import "doc/example.typ": example
Expand Down Expand Up @@ -921,7 +922,7 @@ plot.plot(
===== Keys
#doc-style.show-parameter-block(
"orientation",
("direction"),
"direction",
default: ttb,
[
The direction the legend items get laid out to.
Expand All @@ -937,79 +938,79 @@ plot.plot(
)
#doc-style.show-parameter-block(
"layer",
("number"),
"number",
default: 1,
[
The layer index the legend gets drawn at, see on-layer.
],
)
#doc-style.show-parameter-block(
"fill",
("paint"),
"paint",
default: rgb(255, 255, 255, 200),
[
The legends frame background color.
],
)
#doc-style.show-parameter-block(
"stroke",
("stroke"),
"stroke",
default: black,
[
The legends frame stroke style.
],
)
#doc-style.show-parameter-block(
"padding",
("float"),
"float",
default: .1,
[
The legends frame padding, that is the distance added between its items and its frame.
],
)
#doc-style.show-parameter-block(
"offset",
("tuple"),
"tuple",
default: (0, 0),
[
An offset tuple (x and y coordinates) to add to the legends position.
],
)
#doc-style.show-parameter-block(
"spacing",
("number"),
"number",
default: .1,
[
The spacing between the legend position and its frame.
],
)
#doc-style.show-parameter-block(
"item.spacing",
("number"),
"number",
default: .05,
[
The spacing between two legend items in canvas units.
],
)
#doc-style.show-parameter-block(
"item.preview.width",
("number"),
"number",
default: .75,
[
The width of a legend items preview picture, a small preview of the graph the legend item belongs to.
],
)
#doc-style.show-parameter-block(
"item.preview.height",
("number"),
"number",
default: .3,
[
The height of a legend items preview picture.
],
)
#doc-style.show-parameter-block(
"item.preview.margin",
("number"),
"number",
default: .1,
[
Margin between the preview picture and the item label.
Expand Down
Loading

0 comments on commit 13f0ce2

Please sign in to comment.