Skip to content

Add --preserve-lambda-breaks option#614

Open
ZacSweers wants to merge 10 commits intofacebook:mainfrom
ZacSweers:z/cascadingDslLambdas
Open

Add --preserve-lambda-breaks option#614
ZacSweers wants to merge 10 commits intofacebook:mainfrom
ZacSweers:z/cascadingDslLambdas

Conversation

@ZacSweers
Copy link
Copy Markdown
Contributor

@ZacSweers ZacSweers commented Apr 28, 2026

This is a continuation of #574 with the updated option control to orient around the requested --preserve-lambda-breaks simple option. With this, lambda block line breaks are simply just respected rather than have ktfmt impose anything.

Aside from finishing up the previous PR, this also adds a setting to the IJ plugin.

I cherrypicked the original commits to preserve authorship history (CC @loganj)

Resolves #547

loganj and others added 10 commits April 28, 2026 16:09
Add new boolean configuration option to FormattingOptions and wire it
through the command-line interface. This option will enable cascading
lambda break decisions to nested trailing lambdas, useful for DSLs
like Jetpack Compose.

Changes:
- Add cascadeNestedLambdaBreaks field to FormattingOptions data class
- Add --cascade-nested-lambda-breaks command-line flag to ParsedArgs
- Update CLI help text to document the new option
- Default value is false to maintain backward compatibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add parentLambdaBroke parameter to track parent lambda break state
through nested lambda expressions. Add helper method to identify
trailing lambdas that should inherit hierarchy preservation.

Changes:
- Add parentLambdaBroke parameter to visitLambdaExpressionInternal
- Add isTrailingLambda helper method to detect trailing lambdas
- Update method documentation to explain new parameter
- All call sites use default value (false) for now

No functional changes yet - context is passed but not used.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Implement the core cascading lambda break functionality. When
cascadeNestedLambdaBreaks is enabled and a parent lambda breaks to
multi-line, all nested trailing lambdas are forced to break as well,
preserving visual hierarchy.

Changes:
- Add insideBreakingLambda member variable to track break state
- Detect when current lambda will break to multi-line
- Force nested trailing lambdas to multi-line when parent broke
- Propagate break state through nested lambda visitor calls
- Update visitArgumentInternal and visitLambdaOrScopingFunction

The feature is now functional when --cascade-nested-lambda-breaks
flag is enabled. Backward compatible - default behavior unchanged.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add 7 comprehensive test cases covering various scenarios for the
cascadeNestedLambdaBreaks feature. Tests verify basic hierarchy
preservation, backward compatibility, deep nesting, edge cases, and
that only trailing lambdas are affected.

Changes:
- Add test for forcing nested lambda hierarchy when option enabled
- Add test for maintaining single line when option disabled
- Add test for forcing only trailing lambdas (not map/filter)
- Add test for forcing behavior when parent breaks
- Add test for preserving hierarchy with multiple statements
- Add test for handling deep nesting levels
- Add test for no effect on non-nested lambdas
- Fix containsTrailingLambdas to correctly detect nested lambdas
- Update break detection logic to handle hierarchy properly

All tests pass, feature fully functional.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Document the new --cascade-nested-lambda-breaks command-line option
in the README with examples and usage guidelines. The documentation
explains how the feature helps preserve visual hierarchy in DSL code
like Jetpack Compose.

Changes:
- Add "Cascading Lambda Breaks" section to README.md
- Include usage examples showing before/after formatting
- Clarify that option only affects trailing lambdas
- Note that it's useful for DSLs with semantic nesting

Feature is now fully implemented, tested, and documented.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 28, 2026
@ZacSweers
Copy link
Copy Markdown
Contributor Author

The CI failure looks like a bootstrap compatibility issue - can't add a new param without breaking ktfmt-gradle since it's statically compiled against the only one? I tried adding @JvmOverloads to the constructor but no dice on that either, it's a chicken-and-egg problem 😅 🥚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants