|
| 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