-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathphprefactor.vim
More file actions
116 lines (90 loc) · 2.85 KB
/
Copy pathphprefactor.vim
File metadata and controls
116 lines (90 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
if !exists("g:php_refactor_patch_command")
let g:php_refactor_patch_command='patch -p1'
endif
func! PhpRefactorShowMenu() range
echohl Title
echo 'Available Refactorings:'
echohl None
echo '(em) Extract Method'
echo '(lv) rename Local Variable'
echo '(li) Local variable to Instance variable'
echo '(ou) Optimize Use'
echo ''
echo '(c) Cancel'
echo ''
let choice = nr2char(getchar())
if choice == 'c'
return
endif
let choice = choice . nr2char(getchar())
if choice == 'em'
call PhpRefactorExtractMethod(a:firstline, a:lastline)
elseif choice == 'lv'
call PhpRefactorRenameLocalVariable()
elseif choice == 'li'
call PhpRefactorLocalVariableToInstanceVariable()
elseif choice == 'ou'
call PhpRefactorOptimizeUse()
endif
endfunc
func! PhpRefactorExtractMethod(startline, endline)
" check the file has been saved
if &modified
echom 'Cannot refactor; file contains unsaved changes'
return
endif
let method = input('Enter extracted method name: ')
let range = a:startline . '-' . a:endline
let args = [range, method]
call PhpRefactorRunCommand('extract-method', args)
" todo : exit visual mode
endfunc
func! PhpRefactorExtractMethodDirectly() range
call PhpRefactorExtractMethod(a:firstline, a:lastline)
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('.')
let args = [lineNo, variable]
call PhpRefactorRunCommand('convert-local-to-instance-variable', args)
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: ')
let args = [lineNo, oldName, newName]
call PhpRefactorRunCommand('rename-local-variable', args)
endfunc
func! PhpRefactorOptimizeUse()
" check the file has been saved
if &modified
echom 'Cannot refactor; file contains unsaved changes'
return
endif
call PhpRefactorRunCommand('optimize-use', [])
endfunc
func! PhpRefactorRunCommand(refactoring, args)
if !exists("g:php_refactor_command")
echom 'You need to set g:php_refactor_command in your .vimrc'
return
endif
" Enable autoread to stop prompting for reload
setlocal autoread
let command = ':!' . g:php_refactor_command
\ . ' ' . a:refactoring . ' %'
for arg in a:args
let command = command . ' ' . arg
endfor
exec command .' | '.g:php_refactor_patch_command
exec ':redraw!'
endfunc