Skip to content

Small refactor for space collapsing#7609

Merged
laurmaedje merged 2 commits intotypst:mainfrom
isuffix:space-collapse-refactor
Jan 15, 2026
Merged

Small refactor for space collapsing#7609
laurmaedje merged 2 commits intotypst:mainfrom
isuffix:space-collapse-refactor

Conversation

@isuffix
Copy link
Collaborator

@isuffix isuffix commented Dec 20, 2025

While working on #7350, I realized there was an opportunity for a nice refactor independent of adding any features. So I'm making this PR, on which #7350 will depend.

This adds a new state Invisible that allows us to centralize the logic in find_regex_match_in_elems and make if read similarly to the logic in collapse_spaces.

I split out the updated SpaceState and state functions into space_collapse.rs because it aids readability to open it side-by-side with the loops in lib.rs and makes it easier to compare the state functions. I wasn't sure whether to also move the collapse_spaces function over, so I left that in lib.rs

This also adds a test case (show-text-styled-space) that confused me for a while because I was only thinking about collapsing not grouping. I find the space behavior in grouping rules very difficult to reason about, and if you agree I'd like to work on changing it. But I'll leave that for later.

@isuffix isuffix force-pushed the space-collapse-refactor branch from cbb8714 to 6671e14 Compare December 20, 2025 17:44
@laurmaedje laurmaedje added styling About set and show rules or style properties refactor A refactoring. waiting-on-review This PR is waiting to be reviewed. labels Jan 5, 2026
Copy link
Member

@laurmaedje laurmaedje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The perfect parallelity between the two matches in collapse_spaces and find_regex_match_in_elems is quite nice. :)

SpaceState::Supportive => SpaceState::Supportive,
SpaceState::Space => {
if state != SpaceState::Supportive {
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the SpaceState::Invisible branch now continue before the if styles != current && !buf.is_empty() { business as opposed to continuing afterwards previously. I'm not sure whether that could have any unintended consequences, but might be worth double checking if it's not intentional.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change also gave me a lot of grief reasoning through it, but I think it's correct.

First, the Invisible case already continued at the beginning of the loop, so nothing's changed there. I'm not certain, but I believe tag elements can never have different styles than the element they precede, so this should never trigger the if-statement on its own. Another non-invisible element with different styles would need to be present, and that element's presence would trigger the if-statement itself.

Second, spaces will only continue if they're being collapsed or discarded. If it's being discarded, that space shouldn't affect regex searching, and if it's being collapsed, its styles don't matter because we keep the styles of the first space in a run.

Also, I'm quite unsure, but I believe that interior spaces with different styles than adjacent text aren't ever included in a group passed to find_regex_match_in_elems because of the general text grouping rules, but I don't have a strong-enough understanding of those rules to be certain. This is part of what the show-text-styled-space test is for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The behavior tested by the two new tests is indeed confusing and I would consider it a bug.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Jan 7, 2026
@isuffix isuffix force-pushed the space-collapse-refactor branch from 6671e14 to e823cb8 Compare January 9, 2026 20:52
@isuffix isuffix force-pushed the space-collapse-refactor branch from e823cb8 to 12ad2b6 Compare January 9, 2026 20:52
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Jan 12, 2026
@laurmaedje laurmaedje added this pull request to the merge queue Jan 15, 2026
@laurmaedje laurmaedje removed this pull request from the merge queue due to a manual request Jan 15, 2026
@laurmaedje laurmaedje added this pull request to the merge queue Jan 15, 2026
@laurmaedje
Copy link
Member

Thanks!

Merged via the queue into typst:main with commit 292adbd Jan 15, 2026
8 checks passed
@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Jan 15, 2026
@isuffix isuffix deleted the space-collapse-refactor branch January 15, 2026 14:04
jassielof pushed a commit to jassielof/typst that referenced this pull request Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor A refactoring. styling About set and show rules or style properties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants