Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select the sql text then call FormatSQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|