Skip to content

Commit bede8fa

Browse files
author
Tom Oram
committed
First try
0 parents  commit bede8fa

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

LICENSE

Whitespace-only changes.

README.md

Whitespace-only changes.

doc/plugin.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.

ftplugin/php.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vnoremap <expr> <Leader>rem PhpRefactorExtractMethod()
2+
noremap <expr> <Leader>rlv PhpRefactorRenameLocalVariable()
3+
noremap <expr> <Leader>rli PhpRefactorLocalVariableToInstanceVariable()
4+
noremap <expr> <Leader>rou PhpRefactorOptimizeUse()

plugin/phprefactor.vim

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
let g:php_refactor_command='php ~/bin/refactor.phar'
2+
let g:php_refactor_patch_command='patch -p1'
3+
4+
func! PhpRefactorExtractMethod()
5+
" check the file has been saved
6+
if &modified
7+
echom 'Cannot refactor; file contains unsaved changes'
8+
return
9+
endif
10+
11+
let startLine = line('v')
12+
let endLine = line('.')
13+
let method = input('Enter extracted method name: ')
14+
15+
" check line numbers are the right way around
16+
if startLine > endLine
17+
let temp = startLine
18+
let startLine = endLine
19+
let endLine = temp
20+
endif
21+
22+
let range = startLine . '-' . endLine
23+
24+
let args = [range, method]
25+
26+
call PhpRefactorRunCommand('extract-method', args)
27+
28+
" todo : exit visual mode
29+
endfunc
30+
31+
func! PhpRefactorLocalVariableToInstanceVariable()
32+
" check the file has been saved
33+
if &modified
34+
echom 'Cannot refactor; file contains unsaved changes'
35+
return
36+
endif
37+
38+
let variable = expand('<cword>')
39+
let lineNo = line('.')
40+
41+
let args = [lineNo, variable]
42+
43+
call PhpRefactorRunCommand('convert-local-to-instance-variable', args)
44+
endfunc
45+
46+
func! PhpRefactorRenameLocalVariable()
47+
" check the file has been saved
48+
if &modified
49+
echom 'Cannot refactor; file contains unsaved changes'
50+
return
51+
endif
52+
53+
let oldName = expand('<cword>')
54+
let lineNo = line('.')
55+
let newName = input('Enter new variable name: ')
56+
57+
let args = [lineNo, oldName, newName]
58+
59+
call PhpRefactorRunCommand('rename-local-variable', args)
60+
endfunc
61+
62+
func! PhpRefactorOptimizeUse()
63+
" check the file has been saved
64+
if &modified
65+
echom 'Cannot refactor; file contains unsaved changes'
66+
return
67+
endif
68+
69+
call PhpRefactorRunCommand('optimize-use', [])
70+
endfunc
71+
72+
func! PhpRefactorRunCommand(refactoring, args)
73+
" Enable autoread to stop prompting for reload
74+
setlocal autoread
75+
76+
let command = ':!' . g:php_refactor_command
77+
\ . ' ' . a:refactoring . ' %'
78+
79+
for arg in a:args
80+
let command = command . ' ' . arg
81+
endfor
82+
83+
exec command .' | '.g:php_refactor_patch_command
84+
85+
setlocal noautoread
86+
endfunc

0 commit comments

Comments
 (0)