branch: externals/sql-indent commit 7edd915f33a326c3bbde97e8be665d8721e47d1a Author: Alex Harsányi <alex-...@users.noreply.github.com> Commit: GitHub <nore...@github.com>
fixups for indenting where clauses (#93) A line under the "where" clause should be lined up with the first element after the where clause, unless it is a logical operator, in which case the operator should line up under the where clause. Fixed how the base column is calculated in sqlind-left-justify-logical-operator. Some tests (pr17 and p19) had to be fixed as a result. Also introduced pr93 to verify this test case. --- sql-indent-test.el | 6 + sql-indent.el | 14 +- test-data/pr17-io-default.eld | 2 +- test-data/pr17-io-right.eld | 2 +- test-data/pr17-syn.eld | 292 +++++++++++++++++++++--------------------- test-data/pr19-syn.eld | 62 ++++----- test-data/pr93-io-default.eld | 1 + test-data/pr93.sql | 21 +++ 8 files changed, 217 insertions(+), 183 deletions(-) diff --git a/sql-indent-test.el b/sql-indent-test.el index 3fdf3dd..8be6df8 100644 --- a/sql-indent-test.el +++ b/sql-indent-test.el @@ -457,4 +457,10 @@ information read from DATA-FILE (as generated by "test-data/pr92b.sql" "test-data/pr92b-syn.eld")) +(ert-deftest sqlind-ert-pr3 () + (sqlind-ert-check-file-indentation + "test-data/pr93.sql" "test-data/pr93-io-default.eld" + (default-value 'sqlind-indentation-offsets-alist) + (default-value 'sqlind-basic-offset))) + ;;; sql-indent-test.el ends here diff --git a/sql-indent.el b/sql-indent.el index ed309b8..4e8e381 100644 --- a/sql-indent.el +++ b/sql-indent.el @@ -1946,11 +1946,14 @@ clause (select, from, where, etc) in which the current point is. (in-select-clause sqlind-lineup-to-clause-end sqlind-right-justify-logical-operator) (insert-clause sqlind-right-justify-clause) - (in-insert-clause sqlind-lineup-to-clause-end) + (in-insert-clause sqlind-lineup-to-clause-end + sqlind-right-justify-logical-operator) (delete-clause sqlind-right-justify-clause) - (in-delete-clause sqlind-lineup-to-clause-end) + (in-delete-clause sqlind-lineup-to-clause-end + sqlind-right-justify-logical-operator) (update-clause sqlind-right-justify-clause) - (in-update-clause sqlind-lineup-to-clause-end)) + (in-update-clause sqlind-lineup-to-clause-end + sqlind-right-justify-logical-operator)) "Define the indentation amount for each syntactic symbol. The value of this variable is an ALIST with the format: @@ -2210,7 +2213,10 @@ with AND, OR or NOT to be aligned so they sit under the WHERE clause." (cl-destructuring-bind ((_sym clause) . anchor) (car syntax) (if (and (equal clause "where") (looking-at "and\\|or\\|not")) - (- base-indentation (1+ (- (match-end 0) (match-beginning 0)))) + (progn + (goto-char anchor) + (+ (current-column) + (- (length clause) (- (match-end 0) (match-beginning 0))))) base-indentation)))) (defun sqlind-left-justify-logical-operator (syntax base-indentation) diff --git a/test-data/pr17-io-default.eld b/test-data/pr17-io-default.eld index f68308b..86bc559 100644 --- a/test-data/pr17-io-default.eld +++ b/test-data/pr17-io-default.eld @@ -1 +1 @@ -(0 0 0 0 0 0 0 0 9 9 9 7 9 2 1 8 4 0 0 7 7 7 7 7 7 2 9 1 4 4 4 4 4 4 25 32 24 27 27 27 27 27 8 0 0 1 8 15 8 8 0 0 3 10 1 7 13 7 7 0 0 12 12 12 0 7 7 7 7 7 7 24 7 24 7 2 31 31 51 51 26 10 10 29 24 14 1 10 1 10 10 0 0 0 0) +(0 0 0 0 0 0 0 0 9 9 9 7 9 2 1 8 3 0 0 7 7 7 7 7 7 2 9 1 3 3 3 3 3 3 24 31 23 25 25 25 25 25 8 0 0 1 3 10 3 8 0 0 3 10 1 3 9 3 7 0 0 12 12 12 0 7 7 7 7 7 7 24 7 24 7 2 31 31 51 51 26 10 10 29 24 14 1 10 1 10 10 0 0 0 0) diff --git a/test-data/pr17-io-right.eld b/test-data/pr17-io-right.eld index 6466e33..9911834 100644 --- a/test-data/pr17-io-right.eld +++ b/test-data/pr17-io-right.eld @@ -1 +1 @@ -(0 0 0 0 0 0 0 0 10 10 10 7 4 2 1 13 4 0 0 7 7 7 7 7 7 2 9 1 4 4 4 4 4 4 25 32 24 27 27 27 27 27 1 0 0 1 4 11 4 1 0 0 3 10 1 3 9 3 1 0 0 12 12 12 0 7 7 7 7 7 7 24 7 24 7 2 31 31 51 51 26 10 10 29 24 14 1 10 1 10 1 0 0 0 0) \ No newline at end of file +(0 0 0 0 0 0 0 0 10 10 10 7 4 2 1 13 3 0 0 7 7 7 7 7 7 2 9 1 3 3 3 3 3 3 24 31 23 25 25 25 25 25 1 0 0 1 3 10 3 1 0 0 3 10 1 3 9 3 1 0 0 12 12 12 0 7 7 7 7 7 7 24 7 24 7 2 31 31 51 51 26 10 10 29 24 14 1 10 1 10 1 0 0 0 0) \ No newline at end of file diff --git a/test-data/pr17-syn.eld b/test-data/pr17-syn.eld index 1e8db9b..7406365 100644 --- a/test-data/pr17-syn.eld +++ b/test-data/pr17-syn.eld @@ -30,181 +30,181 @@ (statement-continuation . 99)) ((toplevel . 33)) ((toplevel . 33)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-column . 375) - (statement-continuation . 375)) - ((select-clause . 375) - (statement-continuation . 375)) - ((select-table . 565) - (statement-continuation . 375)) - ((select-clause . 375) - (statement-continuation . 375)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-column . 374) + (statement-continuation . 374)) + ((select-clause . 374) + (statement-continuation . 374)) + ((select-table . 564) + (statement-continuation . 374)) + ((select-clause . 374) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) - ((select-clause . 888) - (nested-statement-continuation . 887) - (statement-continuation . 887)) - ((select-table . 922) - (nested-statement-continuation . 887) - (statement-continuation . 887)) - ((select-clause . 888) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 623) + (statement-continuation . 374)) + ((select-clause . 881) + (nested-statement-continuation . 880) + (statement-continuation . 880)) + ((select-table . 914) + (nested-statement-continuation . 880) + (statement-continuation . 880)) + ((select-clause . 881) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 1029) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 1019) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 1029) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 1019) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 1029) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 1019) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 1029) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 1019) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 1029) - (nested-statement-continuation . 887) - (statement-continuation . 887)) + . 1019) + (nested-statement-continuation . 880) + (statement-continuation . 880)) (((in-select-clause "where") - . 624) - (statement-continuation . 375)) + . 623) + (statement-continuation . 374)) ((toplevel . 33)) ((toplevel . 33)) - ((delete-clause . 1417) - (statement-continuation . 1417)) + ((delete-clause . 1397) + (statement-continuation . 1397)) (((in-delete-clause "where") - . 1442) - (statement-continuation . 1417)) - ((nested-statement-continuation . 1481) - (statement-continuation . 1481)) + . 1422) + (statement-continuation . 1397)) + ((nested-statement-continuation . 1456) + (statement-continuation . 1456)) (((in-delete-clause "where") - . 1442) - (statement-continuation . 1417)) + . 1422) + (statement-continuation . 1397)) (((in-delete-clause "where") - . 1442) - (statement-continuation . 1417)) + . 1422) + (statement-continuation . 1397)) ((toplevel . 33)) ((toplevel . 33)) - ((update-clause . 1576) - (statement-continuation . 1576)) + ((update-clause . 1541) + (statement-continuation . 1541)) (((in-update-clause "set") - . 1595) - (statement-continuation . 1576)) - ((update-clause . 1576) - (statement-continuation . 1576)) + . 1560) + (statement-continuation . 1541)) + ((update-clause . 1541) + (statement-continuation . 1541)) (((in-update-clause "where") - . 1674) - (statement-continuation . 1576)) - ((nested-statement-continuation . 1710) - (statement-continuation . 1710)) + . 1639) + (statement-continuation . 1541)) + ((nested-statement-continuation . 1671) + (statement-continuation . 1671)) (((in-update-clause "where") - . 1674) - (statement-continuation . 1576)) + . 1639) + (statement-continuation . 1541)) (((in-update-clause "where") - . 1674) - (statement-continuation . 1576)) + . 1639) + (statement-continuation . 1541)) ((toplevel . 33)) ((toplevel . 33)) (((in-insert-clause "insert into") - . 1800) - (statement-continuation . 1800)) - ((nested-statement-continuation . 1830) - (statement-continuation . 1830)) - ((nested-statement-continuation . 1830) - (statement-continuation . 1830)) - ((insert-clause . 1800) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((nested-statement-continuation . 2081) - (statement-continuation . 2081)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((nested-statement-continuation . 2174) - (statement-continuation . 2174)) - ((select-column . 1902) - (statement-continuation . 1800)) - ((select-clause . 1902) - (statement-continuation . 1800)) - ((select-column . 2293) - (nested-statement-continuation . 2292) - (statement-continuation . 2292)) - ((select-column . 2293) - (nested-statement-continuation . 2292) - (statement-continuation . 2292)) - ((nested-statement-continuation . 2403) - (statement-continuation . 2403)) - ((nested-statement-continuation . 2403) - (statement-continuation . 2403)) - ((select-clause . 2293) - (nested-statement-continuation . 2292) - (statement-continuation . 2292)) - ((select-clause . 2278) - (nested-statement-continuation . 2277) - (statement-continuation . 2277)) - ((select-table-continuation . 2271) - (statement-continuation . 1800)) - ((select-column . 2650) - (nested-statement-continuation . 2649) - (statement-continuation . 2649)) - ((select-clause . 2650) - (nested-statement-continuation . 2649) - (statement-continuation . 2649)) - ((select-join-condition . 2638) - (statement-continuation . 1800)) - ((select-clause . 1902) - (statement-continuation . 1800)) + . 1753) + (statement-continuation . 1753)) + ((nested-statement-continuation . 1783) + (statement-continuation . 1783)) + ((nested-statement-continuation . 1783) + (statement-continuation . 1783)) + ((insert-clause . 1753) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((nested-statement-continuation . 2034) + (statement-continuation . 2034)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((nested-statement-continuation . 2127) + (statement-continuation . 2127)) + ((select-column . 1855) + (statement-continuation . 1753)) + ((select-clause . 1855) + (statement-continuation . 1753)) + ((select-column . 2246) + (nested-statement-continuation . 2245) + (statement-continuation . 2245)) + ((select-column . 2246) + (nested-statement-continuation . 2245) + (statement-continuation . 2245)) + ((nested-statement-continuation . 2356) + (statement-continuation . 2356)) + ((nested-statement-continuation . 2356) + (statement-continuation . 2356)) + ((select-clause . 2246) + (nested-statement-continuation . 2245) + (statement-continuation . 2245)) + ((select-clause . 2231) + (nested-statement-continuation . 2230) + (statement-continuation . 2230)) + ((select-table-continuation . 2224) + (statement-continuation . 1753)) + ((select-column . 2603) + (nested-statement-continuation . 2602) + (statement-continuation . 2602)) + ((select-clause . 2603) + (nested-statement-continuation . 2602) + (statement-continuation . 2602)) + ((select-join-condition . 2591) + (statement-continuation . 1753)) + ((select-clause . 1855) + (statement-continuation . 1753)) (((in-select-clause "group by") - . 2773) - (statement-continuation . 1800)) - ((select-clause . 1902) - (statement-continuation . 1800)) + . 2726) + (statement-continuation . 1753)) + ((select-clause . 1855) + (statement-continuation . 1753)) (((in-select-clause "order by") - . 2802) - (statement-continuation . 1800)) + . 2755) + (statement-continuation . 1753)) (((in-select-clause "order by") - . 2802) - (statement-continuation . 1800)) + . 2755) + (statement-continuation . 1753)) ((comment-start . 33) (toplevel . 33)) ((comment-start . 33) diff --git a/test-data/pr19-syn.eld b/test-data/pr19-syn.eld index 8939a77..8a96fab 100644 --- a/test-data/pr19-syn.eld +++ b/test-data/pr19-syn.eld @@ -20,36 +20,36 @@ (((package-body "my_pacakge") . 1)) (((defun-start "get_my_value") - . 198)) + . 197)) (((defun-start "get_my_value") - . 198)) + . 197)) (((defun-start "get_my_value") - . 198)) - ((statement-continuation . 286)) - ((select-clause . 340) - (statement-continuation . 286)) - ((select-clause . 340) - (statement-continuation . 286)) + . 197)) + ((statement-continuation . 285)) + ((select-clause . 339) + (statement-continuation . 285)) + ((select-clause . 339) + (statement-continuation . 285)) (((defun-start "get_my_value") - . 198)) + . 197)) (((defun-start "get_my_value") - . 198)) + . 197)) (((block-start begin) - . 198) + . 197) ((defun-start "get_my_value") - . 198)) + . 197)) (((in-begin-block defun "get_my_value") - . 437)) + . 436)) (((in-block loop "") - . 445)) + . 444)) (((block-end loop "") - . 445) + . 444) ((in-block loop "") - . 445)) + . 444)) (((block-end defun "get_my_value") - . 437) + . 436) ((in-begin-block defun "get_my_value") - . 437)) + . 436)) (((package-body "my_pacakge") . 1)) (((block-start begin) @@ -57,31 +57,31 @@ ((package-body "my_pacakge") . 1)) (((in-begin-block package "my_pacakge") - . 533)) + . 532)) (((in-block loop "") - . 541)) + . 540)) (((block-end loop "") - . 541) + . 540) ((in-block loop "") - . 541)) + . 540)) (((in-begin-block package "my_pacakge") - . 533)) + . 532)) (((in-begin-block package "my_pacakge") - . 533)) + . 532)) (((in-begin-block nil "") - . 614)) + . 613)) (((block-end nil "") - . 614) + . 613) ((in-begin-block nil "") - . 614)) + . 613)) (((in-begin-block package "my_pacakge") - . 533)) + . 532)) (((in-begin-block package "my_pacakge") - . 533)) + . 532)) (((block-end package "my_pacakge") - . 533) + . 532) ((in-begin-block package "my_pacakge") - . 533)) + . 532)) ((toplevel . 1)) ((comment-start . 1) (toplevel . 1)) diff --git a/test-data/pr93-io-default.eld b/test-data/pr93-io-default.eld new file mode 100644 index 0000000..7fa969a --- /dev/null +++ b/test-data/pr93-io-default.eld @@ -0,0 +1 @@ +(0 0 0 0 0 1 10 3 3 0 0 2 1 9 3 0 0 3 1 10 3 0) diff --git a/test-data/pr93.sql b/test-data/pr93.sql new file mode 100644 index 0000000..b308121 --- /dev/null +++ b/test-data/pr93.sql @@ -0,0 +1,21 @@ + +-- This code illustrates how "where" clauses are indented by chaining +-- `sqlind-lineup-to-clause-end` and `sqlind-right-justify-logical-operator` + +delete from foo + where a = + b -- b is lined up with a + and c = d -- the "and" clause is right-aligned under the where clause + and d = e; + +select * + from foo + where a = + b + and c = d; + +update foo + set a = 1 + where a = + b + and c = d;