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

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

[elpa] externals/sql-indent 146655b 02/13: Add new indentation function


From: Stefan Monnier
Subject: [elpa] externals/sql-indent 146655b 02/13: Add new indentation function for select columns. (#50)
Date: Sun, 11 Feb 2018 22:24:12 -0500 (EST)

branch: externals/sql-indent
commit 146655bd36a2972d33235a0c9aef743b9fcf76de
Author: Pierre Téchoueyres <address@hidden>
Commit: Alex Harsányi <address@hidden>

    Add new indentation function for select columns. (#50)
    
    * Add new indentation function for select columns.
    
    * sql-indent-left.el: Improve indentation of select columns
    (sqlind-indent-select-keywords): tweak indentation after DISTINCT,
    UNIQUE or TOP keywords.
    
    * Add tests for pr50, correct sqlind-indentation-left-offsets-alist.
    
    * sql-indent-left.el: correct sqlind-indent-select-keywords
    (sqlind-indent-select-keywords): avoid error when no keyword found.
    
    * sql-indent-test.el: add test for pr50.
    
    * Add `sqlind-indent-select-column-alt'.
    
    Clone `sqlind-indent-select-column' into `sqlind-indent-select-column-alt'
    to allow alignment of select colums with KEYWORDS.
    
    * sql-indent-left.el: Change setup to use `sqlind-indent-select-column-alt'
    (sqlind-indent-select-column-alt): extends `sqlind-indent-select-column'.
    
    * Checkdoc style corrections.
    
    sql-indent-left.el: apply chechdoc advices.
---
 sql-indent-left.el         | 40 +++++++++++++++++++++++++++++++---------
 sql-indent-test.el         |  4 ++++
 test-data/pr50-io-left.eld |  1 +
 test-data/pr50.sql         | 34 ++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 9 deletions(-)

diff --git a/sql-indent-left.el b/sql-indent-left.el
index a326964..17bd067 100644
--- a/sql-indent-left.el
+++ b/sql-indent-left.el
@@ -67,8 +67,8 @@
 (require 'sql-indent)
 
 (defun indent-case-statement-items (syntax base-indentation)
-  ;; Look for a syntax of ((block-start when) (in-block case "") ...)
-  ;; or ((block-start else) (in-block case "") ...)
+  "Look for a SYNTAX of ((block-start when) (in-block case \"\") ...)
+or ((block-start else) (in-block case \"\") ...)."
   (let ((outer (sqlind-outer-context syntax)))
     (if (and (eq 'in-block (sqlind-syntax-symbol outer))
              (eq 'case (sqlind-syntax-keyword outer))
@@ -97,7 +97,7 @@
                       sqlind-adjust-operator
                      sqlind-right-justify-logical-operator
                      sqlind-lone-semicolon)
-    ;; mandatory 
+    ;; mandatory
     (select-table-continuation sqlind-indent-select-table +
                                sqlind-lone-semicolon)
     ;; rest picked up from the original indentation offsets
@@ -180,9 +180,7 @@ select aaa,
           xxx
  order by xxx desc,
           aaa asc
- ;
-
-")
+ ;")
 
 (defvar sqlind-indentation-left-offsets-alist
   `((select-clause 0)
@@ -195,9 +193,13 @@ select aaa,
     (case-clause +)
     (package +)
     (package-body +)
+    (statement-continuation + sqlind-adjust-operator)
     (nested-statement-continuation 1)
     (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
                                 sqlind-adjust-operator
                                 sqlind-lone-semicolon)
@@ -226,7 +228,8 @@ clear columns
 set linesize 2500
 set trimout on trimspool on
 
-select atc.column_name,
+select DISTINCT
+       atc.column_name,
        atc.data_type,
        data_length,
        data_precision,
@@ -298,9 +301,28 @@ group by aaa,
          xxx
 order by xxx desc,
          aaa asc
-;
+;")
+
+(defun sqlind-indent-select-column-alt (syntax base-indentation)
+  "Return the indentation for a column after a SELECT DISTINCT clause.
+
+SYNTAX is the syntax of the current line, BASE-INDENTATION is the
+current indentation, which we need to update.
 
-")
+Like `sqlind-indent-select-column' but we try to align to the KEYWORD,
+but if we are the first column after the SELECT clause we simply
+add `sqlind-basic-offset'."
+  (save-excursion
+    (goto-char (sqlind-anchor-point syntax))
+    (when (looking-at "select\\s *\\(top\\s +[0-9]+\\|distinct\\|unique\\)?")
+      (if (match-beginning 1)
+         (goto-char (match-beginning 1))
+       (goto-char (match-end 0))))
+    (skip-syntax-forward " ")
+    (if (or (looking-at sqlind-comment-start-skip)
+           (looking-at "$"))
+       (+ base-indentation sqlind-basic-offset)
+      (current-column))))
 
 ;;;###autoload
 (defun sqlind-setup-style-left ()
diff --git a/sql-indent-test.el b/sql-indent-test.el
index 6bcc345..97d6082 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -348,4 +348,8 @@ information read from DATA-FILE (as generated by
 (ert-deftest sqlind-ert-pr49 ()
   (sqlind-ert-check-file-syntax "test-data/pr49.sql" "test-data/pr49-syn.eld"))
 
+(ert-deftest sqlind-ert-pr50-io-left ()
+  (sqlind-ert-check-file-indentation "test-data/pr50.sql" 
"test-data/pr50-io-left.eld"
+                                    sqlind-indentation-left-offsets-alist 2))
+
 ;;; sql-indent-test.el ends here
diff --git a/test-data/pr50-io-left.eld b/test-data/pr50-io-left.eld
new file mode 100644
index 0000000..5e921c3
--- /dev/null
+++ b/test-data/pr50-io-left.eld
@@ -0,0 +1 @@
+(0 7 7 7 7 7 7 7 0 7 0 0 0 0 0 0 0 19 26 19 19 19 19 19 19 0 0 0 0 0 0 0 0 0 0)
diff --git a/test-data/pr50.sql b/test-data/pr50.sql
new file mode 100644
index 0000000..77f5a77
--- /dev/null
+++ b/test-data/pr50.sql
@@ -0,0 +1,34 @@
+select distinct
+       atc.column_name,
+       atc.data_type,
+       data_length,
+       data_precision,
+       nullable,
+       data_scale,
+       nvl(substr(comments, 1, 100), atc.column_name) comments
+from   all_tab_columns atc,
+       all_col_comments acc
+where  atc.owner       = acc.owner
+and    atc.table_name  = acc.table_name
+and    atc.column_name = acc.column_name
+and    atc.owner       = user
+and    atc.table_name  = 'MY_TABLE'
+and    atc.column_name = p_column_name
+and    not exists (select 1
+                   from   all_tab_columns atc1,
+                          all_col_comments acc1
+                   where  atc1.owner       = acc1.owner
+                   and    atc1.table_name  = acc1.table_name
+                   and    atc1.column_name = acc1.column_name
+                   and    atc1.owner       = atc.owner
+                   and    atc1.table_name  = atc.table_name
+                   and    acc1.column_name = acc.column_name)
+;
+
+-- Local Variables:
+-- mode: sql
+-- mode: sqlind-minor
+-- tab-width: 2
+-- indent-tabs-mode: nil
+-- sql-product: oracle
+-- End:



reply via email to

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