From 8c297f818e5471aa4a90868446fc96012cf7587a Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Tue, 12 Nov 2024 13:31:19 -0600 Subject: [PATCH] Issue #628 do not split comments with jinja tags (#642) * fix sqlfmt breaks up depends_on statements across multiple lines, causing an error in parsing #628 * add test to not split comments with jinja tags --- src/sqlfmt/comment.py | 3 +++ tests/data/unformatted/127_more_comments.sql | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/sqlfmt/comment.py b/src/sqlfmt/comment.py index c526f70c..165d7f2a 100644 --- a/src/sqlfmt/comment.py +++ b/src/sqlfmt/comment.py @@ -153,6 +153,9 @@ def _split_before(cls, text: str, max_length: int) -> Iterator[str]: """ if len(text) < max_length: yield text.rstrip() + elif re.match(r".*({{.*?}}|{%.*?%}).*", text): + # jinja comments are not split + yield text.rstrip() else: for idx, char in enumerate(reversed(text[:max_length])): if char.isspace(): diff --git a/tests/data/unformatted/127_more_comments.sql b/tests/data/unformatted/127_more_comments.sql index 56d64a41..c33513bc 100644 --- a/tests/data/unformatted/127_more_comments.sql +++ b/tests/data/unformatted/127_more_comments.sql @@ -1,4 +1,6 @@ -- source: https://github.com/tconbeer/sqlfmt/issues/348 +-- source: https://github.com/tconbeer/sqlfmt/issues/628 +-- depends_on: {{ ref('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') }} with table_a as ( select /* Notice that this select statement can fit on a single line without comments */ @@ -26,6 +28,8 @@ select 1 , 4 -- four inline )))))__SQLFMT_OUTPUT__((((( -- source: https://github.com/tconbeer/sqlfmt/issues/348 +-- source: https://github.com/tconbeer/sqlfmt/issues/628 +-- depends_on: {{ ref('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') }} with table_a as ( select