diff --git a/docs/core.html b/docs/core.html index 0a26773..70db96c 100644 --- a/docs/core.html +++ b/docs/core.html @@ -2003,6 +2003,53 @@
reformat_too_long_line<
+
+
+assert_and_print(
+ reformat_too_long_line(
+ "select case when asdf in ('1234', '2345', '1234', '2354', '2345', '2352345', '245623462', '124123412', '124312341234') then 1",
+ max_len=40
+ ),
+"""
+select case when asdf in ('1234', '2345',
+ '1234', '2354',
+ '2345', '2352345',
+ '245623462', '124123412',
+ '124312341234') then 1
+""".strip()
+)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {% endraw %}
+
+ {% raw %}
+
+
+
+
assert_and_print(
@@ -3572,6 +3619,59 @@ format_select
+
+Long line with in (...)
and quotes
+
+
+
+
+ {% raw %}
+
+
+
+
+
+
+assert_and_print(
+ format_select(
+ "select asdf, case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234', '12341234', '1234') then 1 else 0 end as qwer, asdf2"
+ ),
+"""
+select asdf,
+ case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234',
+ '12341234', '1234') then 1
+ else 0 end as qwer,
+ asdf2
+""".strip()
+)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
{% endraw %}
@@ -3627,20 +3727,6 @@ format_from
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3696,20 +3782,6 @@ format_join
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3770,20 +3842,6 @@ format_on
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3807,21 +3865,6 @@ format_on
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3846,22 +3889,6 @@ format_on
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3893,22 +3920,6 @@ format_on
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -3968,22 +3979,6 @@ format_where
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4005,22 +4000,6 @@ format_where
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4042,22 +4021,6 @@ format_where
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4118,21 +4081,6 @@ format_statement_line
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4153,20 +4101,6 @@ format_statement_line
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4191,23 +4125,6 @@ format_statement_line
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4264,22 +4181,6 @@ format_statements
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4386,23 +4287,6 @@ add_semicolon
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4433,23 +4317,6 @@ add_semicolon
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4480,23 +4347,6 @@ add_semicolon
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4556,45 +4406,6 @@ format_simple_sql
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4629,24 +4440,6 @@ format_simple_sql
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4782,32 +4575,6 @@ format_sql
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4835,45 +4602,6 @@ format_sql
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -4954,32 +4682,6 @@ Nested subqueries
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -5016,24 +4718,6 @@ With SELECT DISTINCT
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -5114,29 +4798,6 @@ More convoluted nested subquery
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -5175,28 +4836,6 @@ More convoluted nested subquery
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -5237,25 +4876,6 @@ More convoluted nested subquery
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
@@ -5301,28 +4921,6 @@ More convoluted nested subquery
-
-
-
-
-
-
-
-
-
-
-
{% endraw %}
diff --git a/nbs/00_core.ipynb b/nbs/00_core.ipynb
index 4622c04..3fdfbac 100644
--- a/nbs/00_core.ipynb
+++ b/nbs/00_core.ipynb
@@ -1406,9 +1406,9 @@
" first_append = True\n",
" lcol = 0 # line code column\n",
" for i, s in enumerate(li):\n",
- " if \"in (\" in li[i-4:i] and not quote_open1 and not quote_open2 and not in_in:\n",
+ " if \"in (\" in li[i-3:i+1] and not quote_open1 and not quote_open2 and not in_in:\n",
" in_in = True\n",
- " indentation = i\n",
+ " indentation = i + 1\n",
" elif s == \")\" and not quote_open1 and not quote_open2 and in_in:\n",
" in_in = False\n",
" elif s == \",\" and in_in and not quote_open1 and not quote_open2:\n",
@@ -1575,6 +1575,39 @@
")"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "select case when asdf in ('1234', '2345',\n",
+ " '1234', '2354',\n",
+ " '2345', '2352345',\n",
+ " '245623462', '124123412',\n",
+ " '124312341234') then 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "assert_and_print(\n",
+ " reformat_too_long_line(\n",
+ " \"select case when asdf in ('1234', '2345', '1234', '2354', '2345', '2352345', '245623462', '124123412', '124312341234') then 1\",\n",
+ " max_len=40\n",
+ " ),\n",
+ "\"\"\"\n",
+ "select case when asdf in ('1234', '2345',\n",
+ " '1234', '2354',\n",
+ " '2345', '2352345',\n",
+ " '245623462', '124123412',\n",
+ " '124312341234') then 1\n",
+ "\"\"\".strip()\n",
+ ")"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -2703,6 +2736,45 @@
")"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Long line with `in (...)` and quotes"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "select asdf,\n",
+ " case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234',\n",
+ " '12341234', '1234') then 1\n",
+ " else 0 end as qwer,\n",
+ " asdf2\n"
+ ]
+ }
+ ],
+ "source": [
+ "assert_and_print(\n",
+ " format_select(\n",
+ " \"select asdf, case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234', '12341234', '1234') then 1 else 0 end as qwer, asdf2\"\n",
+ " ),\n",
+ "\"\"\"\n",
+ "select asdf,\n",
+ " case when asdf in ('1231', '123123', '12312', '21412431234', '12341234123', '12341234',\n",
+ " '12341234', '1234') then 1\n",
+ " else 0 end as qwer,\n",
+ " asdf2\n",
+ "\"\"\".strip()\n",
+ ")"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
diff --git a/sql_formatter/core.py b/sql_formatter/core.py
index c4e98ea..0826d55 100644
--- a/sql_formatter/core.py
+++ b/sql_formatter/core.py
@@ -232,9 +232,9 @@ def reformat_too_long_line(li, max_len=82):
first_append = True
lcol = 0 # line code column
for i, s in enumerate(li):
- if "in (" in li[i-4:i] and not quote_open1 and not quote_open2 and not in_in:
+ if "in (" in li[i-3:i+1] and not quote_open1 and not quote_open2 and not in_in:
in_in = True
- indentation = i
+ indentation = i + 1
elif s == ")" and not quote_open1 and not quote_open2 and in_in:
in_in = False
elif s == "," and in_in and not quote_open1 and not quote_open2: