branch: externals/sql-indent
commit 0a05fe3db10fa512103dc407fec00d3c5f976b61
Author: Alex Harsanyi <[email protected]>
Commit: Alex Harsanyi <[email protected]>

    Add initial align rules for SQL statements
---
 README.md     | 22 ++++++++++++++++++++++
 sql-indent.el | 33 ++++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index e57703e..09a48ec 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,28 @@ The actual indentation rules are fully configurable, so they 
can be adjusted
 to your preferences. See [customize-indentation.md](customize-indentation.md)
 for how to customize the indentation rules.
 
+The package also supports aligning sql statements, like this:
+
+```sql
+update my_table
+   set col1_has_a_long_name = value1,
+       col2_is_short        = value2,
+       col3_med             = v2,
+       c4                   = v5
+ where cond1 is not null;
+
+select long_colum as lcol,
+       scol       as short_column,
+       mcolu      as mcol,
+  from my_table;
+```
+
+To use that feature, select the relevant region and type
+
+    M-x align RET
+    
+# Instalation
+
 To install this package, open the file `sql-indent.el` in Emacs and type
 
     M-x install-package-from-buffer RET
diff --git a/sql-indent.el b/sql-indent.el
index 12cf31d..c3799b5 100644
--- a/sql-indent.el
+++ b/sql-indent.el
@@ -1679,13 +1679,44 @@ determine how to indent each type of syntactic element."
        (when (> offset 0)
          (forward-char offset))))))
 
+;;; alignment rules
+
+(defvar sqlind-align-rules
+  ;; Line up he two sides of an equal sign in an update expression
+  `((sql-update-lineup-equals
+     (regexp . , ".*?\\(\\s *\\)=\\(\\s *\\).*")
+     (modes . '(sql-mode))
+     (group . (1 2))
+     (case-fold . t)
+     (repeat . t))
+    ;; lineup the column aliases (the "as name" part) in a select statement
+    (sql-sqlect-lineup-column-names
+     (regexp . , ".*?\\(\\s +\\)as\\(\\s +\\).*")
+     (modes . '(sql-mode))
+     (group . (1 2))
+     (case-fold . t)
+     (repeat . t))
+    )
+  "Align rules for SQL codes.
+
+These rules help aligning some SQL statements, such as the column
+names in select queries, or equal signs in update statements or
+where clauses.
+
+To use it, select the region to be aligned and type 
+
+    M-x align RET.
+
+See also `align' and `align-rules-list'")
+
 ;;;; sqlind-setup
 
 ;;;###autoload
 (defun sqlind-setup ()
   (set-syntax-table sqlind-syntax-table)
   (set (make-local-variable 'indent-line-function) 'sqlind-indent-line)
-  (define-key sql-mode-map [remap beginning-of-defun] 
'sqlind-beginning-of-statement))
+  (define-key sql-mode-map [remap beginning-of-defun] 
'sqlind-beginning-of-statement)
+  (setq align-mode-rules-list sqlind-align-rules))
 
 (provide 'sql-indent)
 

Reply via email to