Skip to content

test(pretty_print): count literals in the snapshot width check - #950

Open
psteinroe wants to merge 1 commit into
fix/pretty-print-width-offendersfrom
test/pretty-print-tighter-width-check
Open

psteinroe wants to merge 1 commit into
fix/pretty-print-width-offendersfrom
test/pretty-print-tighter-width-check

Conversation

@psteinroe

Copy link
Copy Markdown
Collaborator

Stacked on #946.

#912 listed this as not covered. The snapshot width check (assert_line_lengths in tests.rs) didn't catch lines past the width in two common cases:

  • It only counted the characters between string literals. A call like json_value(cast('{"a": ...}' as jsonb), '$.a.b' of 95 columns passed at width 80, because most of it was inside quotes.
  • It excused a line whose longest identifier plus 20 columns didn't fit.

That is how the json_value layout fixed in #898 got through.

The new check

common::width_problems replaces it. It uses the Postgres lexer and measures the whole line, with tabs counted as indentSize columns. A line is only excused when its indentation, its longest token (string literals included) and 12 more columns don't fit. That covers a long literal, a long quoted identifier, a function body, or code nested so deeply that there is no room left. A -- comment ending the line is written as in the source and is not counted. The snapshot tests and the option matrix from #912 both use it. The matrix checks width only at 80 and 120 with castStyle: cast: at 40 some statements can't fit, and a :: cast can't break either (select '...'::int8 with a compact select list).

Formatter changes it needed

The stricter check found these, and they are fixed here:

  • Text after a multi-line literal. A group that contained a multi-line literal or function body stayed on one line as long as the literal's first line fit. Whatever followed its last line was never measured, e.g. a chain of #- operators running to 206 columns in explain. The renderer now checks that the text after the last line of such a token fits too.
  • is [not] distinct from, like, ilike, similar to. These now wrap their right-hand side like a comparison operator does, instead of always printing a space.
  • SQL-syntax calls (extract, position, overlay, substring, xmlexists). Their arguments are a group inside the call's parentheses, so second from cast(...) stays together on the indented line when it fits there, and from only breaks when it doesn't.

Snapshot changes

All reviewed:

  • text after a multi-line literal moves to the next line, or the call around the literal breaks (explain, jsonb_jsonpath, misc_functions, tsearch);
  • is [not] distinct from / like wrap before the right-hand side (xid, txid, opr_sanity, strings);
  • extract(field from source) breaks after from only when the arguments don't fit the indented line (timetz, interval).

New tests in width_layout.rs cover the check itself, is not distinct from, similar to, and text after a multi-line literal. All fixtures pass the stricter check, and the option matrix passes with it.

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