From 0d1d4c145e28b3134c2093e53cd2188a7f68e7e9 Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 10:08:35 -0500 Subject: [PATCH 1/6] Clean up indentation and whitespace - Stripped trailing spaces - Replaced mixed indentation styles with one --- ftplugin/php.vim | 113 +++++++++++++++++++++++------------------------ 1 file changed, 56 insertions(+), 57 deletions(-) diff --git a/ftplugin/php.vim b/ftplugin/php.vim index 7edb175..7cc0e89 100644 --- a/ftplugin/php.vim +++ b/ftplugin/php.vim @@ -35,13 +35,13 @@ " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. if !exists("no_plugin_maps") && !exists("no_php_maps") - vmap \em :call ExtractMethod() - nnoremap \ev :call ExtractVariable() - nnoremap \ep :call ExtractClassProperty() - nnoremap \ei :call ExtractInterface() - nnoremap \rlv :call RenameLocalVariable() - nnoremap \rcv :call RenameClassVariable() - nnoremap \iaf :call ImplementAbstractFunctions() + vmap \em :call ExtractMethod() + nnoremap \ev :call ExtractVariable() + nnoremap \ep :call ExtractClassProperty() + nnoremap \ei :call ExtractInterface() + nnoremap \rlv :call RenameLocalVariable() + nnoremap \rcv :call RenameClassVariable() + nnoremap \iaf :call ImplementAbstractFunctions() endif function! ExtractMethod() range @@ -60,70 +60,69 @@ endfunction " ci,$tmp^[ko$tmp = ^[pa;^[ function! ExtractVariable() - let name = inputdialog("Name of new variable:") - exe "normal ci,$" . name . "\" - exe "normal ko$" . name . " = \" - normal pa; + let name = inputdialog("Name of new variable:") + exe "normal ci,$" . name . "\" + exe "normal ko$" . name . " = \" + normal pa; endfunction " caW$this->tmp ^[/^class^Mjoprivate ^[pi;^[:w^Ml function! ExtractClassProperty() - normal mr - normal ^l"ryW - let name = substitute(@r,"^\\s\\+\\|\\s\\+$","","g") - exe "normal ^cW$this->" . name . "\" - /^class - exe "normal! joprivate $" . name .";" - normal `r + normal mr + normal ^l"ryW + let name = substitute(@r,"^\\s\\+\\|\\s\\+$","","g") + exe "normal ^cW$this->" . name . "\" + /^class + exe "normal! joprivate $" . name .";" + normal `r endfunction function! ExtractInterface() - let name = inputdialog("Name of new interface:") - exe "normal Gointerface " . name . "\{" - :g/const/ :normal yyGp - :g/public \$/ :normal yyGp - :g/public function/ :normal yyGp$a; - normal Go} + let name = inputdialog("Name of new interface:") + exe "normal Gointerface " . name . "\{" + :g/const/ :normal yyGp + :g/public \$/ :normal yyGp + :g/public function/ :normal yyGp$a; + normal Go} endfunction function! RenameLocalVariable() - normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") - let newName = inputdialog("Rename " . oldName . " to:") - call search('function', 'bW') - call search('{', 'W') - exec 'normal! [[' - let startLine = line('.') - exec "normal! %" - let stopLine = line('.') - exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/'. newName .'/g' + normal "zyaw + let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") + let newName = inputdialog("Rename " . oldName . " to:") + call search('function', 'bW') + call search('{', 'W') + exec 'normal! [[' + let startLine = line('.') + exec "normal! %" + let stopLine = line('.') + exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/'. newName .'/g' endfunction function! RenameClassVariable() - normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") - let newName = inputdialog("Rename " . oldName . " to:") - call search('class ', 'bW') - call search('{', 'w') - let startLine = line('.') - exec "normal! %" - let stopLine = line('.') - exec startLine . ',' . stopLine . ':s/public $' . oldName . '/public $'. newName .'/ge' - exec startLine . ',' . stopLine . ':s/protected $' . oldName . '/protected $'. newName .'/ge' - exec startLine . ',' . stopLine . ':s/private $' . oldName . '/private '. newName .'/ge' - exec startLine . ',' . stopLine . ':s/$this->' . oldName . '/$this->'. newName .'/ge' - exec ":vimgrep /" . newName . "/ %" - :copen + normal "zyaw + let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") + let newName = inputdialog("Rename " . oldName . " to:") + call search('class ', 'bW') + call search('{', 'w') + let startLine = line('.') + exec "normal! %" + let stopLine = line('.') + exec startLine . ',' . stopLine . ':s/public $' . oldName . '/public $'. newName .'/ge' + exec startLine . ',' . stopLine . ':s/protected $' . oldName . '/protected $'. newName .'/ge' + exec startLine . ',' . stopLine . ':s/private $' . oldName . '/private '. newName .'/ge' + exec startLine . ',' . stopLine . ':s/$this->' . oldName . '/$this->'. newName .'/ge' + exec ":vimgrep /" . newName . "/ %" + :copen endfunction function! ImplementAbstractFunctions() - if (getline(line(".")) =~ 'function.*;$') - g/function.*;$/norm! o{ - g/function.*;$/norm! jothrow new \RuntimeException('Not implemented, yet.'); - g/function.*;$/norm! jjo} - g/function.*;$/s/abstract // - g/ function.*;$/s/function/public function/ - g/function.*;$/s/;$// - endif + if (getline(line(".")) =~ 'function.*;$') + g/function.*;$/norm! o{ + g/function.*;$/norm! jothrow new \RuntimeException('Not implemented, yet.'); + g/function.*;$/norm! jjo} + g/function.*;$/s/abstract // + g/ function.*;$/s/function/public function/ + g/function.*;$/s/;$// + endif endfunction - From 65317ae95471e8f4110dedf11b159834f460354d Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 10:25:49 -0500 Subject: [PATCH 2/6] Make command usage, formatting consistent - Use consistent command abbreviations - Use ! with normal where appropriate - Use nore- mappings where appropriate - Other misc. formatting --- README.md | 2 +- ftplugin/php.vim | 65 ++++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 9fb7c38..e63cbc1 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Some very simple refactorings for PHP code using VIM: Default key mappings defined: - vmap \em :call ExtractMethod() + vnoremap \em :call ExtractMethod() nnoremap \ev :call ExtractVariable() nnoremap \ep :call ExtractClassProperty() nnoremap \ei :call ExtractInterface() diff --git a/ftplugin/php.vim b/ftplugin/php.vim index 7cc0e89..f3a05af 100644 --- a/ftplugin/php.vim +++ b/ftplugin/php.vim @@ -35,7 +35,7 @@ " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. if !exists("no_plugin_maps") && !exists("no_php_maps") - vmap \em :call ExtractMethod() + vnoremap \em :call ExtractMethod() nnoremap \ev :call ExtractVariable() nnoremap \ep :call ExtractClassProperty() nnoremap \ei :call ExtractInterface() @@ -47,13 +47,13 @@ endif function! ExtractMethod() range let name = inputdialog("Name of new method:") '< - exe "normal! O\private function " . name ."()\{\" + exec "normal! O\private function " . name . "()\{\" '> - exe "normal! oreturn ;\}\k" + exec "normal! oreturn ;\}\k" s/return/\/\/ return/ge normal! j% normal! kf( - exe "normal! yyPi// = \wdwA;\" + exec "normal! yyPi// = \wdwA;\" normal! == normal! j0w endfunction @@ -61,68 +61,67 @@ endfunction " ci,$tmp^[ko$tmp = ^[pa;^[ function! ExtractVariable() let name = inputdialog("Name of new variable:") - exe "normal ci,$" . name . "\" - exe "normal ko$" . name . " = \" - normal pa; + exec "normal ci,$" . name . "\" + exec "normal! ko$" . name . " = \" + normal! pa; endfunction " caW$this->tmp ^[/^class^Mjoprivate ^[pi;^[:w^Ml function! ExtractClassProperty() - normal mr - normal ^l"ryW - let name = substitute(@r,"^\\s\\+\\|\\s\\+$","","g") - exe "normal ^cW$this->" . name . "\" + normal! mr + normal! ^l"ryW + let name = substitute(@r, "^\\s\\+\\|\\s\\+$", "", "g") + exec "normal! ^cW$this->" . name . "\" /^class - exe "normal! joprivate $" . name .";" - normal `r + exec "normal! joprivate $" . name . ";" + normal! `r endfunction function! ExtractInterface() let name = inputdialog("Name of new interface:") - exe "normal Gointerface " . name . "\{" - :g/const/ :normal yyGp - :g/public \$/ :normal yyGp - :g/public function/ :normal yyGp$a; - normal Go} + exec "normal! Gointerface " . name . "\{" + g/const/normal! yyGp + g/public \$/normal! yyGp + g/public function/normal! yyGp$a; + normal! Go} endfunction function! RenameLocalVariable() - normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") + normal! "zyaw + let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") let newName = inputdialog("Rename " . oldName . " to:") call search('function', 'bW') call search('{', 'W') - exec 'normal! [[' + normal! [[ let startLine = line('.') - exec "normal! %" + normal! % let stopLine = line('.') - exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/'. newName .'/g' + exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/' . newName . '/g' endfunction function! RenameClassVariable() normal "zyaw - let oldName = substitute(@z,"^\\s\\+\\|\\s\\+$","","g") + let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") let newName = inputdialog("Rename " . oldName . " to:") call search('class ', 'bW') call search('{', 'w') let startLine = line('.') - exec "normal! %" + normal! % let stopLine = line('.') exec startLine . ',' . stopLine . ':s/public $' . oldName . '/public $'. newName .'/ge' exec startLine . ',' . stopLine . ':s/protected $' . oldName . '/protected $'. newName .'/ge' exec startLine . ',' . stopLine . ':s/private $' . oldName . '/private '. newName .'/ge' exec startLine . ',' . stopLine . ':s/$this->' . oldName . '/$this->'. newName .'/ge' - exec ":vimgrep /" . newName . "/ %" - :copen + exec "vimgrep /" . newName . "/ %" + copen endfunction function! ImplementAbstractFunctions() - if (getline(line(".")) =~ 'function.*;$') - g/function.*;$/norm! o{ - g/function.*;$/norm! jothrow new \RuntimeException('Not implemented, yet.'); - g/function.*;$/norm! jjo} - g/function.*;$/s/abstract // - g/ function.*;$/s/function/public function/ + if (getline(line('.')) =~# 'function.*;$') + g/function.*;$/normal! o{ + g/function.*;$/normal! jothrow new \RuntimeException('Not implemented yet.'); + g/function.*;$/normal! jjo} + g/function.*;$/s/abstract /public / g/function.*;$/s/;$// endif endfunction From 522631671470b944fd9f04d3c8073f24dcfa400a Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 11:27:21 -0500 Subject: [PATCH 3/6] Fix mapping and function scope - Only set buffer-local mappings - Use rather than hardcoding "\" - Use script-local functions with maps - Only set default maps if none already exist --- README.md | 17 +++++++++------- ftplugin/php.vim | 50 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index e63cbc1..3b01457 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,21 @@ Some very simple refactorings for PHP code using VIM: Default key mappings defined: - vnoremap \em :call ExtractMethod() - nnoremap \ev :call ExtractVariable() - nnoremap \ep :call ExtractClassProperty() - nnoremap \ei :call ExtractInterface() - nnoremap \rlv :call RenameLocalVariable() - nnoremap \rcv :call RenameClassVariable() - nnoremap \iaf :call ImplementAbstractFunctions() + xmap em ExtractMethod + nmap ev ExtractVariable + nmap ep ExtractClassProperty + nmap ei ExtractInterface + nmap rlv RenameLocalVariable + nmap rcv RenameClassVariable + nmap iaf ImplementAbstractFunctions If you want avoid them being set put the following in your vimrc: let g:no_php_maps = 1 +Alternatively, you create your own maps, which will prevent the default ones +from being set. + ## Documentation ### Extract Method diff --git a/ftplugin/php.vim b/ftplugin/php.vim index f3a05af..6dafa9a 100644 --- a/ftplugin/php.vim +++ b/ftplugin/php.vim @@ -35,16 +35,38 @@ " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. if !exists("no_plugin_maps") && !exists("no_php_maps") - vnoremap \em :call ExtractMethod() - nnoremap \ev :call ExtractVariable() - nnoremap \ep :call ExtractClassProperty() - nnoremap \ei :call ExtractInterface() - nnoremap \rlv :call RenameLocalVariable() - nnoremap \rcv :call RenameClassVariable() - nnoremap \iaf :call ImplementAbstractFunctions() + if !hasmapto('ExtractMethod') + xmap em ExtractMethod + endif + if !hasmapto('ExtractVariable') + nmap ev ExtractVariable + endif + if !hasmapto('ExtractClassProperty') + nmap ep ExtractClassProperty + endif + if !hasmapto('ExtractInterface') + nmap ei ExtractInterface + endif + if !hasmapto('RenameLocalVariable') + nmap rlv RenameLocalVariable + endif + if !hasmapto('RenameClassVariable') + nmap rcv RenameClassVariable + endif + if !hasmapto('ImplementAbstractFunctions') + nmap iaf ImplementAbstractFunctions + endif endif -function! ExtractMethod() range +noremap ExtractMethod :call ExtractMethod() +noremap ExtractVariable :call ExtractVariable() +noremap ExtractClassProperty :call ExtractClassProperty() +noremap ExtractInterface :call ExtractInterface() +noremap RenameLocalVariable :call RenameLocalVariable() +noremap RenameClassVariable :call RenameClassVariable() +noremap ImplementAbstractFunctions :call ImplementAbstractFunctions() + +function! s:ExtractMethod() range let name = inputdialog("Name of new method:") '< exec "normal! O\private function " . name . "()\{\" @@ -59,7 +81,7 @@ function! ExtractMethod() range endfunction " ci,$tmp^[ko$tmp = ^[pa;^[ -function! ExtractVariable() +function! s:ExtractVariable() let name = inputdialog("Name of new variable:") exec "normal ci,$" . name . "\" exec "normal! ko$" . name . " = \" @@ -67,7 +89,7 @@ function! ExtractVariable() endfunction " caW$this->tmp ^[/^class^Mjoprivate ^[pi;^[:w^Ml -function! ExtractClassProperty() +function! s:ExtractClassProperty() normal! mr normal! ^l"ryW let name = substitute(@r, "^\\s\\+\\|\\s\\+$", "", "g") @@ -77,7 +99,7 @@ function! ExtractClassProperty() normal! `r endfunction -function! ExtractInterface() +function! s:ExtractInterface() let name = inputdialog("Name of new interface:") exec "normal! Gointerface " . name . "\{" g/const/normal! yyGp @@ -86,7 +108,7 @@ function! ExtractInterface() normal! Go} endfunction -function! RenameLocalVariable() +function! s:RenameLocalVariable() normal! "zyaw let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") let newName = inputdialog("Rename " . oldName . " to:") @@ -99,7 +121,7 @@ function! RenameLocalVariable() exec startLine . ',' . stopLine . ':s/\<' . oldName . '\>/' . newName . '/g' endfunction -function! RenameClassVariable() +function! s:RenameClassVariable() normal "zyaw let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") let newName = inputdialog("Rename " . oldName . " to:") @@ -116,7 +138,7 @@ function! RenameClassVariable() copen endfunction -function! ImplementAbstractFunctions() +function! s:ImplementAbstractFunctions() if (getline(line('.')) =~# 'function.*;$') g/function.*;$/normal! o{ g/function.*;$/normal! jothrow new \RuntimeException('Not implemented yet.'); From aa16a1a4a985faa170f3012f377cf79da4510104 Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 11:34:24 -0500 Subject: [PATCH 4/6] Rename plugin file Users likely already have an ftplugin/php.vim file --- ftplugin/{php.vim => php_refactor.vim} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ftplugin/{php.vim => php_refactor.vim} (100%) diff --git a/ftplugin/php.vim b/ftplugin/php_refactor.vim similarity index 100% rename from ftplugin/php.vim rename to ftplugin/php_refactor.vim From 00598736610969608f08bc27e83664c640ce4c1c Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 12:26:55 -0500 Subject: [PATCH 5/6] Add space after prompt text --- ftplugin/php_refactor.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ftplugin/php_refactor.vim b/ftplugin/php_refactor.vim index 6dafa9a..b4b7b7f 100644 --- a/ftplugin/php_refactor.vim +++ b/ftplugin/php_refactor.vim @@ -67,7 +67,7 @@ noremap RenameClassVariable :call RenameClas noremap ImplementAbstractFunctions :call ImplementAbstractFunctions() function! s:ExtractMethod() range - let name = inputdialog("Name of new method:") + let name = inputdialog("Name of new method: ") '< exec "normal! O\private function " . name . "()\{\" '> @@ -82,7 +82,7 @@ endfunction " ci,$tmp^[ko$tmp = ^[pa;^[ function! s:ExtractVariable() - let name = inputdialog("Name of new variable:") + let name = inputdialog("Name of new variable: ") exec "normal ci,$" . name . "\" exec "normal! ko$" . name . " = \" normal! pa; @@ -100,7 +100,7 @@ function! s:ExtractClassProperty() endfunction function! s:ExtractInterface() - let name = inputdialog("Name of new interface:") + let name = inputdialog("Name of new interface: ") exec "normal! Gointerface " . name . "\{" g/const/normal! yyGp g/public \$/normal! yyGp @@ -111,7 +111,7 @@ endfunction function! s:RenameLocalVariable() normal! "zyaw let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") - let newName = inputdialog("Rename " . oldName . " to:") + let newName = inputdialog("Rename " . oldName . " to: ") call search('function', 'bW') call search('{', 'W') normal! [[ @@ -124,7 +124,7 @@ endfunction function! s:RenameClassVariable() normal "zyaw let oldName = substitute(@z, "^\\s\\+\\|\\s\\+$", "", "g") - let newName = inputdialog("Rename " . oldName . " to:") + let newName = inputdialog("Rename " . oldName . " to: ") call search('class ', 'bW') call search('{', 'w') let startLine = line('.') From 3c11a3290304d01fd7ca5a3da8f41a3e8f3596ca Mon Sep 17 00:00:00 2001 From: Noah Frederick Date: Mon, 22 Jul 2013 12:39:55 -0500 Subject: [PATCH 6/6] Improve placement of extracted class property - Ensure property is placed in containing class, not next class - Do not assume opening bracket is on next line --- ftplugin/php_refactor.vim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ftplugin/php_refactor.vim b/ftplugin/php_refactor.vim index b4b7b7f..fb760bc 100644 --- a/ftplugin/php_refactor.vim +++ b/ftplugin/php_refactor.vim @@ -94,14 +94,15 @@ function! s:ExtractClassProperty() normal! ^l"ryW let name = substitute(@r, "^\\s\\+\\|\\s\\+$", "", "g") exec "normal! ^cW$this->" . name . "\" - /^class - exec "normal! joprivate $" . name . ";" + ?^class + /{$ + exec "normal! oprivate $" . name . ";" normal! `r endfunction function! s:ExtractInterface() let name = inputdialog("Name of new interface: ") - exec "normal! Gointerface " . name . "\{" + exec "normal! Go\interface " . name . "\{" g/const/normal! yyGp g/public \$/normal! yyGp g/public function/normal! yyGp$a;