Skip to main content

Collapse short statements

collapseShortStatements decides whether a query that fits within maxLineWidth may take one line. It applies to every query: a whole statement, and a query inside brackets alike — a subquery, a derived table, a CTE body.

Default: true. Short things stay short: select count(*) from t on three lines is three lines of noise, a file of short lookups reads as a list when each one is a line, and a short subquery reads at a glance beside the condition it belongs to. A query that does not fit takes one line per clause, with the option on or off.

With it on, a bracketed query that stays on one line still never sits inline on a line that goes on to break after it: the formatter either opens the query or ends the line after it.

false gives every query at least one line per clause however short, and every bracketed query opens. The layout no longer depends on how long a query happens to be, so the same construct has the same shape everywhere and a file diffs clause by clause. Choose it for a project whose style guide says a clause always starts a line.

The two settings answer different questions. On, the formatter changes as little as it can and keeps short things short — and with preserveBreaks enabled, a line break you write inside a short query is kept, so you decide case by case. Off, the formatter decides, the same way every time.

The same short statement, on and off​

select a from t where x > 1
select a from t where x > 1
select a from t where x > 1
select a
from t
where x > 1

"Short" is measured against a width. maxLineWidth: 0 turns width off, so there is nothing to be shorter than, and the collapse goes off with it.

A subquery, on and off​

select a from t where a in (select b from t2)
select a from t where a in (select b from t2)
select a from t where a in (select b from t2)
select a
from t
where a in (
    select b
    from t2
)

A long statement keeps a short subquery whole​

The statement is too long for one line, so it takes a line per clause either way. The subquery inside it fits, so with the option on it stays on one line; off, it opens like any other query.

select customer_id, order_total, ordered_at, shipping_region from orders where customer_id in (select id from vip) and ordered_at >= '2024-01-01'
select customer_id, order_total, ordered_at, shipping_region
from orders
where customer_id in (select id from vip) and ordered_at >= '2024-01-01'
select customer_id, order_total, ordered_at, shipping_region from orders where customer_id in (select id from vip) and ordered_at >= '2024-01-01'
select customer_id, order_total, ordered_at, shipping_region
from orders
where
    customer_id in (
        select id
        from vip
    )
    and ordered_at >= '2024-01-01'

A short subquery never sits on a line that breaks after it​

The join condition does not fit on the derived table's line. With the option on, the derived table stays whole and the condition takes a line of its own below it; off, the derived table opens.

select * from problems join (select * from items_src) items on (problems.org = items.org and problems.course_key = items.course_key and problems.section_number = items.section_number)
select *
from problems
join (select * from items_src) items
    on (
        problems.org = items.org
        and problems.course_key = items.course_key
        and problems.section_number = items.section_number
    )
select * from problems join (select * from items_src) items on (problems.org = items.org and problems.course_key = items.course_key and problems.section_number = items.section_number)
select *
from problems
join (
    select *
    from items_src
) items on (
    problems.org = items.org
    and problems.course_key = items.course_key
    and problems.section_number = items.section_number
)

A CTE body, on and off​

with c as (select 1 as a) select a from c
with c as (select 1 as a) select a from c
with c as (select 1 as a) select a from c
with
    c as (
        select 1 as a
    )
select a
from c