refactor(formatter): add speculate_will_break and fix is_complex_type_arguments#19661
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
Merging this PR will not alter performance
Comparing Footnotes
|
|
https://github.com/oxc-project/monitor-oxc/actions/runs/22346484993/job/64662503654 No comments are being removed now |
leaysgur
left a comment
There was a problem hiding this comment.
It's similar to the state_snapshot() APIs that were removed when porting Biome, right? 👀
Seems a lightweight version specializing in comments.
8b6caf1 to
453da1a
Compare
dddd21b to
b85f97b
Compare
453da1a to
4c037e3
Compare
Yes, during the time I was porting, I didn't know what |
Merge activity
|
…ype_arguments` (#19661) Closes: #19436 ## Summary - Add `Formatter::speculate_will_break` to encapsulate the snapshot/skip/intern/restore pattern for safely checking if formatted content would break across lines - Replace the approximation heuristics in `is_complex_type_arguments` with Prettier's actual `willBreak(print(typeArgs))` logic - Add `Comments::snapshot`, `restore`, and `skip_comments_before` methods to support speculative formatting without losing comments ## Details The old `is_complex_type_arguments` couldn't call `f.intern()` because it would permanently advance the comment cursor, causing comments to be lost. It used heuristic checks (complex type references, mapped types, etc.) as an approximation instead. With the new comment snapshot/restore infrastructure, we can now match Prettier's behavior exactly: ```rust // Before: ~30 lines of heuristic checks // After: f.speculate_will_break(type_arguments) ``` Prettier ref: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L432-L459 ## Test plan - [x] Existing formatter tests pass (193/193) - [x] Clippy clean - [x] Added snapshot tests for comment preservation with call expressions and type arguments 🤖 Generated with [Claude Code](https://claude.com/claude-code)
4c037e3 to
b40e562
Compare

Closes: #19436
Summary
Formatter::speculate_will_breakto encapsulate the snapshot/skip/intern/restore pattern for safely checking if formatted content would break across linesis_complex_type_argumentswith Prettier's actualwillBreak(print(typeArgs))logicComments::snapshot,restore, andskip_comments_beforemethods to support speculative formatting without losing commentsDetails
The old
is_complex_type_argumentscouldn't callf.intern()because it would permanently advance the comment cursor, causing comments to be lost. It used heuristic checks (complex type references, mapped types, etc.) as an approximation instead.With the new comment snapshot/restore infrastructure, we can now match Prettier's behavior exactly:
Prettier ref: https://github.com/prettier/prettier/blob/a043ac0d733c4d53f980aa73807a63fc914f23bd/src/language-js/print/assignment.js#L432-L459
Test plan
🤖 Generated with Claude Code