forked from purcell/sqlformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlformat.el
120 lines (91 loc) · 4.11 KB
/
sqlformat.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; sqlformat.el --- Reformat SQL using sqlformat or pgformatter -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Steve Purcell
;; Author: Steve Purcell <[email protected]>
;; Keywords: languages
;; URL: https://github.com/purcell/sqlformat
;; Package-Requires: ((emacs "24.3") (reformatter "0.3"))
;; Version: 0
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Provides commands and a minor mode for easily reformatting SQL
;; using external programs such as "sqlformat" and "pg_format".
;; Install the "sqlparse" (Python) package to get "sqlformat",
;; or "pgformatter" to get "pg_format",
;; or "sqlfluff" (Python) to get "sqlfluff",
;; Customise the `sqlformat-command' variable as desired. For example,
;; to use "pgformatter" (i.e., the `pg_format` command) with
;; two-character indent and no statement grouping,
;; (setq sqlformat-command 'pgformatter)
;; (setq sqlformat-args '("-s2" "-g"))
;; Then call `sqlformat', `sqlformat-buffer' or `sqlformat-region' as
;; convenient.
;; Enable `sqlformat-on-save-mode' in SQL buffers like this:
;; (add-hook 'sql-mode-hook 'sqlformat-on-save-mode)
;; or locally to your project with a form in your .dir-locals.el like
;; this:
;; ((sql-mode
;; (mode . sqlformat-on-save)))
;; You might like to bind `sqlformat' or `sqlformat-buffer' to a key,
;; e.g. with:
;; (define-key 'sql-mode-map (kbd "C-c C-f") 'sqlformat)
;;; Code:
;; Minor mode and customisation
(require 'reformatter)
(defgroup sqlformat nil
"Reformat SQL using sqlformat, sqlfluff, or pgformatter."
:group 'sql)
(defcustom sqlformat-command 'sqlformat
"Command used for reformatting.
This command should receive SQL input via STDIN and output the
reformatted SQL to STDOUT, returning an appropriate exit code."
:type '(choice (const :tag "Use \"sqlformat\"" sqlformat)
(const :tag "Use \"pgformatter\"" pgformatter)
(const :tag "Use \"sqlfluff\"" sqlfluff)))
(defcustom sqlformat-args '()
"List of args for reformatting command.
For example these options may be useful for sqlformat command: '(\"-k\" \"upper\")"
:type '(repeat string))
;; Commands for reformatting
;;;###autoload (autoload 'sqlformat-buffer "sqlformat" nil t)
;;;###autoload (autoload 'sqlformat-region "sqlformat" nil t)
;;;###autoload (autoload 'sqlformat-on-save-mode "sqlformat" nil t)
(reformatter-define sqlformat
:program (pcase sqlformat-command
(`sqlformat "sqlformat")
(`pgformatter "pg_format")
(`sqlfluff "sqlfluff"))
:args (pcase sqlformat-command
(`sqlformat (append sqlformat-args '("-r" "-")))
(`pgformatter (append sqlformat-args '("-")))
(`sqlfluff (append '("fix") sqlformat-args '("-f" "-")))
:lighter " SQLFmt"
:group 'sqlformat)
;;;###autoload
(defun sqlformat (beg end)
"Reformat SQL in region from BEG to END using `sqlformat-region'.
If no region is active, the current statement (paragraph) is reformatted.
Install the \"sqlparse\" (Python) package to get \"sqlformat\", or
\"pgformatter\" to get \"pg_format\", or install the \"sqlfluff\" (Python)
package to get \"sqlfluff\"."
(interactive "r")
(unless (use-region-p)
(setq beg (save-excursion
(backward-paragraph)
(skip-syntax-forward " >")
(point))
end (save-excursion
(forward-paragraph)
(skip-syntax-backward " >")
(point))))
(sqlformat-region beg end (called-interactively-p 'any)))
(provide 'sqlformat)
;;; sqlformat.el ends here