Skip to content

Instantly share code, notes, and snippets.

@thinca
Created April 17, 2009 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinca/97159 to your computer and use it in GitHub Desktop.
Save thinca/97159 to your computer and use it in GitHub Desktop.
" Vim additional ftplugin: javascript_tofunc - TOFunc settings
" Author : thinca <http://d.hatena.ne.jp/thinca/>
" License: Creative Commons Attribution 2.1 Japan License
" <http://creativecommons.org/licenses/by/2.1/jp/deed.en>
if exists('g:TOFunc.javascript')
finish
endif
let s:save_cpo = &cpo
set cpo&vim
let g:TOFunc.javascript = {}
function! g:TOFunc.javascript.GetRangeA()
let start = getpos('.')
while search('\<function\>', 'bcW') != 0
let b = getpos('.')
call search('\v<function>\s*\k*\s*\(', 'ceW')
normal! %
call search('.', 'W') " right
while search('\S', 'cW') != 0 && s:cursor_syn() == 'Comment'
endwhile
if s:cursor_char() == '{'
normal! %
else
" braceless style: function() expr
let s:head = getpos('.') " to inner
let c = ''
while c !~ '[,;)}]'
call search('[[({,;)}]', 'W')
let c = s:cursor_char()
if c =~ '[[({]' && s:cursor_syn() !~ '\%(Constant\|Comment\)'
normal! %
endif
endwhile
call search('\S', 'bW')
endif
let e = getpos('.')
if e[1] < start[1] || (e[1] == start[1] && e[2] < start[2])
call setpos('.', b)
call s:left()
continue
endif
return [b, e]
endwhile
return 0
endfunction
function! g:TOFunc.javascript.GetRangeI()
let range = g:TOFunc.javascript.GetRangeA()
if type(range) == type(0)
return 0
endif
let endpos = range[1]
call setpos('.', endpos)
if s:cursor_char() != '}'
" braceless style: function() expr
let b = s:head
let e = endpos
else
call s:left()
let e = getpos('.')
call setpos('.', endpos)
normal! %
let save_ve = &virtualedit
set virtualedit=all
normal! l
let b = getpos('.')
let &virtualedit = save_ve
endif
return [b, e]
endfunction
function! s:left()
if col('.') == 1
normal! k$
else
normal! h
endif
endfunction
function! s:cursor_char()
return getline('.')[col('.') - 1]
endfunction
function! s:cursor_syn()
return synIDattr(synIDtrans(synID(line('.'), col('.'), 0)), 'name')
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment