Skip to content

Commit

Permalink
fix #236: do not subtract prefix from running length in jinjafmt (#237)
Browse files Browse the repository at this point in the history
* fix: do not subtract prefix from running length in jinjafmt

* chore: bump primer refs

* fix: fix utils primer stats

* chore: update changelog
  • Loading branch information
tconbeer authored Aug 8, 2022
1 parent 24e75e3 commit 8f2d019
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## Formatting Changes + Bug Fixes
- fixed a bug that could cause lines with long jinja tags to be one character over the line length limit, and could result in unstable formatting ([#237](https://github.com/tconbeer/sqlfmt/pull/237) - thank you [@nfcampos](https://github.com/nfcampos)!)

## [0.10.1] - 2022-08-05

### Features
Expand Down
4 changes: 2 additions & 2 deletions src/sqlfmt/jinjafmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def format_line(self, line: Line) -> List[Line]:
"""
line_length = self.mode.line_length
if line.contains_jinja:
running_length = len(line.prefix) - len(line.nodes[0].prefix)
running_length = len(line.prefix)
for i, node in enumerate(line.nodes):
is_blackened = self._format_jinja_node(
node, max_length=line_length - running_length
Expand All @@ -360,7 +360,7 @@ def format_line(self, line: Line) -> List[Line]:
)
)
else:
running_length += len(node)
running_length += len(node) - (len(node.prefix) if i == 0 else 0)
else:
return [line]
else:
Expand Down
12 changes: 6 additions & 6 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="e234957c16c90ce3174f9bb4ed21f78a7f4277f1", # sqlfmt bcf660d
git_ref="17daa5c", # sqlfmt 6d33371
expected_changed=4,
expected_unchanged=2413,
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="1315cdb9aeff129a16e6be0ba5696c040058a34c", # sqlfmt 5b2d861
git_ref="da9d1eb", # sqlfmt 6d33371
expected_changed=0,
expected_unchanged=307,
expected_errored=4, # true mismatching brackets
Expand All @@ -57,7 +57,7 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="aqi",
git_url="https://github.com/tconbeer/aqi_livibility_analysis.git",
git_ref="8004f3383897d9066e3167fdfddcdd4c8418b7ea", # sqlfmt 0.5.0
git_ref="cab1292", # sqlfmt 6d33371
expected_changed=0,
expected_unchanged=7,
expected_errored=0,
Expand All @@ -75,9 +75,9 @@ def get_projects() -> List[SQLProject]:
SQLProject(
name="dbt_utils",
git_url="https://github.com/tconbeer/dbt-utils.git",
git_ref="bfe3e2d33bea6a3cbc07d27e5d6c21eaf3273126", # sqlfmt bcf660d
expected_changed=1,
expected_unchanged=130,
git_ref="98ef570", # sqlfmt 6d33371
expected_changed=0,
expected_unchanged=131,
expected_errored=0,
sub_directory=Path(""),
),
Expand Down
7 changes: 5 additions & 2 deletions tests/data/unformatted/213_gitlab_fct_sales_funnel_target.sql
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ unioned_targets as (
left join
fy22_user_hierarchy
on {{ sales_funnel_text_slugify("target_matrix.area") }}
=
{{ sales_funnel_text_slugify("fy22_user_hierarchy.crm_opp_owner_area_stamped") }}
= {{
sales_funnel_text_slugify(
"fy22_user_hierarchy.crm_opp_owner_area_stamped"
)
}}
where target_matrix.fiscal_year = 2022

union all
Expand Down
35 changes: 35 additions & 0 deletions tests/data/unformatted/300_jinjafmt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
else 'default.html'
%}

with
a as (

select
{{ dbt_utils.surrogate_key(var("surrogate_key_columns_menu_item_123456")) }}
as order_item_id,
-- this next line's jinja tag is one char too long
{{ dbt_utils.surrogate_key(var("surrogate_key_columns_menu_item_1234567")) }}
as menu_item_id,
from b

)

select *
from a

)))))__SQLFMT_OUTPUT__(((((
{{
config(
Expand Down Expand Up @@ -77,3 +93,22 @@
if layout_template is defined
else 'default.html'
%}

with
a as (

select
{{ dbt_utils.surrogate_key(var("surrogate_key_columns_menu_item_123456")) }}
as order_item_id,
-- this next line's jinja tag is one char too long
{{
dbt_utils.surrogate_key(
var("surrogate_key_columns_menu_item_1234567")
)
}} as menu_item_id,
from b

)

select *
from a

0 comments on commit 8f2d019

Please sign in to comment.