Skip to content

Python: Integrate auto-formatting into template engine#6728

Merged
knutwannheden merged 1 commit intomainfrom
test-templating
Feb 13, 2026
Merged

Python: Integrate auto-formatting into template engine#6728
knutwannheden merged 1 commit intomainfrom
test-templating

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Summary

  • Add auto_format() and maybe_auto_format() helper functions to the Python format module, mirroring the JS template system's maybeAutoFormat pattern
  • Call maybe_auto_format in _apply_coordinates after template application so templated AST nodes get properly formatted (operator spacing, indentation, etc.)
  • The formatter resolves styles from the CompilationUnit in the cursor ancestry and formats only the templated subtree, gracefully skipping when no CU context is available
  • Fix comparator element access to use unwrapped elements directly

Test plan

  • rewrite_run integration test: template with bad operator spacing ({expr}+2) produces correct output (a + 2)
  • rewrite_run integration test: template with missing comma space (print('prefix',{msg})) produces correct output (print('prefix', 'hello'))
  • rewrite_run integration test: formatting preserves correct indentation in deeply nested scope (class > def > if)
  • Unit test: maybe_auto_format identity guard (same object → skip formatting)
  • Unit test: auto_format without cursor returns tree unchanged
  • Unit test: _apply_coordinates without CU context does not crash
  • All 158 existing template + format tests pass with no regressions

🤖 Generated with Claude Code

Add auto_format() and maybe_auto_format() helper functions to the Python
format module, mirroring the JavaScript template system's pattern. After
template application, _apply_coordinates now calls maybe_auto_format to
normalize spacing, indentation, and other formatting on the templated
subtree using the enclosing CompilationUnit's style context.

The formatter resolves styles from the CompilationUnit in the cursor
ancestry and formats only the templated subtree, not the whole file.
When no CompilationUnit context is available (e.g. synthetic cursors in
unit tests), formatting is gracefully skipped.

Includes rewrite_run integration tests verifying operator spacing,
method call formatting, and correct behavior in deeply nested scopes.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Feb 13, 2026
@knutwannheden knutwannheden merged commit 398b8c4 into main Feb 13, 2026
1 check passed
@knutwannheden knutwannheden deleted the test-templating branch February 13, 2026 07:53
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant