Fix indentation handling for comments in bracketed expressions#2429
Open
Fix indentation handling for comments in bracketed expressions#2429
Conversation
When a comment appeared inside parentheses (e.g., before an expression), the formatter would add extra indentation to subsequent lines. This was caused by `take()` emptying the `untaken_indents` vector during cache flush in `crawl_indent_points`, which meant the subsequent balance update via `update_crawl_balances` started with an empty vector instead of preserving the untaken indents. Changed to `clone()` to match the behavior of the normal (non-cached) code path. https://claude.ai/code/session_016UQdggVzpod5MEX18Gnya5
Contributor
Benchmark for 046e219Click to view benchmark
|
|
Hi! Any plans to merge this? We've noticed this problem as well in several SQL files. |
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
Fixed a bug where comments inside parentheses were causing incorrect indentation in SQL formatting. The issue was in the indent point tracking logic where
untaken_indentswas being moved instead of cloned, causing loss of indent state information.Changes
take(&mut untaken_indents)tountaken_indents.clone()when creating indent points at line breaks. This ensures the indent state is preserved for subsequent processing rather than being consumed.test_pass_comment_in_bracketed_expression: Validates that correctly indented SQL with comments in parentheses passes validationtest_fix_comment_in_bracketed_expression: Validates that incorrectly indented SQL with comments in parentheses is properly fixedImplementation Details
The root cause was that
take()was transferring ownership ofuntaken_indents, leaving it empty for subsequent indent point processing. By cloning instead, the indent tracking state is properly maintained throughout the crawl of indent points, allowing comments within bracketed expressions to be handled without disrupting the indentation logic.https://claude.ai/code/session_016UQdggVzpod5MEX18Gnya5