diff --git a/README.md b/README.md index cd3d451..d6fe799 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ It outputs a diff to the screen and you can apply it to your code by piping it t php refactor.phar ... | patch -p1 +### VIM Bindings +Some basic VIM bindings are included in the `docs` folder. A more complete VIM +plugin will be available in the future. + ## Why? Users of PHPStorm (or Netbeans) might wonder why this project exists, all the diff --git a/docs/vim-bindings.vim b/docs/vim-bindings.vim new file mode 100644 index 0000000..5db9bd9 --- /dev/null +++ b/docs/vim-bindings.vim @@ -0,0 +1,125 @@ +" Some basic VIM bindings to run the refactor commands. +" +" This needs to be put into a proper vim plugin bundle but this bit of +" vimscript provides some basic bindings until it is done properly. +" +" INSTALLATION +" +" Either save this file some where safe and add the following line to your +" .vimrc file: +" +" source path/to/this/file +" +" Or simply copy the contents of this file into your .vimrc +" +" USAGE +" +" The file you are refactoring MUST be saved before any refactoring commands +" will work. +" +" - EXTRACT METHOD +" Go into visual mode and select the code you want to extract to a new +" method the press rem +" +" You will be prompted for the name of the new method. +" +" - RENAME LOCAL VARIABLE +" In normal mode move the cursor so it's inside the name of the variable +" which you want to rename. Press rlv +" +" You will be prompted for the new name of the variable. +" +" - LOCAL VARIABLE TO INSTANCE VARIABLE +" In normal mode move the cursor so it's inside the name of the variable +" which you want to rename. Press rli +" +" - OPTIMIZE USE +" Simple press rou to run the optimize use refactoring. + +let g:php_refactor_command='php /usr/bin/refactor.phar' +let g:php_refactor_patch_command='patch' + +func! PhpRefactorExtractMethod() + " check the file has been saved + if &modified + echom 'Cannot refactor; file contains unsaved changes' + return + endif + + let startLine=line('v') + let endLine=line('.') + let method=input('Enter extracted method name: ') + + " check line numbers are the right way around + if startLine > endLine + let temp=startLine + let startLine=endLine + let endLine=temp + endif + + exec ':!'.g:php_refactor_command + \ .' extract-method' + \ .' %' + \ .' '.startLine.'-'.endLine + \ .' '.method + \ .' | '.g:php_refactor_patch_command + + " todo : exit visual mode +endfunc + +func! PhpRefactorLocalVariableToInstanceVariable() + " check the file has been saved + if &modified + echom 'Cannot refactor; file contains unsaved changes' + return + endif + + let variable=expand('') + let lineNo=line('.') + + exec ':!'.g:php_refactor_command + \ .' convert-local-to-instance-variable' + \ .' %' + \ .' '.lineNo + \ .' '.variable + \ .' | '.g:php_refactor_patch_command +endfunc + +func! PhpRefactorRenameLocalVariable() + " check the file has been saved + if &modified + echom 'Cannot refactor; file contains unsaved changes' + return + endif + + let oldName=expand('') + let lineNo=line('.') + let newName=input('Enter new variable name: ') + + + exec ':!'.g:php_refactor_command + \ .' rename-local-variable' + \ .' %' + \ .' '.lineNo + \ .' '.oldName + \ .' '.newName + \ .' | '.g:php_refactor_patch_command +endfunc + +func! PhpRefactorOptimizeUse() + " check the file has been saved + if &modified + echom 'Cannot refactor; file contains unsaved changes' + return + endif + + exec ':!'.g:php_refactor_command + \ .' optimize-use' + \ .' %' + \ .' | '.g:php_refactor_patch_command +endfunc + +vnoremap rem PhpRefactorExtractMethod() +noremap rlv PhpRefactorRenameLocalVariable() +noremap rli PhpRefactorLocalVariableToInstanceVariable() +noremap rou PhpRefactorOptimizeUse()