Skip to content

fix(pretty_print): preserve comment order and CASE END boundaries - #1025

Open
edjubert wants to merge 1 commit into
supabase-community:mainfrom
edjubert:edjubert/preserve-comment-keyword-boundaries
Open

edjubert wants to merge 1 commit into
supabase-community:mainfrom
edjubert:edjubert/preserve-comment-keyword-boundaries

Conversation

@edjubert

Copy link
Copy Markdown
Contributor

Follow-up to #934 and #935.

Consecutive comments before ELSE, THEN, END, and GRANT/REVOKE keywords could be emitted in reverse order because only the last comment received the keyword anchor:

-- Before
select case when a then 1 /* second */ else /* first */ 2 end from t;
-- After
select case when a then 1 /* first */ /* second */ else 2 end from t;

Comments before END could also move into a nested CASE, or out into FROM when the last branch was a function call:

-- Before
select case when a then 1 else case when b then 2 else 3 /* outer */ end end from t;
-- After
select case when a then 1 else case when b then 2 else 3 end /* outer */ end from t;

The formatter now anchors every comment in a consecutive group to the same keyword and keeps mixed block/line comments on the same side of their node. Comments before a CASE's END are attached to that CASE and emitted after its complete final branch. CASE/END matching uses actual CASE nodes, parenthesis depth, and field-name context so names such as t.end and subquery aliases do not change the closing boundary.

Six new regression tests cover both layouts, mixed comment groups, GRANT/REVOKE, nested CASEs in THEN and ELSE, compound final expressions, keyword names, and idempotence.

Validation:

  • cargo test -p pgls_pretty_print — 759 tests passed.
  • cargo test -p pgls_pretty_print --test option_matrix -- --ignored — fixtures and PostgreSQL regression corpus across all 32 layout combinations.
  • cargo clippy --all-targets --all-features.
  • cargo fmt --all -- --check and git diff --check.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant