Skip to content

fix(pretty_print): break CAST like a function call - #959

Open
psteinroe wants to merge 1 commit into
test/pretty-print-tighter-width-checkfrom
fix/pretty-print-cast-indent
Open

psteinroe wants to merge 1 commit into
test/pretty-print-tighter-width-checkfrom
fix/pretty-print-cast-indent

Conversation

@psteinroe

Copy link
Copy Markdown
Collaborator

Stacked on #950.

cast(x as type) broke before as with no indent, so the type sat at the column of the enclosing expression, and ) stuck to it. #946 listed this as not covered:

-- before
select
  cast('4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'
  as interval);
-- after
select
  cast(
    '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'
    as interval
  );

What changed

CAST now uses the function-call layout from #946: on one line when it fits; otherwise (, then expr as type one level in, and ) back on the cast's line. expr as type is a group of its own, so it stays together on the indented line when it fits there, and breaks before as only when it doesn't:

-- before
  -cast('-2147483647 months -2147483647 days -9223372036854775807 us'
  as interval);
-- after
  -cast(
    '-2147483647 months -2147483647 days -9223372036854775807 us' as interval
  );

castStyle: operator (x::type) is unchanged.

Snapshot changes

51 snapshot files change, all reviewed. The categories:

  • A cast that doesn't fit: cast(x\nas type) becomes cast(\n x as type\n) when x as type fits on the indented line, or cast(\n x\n as type\n) when it doesn't. This is most of the diff, mostly long literals: jsonb, json, interval, tstypes, horology, xml, sqljson_queryfuncs, int8, md5, name, timestamptz, xid, txid.
  • A cast around a call that breaks: cast(format( / cast(row( used to hug the call, with as type) on the last line at the enclosing indent. Now the call sits one level inside cast(, followed by as type and ) on their own lines (brin, brin_bloom, brin_multi, amutils, type_sanity, stats_import, fast_default, jsonb).
  • Deep nesting: a broken cast around a deep chain of calls moves the whole chain one level right. The deep replace(...) chain in complex_select_0 / complex_select_part_2 shows this, and it is the most visible change.
  • A few casts inside extract(, tuplesort, window, arrays and plpgsql move the same way.

New cast_breaks_like_a_function_call test in width_layout.rs. All pgls_pretty_print tests and the option matrix (fixtures and regression suite) pass, and clippy is clean.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant