diff --git a/README.md b/README.md index 9fb7c38..3b01457 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,21 @@ Some very simple refactorings for PHP code using VIM: Default key mappings defined: - vmap \em :call ExtractMethod() - nnoremap \ev :call ExtractVariable() - nnoremap \ep :call ExtractClassProperty() - nnoremap \ei :call ExtractInterface() - nnoremap \rlv :call RenameLocalVariable() - nnoremap \rcv :call RenameClassVariable() - nnoremap \iaf :call ImplementAbstractFunctions() + xmap em ExtractMethod + nmap ev ExtractVariable + nmap ep ExtractClassProperty + nmap ei ExtractInterface + nmap rlv RenameLocalVariable + nmap rcv RenameClassVariable + nmap iaf ImplementAbstractFunctions If you want avoid them being set put the following in your vimrc: let g:no_php_maps = 1 +Alternatively, you create your own maps, which will prevent the default ones +from being set. + ## Documentation ### Extract Method diff --git a/ftplugin/php.vim b/ftplugin/php.vim deleted file mode 100644 index 7edb175..0000000 --- a/ftplugin/php.vim +++ /dev/null @@ -1,129 +0,0 @@ -" PHP Refactoring Functions for VIM -" -" Maintainer: Benjamin Eberlei -" License: MIT -" Language: PHP 5 -" -" ======================================================================= -" -" Redistribution and use in source and binary forms, with or without -" modification, are permitted provided that the following conditions -" are met: -" -" 1. Redistributions of source code must retain the above copyright -" notice, this list of conditions and the following disclaimer. -" -" 2. Redistributions in binary form must reproduce the above -" copyright notice, this list of conditions and the following -" disclaimer in the documentation and/or other materials provided -" with the distribution. -" -" 3. The name of the author may not be used to endorse or promote -" products derived from this software without specific prior -" written permission. -" -" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS -" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY -" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -if !exists("no_plugin_maps") && !exists("no_php_maps") - vmap \em :call ExtractMethod() - nnoremap \ev :call ExtractVariable() - nnoremap \ep :call ExtractClassProperty() - nnoremap \ei :call ExtractInterface() - nnoremap \rlv :call RenameLocalVariable() - nnoremap \rcv :call RenameClassVariable() - nnoremap \iaf :call ImplementAbstractFunctions() -endif - -function! ExtractMethod() range - let name = inputdialog("Name of new method:") - '< - exe "normal! O\private function " . name ."()\{\" - '> - exe "normal! oreturn ;\}\k" - s/return/\/\/ return/ge - normal! j% - normal! kf( - exe "normal! yyPi// = \wdwA;\" - normal! == - normal! j0w -endfunction - -" ci,$tmp^[ko$tmp = ^[pa;^[ -function! ExtractVariable() - let name = inputdialog("Name of new variable:") - exe "normal ci,$" . name . "\" - exe "normal ko$" . name . " = \" - normal pa; -endfunction - -" caW$this->tmp ^[/^class^Mjoprivate ^[pi;^[:w^Ml -function! ExtractClassProperty() - normal mr - normal ^l"ryW - let name = substitute(@r,"^\\s\\+\\|\\s\\+$","","g") - exe "normal ^cW$this->" . name . "\" - /^class - exe "normal! joprivate $" . name .";" - normal `r -endfunction - -function! ExtractInterface() - let name = inputdialog("Name of new interface:") - exe "normal Gointerface " . name . "\{" - :g/const/ :normal yyGp - :g/public \$/ :normal yyGp - :g/public function/ :normal yyGp$a; - normal Go} -endfunction - -function! RenameLocalVariable() - normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") - let newName = inputdialog("Rename " . oldName . " to:") - call search('function', 'bW') - call search('{', 'W') - exec 'normal! [[' - let startLine = line('.') - exec "normal! %" - let stopLine = line('.') - exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/'. newName .'/g' -endfunction - -function! RenameClassVariable() - normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") - let newName = inputdialog("Rename " . oldName . " to:") - call search('class ', 'bW') - call search('{', 'w') - let startLine = line('.') - exec "normal! %" - let stopLine = line('.') - exec startLine . ',' . stopLine . ':s/public $' . oldName . '/public $'. newName .'/ge' - exec startLine . ',' . stopLine . ':s/protected $' . oldName . '/protected $'. newName .'/ge' - exec startLine . ',' . stopLine . ':s/private $' . oldName . '/private '. newName .'/ge' - exec startLine . ',' . stopLine . ':s/$this->' . oldName . '/$this->'. newName .'/ge' - exec ":vimgrep /" . newName . "/ %" - :copen -endfunction - -function! ImplementAbstractFunctions() - if (getline(line(".")) =~ 'function.*;$') - g/function.*;$/norm! o{ - g/function.*;$/norm! jothrow new \RuntimeException('Not implemented, yet.'); - g/function.*;$/norm! jjo} - g/function.*;$/s/abstract // - g/ function.*;$/s/function/public function/ - g/function.*;$/s/;$// - endif -endfunction - diff --git a/ftplugin/php_refactor.vim b/ftplugin/php_refactor.vim new file mode 100644 index 0000000..fb760bc --- /dev/null +++ b/ftplugin/php_refactor.vim @@ -0,0 +1,150 @@ +" PHP Refactoring Functions for VIM +" +" Maintainer: Benjamin Eberlei +" License: MIT +" Language: PHP 5 +" +" ======================================================================= +" +" Redistribution and use in source and binary forms, with or without +" modification, are permitted provided that the following conditions +" are met: +" +" 1. Redistributions of source code must retain the above copyright +" notice, this list of conditions and the following disclaimer. +" +" 2. Redistributions in binary form must reproduce the above +" copyright notice, this list of conditions and the following +" disclaimer in the documentation and/or other materials provided +" with the distribution. +" +" 3. The name of the author may not be used to endorse or promote +" products derived from this software without specific prior +" written permission. +" +" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS +" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY +" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +if !exists("no_plugin_maps") && !exists("no_php_maps") + if !hasmapto('ExtractMethod') + xmap em ExtractMethod + endif + if !hasmapto('ExtractVariable') + nmap ev ExtractVariable + endif + if !hasmapto('ExtractClassProperty') + nmap ep ExtractClassProperty + endif + if !hasmapto('ExtractInterface') + nmap ei ExtractInterface + endif + if !hasmapto('RenameLocalVariable') + nmap rlv RenameLocalVariable + endif + if !hasmapto('RenameClassVariable') + nmap rcv RenameClassVariable + endif + if !hasmapto('ImplementAbstractFunctions') + nmap iaf ImplementAbstractFunctions + endif +endif + +noremap ExtractMethod :call ExtractMethod() +noremap ExtractVariable :call ExtractVariable() +noremap ExtractClassProperty :call ExtractClassProperty() +noremap ExtractInterface :call ExtractInterface() +noremap RenameLocalVariable :call RenameLocalVariable() +noremap RenameClassVariable :call RenameClassVariable() +noremap ImplementAbstractFunctions :call ImplementAbstractFunctions() + +function! s:ExtractMethod() range + let name = inputdialog("Name of new method: ") + '< + exec "normal! O\private function " . name . "()\{\" + '> + exec "normal! oreturn ;\}\k" + s/return/\/\/ return/ge + normal! j% + normal! kf( + exec "normal! yyPi// = \wdwA;\" + normal! == + normal! j0w +endfunction + +" ci,$tmp^[ko$tmp = ^[pa;^[ +function! s:ExtractVariable() + let name = inputdialog("Name of new variable: ") + exec "normal ci,$" . name . "\" + exec "normal! ko$" . name . " = \" + normal! pa; +endfunction + +" caW$this->tmp ^[/^class^Mjoprivate ^[pi;^[:w^Ml +function! s:ExtractClassProperty() + normal! mr + normal! ^l"ryW + let name = substitute(@r, "^\\s\\+\\|\\s\\+$", "", "g") + exec "normal! ^cW$this->" . name . "\" + ?^class + /{$ + exec "normal! oprivate $" . name . ";" + normal! `r +endfunction + +function! s:ExtractInterface() + let name = inputdialog("Name of new interface: ") + exec "normal! Go\interface " . name . "\{" + g/const/normal! yyGp + g/public \$/normal! yyGp + g/public function/normal! yyGp$a; + normal! Go} +endfunction + +function! s:RenameLocalVariable() + normal! "zyaw + let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") + let newName = inputdialog("Rename " . oldName . " to: ") + call search('function', 'bW') + call search('{', 'W') + normal! [[ + let startLine = line('.') + normal! % + let stopLine = line('.') + exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/' . newName . '/g' +endfunction + +function! s:RenameClassVariable() + normal "zyaw + let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") + let newName = inputdialog("Rename " . oldName . " to: ") + call search('class ', 'bW') + call search('{', 'w') + let startLine = line('.') + normal! % + let stopLine = line('.') + exec startLine . ',' . stopLine . ':s/public $' . oldName . '/public $'. newName .'/ge' + exec startLine . ',' . stopLine . ':s/protected $' . oldName . '/protected $'. newName .'/ge' + exec startLine . ',' . stopLine . ':s/private $' . oldName . '/private '. newName .'/ge' + exec startLine . ',' . stopLine . ':s/$this->' . oldName . '/$this->'. newName .'/ge' + exec "vimgrep /" . newName . "/ %" + copen +endfunction + +function! s:ImplementAbstractFunctions() + if (getline(line('.')) =~# 'function.*;$') + g/function.*;$/normal! o{ + g/function.*;$/normal! jothrow new \RuntimeException('Not implemented yet.'); + g/function.*;$/normal! jjo} + g/function.*;$/s/abstract /public / + g/function.*;$/s/;$// + endif +endfunction