branch: externals/sql-indent
commit a4bacebe9e4962e7e321f24e710efee86cc1f02d
Author: Alex Harsányi <[email protected]>
Commit: GitHub <[email protected]>
Don't be confused by names that contain keywords (#42)
Don't be confused by names that contain keywords
for example, the "FOR" in "V_SPORT_ZONE_FOR_SESSION" is not a keyword.
* Add test cases for PR42
---
sql-indent-test.el | 3 +++
sql-indent.el | 16 ++++++++--------
test-data/pr42-syn.eld | 33 +++++++++++++++++++++++++++++++++
test-data/pr42.sql | 12 ++++++++++++
4 files changed, 56 insertions(+), 8 deletions(-)
diff --git a/sql-indent-test.el b/sql-indent-test.el
index 6ddc42e..adf6e08 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -339,4 +339,7 @@ information read from DATA-FILE (as generated by
(ert-deftest sqlind-ert-pr40 ()
(sqlind-ert-check-file-syntax "test-data/pr40.sql" "test-data/pr40-syn.eld"))
+(ert-deftest sqlind-ert-pr42 ()
+ (sqlind-ert-check-file-syntax "test-data/pr42.sql" "test-data/pr42-syn.eld"))
+
;;; sql-indent-test.el ends here
diff --git a/sql-indent.el b/sql-indent.el
index bcd97b6..f5badef 100644
--- a/sql-indent.el
+++ b/sql-indent.el
@@ -300,7 +300,7 @@ But don't go before LIMIT."
(catch 'done
(while (> (point) (or limit (point-min)))
(when (re-search-backward
-
";\\|:=\\|\\b\\(declare\\|begin\\|cursor\\|for\\|loop\\|if\\|then\\|else\\|elsif\\)\\b\\|)"
+
";\\|:=\\|\\_<\\(declare\\|begin\\|cursor\\|for\\|loop\\|if\\|then\\|else\\|elsif\\)\\_>\\|)"
limit 'noerror)
(unless (sqlind-in-comment-or-string (point))
(let ((candidate-pos (match-end 0)))
@@ -309,20 +309,20 @@ But don't go before LIMIT."
;; of the keywords inside one of them and think this is a
;; statement start.
(progn (forward-char 1) (forward-sexp -1)))
- ((looking-at "\\b\\(cursor\\|for\\)\\b")
+ ((looking-at "cursor\\|for")
;; statement begins at the start of the keyword
(throw 'done (point)))
- ((looking-at "\\b\\(then\\|else\\)\\b")
+ ((looking-at "then\\|else")
;; then and else start statements when they are inside
;; blocks, not expressions.
(sqlind-backward-syntactic-ws)
(when (looking-at ";")
;; Statement begins after the keyword
(throw 'done candidate-pos)))
- ((looking-at "\\b\\(elsif\\)\\b")
+ ((looking-at "elsif")
;; statement begins at the start of the keyword
(throw 'done (point)))
- ((looking-at "\\b\\(if\\)\\b")
+ ((looking-at "if")
(when (sqlind-good-if-candidate)
;; statement begins at the start of the keyword
(throw 'done (point))))
@@ -733,11 +733,11 @@ See also `sqlind-beginning-of-block'"
(throw 'finished (list 'in-block 'exception))))))
(defconst sqlind-start-block-regexp
- (concat "\\(\\b"
+ (concat "\\(\\_<"
(regexp-opt '("if" "then" "else" "elsif" "loop"
"begin" "declare" "create" "alter" "exception"
"procedure" "function" "end" "case") t)
- "\\b\\)\\|)")
+ "\\_>\\)\\|)")
"Regexp to match the start of a block.")
(defun sqlind-beginning-of-block (&optional end-statement-stack)
@@ -783,7 +783,7 @@ reverse order (a stack) and is used to skip over nested
blocks."
((looking-at "then")
;; THEN and ELSE clauses are indented relative to the start of the
;; when clause, which we must find
- (while (not (and (re-search-backward "\\bwhen\\b")
+ (while (not (and (re-search-backward "\\_<when\\_>")
(sqlind-same-level-statement (point) start)))
nil)
(cons 'case-clause-item (point)))
diff --git a/test-data/pr42-syn.eld b/test-data/pr42-syn.eld
new file mode 100644
index 0000000..9535a1a
--- /dev/null
+++ b/test-data/pr42-syn.eld
@@ -0,0 +1,33 @@
+(((comment-start . 1)
+ (toplevel . 1))
+ ((comment-start . 1)
+ (toplevel . 1))
+ ((toplevel . 1))
+ (((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((select-column . 151)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((select-column . 151)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((select-clause . 151)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((select-clause . 151)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ (((in-select-clause "where")
+ . 299)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((nested-statement-continuation . 341)
+ (statement-continuation . 341))
+ ((nested-statement-continuation . 340)
+ (statement-continuation . 340))
+ (((in-select-clause "where")
+ . 299)
+ ((create-statement view "v_sport_zone_for_session")
+ . 109))
+ ((toplevel . 1)))
+
\ No newline at end of file
diff --git a/test-data/pr42.sql b/test-data/pr42.sql
new file mode 100644
index 0000000..a70d0f7
--- /dev/null
+++ b/test-data/pr42.sql
@@ -0,0 +1,12 @@
+-- sql-indent used to think that the statement begins at the "FOR" keyword in
+-- "V_SPORT_ZONE_FOR_SESSION"
+create view V_SPORT_ZONE_FOR_SESSION as
+ select S.id as session_id,
+ VSZ.zone_id as zone_id,
+ VSZ.zone_metric_id as zone_metric_id
+ from A_SESSION S, V_SPORT_ZONE VSZ
+ where S.sport_id = VSZ.sport_id
+ and (((S.sub_sport_id is null or S.sub_sport_id = 0)
+ and (VSZ.sub_sport_id is null or VSZ.sub_sport_id = 0))
+ or S.sub_sport_id = VSZ.sub_sport_id)
+ and S.start_time between VSZ.valid_from and VSZ.valid_until;