From 970e45c6344afdcf4d319f96a7a51f335bf72d9c Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Mira <36644554+PabloRMira@users.noreply.github.com> Date: Sat, 23 Jan 2021 21:27:04 +0100 Subject: [PATCH] [FIX] Whole line comments should be aligned with the line under them (#140) --- docs/core.html | 8 ++++++-- docs/utils.html | 21 ++++++++++++--------- nbs/00_core.ipynb | 8 ++++++-- nbs/02_utils.ipynb | 30 +++++++++++++++++++----------- sql_formatter/utils.py | 9 +++++++-- 5 files changed, 50 insertions(+), 26 deletions(-) diff --git a/docs/core.html b/docs/core.html index 675c224..ec7339a 100644 --- a/docs/core.html +++ b/docs/core.html @@ -68,6 +68,7 @@

General formattingthere */ case when a.asdf= 1 then 'b' /* here a case comment */ when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment +/* and here some inline comment */ b.asdf2 frOm table1 as a leFt join table2 as b -- and here a comment on a.asdf = b.asdf /* joiN this way */ @@ -113,6 +114,7 @@

General formatting case when a.asdf = 1 then 'b' /* here a case comment */ when b.qwer = 2 then 'c' else 'd' end as new_field, -- Some comment + /* and here some inline comment */ b.asdf2 FROM table1 as a LEFT JOIN table2 as b -- and here a comment @@ -475,7 +477,7 @@

clean_query
-
create or replace table mytable as -- Mytable example[C][CS]/* multi line[CI]comment */[C]seLecT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C]b.asdf2 frOm table1 as a leFt join table2 as b -- and here a comment[C]on a.asdf = b.asdf /* joiN this way */[C]inner join table3 as c on a.asdf=c.asdf and a.qwer= b.qwer whEre a.asdf= 1 -- comment this[C]anD b.qwer =2 and a.asdf<=1 --comment that[C]or b.qwer>=5 groUp by a.asdf
+
create or replace table mytable as -- Mytable example[C][CS]/* multi line[CI]comment */[C]seLecT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C][CS]/* and here some inline comment */[C]b.asdf2 frOm table1 as a leFt join table2 as b -- and here a comment[C]on a.asdf = b.asdf /* joiN this way */[C]inner join table3 as c on a.asdf=c.asdf and a.qwer= b.qwer whEre a.asdf= 1 -- comment this[C]anD b.qwer =2 and a.asdf<=1 --comment that[C]or b.qwer>=5 groUp by a.asdf
 
@@ -1080,7 +1082,7 @@

preformat_statements
CREATE OR REPLACE TABLE mytable AS -- Mytable example[C][CS]/* multi line[CI]comment */[C]
-SELECT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C]b.asdf2
+SELECT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C][CS]/* and here some inline comment */[C]b.asdf2
 FROM table1 as a
 LEFT JOIN table2 as b -- and here a comment[C]
 ON a.asdf = b.asdf /* joiN this way */[C]
@@ -4071,6 +4073,7 @@ 

format_simple_sqlformat_sqlassign_comment qwer, /* some comment */ case when asdf = 1 and -- comment there asdf = 2 then 2 -- comment here - /* Whole line comment */ + /* Whole line comment */ when asdf = 3 then 3 /* bla bla */ else 0 end as qwerty, qwer2 @@ -2914,7 +2914,7 @@

assign_commentassign_comment qwer, /* some comment */ case when asdf = 1 and -- comment there asdf = 2 then 2 - /* Whole line comment */ + /* Whole line comment */ when asdf = 3 then 3 /* bla bla */ else 0 end as qwerty, qwer2 @@ -2980,7 +2980,7 @@

assign_commentassign_comment{"comment": "/* some comment */[C]", "preceding": "qwer, "}, {"comment": "-- comment there[C]", "preceding": "case when asdf = 1"}, {"comment": "[CS]/* Whole line comment */[C]", "preceding": "and asdf = 2"}, - {"comment": "/* bla bla */[C]", "preceding": "then 2 when asdf = 3 then 3"} + {"comment": "/* bla bla */[C]", "preceding": "then 2 when asdf = 3 then 3"}, + {"comment": "[CS]/* another whole line comment */[C]", "preceding": "else 0 end as qwerty,"} ] ), """ @@ -3026,9 +3027,10 @@

assign_comment qwer, /* some comment */ case when asdf = 1 and -- comment there asdf = 2 then 2 - /* Whole line comment */ + /* Whole line comment */ when asdf = 3 then 3 /* bla bla */ else 0 end as qwerty, + /* another whole line comment */ qwer2 """.strip() ) @@ -3049,9 +3051,10 @@

assign_commentassign_comment qwer, /* some comment */ case when asdf = 1 and -- comment there asdf = 2 then 2 - /* Whole line comment */ + /* Whole line comment */ when asdf = 3 then 3 /* bla bla */ else 0 end as qwerty, qwer2 @@ -3118,7 +3121,7 @@

assign_comment=5 groUp by a.asdf\n" + "create or replace table mytable as -- Mytable example[C][CS]/* multi line[CI]comment */[C]seLecT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C][CS]/* and here some inline comment */[C]b.asdf2 frOm table1 as a leFt join table2 as b -- and here a comment[C]on a.asdf = b.asdf /* joiN this way */[C]inner join table3 as c on a.asdf=c.asdf and a.qwer= b.qwer whEre a.asdf= 1 -- comment this[C]anD b.qwer =2 and a.asdf<=1 --comment that[C]or b.qwer>=5 groUp by a.asdf\n" ] } ], @@ -829,7 +831,7 @@ "output_type": "stream", "text": [ "CREATE OR REPLACE TABLE mytable AS -- Mytable example[C][CS]/* multi line[CI]comment */[C]\n", - "SELECT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C]b.asdf2\n", + "SELECT a.asdf, b.qwer, -- some comment here[C][CS]/* and here is a line comment inside select */[C]substr(c.asdf, 1, 2) as substr_asdf,[CS]/* some commenT[CI]there */[C]case when a.asdf= 1 then 'b' /* here a case comment */[C]when b.qwer =2 then 'c' else 'd' end as new_field, -- Some comment[C][CS]/* and here some inline comment */[C]b.asdf2\n", "FROM table1 as a\n", "LEFT JOIN table2 as b -- and here a comment[C]\n", "ON a.asdf = b.asdf /* joiN this way */[C]\n", @@ -2978,6 +2980,7 @@ " case when a.asdf = 1 then 'b' /* here a case comment */\n", " when b.qwer = 2 then 'c'\n", " else 'd' end as new_field, -- Some comment\n", + " /* and here some inline comment */\n", " b.asdf2\n", "FROM table1 as a\n", " LEFT JOIN table2 as b -- and here a comment\n", @@ -3198,6 +3201,7 @@ " case when a.asdf = 1 then 'b' /* here a case comment */\n", " when b.qwer = 2 then 'c'\n", " else 'd' end as new_field, -- Some comment\n", + " /* and here some inline comment */\n", " b.asdf2\n", "FROM table1 as a\n", " LEFT JOIN table2 as b -- and here a comment\n", diff --git a/nbs/02_utils.ipynb b/nbs/02_utils.ipynb index 58b236c..2217510 100644 --- a/nbs/02_utils.ipynb +++ b/nbs/02_utils.ipynb @@ -2158,8 +2158,8 @@ " \n", " The comment dictionaries `cds` should contain the keys \"comment\" and \"preceding\" (string)\n", " \"\"\"\n", - " # split by newline\n", " fsplit_s = fs.split(\"\\n\")\n", + " number_of_lines = len(fsplit_s)\n", " # define container for output\n", " fsplit_s_out = fsplit_s.copy()\n", " # compile regex before loop\n", @@ -2176,7 +2176,12 @@ " # get line number with maximal jaccard distance (most similar)\n", " line_number = max(enumerate(cp_list), key=lambda x: x[1])[0]\n", " line = fsplit_s[line_number]\n", - " indentation = len(line) - len(replace_select.sub(\"\", line.lstrip()))\n", + " next_line = (\n", + " fsplit_s[line_number + 1] # next line is relevant for indentation of whole line comments\n", + " if line_number < number_of_lines - 1\n", + " else fsplit_s[line_number] # if there is no next line then take the current line\n", + " )\n", + " indentation = len(next_line) - len(replace_select.sub(\"\", next_line.lstrip()))\n", " # add comment to it and replace [C] by empty string and [CS] by newline + proper indentation\n", " whitespace = \"\" if match_beginn_cs.match(d[\"comment\"]) else \" \"\n", " fsplit_s_out[line_number] += whitespace + re.sub(\n", @@ -2251,7 +2256,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2 -- comment here\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n" @@ -2282,7 +2287,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2 -- comment here\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n", @@ -2303,7 +2308,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n" @@ -2334,7 +2339,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n", @@ -2356,9 +2361,10 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", + " /* another whole line comment */\n", " qwer2\n" ] } @@ -2380,7 +2386,8 @@ " {\"comment\": \"/* some comment */[C]\", \"preceding\": \"qwer, \"},\n", " {\"comment\": \"-- comment there[C]\", \"preceding\": \"case when asdf = 1\"},\n", " {\"comment\": \"[CS]/* Whole line comment */[C]\", \"preceding\": \"and asdf = 2\"},\n", - " {\"comment\": \"/* bla bla */[C]\", \"preceding\": \"then 2 when asdf = 3 then 3\"}\n", + " {\"comment\": \"/* bla bla */[C]\", \"preceding\": \"then 2 when asdf = 3 then 3\"},\n", + " {\"comment\": \"[CS]/* another whole line comment */[C]\", \"preceding\": \"else 0 end as qwerty,\"}\n", " ]\n", " ),\n", "\"\"\"\n", @@ -2389,9 +2396,10 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", + " /* another whole line comment */\n", " qwer2\n", "\"\"\".strip()\n", ")" @@ -2411,7 +2419,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n" @@ -2444,7 +2452,7 @@ " qwer, /* some comment */\n", " case when asdf = 1 and -- comment there\n", " asdf = 2 then 2\n", - " /* Whole line comment */\n", + " /* Whole line comment */\n", " when asdf = 3 then 3 /* bla bla */\n", " else 0 end as qwerty,\n", " qwer2\n", diff --git a/sql_formatter/utils.py b/sql_formatter/utils.py index 029b706..e2614e4 100644 --- a/sql_formatter/utils.py +++ b/sql_formatter/utils.py @@ -611,8 +611,8 @@ def assign_comment(fs, cds): The comment dictionaries `cds` should contain the keys "comment" and "preceding" (string) """ - # split by newline fsplit_s = fs.split("\n") + number_of_lines = len(fsplit_s) # define container for output fsplit_s_out = fsplit_s.copy() # compile regex before loop @@ -629,7 +629,12 @@ def assign_comment(fs, cds): # get line number with maximal jaccard distance (most similar) line_number = max(enumerate(cp_list), key=lambda x: x[1])[0] line = fsplit_s[line_number] - indentation = len(line) - len(replace_select.sub("", line.lstrip())) + next_line = ( + fsplit_s[line_number + 1] # next line is relevant for indentation of whole line comments + if line_number < number_of_lines - 1 + else fsplit_s[line_number] # if there is no next line then take the current line + ) + indentation = len(next_line) - len(replace_select.sub("", next_line.lstrip())) # add comment to it and replace [C] by empty string and [CS] by newline + proper indentation whitespace = "" if match_beginn_cs.match(d["comment"]) else " " fsplit_s_out[line_number] += whitespace + re.sub(