├── after └── ftplugin │ └── javascript │ └── textobj-function.vim ├── autoload └── textobj │ └── function │ └── javascript.vim └── doc └── textobj-function-javascript.txt /after/ftplugin/javascript/textobj-function.vim: -------------------------------------------------------------------------------- 1 | " Vim additional ftplugin: javascript/textobj-function 2 | " Version: 0.2 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | let s:save_cpo = &cpo 7 | set cpo&vim 8 | 9 | 10 | let b:textobj_function_select = function('textobj#function#javascript#select') 11 | 12 | 13 | if exists('b:undo_ftplugin') 14 | let b:undo_ftplugin .= ' | ' 15 | else 16 | let b:undo_ftplugin = '' 17 | endif 18 | let b:undo_ftplugin .= 'unlet! b:textobj_function_select' 19 | 20 | 21 | let &cpo = s:save_cpo 22 | unlet s:save_cpo 23 | -------------------------------------------------------------------------------- /autoload/textobj/function/javascript.vim: -------------------------------------------------------------------------------- 1 | " Text objects for functions in javascript. 2 | " Version: 0.2 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | let s:save_cpo = &cpo 7 | set cpo&vim 8 | 9 | function! textobj#function#javascript#select(object_type) 10 | return s:select_{a:object_type}() 11 | endfunction 12 | 13 | function! s:select_a() 14 | let c = v:count1 15 | let range = 0 16 | while c 17 | unlet! r 18 | let r = s:function_range() 19 | if type(r) == type(0) 20 | break 21 | endif 22 | 23 | call setpos('.', r[0]) 24 | call s:left() 25 | 26 | unlet! range 27 | let range = r 28 | let c -= 1 29 | endwhile 30 | 31 | if type(range) == type([]) 32 | let type = 'v' 33 | call setpos('.', range[0]) 34 | if col('.') == 1 || getline('.')[:col('.') - 2] =~ '^\s*$' 35 | call setpos('.', range[1]) 36 | if getline('.')[col('.'):] =~ '^\s*$' 37 | let type = 'V' 38 | endif 39 | endif 40 | let range = [type] + range 41 | endif 42 | return range 43 | endfunction 44 | 45 | 46 | function! s:select_i() 47 | let range = s:select_a() 48 | if type(range) == type(0) 49 | return 0 50 | endif 51 | 52 | let type = 'v' 53 | 54 | let endpos = range[2] 55 | call setpos('.', endpos) 56 | 57 | if s:cursor_char() != '}' 58 | " Expression closures: function() expr 59 | let b = s:head 60 | let e = endpos 61 | else 62 | let linewise = 0 63 | if col('.') == 1 || getline('.')[:col('.') - 2] =~ '^\s*$' 64 | normal! k$ 65 | let linewise = 1 66 | else 67 | call s:left() 68 | endif 69 | let e = getpos('.') 70 | 71 | call setpos('.', endpos) 72 | call s:jump_to_pair() 73 | 74 | if getline('.')[col('.'):] =~ '^\s*$' 75 | normal! j0 76 | if linewise 77 | let type = 'V' 78 | endif 79 | else 80 | normal! l 81 | endif 82 | let b = getpos('.') 83 | endif 84 | 85 | return [type, b, e] 86 | endfunction 87 | 88 | function! s:function_range() 89 | let start = getpos('.') 90 | " look backward for function definition or fat arrow 91 | while search('\v(\s+)?(|(\(%(\k|,|\s)*\)|\k+)\s*\=\>\s*)', 'bcW') != 0 92 | let b = getpos('.') 93 | 94 | " go to either the start of the function argument list or right after the fat arrow 95 | if (search('\v(\s+)?(\s*\k*\s*\(|\=\>\s*)', 'ceW')) 96 | " if we're on the start of a normal function argument list - skip it 97 | if s:cursor_char() == '(' 98 | call s:jump_to_pair() 99 | endif 100 | endif 101 | 102 | while search('\S', 'W') != 0 && s:cursor_syn() ==# 'Comment' 103 | endwhile 104 | if s:cursor_char() == '{' 105 | call s:jump_to_pair() 106 | else 107 | " Expression closures: function() expr 108 | let s:head = getpos('.') " to inner 109 | 110 | while search('[])}[({,;]', 'W') 111 | if s:cursor_syn() =~# '\%(Constant\|Comment\)' 112 | continue 113 | endif 114 | let char = s:cursor_char() 115 | if char =~ '[])},;]' 116 | break 117 | elseif char =~ '[[({]' 118 | call s:jump_to_pair() 119 | endif 120 | endwhile 121 | 122 | call search('\S', 'bW') 123 | endif 124 | let e = getpos('.') 125 | 126 | if e[1] < start[1] || (e[1] == start[1] && e[2] < start[2]) 127 | call setpos('.', b) 128 | call s:left() 129 | continue 130 | endif 131 | return [b, e] 132 | endwhile 133 | return 0 134 | endfunction 135 | 136 | function! s:jump_to_pair() 137 | normal % 138 | endfunction 139 | 140 | function! s:left() 141 | if col('.') == 1 142 | if line('.') != 1 143 | normal! k$ 144 | endif 145 | else 146 | normal! h 147 | endif 148 | endfunction 149 | 150 | function! s:cursor_char() 151 | return getline('.')[col('.') - 1] 152 | endfunction 153 | 154 | function! s:cursor_syn() 155 | return synIDattr(synIDtrans(synID(line('.'), col('.'), 0)), 'name') 156 | endfunction 157 | 158 | let &cpo = s:save_cpo 159 | unlet s:save_cpo 160 | -------------------------------------------------------------------------------- /doc/textobj-function-javascript.txt: -------------------------------------------------------------------------------- 1 | *textobj-function-javascript.txt* Text objects for functions in javascript. 2 | 3 | Version: 0.2 4 | Author : thinca 5 | License: zlib License 6 | 7 | ============================================================================== 8 | CONTENTS *textobj-function-javascript-contents* 9 | 10 | Introduction |textobj-function-javascript-introduction| 11 | Interface |textobj-function-javascript-interface| 12 | Bugs |textobj-function-javascript-bugs| 13 | Changelog |textobj-function-javascript-changelog| 14 | 15 | 16 | ============================================================================== 17 | INTRODUCTION *textobj-function-javascript-introduction* 18 | 19 | *textobj-function-javascript* is a Vim additional filetype plugin to provide 20 | text objects for functions in javascript. This is a support script for 21 | |textobj-function|, so that you have to install it to enable the text objects. 22 | Unless installing, this script does nothing. 23 | 24 | 25 | Requirements: 26 | - Vim 7.2 or later 27 | - |textobj-function| 0.1.0 or later (vimscript#2619) 28 | 29 | Options: 30 | - |matchit| (vimscript#39) 31 | 32 | 33 | ============================================================================== 34 | INTERFACE *textobj-function-javascript-interface* 35 | 36 | See |textobj-function| for the details. Because this plugin just enhances the 37 | power of textobj-function and does not provide any user interface. 38 | 39 | 40 | ============================================================================== 41 | BUGS *textobj-function-javascript-bugs* 42 | 43 | - This script uses |%| to get the region of function. And use the user mapped 44 | |%| to use |matchit|. So this script do not work if you map |%| to other. 45 | 46 | - This script use the information of |syntax|. You must enable |syntax|. 47 | 48 | - See |textobj-function-bugs| for further information. 49 | 50 | 51 | 52 | 53 | ============================================================================== 54 | CHANGELOG *textobj-function-javascript-changelog* 55 | 56 | 0.2 2014-04-29 57 | - Use autoload function. 58 | - The previous version doesn't work on Vim 7.4.260 or later. 59 | - Change the license. 60 | - Creative Commons Attribution 2.1 Japan License => zlib License 61 | 62 | 0.1.1 2009-05-01 63 | - Improved the expression closures support. 64 | - Add this document. 65 | - "af" uses linewise if possible. 66 | 67 | 0.1 2009-04-25 68 | - Initial version. 69 | 70 | 71 | ============================================================================== 72 | vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl 73 | --------------------------------------------------------------------------------