[sql] Add a code formatter for sql-mode

Add sql layer options for sqlfmt. Add a function spacemacs/sql-format-buffer to
format the whole buffer with sqlfmt. Add a key binding for format.
This commit is contained in:
Seong Yong-ju 2018-09-17 23:24:16 +09:00 committed by duianto
parent 91914378c1
commit 09835e1ed2
4 changed files with 38 additions and 0 deletions

View file

@ -2006,6 +2006,7 @@ Other:
Christianson)
- Added layer variable =sql-auto-indent= to toggle the use of =sql-indent= package
(thanks to David Chen)
- Add a code formatter for sql-mode (thanks to Seong Yong-ju)
**** Spell-checking
- Added spell checking transient state (thanks to Francesc Elies Henar)
- Update to flyspell-correct v0.5 (thanks to Boris Buliga)

View file

@ -37,6 +37,7 @@ This layer adds support for a wide range of SQL dialects to Spacemacs.
- Sybase
- Vertica
- Syntax-checking via [[https://github.com/purcell/sqlint][sqlint]] for ANSI SQL.
- Format code with =sqlfmt=
- Snippet insertion for the more general SQL constructs.
- REPL support via =SQLi= buffer.
- Automatic capitalization of keywords.

View file

@ -0,0 +1,28 @@
(defcustom sqlfmt-executable
"sqlfmt"
"Location of sqlfmt executable."
:type 'string)
(defcustom sqlfmt-options
'("-u")
"Command line options to pass to sqlfmt."
:type '(repeat string))
(defun sqlfmt-buffer ()
(interactive)
(let* ((orig-buffer (current-buffer))
(orig-point (point))
(tmpbuf (generate-new-buffer "*sqlfmt*"))
(status-code (apply #'call-process-region (point-min) (point-max)
sqlfmt-executable nil tmpbuf nil
sqlfmt-options)))
(deactivate-mark)
(with-current-buffer tmpbuf
(setq buffer-read-only t))
(if (eq status-code 0)
(progn
(with-current-buffer tmpbuf
(copy-to-buffer orig-buffer (point-min) (point-max)))
(kill-buffer tmpbuf)
(goto-char orig-point))
(error "sqlfmt failed, see %s buffer for details." (buffer-name tmpbuf)))))

View file

@ -22,6 +22,7 @@
:fetcher github
:repo "alex-hhh/emacs-sql-indent"
:files ("sql-indent.el")))
(sqlfmt :location local)
(sqlup-mode :toggle sql-capitalize-keywords)
))
@ -139,6 +140,13 @@
:init (add-hook 'sql-mode-hook 'sqlind-minor-mode)
:config (spacemacs|hide-lighter sqlind-minor-mode)))
(defun sql/init-sqlfmt ()
(use-package sqlfmt
:commands sqlfmt-buffer
:init
(spacemacs/set-leader-keys-for-major-mode 'sql-mode
"=" 'sqlfmt-buffer)))
(defun sql/init-sqlup-mode ()
(use-package sqlup-mode
:defer t