Fix inline comments mangling CREATE TABLE formatting#42
Conversation
Inline `-- ...` comments on column definitions were treated as bogus columns: given alignment padding and a trailing comma (which mangled the comment text and broke idempotency), and a comment after the last element was dropped entirely. Two CST facts drove the bug. Comments between columns parse as `comment` sibling nodes inside `TableElementList`, so `flatten_list` returned them alongside real `TableElement`s and `classify_table_element`'s catch-all arm turned each into a `Column`. A comment after the final element parses as a direct child of `CreateStmt` (before the closing paren), which the formatter never inspected. `format_create_table_stmt` now groups each element with the comments that trail it (new `group_table_elements` helper) and also scans the statement node's direct `comment` children, routing them by byte position. Both the river and left-aligned render branches emit comments at the end of the preceding element's line, after the comma; river style aligns them to a common column. Scoped to CREATE TABLE; CREATE FOREIGN TABLE has the same latent bug, tracked in #41. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
✅ Files skipped from review due to trivial changes (3)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR updates ChangesInline Comment Preservation in CREATE TABLE Formatting
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/formatter/stmt.rs`:
- Around line 526-544: The comment handling in stmt.rs is routing any comment
after the table-element list into the last grouped table element, so update the
comment scan in the code around group_table_elements and elem_list to stop at
the end of OptTableElementList and keep later clauses out of the column list.
Also preserve source order for leading comments by collecting and emitting them
in the same order they appear before the list, instead of allowing later
processing to reorder them.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 195af3c7-b40d-47ff-88ed-74d59bbddecf
📒 Files selected for processing (6)
src/formatter/stmt.rstests/fixtures/mozilla/create_table_inline_comments.expectedtests/fixtures/mozilla/create_table_inline_comments.sqltests/fixtures/river/create_table_inline_comments.expectedtests/fixtures/river/create_table_inline_comments.sqltests/fixtures_test.rs
The comment scan in format_create_table_stmt appended any CreateStmt-level comment after the element list to the last column, so a comment attached to a following clause (WITH, INHERITS, a partition spec) was folded into the column list. It now stops at the element list's closing paren — a direct child of CreateStmt — and leaves later-clause comments out of the columns. Leading comments are also emitted in source order: comments that precede the element list are kept ahead of comments inside it, rather than after them. Addresses CodeRabbit review feedback on #42. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
🤖 This comment was posted by Claude on behalf of @gmr Addressed the CodeRabbit review on this cycle (commit 4477ae9):
CI: all checks were green on the prior commit; verified locally with |
pr-agent: PR is ready to merge ✅Automated monitoring complete. Summary of this session:
No outstanding blockers — ready for human merge. |
|
This addresses gmr/pgfmt#11 (comment) |
Problem
Reported in gmr/pgfmt#12. Inline
-- ...comments onCREATE TABLEcolumn definitions were mangled:produced (aweber/river):
Comments got column-alignment padding and a trailing comma (mangling the comment text and breaking idempotency), and the final comment (
--contrainte de table) was dropped entirely.Root cause
Two CST facts:
commentsibling nodes insideTableElementList, soflatten_listreturned them alongside realTableElements andclassify_table_element's catch-all arm turned each into aColumn.CreateStmt(before the closing paren), which the formatter never inspected.Fix
format_create_table_stmtnow groups each element with the comments that trail it (newgroup_table_elementshelper) and scans the statement node's directcommentchildren, routing them by byte position (before the element list → leading; after → trailing on the last element). Both the river and left-aligned render branches emit comments at the end of the preceding element's line, after the comma; river style aligns them to a common column.Result:
Tests
create_table_inline_commentsfixtures for river and mozilla styles.cargo fmt --checkandcargo clippy -D warningsclean.Out of scope
CREATE FOREIGN TABLEshares the same pipeline and has the same latent bug — tracked in #41.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
CREATE TABLEformatting to preserve and correctly attach inline--comments within column and constraint lists.Tests