Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ debug = true

[patch.crates-io]
# tinymist-world = { git = "https://github.com/Myriad-Dreamin/tinymist", rev = "84569fb98c4eeea65d91677c31d803718143fc10" }
# prettyless = { git = "https://github.com/typstyle-rs/prettyless", rev = "9afa5bdece2dbf14c54ade8bef46cd0828cb952b" }
prettyless = { git = "https://github.com/typstyle-rs/prettyless", rev = "ba88d74267b1ed2293374a74c307343d9e4d11fc" }
6 changes: 4 additions & 2 deletions crates/typstyle-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ impl<'a> Formatter<'a> {
let mut buf = String::new();
doc.render_fmt(self.printer.config().max_width, &mut buf)
.map_err(|_| Error::RenderError)?;
let result = utils::strip_trailing_whitespace(&buf);
Ok(result)
if buf.is_empty() {
buf.push('\n'); // Ensure at least one newline for empty documents
}
Ok(buf)
}

fn build_doc(&'a self) -> Result<ArenaDoc<'a>, Error> {
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/code_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<'a> PrettyPrinter<'a> {
&& child.kind() == SyntaxKind::Space
&& child.text().has_linebreak()
{
flow.push_doc(self.arena.hardline(), false, false);
flow.push_doc(self.arena.hard_line(), false, false);
flow.enter_new_line();
} else if let Some(repr) = item.0 {
flow.push_doc(repr.doc, repr.space_before, repr.space_after);
Expand Down
8 changes: 4 additions & 4 deletions crates/typstyle-core/src/pretty/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn comment<'a>(arena: &'a Arena<'a>, node: &'a SyntaxNode) -> ArenaDoc<'a> {
}

pub fn line_comment<'a>(arena: &'a Arena<'a>, node: &'a SyntaxNode) -> ArenaDoc<'a> {
arena.text(node.text().as_str())
arena.text(node.text().as_str().trim_end())
}

/// It does not add a hardline to the doc.
Expand Down Expand Up @@ -73,7 +73,7 @@ fn align_multiline<'a>(arena: &'a Arena<'a>, text: &'a str) -> ArenaDoc<'a> {
if i == 0 {
doc += line;
} else {
doc += arena.hardline();
doc += arena.hard_line();
if line.len() > leading {
doc += &line[leading..]; // Remove line prefix
} // otherwise this line is blank
Expand All @@ -87,9 +87,9 @@ fn align_multiline_simple<'a>(arena: &'a Arena<'a>, text: &'a str) -> ArenaDoc<'
let mut doc = arena.nil();
for (i, line) in text.lines().enumerate() {
if i > 0 {
doc += arena.hardline();
doc += arena.hard_line();
}
doc += line.trim_start();
doc += line.trim();
}
doc.nest(1).align()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/func_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<'a> PrettyPrinter<'a> {
peek_hashed_arg = at_hashed_arg;
if child.text().has_linebreak() {
peek_linebreak = true;
FlowItem::tight(self.arena.hardline())
FlowItem::tight(self.arena.hard_line())
} else {
FlowItem::none()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/layout/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<'a> ChainStylist<'a> {
if !leading {
leading = true;
space_after = false;
follow_docs += arena.hardline();
follow_docs += arena.hard_line();
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/typstyle-core/src/pretty/layout/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,20 +351,20 @@ impl<'a> ListStylist<'a> {
let mut inner = if sty.tight_delim {
arena.nil()
} else {
arena.hardline()
arena.hard_line()
};
for (i, item) in self.items.into_iter().enumerate() {
let is_last = i + 1 == item_count;
match item {
Item::Comment(cmt) => inner += cmt + arena.hardline(),
Item::Comment(cmt) => inner += cmt + arena.hard_line(),
Item::Commented { body, after } => {
seen_real_items += 1;
inner += body + sep.clone() + after;
if !sty.tight_delim || !is_last {
inner += arena.hardline();
inner += arena.hard_line();
}
}
Item::Linebreak(n) => inner += arena.hardline().repeat(n),
Item::Linebreak(n) => inner += arena.hard_line().repeat(n),
}
}
if !sty.no_indent {
Expand Down Expand Up @@ -476,7 +476,7 @@ impl<'a> ListStylist<'a> {
inner += if is_last && sty.tight_delim {
cmt
} else {
cmt + arena.hardline()
cmt + arena.hard_line()
}
}
Item::Commented { body, after } => {
Expand Down
4 changes: 2 additions & 2 deletions crates/typstyle-core/src/pretty/layout/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ impl<'a> PlainStylist<'a> {
match item {
PlainItem::Item(body) => flow.push_doc(body, true, true),
PlainItem::Comma => flow.push_doc(arena.text(","), false, true),
PlainItem::Linebreak(n) => flow.push_doc(arena.hardline().repeat(n), false, false),
PlainItem::Linebreak(n) => flow.push_doc(arena.hard_line().repeat(n), false, false),
PlainItem::LineComment(cmt) => flow.push_doc(cmt, true, false),
PlainItem::BlockComment(cmt) => flow.push_doc(cmt, true, true),
}
}

let doc = flow.into_doc();
if self.is_multiline {
doc.enclose(arena.hardline(), arena.hardline())
doc.enclose(arena.hard_line(), arena.hard_line())
} else {
doc
}
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/layout/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> TableCollector<'a> {
Row::Comment(doc) => doc,
Row::Linebreak => self.arena.nil(),
}),
self.arena.hardline(),
self.arena.hard_line(),
)
}
}
18 changes: 9 additions & 9 deletions crates/typstyle-core/src/pretty/markup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ impl<'a> PrettyPrinter<'a> {
FlowItem::tight_spaced(self.arena.text(child.text().as_str()))
}
SyntaxKind::Space if child.text().has_linebreak() => {
FlowItem::tight(self.arena.hardline())
FlowItem::tight(self.arena.hard_line())
}
SyntaxKind::Parbreak => FlowItem::tight(
self.arena
.hardline()
.hard_line()
.repeat(child.text().count_linebreaks()),
),
SyntaxKind::Markup => {
Expand Down Expand Up @@ -158,11 +158,11 @@ impl<'a> PrettyPrinter<'a> {
FlowItem::spaced(self.arena.text(child.text().as_str()))
}
SyntaxKind::Space if child.text().has_linebreak() => {
FlowItem::tight(self.arena.hardline())
FlowItem::tight(self.arena.hard_line())
}
SyntaxKind::Parbreak => FlowItem::tight(
self.arena
.hardline()
.hard_line()
.repeat(child.text().count_linebreaks()),
),
SyntaxKind::Markup if child.children().next().is_some() => {
Expand Down Expand Up @@ -207,7 +207,7 @@ impl<'a> PrettyPrinter<'a> {
if scope == MarkupScope::Document || scope.can_trim() {
// should not add extra lines to the document
return if bound == Boundary::Break {
self.arena.hardline()
self.arena.hard_line()
} else {
self.arena.nil()
};
Expand Down Expand Up @@ -240,7 +240,7 @@ impl<'a> PrettyPrinter<'a> {
self.arena.spaces(n)
}
}
Boundary::Break | Boundary::WeakBreak => self.arena.hardline(),
Boundary::Break | Boundary::WeakBreak => self.arena.hard_line(),
}
};

Expand Down Expand Up @@ -288,7 +288,7 @@ impl<'a> PrettyPrinter<'a> {
};
}
if breaks > 0 {
doc += self.arena.hardline().repeat(breaks);
doc += self.arena.hard_line().repeat(breaks);
}
}
doc
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<'a> PrettyPrinter<'a> {
} else if nodes.get(j + 1).is_some_and(prefer_exclusive)
|| nodes.get(j - 1).is_some_and(prefer_exclusive)
{
self.arena.hardline()
self.arena.hard_line()
} else {
self.arena.softline()
}
Expand All @@ -394,7 +394,7 @@ impl<'a> PrettyPrinter<'a> {
{
doc += self.arena.softline();
} else if breaks > 0 {
doc += self.arena.hardline().repeat(breaks);
doc += self.arena.hard_line().repeat(breaks);
}
}
doc
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a> PrettyPrinter<'a> {
inner_nodes = rest;
if first.text().has_linebreak() {
has_open_linebreak = true;
self.arena.hardline()
self.arena.hard_line()
} else {
self.arena.space()
}
Expand Down
6 changes: 3 additions & 3 deletions crates/typstyle-core/src/pretty/math_align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<'a> PrettyPrinter<'a> {
Row::Comment(cmt) => {
// Emit a full‑line comment followed by a hard linebreak
// NOTE: this should not be the last row.
self.arena.text(cmt) + self.arena.hardline()
self.arena.text(cmt) + self.arena.hard_line()
}
Row::Cells(cells) => {
let mut row_doc = self.arena.nil();
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'a> PrettyPrinter<'a> {
let doc = self.arena.spaces(padding_left)
+ self.arena.intersperse(
lines.into_iter().map(|(line, _)| line),
self.arena.hardline(),
self.arena.hard_line(),
)
+ self.arena.spaces(trailing_padding);
(doc.nest(indent as isize), false)
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'a> PrettyPrinter<'a> {
});
}
if !is_last_row {
row_doc += self.arena.hardline();
row_doc += self.arena.hard_line();
}
row_doc
}
Expand Down
2 changes: 1 addition & 1 deletion crates/typstyle-core/src/pretty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'a> PrettyPrinter<'a> {
self.arena
.intersperse(
node.clone().into_text().lines().map(str::to_string),
self.arena.hardline(),
self.arena.hard_line(),
)
.dedent_to_root()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/typstyle-core/src/pretty/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> PrettyPrinter<'a> {
node: &'a SyntaxNode,
) -> ArenaDoc<'a> {
if node.text().has_linebreak() {
self.arena.hardline()
self.arena.hard_line()
} else if ctx.mode.is_markup() && !self.config.collapse_markup_spaces {
self.arena.text(node.text().as_str())
} else {
Expand All @@ -33,7 +33,7 @@ impl<'a> PrettyPrinter<'a> {

pub(super) fn convert_parbreak(&'a self, parbreak: Parbreak) -> ArenaDoc<'a> {
let newline_count = parbreak.to_untyped().text().count_linebreaks();
self.arena.hardline().repeat(newline_count)
self.arena.hard_line().repeat(newline_count)
}
}

Expand Down
27 changes: 0 additions & 27 deletions crates/typstyle-core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
/// Strip trailing whitespace in each line of the input string.
pub fn strip_trailing_whitespace(s: &str) -> String {
if s.is_empty() {
return "\n".to_string();
}
let mut res = String::with_capacity(s.len());
for line in s.lines() {
res.push_str(line.trim_end());
res.push('\n');
}
res
}

pub fn count_spaces_after_last_newline(s: &str, i: usize) -> usize {
// Ensure the byte position `i` is a valid UTF-8 boundary
debug_assert!(
Expand Down Expand Up @@ -92,20 +79,6 @@ pub fn indent_4_to_2(text: &str) -> String {
mod tests {
use super::*;

#[test]
fn test_strip_trailing_whitespace() {
let s = strip_trailing_whitespace("");
assert_eq!(s, "\n");
let s = strip_trailing_whitespace(" ");
assert_eq!(s, "\n");
let s = strip_trailing_whitespace("\n");
assert_eq!(s, "\n");
let s = strip_trailing_whitespace(" \n - \n");
assert_eq!(s, "\n -\n");
let s = strip_trailing_whitespace(" \n - \n ");
assert_eq!(s, "\n -\n\n");
}

#[test]
fn test_change_indent_basic() {
let input = " line1\n line2\n line3";
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/cetz-manual.typ-0.snap
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ line((0,0), (angle: 30deg, radius: 1cm))
The implicit form is an array of the angle then the radius `(angle, radius)` or `(angle, (x, y))`.

```example
line((0,0), (30deg, 1), (60deg, 1),
line((0,0), (30deg, 1), (60deg, 1),
(90deg, 1), (120deg, 1), (150deg, 1), (180deg, 1))
```

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/cetz-manual.typ-120.snap
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ line((0,0), (angle: 30deg, radius: 1cm))
The implicit form is an array of the angle then the radius `(angle, radius)` or `(angle, (x, y))`.

```example
line((0,0), (30deg, 1), (60deg, 1),
line((0,0), (30deg, 1), (60deg, 1),
(90deg, 1), (120deg, 1), (150deg, 1), (180deg, 1))
```

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/cetz-manual.typ-40.snap
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ line((0,0), (angle: 30deg, radius: 1cm))
The implicit form is an array of the angle then the radius `(angle, radius)` or `(angle, (x, y))`.

```example
line((0,0), (30deg, 1), (60deg, 1),
line((0,0), (30deg, 1), (60deg, 1),
(90deg, 1), (120deg, 1), (150deg, 1), (180deg, 1))
```

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/cetz-manual.typ-80.snap
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ line((0,0), (angle: 30deg, radius: 1cm))
The implicit form is an array of the angle then the radius `(angle, radius)` or `(angle, (x, y))`.

```example
line((0,0), (30deg, 1), (60deg, 1),
line((0,0), (30deg, 1), (60deg, 1),
(90deg, 1), (120deg, 1), (150deg, 1), (180deg, 1))
```

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/tidy-new-parser.typ-0.snap
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ
///Description
let func(
pos, // some comment

named: 2 // another comment
)
```.text
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/tidy-new-parser.typ-120.snap
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ
///Description
let func(
pos, // some comment

named: 2 // another comment
)
```.text
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/tidy-new-parser.typ-40.snap
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ
///Description
let func(
pos, // some comment

named: 2 // another comment
)
```.text
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/packages/snap/tidy-new-parser.typ-80.snap
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ
///Description
let func(
pos, // some comment

named: 2 // another comment
)
```.text
Expand Down
Loading
Loading