Python: Integrate auto-formatting into template engine#6728
Merged
knutwannheden merged 1 commit intomainfrom Feb 13, 2026
Merged
Python: Integrate auto-formatting into template engine#6728knutwannheden merged 1 commit intomainfrom
knutwannheden merged 1 commit intomainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
auto_format()andmaybe_auto_format()helper functions to the Python format module, mirroring the JS template system'smaybeAutoFormatpatternmaybe_auto_formatin_apply_coordinatesafter template application so templated AST nodes get properly formatted (operator spacing, indentation, etc.)Test plan
rewrite_runintegration test: template with bad operator spacing ({expr}+2) produces correct output (a + 2)rewrite_runintegration test: template with missing comma space (print('prefix',{msg})) produces correct output (print('prefix', 'hello'))rewrite_runintegration test: formatting preserves correct indentation in deeply nested scope (class > def > if)maybe_auto_formatidentity guard (same object → skip formatting)auto_formatwithout cursor returns tree unchanged_apply_coordinateswithout CU context does not crash🤖 Generated with Claude Code