branch: externals/sql-indent
commit 775091c9bcf30dff84a9da52b90e46eefddf2072
Author: Alex Harsanyi <[email protected]>
Commit: Alex Harsanyi <[email protected]>
Recognize assignments as statement starts (#24)
---
sql-indent-test.el | 29 ++++++++++++++++++-
sql-indent.el | 7 ++++-
test-data/pr24-syn.eld | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++
test-data/pr24.sql | 43 ++++++++++++++++++++++++++++
4 files changed, 154 insertions(+), 2 deletions(-)
diff --git a/sql-indent-test.el b/sql-indent-test.el
index 39b9d4f..291a43f 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -49,7 +49,8 @@
;; prepared separately using `sqlind-collect-syntax-from-buffer' and
;; `sqlind-collect-indentation-offsets-from-buffer'.
;;
-;;;; Preparing new tests
+;;
+;;;; PREPARING NEW TESTS
;;
;; To create a syntax check file, open an *ielm* buffer (M-x ielm RET) and
;; run:
@@ -70,6 +71,29 @@
;; .eld file.
;;
;; See the end of file for examples on how to put together the actual tests.
+;;
+;;
+;;;; NAMING CONVENTION
+;;
+;; If there's a problem report on GitHub (issue or pull request), use the
+;; issue number as the "tag" for the test. For example, a test that addresses
+;; issue number 30 will have a tag "pr30", otherwise choose a short tag for
+;; the test. With this tag the following files are created:
+;;
+;; * "test-data/pr30.sql" -- contains the sample SQL code that is verified
+;;
+;; * "test-data/pr30-syn.eld" -- contains the syntactic information for the ;;
+;; test (as generated by `sqlind-collect-syntax-from-buffer')
+;;
+;; * "test-data/pr30-io-XXX.eld" -- contains the indentation offsets for the
+;; indentation rules beging tested (where XXX is a short name for those
+;; rules)
+;;
+;; * `sqlind-ert-pr30' -- is the name of the test function in this file. If
+;; there are multiple tests, append another tag (e.g. `sqlind-ert-pr30-syn',
+;; for the syntactic check test, `sqlind-ert-pr30-io-XXX' for the indentation
+;; offset test).
+;;
;;; Code
(require 'ert)
@@ -275,4 +299,7 @@ information read from DATA-FILE (as generated by
(ert-deftest sqlind-ert-pr19 ()
(sqlind-ert-check-file-syntax "test-data/pr19.sql" "test-data/pr19-syn.eld"))
+(ert-deftest sqlind-ert-pr24 ()
+ (sqlind-ert-check-file-syntax "test-data/pr24.sql" "test-data/pr24-syn.eld"))
+
;;; sql-indent-test.el ends here
diff --git a/sql-indent.el b/sql-indent.el
index 53d7c44..afc4247 100644
--- a/sql-indent.el
+++ b/sql-indent.el
@@ -270,7 +270,7 @@ But don't go before LIMIT."
(catch 'done
(while (not (eq (point) (or limit (point-min))))
(when (re-search-backward
-
";\\|\\b\\(declare\\|begin\\|cursor\\|loop\\|if\\|then\\|else\\|elsif\\)\\b\\|)"
+
";\\|:=\\|\\b\\(declare\\|begin\\|cursor\\|loop\\|if\\|then\\|else\\|elsif\\)\\b\\|)"
limit 'noerror)
(unless (sqlind-in-comment-or-string (point))
(let ((candidate-pos (match-end 0)))
@@ -292,6 +292,11 @@ But don't go before LIMIT."
((looking-at "\\b\\(if\\|elsif\\)\\b")
;; statement begins at the start of the keyword
(throw 'done (point)))
+ ((looking-at ":=")
+ ;; assignment statements start at the assigned variable
+ (sqlind-backward-syntactic-ws)
+ (forward-sexp -1)
+ (throw 'done (point)))
((not (sqlind-in-comment-or-string (point)))
(throw 'done candidate-pos))))))))))
diff --git a/test-data/pr24-syn.eld b/test-data/pr24-syn.eld
new file mode 100644
index 0000000..a38437c
--- /dev/null
+++ b/test-data/pr24-syn.eld
@@ -0,0 +1,77 @@
+(((toplevel . 1))
+ ((toplevel . 1))
+ ((toplevel . 1))
+ ((toplevel . 1))
+ ((declare-statement . 50))
+ ((declare-statement . 50))
+ (((block-start begin)
+ . 50)
+ (declare-statement . 50))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ (((in-block if "")
+ . 142))
+ ((statement-continuation . 171))
+ ((statement-continuation . 171))
+ (((in-block if "")
+ . 142))
+ ((statement-continuation . 253))
+ ((statement-continuation . 253))
+ (((block-start elsif)
+ . 142)
+ ((in-block if "")
+ . 142))
+ (((in-block elsif "")
+ . 299))
+ ((statement-continuation . 331))
+ ((statement-continuation . 331))
+ (((in-block elsif "")
+ . 299))
+ ((statement-continuation . 413))
+ ((statement-continuation . 413))
+ (((block-start else)
+ . 299)
+ ((in-block elsif "")
+ . 299))
+ (((in-block else "")
+ . 459))
+ (((in-block else "")
+ . 459))
+ (((block-end if "")
+ . 142)
+ ((in-block else "")
+ . 459))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ ((statement-continuation . 531))
+ ((statement-continuation . 531))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ ((statement-continuation . 607))
+ ((statement-continuation . 607))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ (((in-begin-block toplevel-block "")
+ . 106))
+ (((block-end toplevel-block "")
+ . 106)
+ ((in-begin-block toplevel-block "")
+ . 106))
+ ((toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((toplevel . 1)))
\ No newline at end of file
diff --git a/test-data/pr24.sql b/test-data/pr24.sql
new file mode 100644
index 0000000..d5f65c0
--- /dev/null
+++ b/test-data/pr24.sql
@@ -0,0 +1,43 @@
+set serveroutput on
+set trimout on trimspool on
+
+declare
+ var1 varchar2(25000);
+ var2 varchar2(25000);
+begin
+ dbms_output.enable(null);
+ if var1 is not null then
+ var2 := 'var1 is set : -*-'
+ || var1
+ || '-*- some text after ...';
+ var1 := '-*-'
+ || var1
+ || '-*-';
+ elsif var2 is not null then
+ var1 := 'var2 is set : -*-'
+ || var2
+ || '-*- some text after ...';
+ var2 := '-*-'
+ || var2
+ || '-*-';
+ else
+ var2 := '-*-empty-*-';
+ var1 := '-*-empty-*-';
+ end if;
+
+ var2 := 'var1 is set : -*-'
+ || var1
+ || '-*- some text after ...';
+ var1 := 'var2 is set : -*-'
+ || var2
+ || '-*- some text after ...';
+ dbms_output.put_line(var1);
+ dbms_output.put_line(var2);
+end;
+/
+-- Local Variables:
+-- mode: sql
+-- tab-width: 2
+-- indent-tabs-mode: nil
+-- sql-product: oracle
+-- End: