Skip to main content

Reanchoring

Multi-line constructs re-flowing to the column the surrounding SQL layout lands them at.

Each pair below is a real, passing case from dfmt's test corpus (suites/dfmt-jinja/cases/reanchoring/): the template as written, then the same template after dfmt — every Jinja tag reproduced exactly, the SQL laid out around it. 9 examples.

A final argument on the closing line is still a body line​

Before

{% set q = union_all_tables(min_last_success_timestamp_for_this_run,
models_matched_from_the_incremental_manifest,
var("snowplow__start_date", "2020-01-01")) %}
select a from t

After

{%
set q = union_all_tables(
min_last_success_timestamp_for_this_run,
models_matched_from_the_incremental_manifest,
var("snowplow__start_date", "2020-01-01")
)
%}

select a
from t

A line inside a string literal is never touched​

Before

  {{
config(
description='line one
line two'
)
}}
select a from t

After

{{ config(description = 'line one
line two') }}

select a
from t

A multiline comment reanchors despite an apostrophe​

Before

    {# it's a note
spanning lines #}
select a from t

After

{# it's a note
spanning lines #}
select a
from t

A placed multiline tag reanchors to column zero​

Before

    {{
config(
materialized='table'
)
}}
select a from t

After

{{ config(materialized = 'table') }}

select a
from t

A ragged closer comes home to its opener​

width 40

Before

with base as (
{{
union_all_tables(
'x'
)
}}
)
select a from base

After

with
base as (
{{
union_all_tables(
'x'
)
}}
)
select a
from base

A tab indented interior is relaid like any other​

width 40

Before

select
a,
{{
var(
'an_undeclared_variable_name',
'a_default_value_wide_enough_yes')
}} as b
from t

After

select
a,
{{
var(
'an_undeclared_variable_name',
'a_default_value_wide_enough_yes'
)
}} as b
from t

A tab inside a multiline string survives the frame​

Before

{% set q = union_all_tables(
"a string
with tab lines
", other_arg) %}
select a from t

After

{% set q = union_all_tables("a string
with tab lines
", other_arg) %}

select a
from t

An interior shifts left when the anchor is shallower​

width 40

Before

with base as (
{{
union_all_tables(
'x'
)
}}
)
select a from base

After

with
base as (
{{
union_all_tables(
'x'
)
}}
)
select a
from base

An interior shifts right when the anchor is deeper​

width 40

Before

with base as (
{{
union_all_tables(
'x'
)
}}
)
select a from base

After

with
base as (
{{
union_all_tables(
'x'
)
}}
)
select a
from base