sql: support for keywords auto capitalization

SQL, by convention, uses upper-case keywords, although lower-case works just as
well. As humans, the separation between upper-case and lower-case helps scan and
parse the code much more quickly.
This commit is contained in:
Kepi 2016-11-20 19:11:56 +01:00 committed by Eivind Fonn
parent 21c3e2fc2d
commit 7cb9c1924a
3 changed files with 78 additions and 2 deletions

View File

@ -5,14 +5,17 @@
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#install][Install]]
- [[#sql-keywords-capitalization][SQL Keywords Capitalization]]
- [[#sql-interactive-mode][SQL Interactive Mode]]
- [[#blacklisting-keywords][Blacklisting keywords]]
- [[#key-bindings][Key bindings]]
- [[#highlighting][Highlighting]]
- [[#inferior-process-interactions-sqli][Inferior Process Interactions (SQLi)]]
- [[#send-sql-queries-to-sqli][Send SQL queries to SQLi:]]
- [[#sqli-buffer][SQLi buffer]]
- [[#code-formating][Code Formating]]
* Description
This layer adds key bindings and configuration for =sql-mode=, which manages
interactive SQL buffers and highlights a wide range of SQL dialects.
@ -21,6 +24,33 @@ To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =sql= to the existing =dotspacemacs-configuration-layers= list in this
file.
** SQL Keywords Capitalization
SQL, by convention, uses upper-case keywords, although lower-case works just as
well. As humans, the separation between upper-case and lower-case helps scan and
parse the code much more quickly.
To install [[https://github.com/Trevoke/sqlup-mode.el][sqlup-mode]] which enables auto capitalization in =sql mode= set the
variable =sql-capitalize-keywords= to =t=.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(sql :variables sql-capitalize-keywords t)))
#+END_SRC
*** SQL Interactive Mode
If you want capitalization only in =sql-mode= and not in =sql-interactive-mode=
you can set the variable =sql-capitalize-keywords-disable-interactive= to =t=.
*** Blacklisting keywords
[[https://github.com/Trevoke/sqlup-mode.el][sqlup-mode]] can be configured to ignore certain keywords. For example if you use
=name= as column name it would be annoying to have it upcased. You can prevent
this behaviour by setting the variable =sql-capitalize-keywords-blacklist= to
string with one keyword (like ="name"=) or to list with multiple keywords (like
='("name" "varchar")=) to ignore.
This layer is blacklisting =name= by default as it is very common name for
column and NAME is non-reserved SQL keyword.
* Key bindings
** Highlighting
@ -58,3 +88,7 @@ file.
|-------------+--------------------------------------------------------------|
| ~SPC m b r~ | rename buffer (follow up in the SQL buffer with ~SPC m b s~) |
| ~SPC m b S~ | save the current connection |
** Code Formating
| ~SPC m = c~ | capitalize SQL keywords in region (if capitalize is enabled) |

View File

@ -0,0 +1,19 @@
;;; config.el --- sql Layer packages File for Spacemacs
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Kepi <kepi@igloonet.cz>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defvar sql-capitalize-keywords nil
"Capitalize keywords in SQL mode.")
(defvar sql-capitalize-keywords-disable-interactive nil
"Do not capitalize keywords in interactive session (e.g. psql).")
(defvar sql-capitalize-keywords-blacklist "name"
"Keyword or list of keywords to ignore during capitalization.")

View File

@ -9,7 +9,12 @@
;;
;;; License: GPLv3
(setq sql-packages '(sql sql-indent))
(setq sql-packages
'(
sql
sql-indent
(sqlup-mode :toggle sql-capitalize-keywords)
))
(defun sql/init-sql ()
(use-package sql
@ -117,3 +122,21 @@
(defun sql/init-sql-indent ()
(use-package sql-indent
:defer t))
(defun sql/init-sqlup-mode ()
(use-package sqlup-mode
:defer t
:init
(progn
(add-hook 'sql-mode-hook 'sqlup-mode)
(unless sql-capitalize-keywords-disable-interactive
(add-hook 'sql-interactive-mode-hook 'sqlup-mode))
(spacemacs/set-leader-keys-for-major-mode 'sql-mode
"=c" 'sqlup-capitalize-keywords-in-region))
:config
(progn
(spacemacs|diminish sqlup-mode)
(when sql-capitalize-keywords-blacklist
(if (listp sql-capitalize-keywords-blacklist)
(setq sqlup-blacklist (append sqlup-blacklist sql-capitalize-keywords-blacklist))
(add-to-list 'sqlup-blacklist sql-capitalize-keywords-blacklist))))))