Skip to content

feat: further preserve boundary spaces and improve text wrapping#396

Merged
QuadnucYard merged 1 commit intotypstyle-rs:masterfrom
QuadnucYard:markup-boundary
Jul 30, 2025
Merged

feat: further preserve boundary spaces and improve text wrapping#396
QuadnucYard merged 1 commit intotypstyle-rs:masterfrom
QuadnucYard:markup-boundary

Conversation

@QuadnucYard
Copy link
Copy Markdown
Collaborator

@QuadnucYard QuadnucYard commented Jul 26, 2025

related: #356. fixes #401
mainly resolves issues found from llm generated testcases.

  • Without text-wrapping, spaces will not turn to linebreaks, unless they are next to blocky elements (list item, heading, etc).
  • With text-wraping, always turn spaces around blocky equations and raws into linebreaks.

The current behavior of boundary handling (taking the left as an example):

  • [a: -> [a
  • [ a: -> [ a by default (preserved), -> [ a with collapse-spaces, [ a or [(lf)a with wrap-text
  • [(lf)a: -> [(lf)a (preserved)
  • [ = a: -> [= a or [(lf)= a

Fixed: #[/* */ 11 /* */] was formatted to #[ /* */ 11 /* */ ], with unintended spaces prepended and appended.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 26, 2025

📊 Benchmark Performance Report

group                        base                                   pr
-----                        ----                                   --
pretty-cetz-manual           1.00    723.5±6.22µs        ? ?/sec    1.02   736.4±22.73µs        ? ?/sec
pretty-codly                 1.00  1765.6±40.47µs        ? ?/sec    1.00  1770.3±32.79µs        ? ?/sec
pretty-cpe                   1.00     12.1±0.03ms        ? ?/sec    1.89     22.8±1.57ms        ? ?/sec
pretty-deep-nested-args      1.00     16.5±0.20µs        ? ?/sec    1.01     16.7±0.22µs        ? ?/sec
pretty-fletcher-diagram      1.00    510.4±7.42µs        ? ?/sec    1.00    511.3±7.59µs        ? ?/sec
pretty-fletcher-draw         1.00  1231.9±21.65µs        ? ?/sec    1.01  1242.1±20.52µs        ? ?/sec
pretty-tablex                1.00      3.3±0.01ms        ? ?/sec    1.00      3.3±0.02ms        ? ?/sec
pretty-touying-core          1.01      2.1±0.01ms        ? ?/sec    1.00      2.1±0.02ms        ? ?/sec
pretty-touying-utils         1.00  1190.3±19.14µs        ? ?/sec    1.00  1191.1±18.60µs        ? ?/sec
pretty-undergraduate-math    1.01   847.1±11.43µs        ? ?/sec    1.00   842.8±12.55µs        ? ?/sec

📏 Binary Size Comparison

Metric Base PR Change
File Size 5.7 MB 5.7 MB 📉 -352 B (-0.01%)
Text Section 1.4 MB 1.4 MB 📉 -256 B (-0.02%)
📦 Detailed Crate Size Diff (cargo-bloat)

Note: Numbers above are a result of guesswork. They are not 100% correct and never will be.

@@ -1,17 +1,17 @@
 std                       391.6 kB
 clap_builder              342.3 kB
-typstyle_core             215.1 kB
+typstyle_core             214.9 kB
 typst_syntax              141.4 kB
 clap_complete             130.8 kB
 similar                   72.5 kB
 typstyle                  54.7 kB
 walkdir                   19.4 kB
 colored                   9.1 kB
 toml_edit                 9.1 kB
 anyhow                    8.6 kB
-prettyless                7.8 kB
+prettyless                7.7 kB
 hashbrown                 5.5 kB
 path_absolutize           4.2 kB
 clap_lex                  4.0 kB
 anstream                  2.6 kB
 parking_lot               2.6 kB

Generated by GitHub Actions on 2025-07-30 02:35:46 UTC

@QuadnucYard QuadnucYard force-pushed the markup-boundary branch 2 times, most recently from f5145d6 to 7ac96c5 Compare July 26, 2025 15:44
@Enter-tainer
Copy link
Copy Markdown
Collaborator

Without text-wrapping, spaces will not turn to linebreaks, unless they are next to blocky elements (list item, heading, etc).

Does this effectively disables the folloing formatting?

if true [ 1 ] else [ 2 ] 

to

if true [
  1
] else [
  2
]

@QuadnucYard
Copy link
Copy Markdown
Collaborator Author

Without text-wrapping, spaces will not turn to linebreaks, unless they are next to blocky elements (list item, heading, etc).

Does this effectively disables the folloing formatting?

I'd like to leave this decision to users.

Previously, we had two rules, but now they expose problems:

  • Tight: when the content is single-line, keep it by preserving boundary spaces. But inner format may change this property, so this rule may lead to divergence.
  • Symmetric: when the two boundaries both not nil, they can turn to linebreak when multiline. This works with the tight rule. Without the tight rule, we can discard the symmetry rule.

@Enter-tainer
Copy link
Copy Markdown
Collaborator

I'm not sure if I get it correctly. How will we "leave the decision to users"? I didn't see this pr adding a config item for this.(BTW I prefer not adding one).

From code, it looks like in this pr, we remove tight & symm rules and only depends on text wrapping. For users who already depends on tight & symm rules, this is a regression. Would it make sense if we keep tight & symm rules, but also take text wrapping into consideration?

@QuadnucYard
Copy link
Copy Markdown
Collaborator Author

I'm not sure if I get it correctly. How will we "leave the decision to users"? I didn't see this pr adding a config item for this.(BTW I prefer not adding one).

Users can change the boundary space to linebreaks if they find the layout bad, which is also respected by the formatter.

From code, it looks like in this pr, we remove tight & symm rules and only depends on text wrapping. For users who already depends on tight & symm rules, this is a regression. Would it make sense if we keep tight & symm rules, but also take text wrapping into consideration?

The boundary part of this PR makes formatting more conservative, so it will not change existing codebase.
When text wrapping is enabled, we do not preserve any space. Boundary spaces can turn into linebreaks, just as inline spaces.

@Enter-tainer
Copy link
Copy Markdown
Collaborator

If the tight rule is causing divergence, would it be possible if we remove tight rule and only keep the symm rule?

@QuadnucYard
Copy link
Copy Markdown
Collaborator Author

If the tight rule is causing divergence, would it be possible if we remove tight rule and only keep the symm rule?

The symm rule targets this case: [ content], [content ]. It becomes implicit as we choose always to preserve spaces.

The key point is whether [ content ] can become

[
  content
]

Considering #296, I want to preserve

[ aaaaa  bbbbbbb cccccccc   ]
[ dddd   eeee    ffffff     ]

where we manually align texts as DSL. In this case, we do not want a wide content block to be broken.

And I forgot one thing. With space collapse enabled, it should keep the original behavior.

@NeilGirdhar
Copy link
Copy Markdown

Fixes #401

(Adding comment so that it will auto-close the bug.)

@QuadnucYard QuadnucYard force-pushed the markup-boundary branch 2 times, most recently from 76bebf0 to bac11db Compare July 30, 2025 02:03
@QuadnucYard QuadnucYard merged commit 131ab4b into typstyle-rs:master Jul 30, 2025
19 checks passed
@QuadnucYard QuadnucYard deleted the markup-boundary branch July 30, 2025 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unstable styling with wrap-text

3 participants