Skip to content

fix(format): don't split hyphenated words when wrapping cooklang - #735

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/700-cooklang-hyphen-wrap
Oct 17, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
drawmeanelephant:fix/700-cooklang-hyphen-wrap

Conversation

@drawmeanelephant

Copy link
Copy Markdown
Contributor

Closes #700

--format cooklang wrapped steps with textwrap's default HyphenSplitter, so @extra-virgin olive oil{1%tbsp} could be emitted as @extra-\nvirgin olive oil{1%tbsp} — reparsing yields the ingredient extra- virgin olive oil, silently changing the name (and plain text like well-known got a space it never had). Both wrap sites now use WordSplitter::NoHyphenation.

Test plan

  • new regression test: hyphenated component survives wrap+reparse with its name intact
  • cargo test -p cooklang-format green
  • repro now emits @extra-virgin olive oil{1%tbsp} whole on the wrapped line

@LeMyst LeMyst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. This fixes the reported case: the test fails without the change (extra- virgin olive oil) and passes with it, and both wrap sites (cooklang_source.rs:179, :193) are covered. The same corruption still happens in a few nearby cases:

  • Other break points. WordSeparator::new() (:215) follows the Unicode line-break rules, so it still breaks after /, em dash and en dash. A sweep over wrap positions reproduced all three: stir—gently → stir— / gently, foo/bar → foo/ / bar, 1–2 minute → 1– / 2 minute. WordSeparator::AsciiSpace for the text between components would only break where the source had a space.
  • A note detaches from its component. The component regex (:210) ends at }, and a break is allowed between } and (. So @salt{1%g}(a pinch or two or more) can be written as @salt{1%g} + newline + (a pinch or two or more), which parses back with the note as plain step text. Adding an optional (\([^)]*\))? to the regex keeps them together.
  • Long words are cut mid-word. break_words is still on, so a word wider than the wrap width is split at an arbitrary character. The width is termwidth().min(80), so on a narrow terminal ordinary component names hit this. .break_words(false) fixes it.

None of these is tested yet, and the > text block path has no test either.

This conflicts with #736 and #737 only where the tests are inserted.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

All three points addressed:

  • Non-component text now uses WordSeparator::AsciiSpace, so wrapping only happens where the source had a space — no more splits after /, —, –. The > text block path got the same separator (plus NoHyphenation and break_words(false)).
  • The component regex now swallows an optional (\([^)]*\))? note, so @salt{1%g}(a pinch) can't detach onto its own line and reparse as step text.
  • .break_words(false) on both wrap sites — an over-width component name overflows the line instead of being cut mid-word.

New tests: wrapping_only_breaks_at_spaces (slash/em/en/hyphen sweep), a_note_stays_attached_to_its_component (including note reparse), a_word_wider_than_the_wrap_is_not_split, and a_text_block_wraps_intact for the > path.

@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the behaviour is right now. I checked 5d34035 by sliding each risky token through every column from 0 to 81 and running -f cooklang, then parsing the output back to JSON and comparing steps, ingredients, cookware and timers with the original. They were identical in every case.

  • Tokens: and/or, stir—gently, 1–2, extra-virgin, a long URL, @salt{1%g}(a pinch or two or more), NBSP, tabs, multi-word components, cookware and timers.
  • On a 30-column terminal, a word longer than the line now overflows instead of being cut.
  • > blocks wrap only at spaces and round-trip too.

main fails all of these, so the three earlier points are fixed. One thing is worth tightening before merging:

1. None of the four new tests fails without its fix

I reverted each fix on its own: all 13 tests in the module still passed every time. As a control, putting the hyphen splitter back does make a_hyphenated_name_survives_wrapping fail, so the method works. The new tests just never reach the case they guard:

  • wrapping_only_breaks_at_spaces (crates/format/src/cooklang_source.rs:553), a_note_stays_attached_to_its_component (:570) and a_text_block_wraps_intact (:606) put the token around column 20-30. The line breaks near column 80, so the token is never at the break.
  • a_word_wider_than_the_wrap_is_not_split (:593) uses a name that is 71 columns with its @ and {}. It fits in 80, so break_words never triggers.

Moving the token through every column with a prefix ("x ".repeat(n) for n in 0..80), and using a name over 80 characters, would make each test fail when its fix is removed.

Optional, the same on main

A break is still allowed between a component and text glued to it, and the newline reparses as a space. So @eggs{2}s can come back as "eggs s". The same goes for (@flour{100%g})., @butter{50%g};, ~{5%min}! and x/@salt{1%g}. The cause is the trailing slice at :253, which only extends over whitespace. It's fine as a follow-up.

This still conflicts with #736 and #737 only where each adds its tests in cooklang_source.rs.

fmt, clippy, cargo test --workspace and CI are green.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

The cooklang writer wrapped steps with textwrap's default
HyphenSplitter, which breaks "extra-virgin" at the dash — inside a
component that reparses as a different ingredient name, and in plain
text it adds a space the source never had. Switch both wrap sites to
WordSplitter::NoHyphenation.

Closes #700
@drawmeanelephant
drawmeanelephant force-pushed the fix/700-cooklang-hyphen-wrap branch from 5d34035 to b4724c2 Compare October 17, 2026 12:17
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Good catch — all four tests were checking assertions that held either way. On b4724c2 (rebased on main with #736/#737 in):

  • wrapping_only_breaks_at_spaces, a_note_stays_attached_to_its_component and a_text_block_wraps_intact now sweep the risky token through every column ("x ".repeat(pad)), so it lands on the break. 1–2 minute also had a legit space in it — now 1–2.
  • a_word_wider_than_the_wrap_is_not_split uses an 81-char name and asserts the whole @name{2%kg} stays on one line — splitting name and {} reparsed to the same name, which is why it couldn't see the break. A bare 100-char word covers break_words itself.

Verified by reverting the fix: all five wrap tests now fail (only the hyphen one did before), and all pass again with it.

Noted on the component-glue follow-up (@eggs{2}s etc.) — agree it belongs in its own PR; it needs the separator to reach past the component into glued text, which is a slightly different rule.

@LeMyst

LeMyst commented Oct 17, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the tests hold up now. I reverted each fix on its own, and each one now makes its test fail:

  • AsciiSpace → wrapping_only_breaks_at_spaces
  • NoHyphenation → that test and a_hyphenated_name_survives_wrapping
  • break_words(false) → a_word_wider_than_the_wrap_is_not_split
  • the note in the regex → a_note_stays_attached_to_its_component
  • the > block options → a_text_block_wraps_intact

I also swept and/or, stir—gently, 1–2, extra-virgin, a long URL, a component note, NBSP and tabs through columns 0-81, in steps and in > blocks. Every case round-trips through -f cooklang unchanged. fmt, clippy, tests and CI are green, and it merges cleanly with main.

This looks good to merge, and the glued-text case can go in its own PR, as you said. Only one nit is left, if you want it: the block's break_words(false) (cooklang_source.rs:199) has no test of its own, and a word over 78 characters in a_text_block_wraps_intact would cover it.


🤖 Analysis and recommendations made by Claude (Anthropic's AI assistant), posted on behalf of @LeMyst.

@LeMyst
LeMyst merged commit 41e5ad9 into cooklang:main Oct 17, 2026
5 checks passed
@drawmeanelephant

Copy link
Copy Markdown
Contributor Author

Thanks — I'll take the >78-char word in a > block onto the follow-up PR with the glued-text case, since this one is merged.

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.

cooklang format wraps lines inside hyphenated components, corrupting names on reparse

2 participants