Skip to content

Commit

Permalink
add vim-sql-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosong committed Jul 7, 2013
1 parent b036e20 commit dfba75b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions vim-sql-formatter/README.md
@@ -0,0 +1 @@
select the sql text then call FormatSQL
41 changes: 41 additions & 0 deletions vim-sql-formatter/plugin/sql-format.vim
@@ -0,0 +1,41 @@
" Vim global plugin for formatting sql in the selected range
" Maintainer: https://github.com/chaosong
" License: DO WHAT THE FUCK YOU WANT TO DO

if exists("sql_format")
finish
endif
let sql_format = 1

if !has('python')
echo "Error: Required vim compiled with +python"
finish
endif

vmap <f6> :FormatSQL<cr>
command -range FormatSQL :call FormatSQL(<line1>, <line2>)

function! FormatSQL(start, end)

python << EOF

import vim
import sqlparse

start = int(vim.eval('a:start')) - 1
end = int(vim.eval('a:end')) - 1
buf = vim.current.buffer
NL = '\n'

try:
sql = NL.join(buf[start:end + 1])
sql_new = sqlparse.format(sql, reindent=True, keyword_case='upper')

lines = [line.encode('utf-8') for line in sql_new.split(NL)]
buf[:] = buf[:start] + lines + buf[end + 1:]
except Exception, e:
print e
EOF

endfunction

0 comments on commit dfba75b

Please sign in to comment.