branch: externals/sql-indent commit 8a1c4ecdfdc971524e73d4f19dcd8e6bcb1a50fe Author: Pierre Téchoueyres <pierre.techouey...@free.fr> Commit: Alex Harsányi <alex-...@users.noreply.github.com>
Correct indentation of column continuation with operators. (#53) * Correct indentation of column continuation with operators. Correct from ```sql select my_col || chr(10) || '-*-' data_type, x from dual; ``` to ```sql select my_col || chr(10) || '-*-' data_type, x from dual; ``` * sql-indent.el (sqlind-column-definition-start): find the first column of the statement. (sqlind-use-anchor-indentation): use the indentation of the anchor instead of the indentation of the line containing the anchor. (sqlind-adjust-operator): search the column start in select-column-continuation. * Add test for PR#53. * Simplify code. (sqlind-column-definition-start): avoid unnecessary (unless (while ...)). (sqlind-adjust-operator): avoid complex goto-char. --- sql-indent-left.el | 4 +++- sql-indent-test.el | 4 ++++ sql-indent.el | 35 ++++++++++++++++++++++------------- test-data/pr53-io-left.eld | 1 + test-data/pr53.sql | 14 ++++++++++++++ 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/sql-indent-left.el b/sql-indent-left.el index 17bd067..b0fac9e 100644 --- a/sql-indent-left.el +++ b/sql-indent-left.el @@ -194,13 +194,15 @@ select aaa, (package +) (package-body +) (statement-continuation + sqlind-adjust-operator) + (nested-statement-open 1) (nested-statement-continuation 1) + (nested-statement-close sqlind-use-anchor-indentation) (string-continuation 0) ;; or shoult it be a begining of line or aligned with the previous block ? ;; Anyway. It's really *BAD* to continue a string accross lines. (select-column sqlind-indent-select-column-alt sqlind-adjust-operator sqlind-lone-semicolon) - (select-column-continuation sqlind-indent-select-column + (select-column-continuation sqlind-indent-select-column-alt sqlind-adjust-operator sqlind-lone-semicolon) (in-select-clause sqlind-lineup-to-clause-end diff --git a/sql-indent-test.el b/sql-indent-test.el index 67cdf1e..a14ddf9 100644 --- a/sql-indent-test.el +++ b/sql-indent-test.el @@ -355,4 +355,8 @@ information read from DATA-FILE (as generated by (ert-deftest sqlind-ert-pr52-io-left () (sqlind-ert-check-file-indentation "test-data/pr52.sql" "test-data/pr52-io-left.eld" sqlind-indentation-left-offsets-alist 2)) + +(ert-deftest sqlind-ert-pr53-io-left () + (sqlind-ert-check-file-indentation "test-data/pr53.sql" "test-data/pr53-io-left.eld" + sqlind-indentation-left-offsets-alist 2)) ;;; sql-indent-test.el ends here diff --git a/sql-indent.el b/sql-indent.el index c16dd9a..8d53878 100644 --- a/sql-indent.el +++ b/sql-indent.el @@ -185,10 +185,15 @@ statement." (goto-char pos) (catch 'found (while (re-search-backward "," limit 'noerror) - (when (sqlind-same-level-statement (point) limit) - (forward-char 1) - (sqlind-forward-syntactic-ws) - (throw 'found (point))))))) + (when (sqlind-same-level-statement (point) limit) + (forward-char 1) + (sqlind-forward-syntactic-ws) + (throw 'found (point)))) + ;; nothing was found in (while ...) so try to find the first column definition. + (goto-char limit) + (forward-sexp) + (sqlind-forward-syntactic-ws) + (point)))) (defun sqlind-syntax (context) "Return the most specific syntax of CONTEXT. @@ -1940,7 +1945,7 @@ with AND, OR or NOT to be aligned so they sit left under the WHERE clause." "\\s-*") "Regexp to match a SQL expression operator.") -(defun sqlind-adjust-operator (_syntax base-indentation) +(defun sqlind-adjust-operator (syntax base-indentation) "Adjust the indentation for operators in select clauses. If the line to be indented starts with an operator, the @@ -1966,14 +1971,18 @@ This is an indentation adjuster and needs to be added to the ;; line, not the operator (cond ((looking-at sqlind-operator-regexp) (let ((ofs (length (match-string 0)))) - (forward-line -1) - (end-of-line) - (sqlind-backward-syntactic-ws) - ;; Previous function leaves us on the first non-white-space - ;; character. This might be a string terminator (') char, move - ;; the cursor one to the left, so 'forward-sexp' works correctly. - (ignore-errors (forward-char 1)) - (forward-sexp -1) + (if (eq (sqlind-syntax-symbol syntax) + 'select-column-continuation) + (goto-char (sqlind-column-definition-start + (point) (sqlind-anchor-point syntax))) + (forward-line -1) + (end-of-line) + (sqlind-backward-syntactic-ws) + ;; Previous function leaves us on the first non-white-space + ;; character. This might be a string terminator (') char, move + ;; the cursor one to the left, so 'forward-sexp' works correctly. + (ignore-errors (forward-char 1)) + (forward-sexp -1)) (max 0 (- (current-column) ofs)))) ('t base-indentation))))) diff --git a/test-data/pr53-io-left.eld b/test-data/pr53-io-left.eld new file mode 100644 index 0000000..73e263f --- /dev/null +++ b/test-data/pr53-io-left.eld @@ -0,0 +1 @@ +(0 4 7 0 0 0 0 0 0 0 0 0 0 0 0) diff --git a/test-data/pr53.sql b/test-data/pr53.sql new file mode 100644 index 0000000..e1d91e6 --- /dev/null +++ b/test-data/pr53.sql @@ -0,0 +1,14 @@ +select my_col || chr(10) + || '-*-' data_type, + x +from dual; + + +-- Local Variables: +-- mode: sql +-- mode: sqlind-minor +-- tab-width: 2 +-- indent-tabs-mode: nil +-- sql-product: oracle +-- End: +