Skip to content

Commit

Permalink
fix/#365/multiline jinja merge (#374)
Browse files Browse the repository at this point in the history
* fix #365: don't merge multiline jinja unless operator

* chore: bump primer refs

* chore: update dbt_utils primer stats
  • Loading branch information
tconbeer authored Jan 24, 2023
1 parent 54b7a2c commit dd485df
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Formatting Changes + Bug Fixes

- sqlfmt no longer merges together lines containing multiline jinja blocks unless those lines start with an operator or comma ([#365](https://github.com/tconbeer/sqlfmt/issues/365) - thank you, [@gavlt](https://github.com/gavlt)!).
- fixed a bug where adding a jinja end tag (e.g., `{% endif %}`) to a line could cause bad formatting of everything on that line

## [0.15.2] - 2023-01-23

### Features

- adds support for ARM-based platforms using Docker.

## [0.15.1] - 2023-01-20
Expand Down
5 changes: 4 additions & 1 deletion src/sqlfmt/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def write_buffers_to_query(self, query: Query) -> None:
# indented too far -- they should be formatted as if they
# have no open_brackets
for line in reversed(self.line_buffer):
if line.closes_jinja_block_from_previous_line:
if (
line.is_standalone_jinja_statement
and line.closes_jinja_block_from_previous_line
):
for node in line.nodes:
node.open_brackets = []
else:
Expand Down
25 changes: 20 additions & 5 deletions src/sqlfmt/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,22 @@ def _extract_components(
Given a list of lines, return 2 components:
1. list of all nodes in those lines, with only a single trailing newline
2. list of all comments in all of those lines
Raise CannotMergeException if lines contain nodes that cannot
be merged.
"""
nodes: List[Node] = []
comments: List[Comment] = []
final_newline: Optional[Node] = None
allow_multiline_jinja = True
has_multiline_jinja = False
for line in lines:
if has_multiline_jinja and not (
line.starts_with_operator or line.starts_with_comma
):
raise CannotMergeException(
"Can't merge lines containing multiline nodes"
)
# skip over newline nodes
content_nodes = [
cls._raise_unmergeable(node, allow_multiline_jinja)
Expand All @@ -73,17 +83,22 @@ def _extract_components(
if content_nodes:
final_newline = line.nodes[-1]
nodes.extend(content_nodes)
# we can merge lines containing multiline jinja nodes iff:
# 1. the multiline node is on the first line (allow_multiline
# is initialized to True)
# 2. the multiline node is on the second line and follows a
# standalone operator
# we can merge a line containing multiline jinja
# into a preceding line iff:
# the multiline node is on the second line and follows a
# standalone operator
if not (
allow_multiline_jinja
and len(content_nodes) == 1
and content_nodes[0].is_operator
):
allow_multiline_jinja = False
# we can merge a line into a preceding line that
# contains multiline jinja iff:
# the line starts with an operator or a comma
has_multiline_jinja = any(
[node.is_multiline_jinja for node in content_nodes]
)
comments.extend(line.comments)

if not nodes or not final_newline:
Expand Down
10 changes: 5 additions & 5 deletions src/sqlfmt_primer/primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="gitlab",
git_url="https://github.com/tconbeer/gitlab-analytics-sqlfmt.git",
git_ref="aa609dc", # sqlfmt e6f9f4c
git_ref="c786f5c", # sqlfmt d97af4e
expected_changed=3,
expected_unchanged=2414,
expected_errored=0,
Expand All @@ -39,7 +39,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="rittman",
git_url="https://github.com/tconbeer/rittman_ra_data_warehouse.git",
git_ref="b1e45fa", # sqlfmt e6f9f4c
git_ref="067c14b", # sqlfmt d97af4e
expected_changed=0,
expected_unchanged=307,
expected_errored=4, # true mismatching brackets
Expand Down Expand Up @@ -75,9 +75,9 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="dbt_utils",
git_url="https://github.com/tconbeer/dbt-utils.git",
git_ref="664fd68", # sqlfmt e6f9f4c
expected_changed=2,
expected_unchanged=129,
git_ref="8d81899", # sqlfmt d97af4e
expected_changed=0,
expected_unchanged=131,
expected_errored=0,
sub_directory=Path(""),
),
Expand Down
8 changes: 5 additions & 3 deletions tests/data/unformatted/201_basic_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
{{
config(
target_database='analytics',
target_schema=target.schema + '_snapshots',
unique_key='id',
target_schema=target.schema+'_snapshots', unique_key='id',
strategy='timestamp',
updated_at='updated_at',
)
Expand All @@ -20,5 +19,8 @@ select * from {{ ref('stg_my_model') }}{% endsnapshot %}
strategy="timestamp",
updated_at="updated_at",
)
}} select * from {{ ref("stg_my_model") }}
}}

select *
from {{ ref("stg_my_model") }}
{% endsnapshot %}

0 comments on commit dd485df

Please sign in to comment.