emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/sql-indent e7795c7 6/9: Mode refinements in recognizing


From: Alex Harsanyi
Subject: [elpa] externals/sql-indent e7795c7 6/9: Mode refinements in recognizing declare statements (#92)
Date: Fri, 20 Mar 2020 19:08:03 -0400 (EDT)

branch: externals/sql-indent
commit e7795c7ca379ed6024db271d522acc858f0a64f7
Author: Alex Harsanyi <address@hidden>
Commit: Alex Harsanyi <address@hidden>

    Mode refinements in recognizing declare statements (#92)
    
    * sql-indent.el (sqlind-maybe-declare-statement): recognize declare 
statements
    in begin blocks, then and else blocks.
    (sqlind-refine-syntax): don't nest declare statements
---
 sql-indent-test.el      |  5 +++
 sql-indent.el           | 13 ++++++-
 test-data/pr92-syn.eld  | 15 ++++----
 test-data/pr92b-syn.eld | 98 +++++++++++++++++++++++++++++++++++++++++++++++++
 test-data/pr92b.sql     | 42 +++++++++++++++++++++
 5 files changed, 164 insertions(+), 9 deletions(-)

diff --git a/sql-indent-test.el b/sql-indent-test.el
index d9752c9..3fdf3dd 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -452,4 +452,9 @@ information read from DATA-FILE (as generated by
    "test-data/pr92a.sql"
    "test-data/pr92a-syn.eld"))
 
+(ert-deftest sqlind-ert-pr92b ()
+  (sqlind-ert-check-file-syntax
+   "test-data/pr92b.sql"
+   "test-data/pr92b-syn.eld"))
+
 ;;; sql-indent-test.el ends here
diff --git a/sql-indent.el b/sql-indent.el
index 9f413af..ed309b8 100644
--- a/sql-indent.el
+++ b/sql-indent.el
@@ -651,8 +651,8 @@ See also `sqlind-beginning-of-block'"
     (when (or (not (eq sql-product 'postgres))
               (save-excursion
                 (sqlind-backward-syntactic-ws)
-                (skip-syntax-backward "_") ; note that the $$ is symbol 
constitent!
-                (looking-at "\\$\\$")))
+                (skip-syntax-backward "_w") ; note that the $$ is symbol 
constituent!
+                (looking-at "\\(\\$\\$\\)\\|begin\\|then\\|else")))
       (throw 'finished
         (if (null sqlind-end-stmt-stack)
             'declare-statement
@@ -1634,6 +1634,15 @@ not a statement-continuation POS is the same as the
      (push (sqlind-refine-end-syntax
             nil "" (point) context)
            context))
+
+    ;; See #92 and pr92b.sql, no such thing as a nested declare statement, use
+    ;; the context of the previous declare-statement!
+    ((and (eq sql-product 'postgres)
+          (eq syntax-symbol 'declare-statement)
+          (looking-at "declare\\_>"))
+     (goto-char anchor)
+     (setq context (sqlind-syntax-of-line)))
+
     )
   context))
 
diff --git a/test-data/pr92-syn.eld b/test-data/pr92-syn.eld
index 41d3581..a069d67 100644
--- a/test-data/pr92-syn.eld
+++ b/test-data/pr92-syn.eld
@@ -31,19 +31,20 @@
    . 292))
  ((declare-statement . 358))
  ((declare-statement . 358))
- ((declare-statement . 358))
- ((statement-continuation . 416))
- ((select-clause . 460)
-  (statement-continuation . 416))
+ (((in-begin-block defun "less_than")
+   . 292))
+ ((statement-continuation . 414))
+ ((select-clause . 456)
+  (statement-continuation . 414))
  (((block-start begin)
    . 358)
   (declare-statement . 358))
  (((in-begin-block toplevel-block "")
-   . 491))
+   . 485))
  (((block-end toplevel-block "")
-   . 491)
+   . 485)
   ((in-begin-block toplevel-block "")
-   . 491))
+   . 485))
  (((block-end defun "less_than")
    . 292)
   ((in-begin-block defun "less_than")
diff --git a/test-data/pr92b-syn.eld b/test-data/pr92b-syn.eld
new file mode 100644
index 0000000..74faaa7
--- /dev/null
+++ b/test-data/pr92b-syn.eld
@@ -0,0 +1,98 @@
+(((comment-start . 1)
+  (toplevel . 1))
+ ((toplevel . 1))
+ ((toplevel . 1))
+ (((in-begin-block defun "less_than")
+   . 47))
+ (((in-begin-block defun "less_than")
+   . 47))
+ (((in-begin-block defun "less_than")
+   . 47))
+ ((statement-continuation . 171))
+ ((declare-statement . 113))
+ (((block-start begin)
+   . 113)
+  (declare-statement . 113))
+ (((in-begin-block toplevel-block "")
+   . 227))
+ (((block-end toplevel-block "")
+   . 227)
+  ((in-begin-block toplevel-block "")
+   . 227))
+ (((block-end defun "less_than")
+   . 47)
+  ((in-begin-block defun "less_than")
+   . 47))
+ ((toplevel . 1))
+ ((toplevel . 1))
+ (((in-begin-block defun "less_than")
+   . 292))
+ (((in-begin-block nil "")
+   . 358))
+ ((declare-statement . 368))
+ ((declare-statement . 368))
+ (((block-start begin)
+   . 368)
+  (declare-statement . 368))
+ (((in-begin-block toplevel-block "")
+   . 430))
+ (((block-end toplevel-block "")
+   . 430)
+  ((in-begin-block toplevel-block "")
+   . 430))
+ (((block-end nil "")
+   . 358)
+  ((in-begin-block nil "")
+   . 358))
+ (((block-end defun "less_than")
+   . 292)
+  ((in-begin-block defun "less_than")
+   . 292))
+ ((toplevel . 1))
+ ((toplevel . 1))
+ (((in-begin-block defun "less_than")
+   . 506))
+ (((in-begin-block nil "")
+   . 572))
+ (((in-block if "")
+   . 582))
+ ((declare-statement . 606))
+ (((block-start begin)
+   . 606)
+  (declare-statement . 606))
+ (((in-begin-block toplevel-block "")
+   . 645))
+ (((block-end toplevel-block "")
+   . 645)
+  ((in-begin-block toplevel-block "")
+   . 645))
+ (((block-start else)
+   . 582)
+  ((in-block if "")
+   . 582))
+ (((in-block else "")
+   . 688))
+ ((declare-statement . 699))
+ ((declare-statement . 699))
+ (((block-start begin)
+   . 699)
+  (declare-statement . 699))
+ (((in-begin-block toplevel-block "")
+   . 767))
+ (((block-end toplevel-block "")
+   . 767)
+  ((in-begin-block toplevel-block "")
+   . 767))
+ (((block-end if "")
+   . 582)
+  ((in-block else "")
+   . 688))
+ (((block-end nil "")
+   . 572)
+  ((in-begin-block nil "")
+   . 572))
+ (((block-end defun "less_than")
+   . 506)
+  ((in-begin-block defun "less_than")
+   . 506))
+ ((toplevel . 1)))
\ No newline at end of file
diff --git a/test-data/pr92b.sql b/test-data/pr92b.sql
new file mode 100644
index 0000000..8d974b9
--- /dev/null
+++ b/test-data/pr92b.sql
@@ -0,0 +1,42 @@
+-- -*- mode: sql; sql-product: postgres; -*-
+
+create function less_than(a text, b text) returns boolean as $$
+  declare local_a text := a;
+  declare local_b text := b;
+  declare
+    local_d text := d;
+    local_c text := c;
+  begin
+    return local_a < local_b;
+  end;
+$$ language plpgsql;
+
+create function less_than(a text, b text) returns boolean as $$
+  begin
+    declare
+      local_a text := a;
+      local_b text := b;
+    begin
+      return local_a < local_b;
+    end;
+  end;
+$$ language plpgsql;
+
+create function less_than(a text, b text) returns boolean as $$
+  begin
+    if a is null then
+      declare
+        local_a text: a;
+      begin
+        return false;
+      end;
+    else
+      declare
+        local_a text := a;
+        local_b text := b;
+      begin
+        return local_a < local_b;
+      end;
+    end if;
+  end;
+$$ language plpgsql;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]