Small refactor for space collapsing#7609
Conversation
cbb8714 to
6671e14
Compare
laurmaedje
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I see. The behavior tested by the two new tests is indeed confusing and I would consider it a bug.
6671e14 to
e823cb8
Compare
e823cb8 to
12ad2b6
Compare
|
Thanks! |
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
Invisiblethat allows us to centralize the logic infind_regex_match_in_elemsand make if read similarly to the logic incollapse_spaces.I split out the updated
SpaceStateand state functions intospace_collapse.rsbecause it aids readability to open it side-by-side with the loops inlib.rsand makes it easier to compare the state functions. I wasn't sure whether to also move thecollapse_spacesfunction over, so I left that inlib.rsThis 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.