Skip to content

Commit

Permalink
Mode refinements in recognizing declare statements (#92)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alex-hhh committed Feb 15, 2020
1 parent 1bb06f8 commit e7795c7
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 9 deletions.
5 changes: 5 additions & 0 deletions sql-indent-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 11 additions & 2 deletions sql-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

This comment has been minimized.

Copy link
@sfllaw

sfllaw Feb 19, 2020

Unfortunately, detecting the previous token is kind of a hack. It isn’t robust against all the places a DECLARE+BEGIN can show up, which is basically anywhere:

CREATE FUNCTION less_than(a text, b text) RETURNS boolean AS $$
  BEGIN
    RAISE DEBUG 'less_than(%, %)', a, b;
    DECLARE
      local_a text := a;
    local_b text := b;
    BEGIN
      RETURN local_a < local_b;
    END;
  END;
$$ LANGUAGE plpgsql;

This is why I suggested that you might need some kind of sqlind-in-function or sqlind-in-declare to determine when you are actually in a declare block?

cc: #92

(throw 'finished
(if (null sqlind-end-stmt-stack)
'declare-statement
Expand Down Expand Up @@ -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))

Expand Down
15 changes: 8 additions & 7 deletions test-data/pr92-syn.eld
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
98 changes: 98 additions & 0 deletions test-data/pr92b-syn.eld
Original file line number Diff line number Diff line change
@@ -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)))
42 changes: 42 additions & 0 deletions test-data/pr92b.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit e7795c7

Please sign in to comment.