Skip to content

Commit

Permalink
feat: allow single item code block be put in a single line
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed May 26, 2024
1 parent e9ceade commit 6aa8351
Show file tree
Hide file tree
Showing 49 changed files with 448 additions and 1,242 deletions.
2 changes: 1 addition & 1 deletion src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ impl PrettyPrinter {
BoxDoc::nil(),
(BoxDoc::text("{"), BoxDoc::text("}")),
true,
FoldStyle::Never,
FoldStyle::Single,
);
doc
}
Expand Down
26 changes: 24 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use pretty::BoxDoc;
use crate::attr::Attributes;

/// 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,
/// Never fold items
Never,
/// Fold items if there is only one item
/// Behaves like `Fit` if there is only one item, otherwise `Never`
Single,
}

impl FoldStyle {
Expand All @@ -25,13 +29,20 @@ impl FoldStyle {
}
}

pub fn comma_seprated_items<'a, I>(items: I, fold_style: FoldStyle) -> BoxDoc<'a, ()>
pub fn comma_seprated_items<'a, I>(items: I, mut fold_style: FoldStyle) -> BoxDoc<'a, ()>
where
I: IntoIterator<Item = BoxDoc<'a, ()>> + ExactSizeIterator,
{
if items.len() == 0 {
return BoxDoc::text("()");
}
if fold_style == FoldStyle::Single {
fold_style = if items.len() == 1 {
FoldStyle::Fit
} else {
FoldStyle::Never
};
}
let comma_ = BoxDoc::text(",").flat_alt(BoxDoc::nil());
match fold_style {
FoldStyle::Fit => {
Expand All @@ -55,6 +66,9 @@ where
.append(BoxDoc::hardline())
.append(")")
}
FoldStyle::Single => {
unreachable!();
}
}
}

Expand All @@ -64,7 +78,7 @@ pub fn pretty_items<'a>(
multi_line_separator: BoxDoc<'a, ()>,
bracket: (BoxDoc<'a, ()>, BoxDoc<'a, ()>),
bracket_space: bool,
fold_style: FoldStyle,
mut fold_style: FoldStyle,
) -> BoxDoc<'a, ()> {
if items.is_empty() {
return bracket.0.append(if bracket_space {
Expand All @@ -73,6 +87,13 @@ pub fn pretty_items<'a>(
bracket.1
});
}
if fold_style == FoldStyle::Single {
fold_style = if items.len() == 1 {
FoldStyle::Fit
} else {
FoldStyle::Never
};
}
pretty_items_impl(
items,
single_line_separator,
Expand Down Expand Up @@ -118,6 +139,7 @@ fn pretty_items_impl<'a>(
match fold_style {
FoldStyle::Fit => auto_items,
FoldStyle::Never => multi,
FoldStyle::Single => unreachable!(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/assets/unit/code/short-if.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#table(
fill: (x, y) => if y == 0 { white.darken(15%) } else { none },
align: (x, y) => if y == 0 { center } else { horizon },
[Hi],
[there],
)
18 changes: 4 additions & 14 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ input_file: tests/assets/cetz-manual.typ

#set terms(indent: 1em)
#set par(justify: true)
#set heading(numbering: (..num) => if num.pos().len() < 4 {
numbering("1.1", ..num)
})
#set heading(numbering: (..num) => if num.pos().len() < 4 { numbering("1.1", ..num) })
#show link: set text(blue)

// Outline
Expand Down Expand Up @@ -58,9 +56,7 @@ Note that draw functions are imported inside the scope of the `canvas` block. Th
#show raw.where(block: false): it => if it.text.starts-with("<") and it.text.ends-with(">") {
set text(1.2em)
doc-style.show-type(it.text.slice(1, -1))
} else {
it
}
} else { it }

== CeTZ Unique Argument Types
Many CeTZ functions expect data in certain formats which we will call types. Note that these are actually made up of Typst primitives.
Expand Down Expand Up @@ -233,11 +229,7 @@ Marks are arrow tips that can be added to the end of path based elements that su
let name-to-mnemonic = (:)
for (name, item) in cetz.mark-shapes.mnemonics {
let list = name-to-mnemonic.at(item.at(0), default: ())
list += (
raw(name) + if item.at(1) {
" (reversed)"
},
)
list += (raw(name) + if item.at(1) { " (reversed)" },)
name-to-mnemonic.insert(item.at(0), list)
}
(
Expand Down Expand Up @@ -1066,9 +1058,7 @@ The palette library provides some predefined palettes.
{
import cetz.draw: *
for i in range(0, p("len")) {
if calc.rem(i, 10) == 0 {
move-to((rel: (0, -.5)))
}
if calc.rem(i, 10) == 0 { move-to((rel: (0, -.5))) }
rect((), (rel: (1, .5)), name: "r", ..p(i))
move-to("r.south-east")
}
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ Note that draw functions are imported inside the scope of the `canvas` block. Th
doc-style.show-type(
it.text.slice(1, -1),
)
} else {
it
}
} else { it }

== CeTZ Unique Argument Types
Many CeTZ functions expect data in certain formats which we will call types. Note that these are actually made up of Typst primitives.
Expand Down
14 changes: 3 additions & 11 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ Note that draw functions are imported inside the scope of the `canvas` block. Th
#show raw.where(block: false): it => if it.text.starts-with("<") and it.text.ends-with(">") {
set text(1.2em)
doc-style.show-type(it.text.slice(1, -1))
} else {
it
}
} else { it }

== CeTZ Unique Argument Types
Many CeTZ functions expect data in certain formats which we will call types. Note that these are actually made up of Typst primitives.
Expand Down Expand Up @@ -238,11 +236,7 @@ Marks are arrow tips that can be added to the end of path based elements that su
let name-to-mnemonic = (:)
for (name, item) in cetz.mark-shapes.mnemonics {
let list = name-to-mnemonic.at(item.at(0), default: ())
list += (
raw(name) + if item.at(1) {
" (reversed)"
},
)
list += (raw(name) + if item.at(1) { " (reversed)" },)
name-to-mnemonic.insert(item.at(0), list)
}
(
Expand Down Expand Up @@ -1111,9 +1105,7 @@ The palette library provides some predefined palettes.
{
import cetz.draw: *
for i in range(0, p("len")) {
if calc.rem(i, 10) == 0 {
move-to((rel: (0, -.5)))
}
if calc.rem(i, 10) == 0 { move-to((rel: (0, -.5))) }
rect((), (rel: (1, .5)), name: "r", ..p(i))
move-to("r.south-east")
}
Expand Down
70 changes: 16 additions & 54 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ input_file: tests/assets/cetz-tree.typ
assert(grow > 0)
assert(spread > 0)

if direction == "down" {
direction = "bottom"
}
if direction == "up" {
direction = "top"
}
if direction == "down" { direction = "bottom" }
if direction == "up" { direction = "top" }

let opposite-dir = (left: "right", right: "left", bottom: "top", top: "bottom")

Expand All @@ -72,28 +68,20 @@ input_file: tests/assets/cetz-tree.typ
}
*/
}
} else if draw-edge == none {
draw-edge = (..) => { }
}
} else if draw-edge == none { draw-edge = (..) => { } }

if draw-node == auto or draw-node in ("rect",) {
draw-node = (node, parent-name) => {
let content = node.content
if type(content) == str {
content = [#content]
} else if type(content) in (float, int) {
if type(content) == str { content = [#content] } else if type(content) in (float, int) {
content = $#content$
} else if type(content) == dictionary and "content" in content {
content = content.content
} else if type(content) != typst-content {
panic("Unsupported content type " + type(content) + "! " + "Provide your own `draw-node` implementation.")
}

if content != none {
draw.content((), content, name: "content")
} else {
draw.content((), [?], name: "content")
}
if content != none { draw.content((), content, name: "content") } else { draw.content((), [?], name: "content") }
if draw-node == "rect" {
draw.rect(
(rel: (-.1, .1), to: "content.top-left"),
Expand All @@ -110,9 +98,7 @@ input_file: tests/assets/cetz-tree.typ
if type(tree) == array {
children = tree.slice(1).enumerate().map(((n, c)) => build-node(c, depth: depth + 1, sibling: n))
cnt = tree.at(0)
} else {
cnt = tree
}
} else { cnt = tree }

return (x: 0, y: depth * grow, n: sibling, depth: depth, children: children, content: cnt)
}
Expand Down Expand Up @@ -147,17 +133,11 @@ input_file: tests/assets/cetz-tree.typ
min-x = util.min(min-x, child-min-x)
max-x = util.max(max-x, child-max-x)

if child-max-x > right {
shift-x = child-max-x
}
if child-max-x > right { shift-x = child-max-x }
shift-x += spread
}

if parent-position == "begin" {
node.x = left
} else {
node.x = left + (right - left) / 2
}
if parent-position == "begin" { node.x = left } else { node.x = left + (right - left) / 2 }

node.direct-min-x = left
node.direct-max-x = right
Expand All @@ -174,31 +154,19 @@ input_file: tests/assets/cetz-tree.typ
}

let node-position(node) = {
if direction == "bottom" {
return (node.x, -node.y)
} else if direction == "top" {
if direction == "bottom" { return (node.x, -node.y) } else if direction == "top" {
return (node.x, node.y)
} else if direction == "left" {
return (-node.y, node.x)
} else if direction == "right" {
} else if direction == "left" { return (-node.y, node.x) } else if direction == "right" {
return (node.y, node.x)
} else {
panic(message: "Invalid tree direction.")
}
} else { panic(message: "Invalid tree direction.") }
}

let anchors(node, parent-path) = {
if parent-path != none {
parent-path += "-"
} else {
parent-path = ""
}
if parent-path != none { parent-path += "-" } else { parent-path = "" }

let d = (:)
d.insert(parent-path + str(node.n), node-position(node))
for child in node.children {
d += anchors(child, parent-path + str(node.n))
}
for child in node.children { d += anchors(child, parent-path + str(node.n)) }
return d
}

Expand All @@ -215,13 +183,9 @@ input_file: tests/assets/cetz-tree.typ
},
)

if parent-name != none {
cmds += draw-edge(parent-name, name, node)
}
if parent-name != none { cmds += draw-edge(parent-name, name, node) }

for child in node.children {
cmds += render(child, name)
}
for child in node.children { cmds += render(child, name) }

return cmds
}
Expand All @@ -244,9 +208,7 @@ input_file: tests/assets/cetz-tree.typ
let self = ctx.groups.pop()
let nodes = ctx.nodes
ctx = self.ctx
if name != none {
ctx.nodes.insert(name, nodes.at(name))
}
if name != none { ctx.nodes.insert(name, nodes.at(name)) }
return ctx
},
custom-anchors-ctx: ctx => {
Expand Down
8 changes: 2 additions & 6 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ input_file: tests/assets/cetz-tree.typ
sibling: n,
))
cnt = tree.at(0)
} else {
cnt = tree
}
} else { cnt = tree }

return (
x: 0,
Expand Down Expand Up @@ -260,9 +258,7 @@ input_file: tests/assets/cetz-tree.typ
let anchors(node, parent-path) = {
if parent-path != none {
parent-path += "-"
} else {
parent-path = ""
}
} else { parent-path = "" }

let d = (:)
d.insert(
Expand Down
Loading

0 comments on commit 6aa8351

Please sign in to comment.