Skip to content

refactor(formatter): add speculate_will_break and fix is_complex_type_arguments#19661

Merged
graphite-app[bot] merged 1 commit intomainfrom
refactor/formatter-speculate-will-break
Feb 25, 2026
Merged

refactor(formatter): add speculate_will_break and fix is_complex_type_arguments#19661
graphite-app[bot] merged 1 commit intomainfrom
refactor/formatter-speculate-will-break

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Feb 24, 2026

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:

// 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

  • Existing formatter tests pass (193/193)
  • Clippy clean
  • Added snapshot tests for comment preservation with call expressions and type arguments

🤖 Generated with Claude Code

Copy link
Member Author

Dunqing commented Feb 24, 2026


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@github-actions github-actions bot added A-formatter Area - Formatter C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels Feb 24, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 24, 2026

Merging this PR will not alter performance

✅ 48 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing refactor/formatter-speculate-will-break (4c037e3) with main (b85f97b)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing
Copy link
Member Author

Dunqing commented Feb 24, 2026

@Dunqing Dunqing marked this pull request as ready for review February 24, 2026 10:37
@Dunqing Dunqing requested a review from leaysgur February 24, 2026 10:37
@graphite-app graphite-app bot changed the base branch from fix/formatter-blank-line-member-chain to graphite-base/19661 February 25, 2026 00:35
Copy link
Member

@leaysgur leaysgur left a comment

Choose a reason for hiding this comment

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

It's similar to the state_snapshot() APIs that were removed when porting Biome, right? 👀

Seems a lightweight version specializing in comments.

@graphite-app graphite-app bot force-pushed the refactor/formatter-speculate-will-break branch from 8b6caf1 to 453da1a Compare February 25, 2026 00:42
@graphite-app graphite-app bot force-pushed the graphite-base/19661 branch from dddd21b to b85f97b Compare February 25, 2026 00:42
@graphite-app graphite-app bot changed the base branch from graphite-base/19661 to main February 25, 2026 00:43
@graphite-app graphite-app bot force-pushed the refactor/formatter-speculate-will-break branch from 453da1a to 4c037e3 Compare February 25, 2026 00:43
@Dunqing
Copy link
Member Author

Dunqing commented Feb 25, 2026

It's similar to the state_snapshot() APIs that were removed when porting Biome, right? 👀

Seems a lightweight version specializing in comments.

Yes, during the time I was porting, I didn't know what state_snapshot() does, since we don't have usages and can remove it without breaking tests. Now I know!

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Feb 25, 2026
Copy link
Member Author

Dunqing commented Feb 25, 2026

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)
@graphite-app graphite-app bot force-pushed the refactor/formatter-speculate-will-break branch from 4c037e3 to b40e562 Compare February 25, 2026 03:59
@graphite-app graphite-app bot merged commit b40e562 into main Feb 25, 2026
21 checks passed
@graphite-app graphite-app bot deleted the refactor/formatter-speculate-will-break branch February 25, 2026 04:06
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatter: Revisit #19399 (port is_complex_type_arguments() from Prettier)

2 participants