├── README.md └── ftplugin └── go └── goimpl.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-goimpl 2 | 3 | `:GoImpl` command for Go 4 | 5 | ## Usage 6 | 7 | When you have an interface 8 | 9 | ```go 10 | type Foo interface { 11 | DoSomething() 12 | } 13 | ``` 14 | 15 | Implement struct of struct/interface under the cursor 16 | 17 | ``` 18 | :GoImpl 19 | ``` 20 | 21 | ```go 22 | type FooImpl struct { 23 | } 24 | 25 | type (f *FooImpl) DoSomething() { 26 | panic("not implemented") // TODO: Implement 27 | } 28 | ``` 29 | 30 | Implement struct of struct/interface with given interface 31 | 32 | ``` 33 | :GoImpl io.Reader 34 | ``` 35 | 36 | ```go 37 | func (r *Reader) Read(p []byte) (n int, err error) { 38 | panic("not implemented") // TODO: Implement 39 | } 40 | ``` 41 | 42 | Implement struct of struct/interface with given interface and given name 43 | 44 | ``` 45 | :GoImpl io.Reader Bar 46 | ``` 47 | 48 | ```go 49 | type Bar struct { 50 | } 51 | 52 | func (b *Bar) Read(p []byte) (n int, err error) { 53 | panic("not implemented") // TODO: Implement 54 | } 55 | ``` 56 | 57 | ## Installation 58 | 59 | 60 | For [vim-plug](https://github.com/junegunn/vim-plug) plugin manager: 61 | 62 | ```viml 63 | Plug 'mattn/vim-goimpl' 64 | ``` 65 | 66 | ## License 67 | 68 | MIT 69 | 70 | ## Author 71 | 72 | Yasuhiro Matsumoto (a.k.a. mattn) 73 | -------------------------------------------------------------------------------- /ftplugin/go/goimpl.vim: -------------------------------------------------------------------------------- 1 | function! s:goimpl(...) 2 | noau update 3 | let l:dir = expand('%:p:h') 4 | if !empty(a:000) 5 | let l:word = a:000[0] 6 | else 7 | let l:word = expand('') 8 | if l:word ==# 'type' 9 | normal! w 10 | let l:word = expand('') 11 | elseif l:word ==# 'struct' || l:word ==# 'interface' 12 | normal! B 13 | let l:word = expand('') 14 | endif 15 | silent let l:package = system(printf('cd %s && go list', shellescape(l:dir))) 16 | if !empty(l:package) 17 | let l:word = substitute(l:package, '[ \t\r\n]', '', 'g') . '.' . l:word 18 | endif 19 | endif 20 | if empty(l:word) 21 | return 22 | endif 23 | let l:recv = split(l:word, '\.')[-1] 24 | let l:strct = l:recv 25 | if empty(a:000) && getline('.') =~# ' interface {' 26 | let l:strct .= 'Impl' 27 | elseif len(a:000) ==# 2 28 | let l:strct = a:000[1] 29 | endif 30 | let l:impl = printf('%s *%s', tolower(l:strct[0]), l:strct) 31 | let l:cmd = printf('impl -dir %s %s %s', shellescape(l:dir), shellescape(l:impl), l:word) 32 | let l:out = system(l:cmd) 33 | let l:lines = split(l:out, '\n') 34 | if v:shell_error != 0 35 | echomsg join(l:lines, "\n") 36 | return 37 | endif 38 | if l:recv !=# l:strct 39 | let l:lines = ['type ' . l:strct . ' struct {', '}', ''] + l:lines 40 | endif 41 | call append('$', ['']+l:lines) 42 | endfunction 43 | 44 | command! -nargs=* -buffer GoImpl call s:goimpl() 45 | --------------------------------------------------------------------------------