From b83c51ffe8bf0c447b85be9615bf187e8566180c Mon Sep 17 00:00:00 2001 From: QuadnucYard Date: Sat, 11 Oct 2025 17:41:03 +0800 Subject: [PATCH 1/5] rename `hardline` to `hard_line` --- Cargo.lock | 3 +-- Cargo.toml | 2 +- crates/typstyle-core/src/pretty/code_flow.rs | 2 +- crates/typstyle-core/src/pretty/comment.rs | 4 ++-- crates/typstyle-core/src/pretty/func_call.rs | 2 +- .../typstyle-core/src/pretty/layout/chain.rs | 2 +- crates/typstyle-core/src/pretty/layout/list.rs | 10 +++++----- .../typstyle-core/src/pretty/layout/plain.rs | 4 ++-- .../typstyle-core/src/pretty/layout/table.rs | 2 +- crates/typstyle-core/src/pretty/markup.rs | 18 +++++++++--------- crates/typstyle-core/src/pretty/math.rs | 2 +- crates/typstyle-core/src/pretty/math_align.rs | 6 +++--- crates/typstyle-core/src/pretty/mod.rs | 2 +- crates/typstyle-core/src/pretty/text.rs | 4 ++-- 14 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 329c07d7..e1b2db22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2288,8 +2288,7 @@ dependencies = [ [[package]] name = "prettyless" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "749d63085aa461d80b3233e48781bf9e9dca8147fbc98f577e0697c6827dc418" +source = "git+https://github.com/typstyle-rs/prettyless?rev=ba88d74267b1ed2293374a74c307343d9e4d11fc#ba88d74267b1ed2293374a74c307343d9e4d11fc" dependencies = [ "typed-arena", "unicode-width 0.2.1", diff --git a/Cargo.toml b/Cargo.toml index a26a7cf3..f63ac7be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/typstyle-core/src/pretty/code_flow.rs b/crates/typstyle-core/src/pretty/code_flow.rs index 870bf1b2..0b36d45b 100644 --- a/crates/typstyle-core/src/pretty/code_flow.rs +++ b/crates/typstyle-core/src/pretty/code_flow.rs @@ -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); diff --git a/crates/typstyle-core/src/pretty/comment.rs b/crates/typstyle-core/src/pretty/comment.rs index a3806158..3ef3b1f0 100644 --- a/crates/typstyle-core/src/pretty/comment.rs +++ b/crates/typstyle-core/src/pretty/comment.rs @@ -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 @@ -87,7 +87,7 @@ 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(); } diff --git a/crates/typstyle-core/src/pretty/func_call.rs b/crates/typstyle-core/src/pretty/func_call.rs index d58d3a2b..c502f3c1 100644 --- a/crates/typstyle-core/src/pretty/func_call.rs +++ b/crates/typstyle-core/src/pretty/func_call.rs @@ -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() } diff --git a/crates/typstyle-core/src/pretty/layout/chain.rs b/crates/typstyle-core/src/pretty/layout/chain.rs index d6a14615..22403dee 100644 --- a/crates/typstyle-core/src/pretty/layout/chain.rs +++ b/crates/typstyle-core/src/pretty/layout/chain.rs @@ -188,7 +188,7 @@ impl<'a> ChainStylist<'a> { if !leading { leading = true; space_after = false; - follow_docs += arena.hardline(); + follow_docs += arena.hard_line(); } } } diff --git a/crates/typstyle-core/src/pretty/layout/list.rs b/crates/typstyle-core/src/pretty/layout/list.rs index 4caf1785..395e09ed 100644 --- a/crates/typstyle-core/src/pretty/layout/list.rs +++ b/crates/typstyle-core/src/pretty/layout/list.rs @@ -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 { @@ -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 } => { diff --git a/crates/typstyle-core/src/pretty/layout/plain.rs b/crates/typstyle-core/src/pretty/layout/plain.rs index 3eb60340..804048d3 100644 --- a/crates/typstyle-core/src/pretty/layout/plain.rs +++ b/crates/typstyle-core/src/pretty/layout/plain.rs @@ -96,7 +96,7 @@ 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), } @@ -104,7 +104,7 @@ impl<'a> PlainStylist<'a> { 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 } diff --git a/crates/typstyle-core/src/pretty/layout/table.rs b/crates/typstyle-core/src/pretty/layout/table.rs index 4d63c58f..99cac648 100644 --- a/crates/typstyle-core/src/pretty/layout/table.rs +++ b/crates/typstyle-core/src/pretty/layout/table.rs @@ -110,7 +110,7 @@ impl<'a> TableCollector<'a> { Row::Comment(doc) => doc, Row::Linebreak => self.arena.nil(), }), - self.arena.hardline(), + self.arena.hard_line(), ) } } diff --git a/crates/typstyle-core/src/pretty/markup.rs b/crates/typstyle-core/src/pretty/markup.rs index 6df0e9f4..06e49b8f 100644 --- a/crates/typstyle-core/src/pretty/markup.rs +++ b/crates/typstyle-core/src/pretty/markup.rs @@ -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 => { @@ -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() => { @@ -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() }; @@ -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(), } }; @@ -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 @@ -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() } @@ -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 diff --git a/crates/typstyle-core/src/pretty/math.rs b/crates/typstyle-core/src/pretty/math.rs index 3690cb60..32bb67ec 100644 --- a/crates/typstyle-core/src/pretty/math.rs +++ b/crates/typstyle-core/src/pretty/math.rs @@ -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() } diff --git a/crates/typstyle-core/src/pretty/math_align.rs b/crates/typstyle-core/src/pretty/math_align.rs index 225996a2..5236bb5d 100644 --- a/crates/typstyle-core/src/pretty/math_align.rs +++ b/crates/typstyle-core/src/pretty/math_align.rs @@ -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(); @@ -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) @@ -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 } diff --git a/crates/typstyle-core/src/pretty/mod.rs b/crates/typstyle-core/src/pretty/mod.rs index 01d1e331..757b2e90 100644 --- a/crates/typstyle-core/src/pretty/mod.rs +++ b/crates/typstyle-core/src/pretty/mod.rs @@ -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() } diff --git a/crates/typstyle-core/src/pretty/text.rs b/crates/typstyle-core/src/pretty/text.rs index c37f4b32..6a06bb1c 100644 --- a/crates/typstyle-core/src/pretty/text.rs +++ b/crates/typstyle-core/src/pretty/text.rs @@ -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 { @@ -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) } } From 7ed25121e5635200ef9c93d7c36d08af5d8c0062 Mon Sep 17 00:00:00 2001 From: QuadnucYard Date: Sat, 11 Oct 2025 17:46:14 +0800 Subject: [PATCH 2/5] do not trim line end --- crates/typstyle-core/src/lib.rs | 6 +++-- crates/typstyle-core/src/pretty/comment.rs | 4 ++-- crates/typstyle-core/src/utils.rs | 27 ---------------------- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/crates/typstyle-core/src/lib.rs b/crates/typstyle-core/src/lib.rs index 4c712fc4..405465d0 100644 --- a/crates/typstyle-core/src/lib.rs +++ b/crates/typstyle-core/src/lib.rs @@ -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, Error> { diff --git a/crates/typstyle-core/src/pretty/comment.rs b/crates/typstyle-core/src/pretty/comment.rs index 3ef3b1f0..ec42729c 100644 --- a/crates/typstyle-core/src/pretty/comment.rs +++ b/crates/typstyle-core/src/pretty/comment.rs @@ -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. @@ -89,7 +89,7 @@ fn align_multiline_simple<'a>(arena: &'a Arena<'a>, text: &'a str) -> ArenaDoc<' if i > 0 { doc += arena.hard_line(); } - doc += line.trim_start(); + doc += line.trim(); } doc.nest(1).align() } diff --git a/crates/typstyle-core/src/utils.rs b/crates/typstyle-core/src/utils.rs index 38369db8..a480e781 100644 --- a/crates/typstyle-core/src/utils.rs +++ b/crates/typstyle-core/src/utils.rs @@ -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!( @@ -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"; From 32d40aa90e09fc47512940ed3435f19708d26960 Mon Sep 17 00:00:00 2001 From: QuadnucYard Date: Sat, 11 Oct 2025 17:46:41 +0800 Subject: [PATCH 3/5] update snapshots --- tests/fixtures/packages/snap/cetz-manual.typ-0.snap | 2 +- tests/fixtures/packages/snap/cetz-manual.typ-120.snap | 2 +- tests/fixtures/packages/snap/cetz-manual.typ-40.snap | 2 +- tests/fixtures/packages/snap/cetz-manual.typ-80.snap | 2 +- .../fixtures/packages/snap/tidy-new-parser.typ-0.snap | 2 +- .../packages/snap/tidy-new-parser.typ-120.snap | 2 +- .../fixtures/packages/snap/tidy-new-parser.typ-40.snap | 2 +- .../fixtures/packages/snap/tidy-new-parser.typ-80.snap | 2 +- .../unit/code/snap/string-multiline.typ-0.snap | 9 ++++----- .../unit/code/snap/string-multiline.typ-120.snap | 9 ++++----- .../unit/code/snap/string-multiline.typ-40.snap | 9 ++++----- .../unit/code/snap/string-multiline.typ-80.snap | 9 ++++----- .../fixtures/unit/document/snap/empty-space.typ-0.snap | 3 --- .../unit/document/snap/empty-space.typ-120.snap | 3 --- .../unit/document/snap/empty-space.typ-40.snap | 3 --- .../unit/document/snap/empty-space.typ-80.snap | 3 --- tests/fixtures/unit/markup/snap/raw.typ-0.snap | 2 +- tests/fixtures/unit/markup/snap/raw.typ-120.snap | 2 +- tests/fixtures/unit/markup/snap/raw.typ-40.snap | 2 +- tests/fixtures/unit/markup/snap/raw.typ-80.snap | 2 +- tests/fixtures/unit/markup/snap/term-empty.typ-0.snap | 10 +++++----- .../fixtures/unit/markup/snap/term-empty.typ-120.snap | 10 +++++----- tests/fixtures/unit/markup/snap/term-empty.typ-40.snap | 10 +++++----- tests/fixtures/unit/markup/snap/term-empty.typ-80.snap | 10 +++++----- .../unit/off/snap/content-func-call.typ-0.snap | 3 +-- .../unit/off/snap/content-func-call.typ-120.snap | 3 +-- .../unit/off/snap/content-func-call.typ-40.snap | 3 +-- .../unit/off/snap/content-func-call.typ-80.snap | 3 +-- 28 files changed, 52 insertions(+), 72 deletions(-) diff --git a/tests/fixtures/packages/snap/cetz-manual.typ-0.snap b/tests/fixtures/packages/snap/cetz-manual.typ-0.snap index a5d5557e..b2482d83 100644 --- a/tests/fixtures/packages/snap/cetz-manual.typ-0.snap +++ b/tests/fixtures/packages/snap/cetz-manual.typ-0.snap @@ -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)) ``` diff --git a/tests/fixtures/packages/snap/cetz-manual.typ-120.snap b/tests/fixtures/packages/snap/cetz-manual.typ-120.snap index c393be03..71b9b63a 100644 --- a/tests/fixtures/packages/snap/cetz-manual.typ-120.snap +++ b/tests/fixtures/packages/snap/cetz-manual.typ-120.snap @@ -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)) ``` diff --git a/tests/fixtures/packages/snap/cetz-manual.typ-40.snap b/tests/fixtures/packages/snap/cetz-manual.typ-40.snap index 246a93d5..7e54e449 100644 --- a/tests/fixtures/packages/snap/cetz-manual.typ-40.snap +++ b/tests/fixtures/packages/snap/cetz-manual.typ-40.snap @@ -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)) ``` diff --git a/tests/fixtures/packages/snap/cetz-manual.typ-80.snap b/tests/fixtures/packages/snap/cetz-manual.typ-80.snap index 285478b7..4f9b7aff 100644 --- a/tests/fixtures/packages/snap/cetz-manual.typ-80.snap +++ b/tests/fixtures/packages/snap/cetz-manual.typ-80.snap @@ -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)) ``` diff --git a/tests/fixtures/packages/snap/tidy-new-parser.typ-0.snap b/tests/fixtures/packages/snap/tidy-new-parser.typ-0.snap index 8e79f4bb..a8e0c805 100644 --- a/tests/fixtures/packages/snap/tidy-new-parser.typ-0.snap +++ b/tests/fixtures/packages/snap/tidy-new-parser.typ-0.snap @@ -907,7 +907,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ ///Description let func( pos, // some comment - + named: 2 // another comment ) ```.text diff --git a/tests/fixtures/packages/snap/tidy-new-parser.typ-120.snap b/tests/fixtures/packages/snap/tidy-new-parser.typ-120.snap index 96338944..e5199a86 100644 --- a/tests/fixtures/packages/snap/tidy-new-parser.typ-120.snap +++ b/tests/fixtures/packages/snap/tidy-new-parser.typ-120.snap @@ -380,7 +380,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ ///Description let func( pos, // some comment - + named: 2 // another comment ) ```.text diff --git a/tests/fixtures/packages/snap/tidy-new-parser.typ-40.snap b/tests/fixtures/packages/snap/tidy-new-parser.typ-40.snap index bb8b8dd8..d48b9a00 100644 --- a/tests/fixtures/packages/snap/tidy-new-parser.typ-40.snap +++ b/tests/fixtures/packages/snap/tidy-new-parser.typ-40.snap @@ -589,7 +589,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ ///Description let func( pos, // some comment - + named: 2 // another comment ) ```.text diff --git a/tests/fixtures/packages/snap/tidy-new-parser.typ-80.snap b/tests/fixtures/packages/snap/tidy-new-parser.typ-80.snap index 94c7e68b..c982fdc1 100644 --- a/tests/fixtures/packages/snap/tidy-new-parser.typ-80.snap +++ b/tests/fixtures/packages/snap/tidy-new-parser.typ-80.snap @@ -415,7 +415,7 @@ input_file: tests/fixtures/packages/tidy-new-parser.typ ///Description let func( pos, // some comment - + named: 2 // another comment ) ```.text diff --git a/tests/fixtures/unit/code/snap/string-multiline.typ-0.snap b/tests/fixtures/unit/code/snap/string-multiline.typ-0.snap index e6f8b355..2162cf81 100644 --- a/tests/fixtures/unit/code/snap/string-multiline.typ-0.snap +++ b/tests/fixtures/unit/code/snap/string-multiline.typ-0.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/code/string-multiline.typ -snapshot_kind: text --- #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ) } @@ -14,12 +13,12 @@ snapshot_kind: text #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa - - + + c", ) } diff --git a/tests/fixtures/unit/code/snap/string-multiline.typ-120.snap b/tests/fixtures/unit/code/snap/string-multiline.typ-120.snap index e6f8b355..2162cf81 100644 --- a/tests/fixtures/unit/code/snap/string-multiline.typ-120.snap +++ b/tests/fixtures/unit/code/snap/string-multiline.typ-120.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/code/string-multiline.typ -snapshot_kind: text --- #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ) } @@ -14,12 +13,12 @@ snapshot_kind: text #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa - - + + c", ) } diff --git a/tests/fixtures/unit/code/snap/string-multiline.typ-40.snap b/tests/fixtures/unit/code/snap/string-multiline.typ-40.snap index e6f8b355..2162cf81 100644 --- a/tests/fixtures/unit/code/snap/string-multiline.typ-40.snap +++ b/tests/fixtures/unit/code/snap/string-multiline.typ-40.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/code/string-multiline.typ -snapshot_kind: text --- #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ) } @@ -14,12 +13,12 @@ snapshot_kind: text #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa - - + + c", ) } diff --git a/tests/fixtures/unit/code/snap/string-multiline.typ-80.snap b/tests/fixtures/unit/code/snap/string-multiline.typ-80.snap index e6f8b355..2162cf81 100644 --- a/tests/fixtures/unit/code/snap/string-multiline.typ-80.snap +++ b/tests/fixtures/unit/code/snap/string-multiline.typ-80.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/code/string-multiline.typ -snapshot_kind: text --- #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ) } @@ -14,12 +13,12 @@ snapshot_kind: text #{ assert( true, - message: "Target + message: "Target qubit for multi qubit gate does not exist aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa - - + + c", ) } diff --git a/tests/fixtures/unit/document/snap/empty-space.typ-0.snap b/tests/fixtures/unit/document/snap/empty-space.typ-0.snap index 49153270..7bfaffa9 100644 --- a/tests/fixtures/unit/document/snap/empty-space.typ-0.snap +++ b/tests/fixtures/unit/document/snap/empty-space.typ-0.snap @@ -1,8 +1,5 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/document/empty-space.typ -snapshot_kind: text --- -// DUMMY -// DUMMY diff --git a/tests/fixtures/unit/document/snap/empty-space.typ-120.snap b/tests/fixtures/unit/document/snap/empty-space.typ-120.snap index 49153270..7bfaffa9 100644 --- a/tests/fixtures/unit/document/snap/empty-space.typ-120.snap +++ b/tests/fixtures/unit/document/snap/empty-space.typ-120.snap @@ -1,8 +1,5 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/document/empty-space.typ -snapshot_kind: text --- -// DUMMY -// DUMMY diff --git a/tests/fixtures/unit/document/snap/empty-space.typ-40.snap b/tests/fixtures/unit/document/snap/empty-space.typ-40.snap index 49153270..7bfaffa9 100644 --- a/tests/fixtures/unit/document/snap/empty-space.typ-40.snap +++ b/tests/fixtures/unit/document/snap/empty-space.typ-40.snap @@ -1,8 +1,5 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/document/empty-space.typ -snapshot_kind: text --- -// DUMMY -// DUMMY diff --git a/tests/fixtures/unit/document/snap/empty-space.typ-80.snap b/tests/fixtures/unit/document/snap/empty-space.typ-80.snap index 49153270..7bfaffa9 100644 --- a/tests/fixtures/unit/document/snap/empty-space.typ-80.snap +++ b/tests/fixtures/unit/document/snap/empty-space.typ-80.snap @@ -1,8 +1,5 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/document/empty-space.typ -snapshot_kind: text --- -// DUMMY -// DUMMY diff --git a/tests/fixtures/unit/markup/snap/raw.typ-0.snap b/tests/fixtures/unit/markup/snap/raw.typ-0.snap index d71bc84c..a8f6ac3d 100644 --- a/tests/fixtures/unit/markup/snap/raw.typ-0.snap +++ b/tests/fixtures/unit/markup/snap/raw.typ-0.snap @@ -54,7 +54,7 @@ let a = 1; ```typ #link()``` -```typ #link() +```typ #link() ``` #[ diff --git a/tests/fixtures/unit/markup/snap/raw.typ-120.snap b/tests/fixtures/unit/markup/snap/raw.typ-120.snap index d71bc84c..a8f6ac3d 100644 --- a/tests/fixtures/unit/markup/snap/raw.typ-120.snap +++ b/tests/fixtures/unit/markup/snap/raw.typ-120.snap @@ -54,7 +54,7 @@ let a = 1; ```typ #link()``` -```typ #link() +```typ #link() ``` #[ diff --git a/tests/fixtures/unit/markup/snap/raw.typ-40.snap b/tests/fixtures/unit/markup/snap/raw.typ-40.snap index d71bc84c..a8f6ac3d 100644 --- a/tests/fixtures/unit/markup/snap/raw.typ-40.snap +++ b/tests/fixtures/unit/markup/snap/raw.typ-40.snap @@ -54,7 +54,7 @@ let a = 1; ```typ #link()``` -```typ #link() +```typ #link() ``` #[ diff --git a/tests/fixtures/unit/markup/snap/raw.typ-80.snap b/tests/fixtures/unit/markup/snap/raw.typ-80.snap index d71bc84c..a8f6ac3d 100644 --- a/tests/fixtures/unit/markup/snap/raw.typ-80.snap +++ b/tests/fixtures/unit/markup/snap/raw.typ-80.snap @@ -54,7 +54,7 @@ let a = 1; ```typ #link()``` -```typ #link() +```typ #link() ``` #[ diff --git a/tests/fixtures/unit/markup/snap/term-empty.typ-0.snap b/tests/fixtures/unit/markup/snap/term-empty.typ-0.snap index ae6c1015..62eeb69b 100644 --- a/tests/fixtures/unit/markup/snap/term-empty.typ-0.snap +++ b/tests/fixtures/unit/markup/snap/term-empty.typ-0.snap @@ -16,9 +16,9 @@ input_file: tests/fixtures/unit/markup/term-empty.typ / : b / c: / d: -/ +/ : -/ +/ : / e @@ -42,15 +42,15 @@ input_file: tests/fixtures/unit/markup/term-empty.typ / : d ] #[ - / + / : ] #[ - / + / : ] #[ - / + / : ] diff --git a/tests/fixtures/unit/markup/snap/term-empty.typ-120.snap b/tests/fixtures/unit/markup/snap/term-empty.typ-120.snap index 8b3ca7ef..3479ab6a 100644 --- a/tests/fixtures/unit/markup/snap/term-empty.typ-120.snap +++ b/tests/fixtures/unit/markup/snap/term-empty.typ-120.snap @@ -14,9 +14,9 @@ input_file: tests/fixtures/unit/markup/term-empty.typ / : b / c: / d: -/ +/ : -/ +/ : / e @@ -28,15 +28,15 @@ input_file: tests/fixtures/unit/markup/term-empty.typ #[/ : c] #[/ : d] #[ - / + / : ] #[ - / + / : ] #[ - / + / : ] diff --git a/tests/fixtures/unit/markup/snap/term-empty.typ-40.snap b/tests/fixtures/unit/markup/snap/term-empty.typ-40.snap index 8b3ca7ef..3479ab6a 100644 --- a/tests/fixtures/unit/markup/snap/term-empty.typ-40.snap +++ b/tests/fixtures/unit/markup/snap/term-empty.typ-40.snap @@ -14,9 +14,9 @@ input_file: tests/fixtures/unit/markup/term-empty.typ / : b / c: / d: -/ +/ : -/ +/ : / e @@ -28,15 +28,15 @@ input_file: tests/fixtures/unit/markup/term-empty.typ #[/ : c] #[/ : d] #[ - / + / : ] #[ - / + / : ] #[ - / + / : ] diff --git a/tests/fixtures/unit/markup/snap/term-empty.typ-80.snap b/tests/fixtures/unit/markup/snap/term-empty.typ-80.snap index 8b3ca7ef..3479ab6a 100644 --- a/tests/fixtures/unit/markup/snap/term-empty.typ-80.snap +++ b/tests/fixtures/unit/markup/snap/term-empty.typ-80.snap @@ -14,9 +14,9 @@ input_file: tests/fixtures/unit/markup/term-empty.typ / : b / c: / d: -/ +/ : -/ +/ : / e @@ -28,15 +28,15 @@ input_file: tests/fixtures/unit/markup/term-empty.typ #[/ : c] #[/ : d] #[ - / + / : ] #[ - / + / : ] #[ - / + / : ] diff --git a/tests/fixtures/unit/off/snap/content-func-call.typ-0.snap b/tests/fixtures/unit/off/snap/content-func-call.typ-0.snap index 5372dea1..89daab25 100644 --- a/tests/fixtures/unit/off/snap/content-func-call.typ-0.snap +++ b/tests/fixtures/unit/off/snap/content-func-call.typ-0.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/off/content-func-call.typ -snapshot_kind: text --- // DUMMY #table( - columns: /* @typstyle off */ (1fr, + columns: /* @typstyle off */ (1fr, auto, auto), inset: 10pt, align: horizon, [], [*Area*], diff --git a/tests/fixtures/unit/off/snap/content-func-call.typ-120.snap b/tests/fixtures/unit/off/snap/content-func-call.typ-120.snap index 28785782..90ab085e 100644 --- a/tests/fixtures/unit/off/snap/content-func-call.typ-120.snap +++ b/tests/fixtures/unit/off/snap/content-func-call.typ-120.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/off/content-func-call.typ -snapshot_kind: text --- // DUMMY #table( - columns: /* @typstyle off */ (1fr, + columns: /* @typstyle off */ (1fr, auto, auto), inset: 10pt, align: horizon, [], [*Area*], diff --git a/tests/fixtures/unit/off/snap/content-func-call.typ-40.snap b/tests/fixtures/unit/off/snap/content-func-call.typ-40.snap index 28785782..90ab085e 100644 --- a/tests/fixtures/unit/off/snap/content-func-call.typ-40.snap +++ b/tests/fixtures/unit/off/snap/content-func-call.typ-40.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/off/content-func-call.typ -snapshot_kind: text --- // DUMMY #table( - columns: /* @typstyle off */ (1fr, + columns: /* @typstyle off */ (1fr, auto, auto), inset: 10pt, align: horizon, [], [*Area*], diff --git a/tests/fixtures/unit/off/snap/content-func-call.typ-80.snap b/tests/fixtures/unit/off/snap/content-func-call.typ-80.snap index 28785782..90ab085e 100644 --- a/tests/fixtures/unit/off/snap/content-func-call.typ-80.snap +++ b/tests/fixtures/unit/off/snap/content-func-call.typ-80.snap @@ -1,12 +1,11 @@ --- source: tests/src/unit.rs input_file: tests/fixtures/unit/off/content-func-call.typ -snapshot_kind: text --- // DUMMY #table( - columns: /* @typstyle off */ (1fr, + columns: /* @typstyle off */ (1fr, auto, auto), inset: 10pt, align: horizon, [], [*Area*], From 4d35b632e8d1173734cdc23c5b6a79b8a68b8149 Mon Sep 17 00:00:00 2001 From: QuadnucYard Date: Sat, 11 Oct 2025 18:42:05 +0800 Subject: [PATCH 4/5] fix final new line --- crates/typstyle-core/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/typstyle-core/src/lib.rs b/crates/typstyle-core/src/lib.rs index 405465d0..12267a62 100644 --- a/crates/typstyle-core/src/lib.rs +++ b/crates/typstyle-core/src/lib.rs @@ -72,7 +72,7 @@ impl<'a> Formatter<'a> { let mut buf = String::new(); doc.render_fmt(self.printer.config().max_width, &mut buf) .map_err(|_| Error::RenderError)?; - if buf.is_empty() { + if !buf.ends_with('\n') { buf.push('\n'); // Ensure at least one newline for empty documents } Ok(buf) From aa8e6a49f0b0d59ac03c6a33bb08373242c0a48b Mon Sep 17 00:00:00 2001 From: QuadnucYard Date: Sat, 11 Oct 2025 19:33:46 +0800 Subject: [PATCH 5/5] handle final new line in markup --- crates/typstyle-core/src/lib.rs | 3 --- crates/typstyle-core/src/pretty/markup.rs | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/typstyle-core/src/lib.rs b/crates/typstyle-core/src/lib.rs index 12267a62..d626aec7 100644 --- a/crates/typstyle-core/src/lib.rs +++ b/crates/typstyle-core/src/lib.rs @@ -72,9 +72,6 @@ impl<'a> Formatter<'a> { let mut buf = String::new(); doc.render_fmt(self.printer.config().max_width, &mut buf) .map_err(|_| Error::RenderError)?; - if !buf.ends_with('\n') { - buf.push('\n'); // Ensure at least one newline for empty documents - } Ok(buf) } diff --git a/crates/typstyle-core/src/pretty/markup.rs b/crates/typstyle-core/src/pretty/markup.rs index 06e49b8f..016d27e5 100644 --- a/crates/typstyle-core/src/pretty/markup.rs +++ b/crates/typstyle-core/src/pretty/markup.rs @@ -194,7 +194,11 @@ impl<'a> PrettyPrinter<'a> { } } - let repr = collect_markup_repr(markup); + let mut repr = collect_markup_repr(markup); + if scope == MarkupScope::Document { + // In document scope, we should insert a trailing linebreak if not present. + repr.end_bound = Boundary::Break; + } let body = if self.config.wrap_text && scope != MarkupScope::InlineItem { self.convert_markup_body_reflow(ctx, &repr) } else {