Skip to content

Commit

Permalink
add simple sql format
Browse files Browse the repository at this point in the history
  • Loading branch information
h2ero committed Apr 16, 2013
1 parent 09a87bf commit 137dc5f
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion plugin/phpcr.vim
Expand Up @@ -27,6 +27,7 @@ function! phpcr#Add_space()
"exec "inoremap <CR> <CR>"
exec "normal! a\<CR>\<Esc>"
let s:n_line = getline(s:now_line_nu)
let s:n_indent = indent(s:now_line_nu)

" str replace
let s:strlist = []
Expand All @@ -39,7 +40,7 @@ function! phpcr#Add_space()
else
let s:rstr = s:replacelist[1].s:replacelist[2].s:replacelist[1]
"escape
let s:rstr = escape(s:rstr,']\/')
let s:rstr = escape(s:rstr,']\/*')
call add(s:strlist,['STR'.s:index,s:rstr])
let s:n_line = substitute(s:n_line,s:rstr,'STR'.s:index,'')
let s:index+=1
Expand Down Expand Up @@ -100,6 +101,8 @@ function! phpcr#Add_space()
"" } newline
"let s:n_line = substitute(s:n_line,'}\(else\|elseif\)\w\@!','}<CR>\1','g')

let s:n_line = phpcr#Sql_format(s:n_line)

" str restore
let s:index = len(s:strlist) - 1
while len(s:strlist) > 0
Expand All @@ -118,6 +121,21 @@ function! phpcr#Add_space()

endfunc

function! phpcr#Sql_format(line_content)

let line_content = a:line_content
" 1 SQL keywords http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/reservewords.htm
let s:sql_keywords = "select,from,where,limit,order,by,desc,asc"
let s:sql_keywords = substitute(s:sql_keywords,',','\\|','g')

let line_content = substitute(line_content,'\s*\w\@<!\('.s:sql_keywords.'\)\w\@!\s*',' \U\1 ','g')

" 2 switch newline
let line_content = substitute(line_content,'\s*\w\@<!\(FROM\|WHERE\|LIMIT\|ORDER\)\w\@!\s*','<CR>'.repeat(' ', s:n_indent*2).'\1 ','g')
return line_content

endfunction

function! phpcr#Check_exec()
let s:now_line = line( '.' )
let s:n_line = getline(s:now_line)
Expand Down

0 comments on commit 137dc5f

Please sign in to comment.