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

Commit dcfbb55

Browse files
committed
Merge pull request #42 from tomphp/feature/some-vim-functionality
Basic vim bindings
2 parents 484d8d7 + 02280f7 commit dcfbb55

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ It outputs a diff to the screen and you can apply it to your code by piping it t
3535

3636
php refactor.phar <refactoring> <arg1>...<argN> | patch -p1
3737

38+
### VIM Bindings
39+
Some basic VIM bindings are included in the `docs` folder. A more complete VIM
40+
plugin will be available in the future.
41+
3842
## Why?
3943

4044
Users of PHPStorm (or Netbeans) might wonder why this project exists, all the

docs/vim-bindings.vim

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
" Some basic VIM bindings to run the refactor commands.
2+
"
3+
" This needs to be put into a proper vim plugin bundle but this bit of
4+
" vimscript provides some basic bindings until it is done properly.
5+
"
6+
" INSTALLATION
7+
"
8+
" Either save this file some where safe and add the following line to your
9+
" .vimrc file:
10+
"
11+
" source path/to/this/file
12+
"
13+
" Or simply copy the contents of this file into your .vimrc
14+
"
15+
" USAGE
16+
"
17+
" The file you are refactoring MUST be saved before any refactoring commands
18+
" will work.
19+
"
20+
" - EXTRACT METHOD
21+
" Go into visual mode and select the code you want to extract to a new
22+
" method the press <Leader>rem
23+
"
24+
" You will be prompted for the name of the new method.
25+
"
26+
" - RENAME LOCAL VARIABLE
27+
" In normal mode move the cursor so it's inside the name of the variable
28+
" which you want to rename. Press <Leader>rlv
29+
"
30+
" You will be prompted for the new name of the variable.
31+
"
32+
" - LOCAL VARIABLE TO INSTANCE VARIABLE
33+
" In normal mode move the cursor so it's inside the name of the variable
34+
" which you want to rename. Press <Leader>rli
35+
"
36+
" - OPTIMIZE USE
37+
" Simple press <Leader>rou to run the optimize use refactoring.
38+
39+
let g:php_refactor_command='php /usr/bin/refactor.phar'
40+
let g:php_refactor_patch_command='patch'
41+
42+
func! PhpRefactorExtractMethod()
43+
" check the file has been saved
44+
if &modified
45+
echom 'Cannot refactor; file contains unsaved changes'
46+
return
47+
endif
48+
49+
let startLine=line('v')
50+
let endLine=line('.')
51+
let method=input('Enter extracted method name: ')
52+
53+
" check line numbers are the right way around
54+
if startLine > endLine
55+
let temp=startLine
56+
let startLine=endLine
57+
let endLine=temp
58+
endif
59+
60+
exec ':!'.g:php_refactor_command
61+
\ .' extract-method'
62+
\ .' %'
63+
\ .' '.startLine.'-'.endLine
64+
\ .' '.method
65+
\ .' | '.g:php_refactor_patch_command
66+
67+
" todo : exit visual mode
68+
endfunc
69+
70+
func! PhpRefactorLocalVariableToInstanceVariable()
71+
" check the file has been saved
72+
if &modified
73+
echom 'Cannot refactor; file contains unsaved changes'
74+
return
75+
endif
76+
77+
let variable=expand('<cword>')
78+
let lineNo=line('.')
79+
80+
exec ':!'.g:php_refactor_command
81+
\ .' convert-local-to-instance-variable'
82+
\ .' %'
83+
\ .' '.lineNo
84+
\ .' '.variable
85+
\ .' | '.g:php_refactor_patch_command
86+
endfunc
87+
88+
func! PhpRefactorRenameLocalVariable()
89+
" check the file has been saved
90+
if &modified
91+
echom 'Cannot refactor; file contains unsaved changes'
92+
return
93+
endif
94+
95+
let oldName=expand('<cword>')
96+
let lineNo=line('.')
97+
let newName=input('Enter new variable name: ')
98+
99+
100+
exec ':!'.g:php_refactor_command
101+
\ .' rename-local-variable'
102+
\ .' %'
103+
\ .' '.lineNo
104+
\ .' '.oldName
105+
\ .' '.newName
106+
\ .' | '.g:php_refactor_patch_command
107+
endfunc
108+
109+
func! PhpRefactorOptimizeUse()
110+
" check the file has been saved
111+
if &modified
112+
echom 'Cannot refactor; file contains unsaved changes'
113+
return
114+
endif
115+
116+
exec ':!'.g:php_refactor_command
117+
\ .' optimize-use'
118+
\ .' %'
119+
\ .' | '.g:php_refactor_patch_command
120+
endfunc
121+
122+
vnoremap <expr> <Leader>rem PhpRefactorExtractMethod()
123+
noremap <expr> <Leader>rlv PhpRefactorRenameLocalVariable()
124+
noremap <expr> <Leader>rli PhpRefactorLocalVariableToInstanceVariable()
125+
noremap <expr> <Leader>rou PhpRefactorOptimizeUse()

0 commit comments

Comments
 (0)