├── README.md ├── autoload └── textobj │ └── methodcall.vim ├── methodcall_demo.gif └── plugin └── textobj └── methodcall.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-textobj-methodcall 2 | 3 | This plugin defines a *methodcall* text object for the [vim-textobj-user](https://github.com/kana/vim-textobj-user) plugin. 4 | 5 | ![demo](./methodcall_demo.gif) 6 | 7 | # Installing 8 | 9 | With [vim-plug](https://github.com/junegunn/vim-plug): 10 | 11 | ``` 12 | Plug 'kana/vim-textobj-user' | Plug 'thalesmello/vim-textobj-methodcall' 13 | ``` 14 | 15 | # Default mappings 16 | 17 | * `am`: around method call. Gets the method call of the surrounding scope. 18 | * `im`: inner method call. Gets the method call of the current scope. 19 | * `aM`: around method call chain. Gets the method call chain of the surrounding scope. 20 | * `iM`: inner method call chain. Gets the method call chain of the current scope. 21 | 22 | # Custom mappings 23 | 24 | ``` 25 | call textobj#user#map('methodcall', { 26 | \ '-': { 27 | \ 'select-a': 'ac', 28 | \ 'select-i': 'ic', 29 | \ }, 30 | \ 'chain': { 31 | \ 'select-a': 'aC', 32 | \ 'select-i': 'iC', 33 | \ }, 34 | \ }) 35 | let g:textobj_methodcall_no_default_key_mappings = 1 36 | ``` 37 | 38 | For more details, look at the [vim-textobj-user documentation](https://github.com/kana/vim-textobj-user/blob/master/doc/textobj-user.txt). 39 | -------------------------------------------------------------------------------- /autoload/textobj/methodcall.vim: -------------------------------------------------------------------------------- 1 | function! textobj#methodcall#select_a() 2 | return textobj#methodcall#select('a') 3 | endfunction 4 | 5 | function! textobj#methodcall#select_i() 6 | return textobj#methodcall#select('i') 7 | endfunction 8 | 9 | 10 | function! textobj#methodcall#select(motion) 11 | if a:motion == 'a' 12 | silent! normal! [( 13 | endif 14 | silent! execute "normal! w?\\v(\\.{0,1}\\w+)+\" 15 | let head_pos = getpos('.') 16 | normal! % 17 | let tail_pos = getpos('.') 18 | if tail_pos == head_pos 19 | return 0 20 | endif 21 | return ['v', head_pos, tail_pos] 22 | endfunction 23 | 24 | function! textobj#methodcall#select_chain_i() 25 | return textobj#methodcall#select_chain('i') 26 | endfunction 27 | 28 | function! textobj#methodcall#select_chain_a() 29 | return textobj#methodcall#select_chain('a') 30 | endfunction 31 | 32 | function! s:char_under_cursor() 33 | return getline('.')[col('.') - 1] 34 | endfunction 35 | 36 | function! textobj#methodcall#select_chain(motion) 37 | if a:motion == 'a' 38 | silent! normal! [( 39 | endif 40 | 41 | silent! execute 'normal! w?\v(\.{0,1}\w+)+' . "\" 42 | let head = getpos('.') 43 | while s:char_under_cursor() == '.' 44 | silent! execute "normal! ?)\%" 45 | silent! execute 'normal! w?\v(\.{0,1}\w+)+' . "\" 46 | let head = getpos('.') 47 | endwhile 48 | 49 | silent! execute "normal! %" 50 | let tail = getpos('.') 51 | silent! execute 'normal! /\v(\.{0,1}\w+)+' . "\" 52 | while s:char_under_cursor() == '.' 53 | silent! execute "normal! %" 54 | let tail = getpos('.') 55 | silent! execute 'normal! /\v(\.{0,1}\w+)+' . "\" 56 | endwhile 57 | call setpos('.', tail) 58 | 59 | if tail == head 60 | return 0 61 | endif 62 | 63 | return ['v', head, tail] 64 | endfunction 65 | 66 | -------------------------------------------------------------------------------- /methodcall_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thalesmello/vim-textobj-methodcall/a8fba763e41be84646080db1e73551c34bf85e00/methodcall_demo.gif -------------------------------------------------------------------------------- /plugin/textobj/methodcall.vim: -------------------------------------------------------------------------------- 1 | call textobj#user#plugin('methodcall', { 2 | \ '-': { 3 | \ 'select-a': 'am', 'select-a-function': 'textobj#methodcall#select_a', 4 | \ 'select-i': 'im', 'select-i-function': 'textobj#methodcall#select_i', 5 | \ }, 6 | \ 'chain': { 7 | \ 'select-a': 'aM', 'select-a-function': 'textobj#methodcall#select_chain_a', 8 | \ 'select-i': 'iM', 'select-i-function': 'textobj#methodcall#select_chain_i' 9 | \ }}) 10 | 11 | --------------------------------------------------------------------------------