Skip to content
This repository has been archived by the owner on Jul 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #42 from tomphp/feature/some-vim-functionality
Browse files Browse the repository at this point in the history
Basic vim bindings
  • Loading branch information
beberlei committed Dec 8, 2013
2 parents 484d8d7 + 02280f7 commit dcfbb55
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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 <refactoring> <arg1>...<argN> | 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
Expand Down
125 changes: 125 additions & 0 deletions 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 <Leader>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 <Leader>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 <Leader>rli
"
" - OPTIMIZE USE
" Simple press <Leader>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('<cword>')
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('<cword>')
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 <expr> <Leader>rem PhpRefactorExtractMethod()
noremap <expr> <Leader>rlv PhpRefactorRenameLocalVariable()
noremap <expr> <Leader>rli PhpRefactorLocalVariableToInstanceVariable()
noremap <expr> <Leader>rou PhpRefactorOptimizeUse()

0 comments on commit dcfbb55

Please sign in to comment.