diff --git a/CHANGELOG.md b/CHANGELOG.md index 180427c1..46f77310 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/sqlfmt/jinjafmt.py b/src/sqlfmt/jinjafmt.py index c1e39ad0..c058439f 100644 --- a/src/sqlfmt/jinjafmt.py +++ b/src/sqlfmt/jinjafmt.py @@ -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 @@ -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: diff --git a/src/sqlfmt_primer/primer.py b/src/sqlfmt_primer/primer.py index 6a551080..28a9615c 100644 --- a/src/sqlfmt_primer/primer.py +++ b/src/sqlfmt_primer/primer.py @@ -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, @@ -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 @@ -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, @@ -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(""), ), diff --git a/tests/data/unformatted/213_gitlab_fct_sales_funnel_target.sql b/tests/data/unformatted/213_gitlab_fct_sales_funnel_target.sql index 32599c5b..15de335f 100644 --- a/tests/data/unformatted/213_gitlab_fct_sales_funnel_target.sql +++ b/tests/data/unformatted/213_gitlab_fct_sales_funnel_target.sql @@ -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 diff --git a/tests/data/unformatted/300_jinjafmt.sql b/tests/data/unformatted/300_jinjafmt.sql index df27c55f..ab2f2358 100644 --- a/tests/data/unformatted/300_jinjafmt.sql +++ b/tests/data/unformatted/300_jinjafmt.sql @@ -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( @@ -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